Skip to content

Instantly share code, notes, and snippets.

@thaaddeus
Last active May 11, 2023 20:23
Show Gist options
  • Save thaaddeus/39211703e378a1f16d65ad05c049939f to your computer and use it in GitHub Desktop.
Save thaaddeus/39211703e378a1f16d65ad05c049939f to your computer and use it in GitHub Desktop.
liquidations_monitor.js
const { streamNormalized, normalizeLiquidations, combine } = require('tardis-dev')
// let's monitor BTC perpetuals swaps only
const monitoredExchanges = [
{ id: 'ftx', symbols: ['BTC-PERP'] },
{ id: 'bitmex', symbols: ['XBTUSD'] },
{ id: 'deribit', symbols: ['BTC-PERPETUAL'] },
{ id: 'binance-futures', symbols: ['BTCUSDT'] },
{ id: 'binance-delivery', symbols: ['BTCUSD_PERP'] },
{ id: 'bitfinex-derivatives', symbols: ['BTCF0:USTF0'] },
{ id: 'cryptofacilities', symbols: ['PI_XBTUSD'] },
{ id: 'huobi-dm-swap', symbols: ['BTC-USD'] }
]
async function monitorLiquidations() {
const monitoredExchangesLiquidationsStreams = monitoredExchanges.map((exchange) => {
return streamNormalized(
{
exchange: exchange.id,
symbols: exchange.symbols,
timeoutIntervalMS: 30 * 60 * 1000
},
normalizeLiquidations
)
})
const combinedLiquidationStream = combine(...monitoredExchangesLiquidationsStreams)
console.log('Liquidations monitor started...')
for await (const liquidation of combinedLiquidationStream) {
formatLiquidation(liquidation)
}
}
const meta = {
ftx: {
name: 'FTX',
contractMultiplier: 1,
inverse: false
},
bitmex: {
name: 'BitMEX',
contractMultiplier: 1,
inverse: true
},
deribit: {
name: 'Deribit',
contractMultiplier: 1,
inverse: true
},
'binance-futures': {
name: 'Binance USDT Futures',
contractMultiplier: 1,
inverse: false
},
'binance-delivery': {
name: 'Binance COIN Futures',
contractMultiplier: 1,
inverse: true
},
'bitfinex-derivatives': {
name: 'Bitfinex Derivatives',
contractMultiplier: 1,
inverse: false
},
cryptofacilities: {
name: 'Kraken Futures',
contractMultiplier: 1,
inverse: true
},
'huobi-dm-swap': {
name: 'Huobi Swap',
contractMultiplier: 100,
inverse: true
}
}
const usdCurrencyFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
})
function formatLiquidation(liquidation) {
const { name, contractMultiplier, inverse } = meta[liquidation.exchange]
const position = liquidation.side === 'sell' ? 'long' : 'short'
const price = usdCurrencyFormatter.format(liquidation.price)
let normalizedAmount = liquidation.amount * contractMultiplier
if (inverse === false) {
normalizedAmount = normalizedAmount * liquidation.price
}
const minSize = 10000
if (normalizedAmount < minSize) {
return;
}
const liquidatedAmunt = usdCurrencyFormatter.format(normalizedAmount)
const timestamp = liquidation.timestamp.toISOString()
const direction = liquidation.side === 'sell' ? '📉' : '📈'
console.log(
`${direction} ${name} liquidated ${position} ${liquidation.symbol} position` +
` at ${price}: ${liquidation.side} ${liquidatedAmunt}, timestmap: ${timestamp}`
)
}
monitorLiquidations()
@didobagi
Copy link

hi! thanks so much for sharing, how could i filter for size? sorry for dumb question im really a noob with java specifically and overall coding :(

@thaaddeus
Copy link
Author

Hi, I've updated the script to include minSize so liquidations is printed only if larger than minSize

@didobagi
Copy link

didobagi commented May 7, 2021

thanks! how could i write to get all symbols for any given exchange? i was looking at tardis documentation but cant figure it out

@didobagi
Copy link

didobagi commented May 9, 2021

sorry if its a silly question,,, i guess i could first get from exchanges endpoint?

@thaaddeus
Copy link
Author

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment