Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{'ap': 6735000.0, 'as': 1.08739294, 'bp': 6713000.0, 'bs': 0.46785444},
{'ap': 6737000.0, 'as': 3.34450006, 'bp': 6712000.0, 'bs': 0.01300915},
{'ap': 6738000.0, 'as': 0.26, 'bp': 6711000.0, 'bs': 0.24701799},
{'ap': 6739000.0, 'as': 0.086, 'bp': 6710000.0, 'bs': 1.97964014},
{'ap': 6740000.0, 'as': 0.00658782, 'bp': 6708000.0, 'bs': 0.0002},
{'ap': 6741000.0, 'as': 0.8004, 'bp': 6707000.0, 'bs': 0.02022364},
{'ap': 6742000.0, 'as': 0.11040396, 'bp': 6706000.0, 'bs': 0.29082183},
{'ap': 6743000.0, 'as': 1.1, 'bp': 6705000.0, 'bs': 0.94493254}],
'st': 'REALTIME', // Streaming type - 'REALTIME' or 'SNAPSHOT'
'tas': 20.67627941, // Total ask size for given 15 depth (not total ask order size)
'tbs': 622.93769692, // Total bid size for given 15 depth (not total bid order size)
'tms': 1584263923870, // Timestamp
}
"""
pair = pair_exchange_to_std(msg['cd'])
orderbook_timestamp = timestamp_normalize(self.id, msg['tms'])
if pair not in self.l2_book:
await self._snapshot(pair)
# forced = True if the snapshot received, otherwise(realtime) forced set to be false
forced = True if msg['st'] == 'SNAPSHOT' else False
update = {
BID: sd({
Decimal(unit['bp']): Decimal(unit['bs'])
for unit in msg['obu'] if unit['bp'] > 0
}),
ASK: sd({
Decimal(unit['ap']): Decimal(unit['as'])
for unit in msg['obu'] if unit['ap'] > 0
})
async def _book_snapshot(self, msg: dict, timestamp: float):
pair = pair_exchange_to_std(msg['symbol'])
self.l2_book[pair] = {
BID: sd({
Decimal(bid['price']): Decimal(bid['size']) for bid in msg['bid']
}),
ASK: sd({
Decimal(ask['price']): Decimal(ask['size']) for ask in msg['ask']
})
}
await self.book_callback(self.l2_book[pair], L2_BOOK, pair, True, None, timestamp_normalize(self.id, msg['timestamp']), timestamp)
async def _trade(self, msg: dict, timestamp: float):
pair = pair_exchange_to_std(msg['symbol'])
price = Decimal(msg['price'])
side = SELL if msg['side'] == 'sell' else BUY
amount = Decimal(msg['quantity'])
await self.callback(TRADES, feed=self.id,
order_id=msg['event_id'],
pair=pair,
side=side,
amount=amount,
price=price,
timestamp=timestamp_normalize(self.id, msg['timestamp']),
receipt_timestamp=timestamp)
async def ticker(self, msg: dict, timestamp: float):
for t in msg['D']:
if (not self.config and t['M'] in self.pairs) or ('SubscribeToSummaryDeltas' in self.config and t['M'] in self.config['SubscribeToSummaryDeltas']):
await self.callback(TICKER, feed=self.id, pair=pair_exchange_to_std(t['M']), bid=Decimal(t['B']), ask=Decimal(t['A']), timestamp=timestamp_normalize(self.id, t['T']), receipt_timestamp=timestamp)
"status": "ok",
"timestamp": 1489473538996,
"symbol": "btcusdt",
"trades": [{
"tradeId ": 14894644510000001,
"price": 4000.00,
"quantity": 1.0000,
"take": "buy",
"time": "2018-03-14 18:36:32"
}]
}
"""
if pair not in self.last_trade_update:
async with session.get(f"{self.address}trades?symbol={pair}") as response:
data = await response.json()
self.last_trade_update[pair] = timestamp_normalize(self.id, data['trades'][-1]['time'])
else:
async with session.get(f"{self.address}trades?symbol={pair}&size=2000") as response:
data = await response.json()
for trade in data['trades']:
if timestamp_normalize(self.id, trade['time']) <= self.last_trade_update[pair]:
continue
price = Decimal(trade['price'])
amount = Decimal(trade['quantity'])
side = BUY if trade['take'] == 'buy' else SELL
await self.callback(TRADES, feed=self.id,
pair=pair_exchange_to_std(pair),
side=side,
amount=amount,
price=price,
order_id=trade['tradeId'],
async def _funding(self, msg: dict, timestamp: float):
for update in msg['data']:
await self.callback(FUNDING,
feed=self.id,
pair=pair_exchange_to_std(update['instrument_id']),
timestamp=timestamp_normalize(self.id, update['funding_time']),
receipt_timestamp=timestamp,
rate=update['funding_rate'],
estimated_rate=update['estimated_rate'],
settlement_time=timestamp_normalize(self.id, update['settlement_time']))
],
"channel": "trades.BTC-PERPETUAL.raw"
},
"method": "subscription",
"jsonrpc": "2.0"
}
"""
for trade in msg["params"]["data"]:
await self.callback(TRADES,
feed=self.id,
pair=trade["instrument_name"],
order_id=trade['trade_id'],
side=BUY if trade['direction'] == 'buy' else SELL,
amount=Decimal(trade['amount']),
price=Decimal(trade['price']),
timestamp=timestamp_normalize(self.id, trade['timestamp']),
receipt_timestamp=timestamp,
)
if 'liquidation' in trade:
await self.callback(LIQUIDATIONS,
feed=self.id,
pair=trade["instrument_name"],
side=BUY if trade['direction'] == 'buy' else SELL,
leaves_qty=Decimal(trade['amount']),
price=Decimal(trade['price']),
order_id=trade['trade_id'],
receipt_timestamp=timestamp
)
def _trade_normalization(self, trade: list) -> dict:
ret = {
'timestamp': timestamp_normalize(self.ID, trade["timestamp"]),
'pair': trade["instrument_name"],
'id': int(trade["trade_id"]),
'feed': self.ID,
'side': BUY if trade["direction"] == 'buy' else SELL,
'amount': trade["amount"],
'price': trade["price"],
}
return ret
continue
async with session.get(f"https://ftx.com/api/funding_rates?future={pair}") as response:
data = await response.text()
data = json.loads(data, parse_float=Decimal)
last_update = self.funding.get(pair, None)
update = str(data['result'][0]['rate']) + str(data['result'][0]['time'])
if last_update and last_update == update:
continue
else:
self.funding[pair] = update
await self.callback(FUNDING, feed=self.id,
pair=pair_exchange_to_std(data['result'][0]['future']),
rate=data['result'][0]['rate'],
timestamp=timestamp_normalize(self.id, data['result'][0]['time']))
await asyncio.sleep(wait_time)
async def _trade(self, msg: dict, timestamp: float):
"""
example message:
{"channel": "trades", "market": "BTC-PERP", "type": "update", "data": [{"id": null, "price": 10738.75,
"size": 0.3616, "side": "buy", "liquidation": false, "time": "2019-08-03T12:20:19.170586+00:00"}]}
"""
for trade in msg['data']:
await self.callback(TRADES, feed=self.id,
pair=pair_exchange_to_std(msg['market']),
side=BUY if trade['side'] == 'buy' else SELL,
amount=Decimal(trade['size']),
price=Decimal(trade['price']),
order_id=None,
timestamp=float(timestamp_normalize(self.id, trade['time'])),
receipt_timestamp=timestamp)
if bool(trade['liquidation']):
await self.callback(LIQUIDATIONS,
feed=self.id,
pair=pair_exchange_to_std(msg['market']),
side=BUY if trade['side'] == 'buy' else SELL,
leaves_qty=Decimal(trade['size']),
price=Decimal(trade['price']),
order_id=None,
receipt_timestamp=timestamp
)