Last active
June 5, 2023 12:18
-
-
Save thaaddeus/cbc8fbd19caf22cbd2dcb8ba8adbb230 to your computer and use it in GitHub Desktop.
Liquidations monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { streamNormalized, normalizeLiquidations, combine, Liquidation } from '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'] } | |
] as const | |
async function monitorLiquidations() { | |
const monitoredExchangesLiquidationsStreams = monitoredExchanges.map((exchange) => { | |
return streamNormalized( | |
{ | |
exchange: exchange.id, | |
symbols: exchange.symbols as any, | |
timeoutIntervalMS: 30 * 60 * 1000 | |
}, | |
normalizeLiquidations | |
) | |
}) | |
const combinedLiquidationStream = combine(...monitoredExchangesLiquidationsStreams) | |
console.log('Liquidations monitor started...') | |
for await (const liquidation of combinedLiquidationStream) { | |
console.log(formatLiquidation(liquidation)) | |
} | |
} | |
type MonitoredExchangeId = typeof monitoredExchanges[number]['id'] | |
const meta: { | |
[key in MonitoredExchangeId]: { | |
inverse: boolean | |
name: string | |
contractMultiplier: number | |
} | |
} = { | |
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: Liquidation) { | |
const { name, contractMultiplier, inverse } = meta[liquidation.exchange as MonitoredExchangeId] | |
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 liquidatedAmunt = usdCurrencyFormatter.format(normalizedAmount) | |
const timestamp = liquidation.timestamp.toISOString() | |
const direction = liquidation.side === 'sell' ? '๐' : '๐' | |
return `${direction} ${name} liquidated ${position} ${liquidation.symbol} position at ${price}: ${liquidation.side} ${liquidatedAmunt}, timestmap: ${timestamp}` | |
} | |
monitorLiquidations() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment