Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"b": [ // Bids to be updated
[
"0.0024", // Price level to be updated
"10" // Quantity
]
],
"a": [ // Asks to be updated
[
"0.0026", // Price level to be updated
"100" // Quantity
]
]
}
"""
exchange_pair = pair
pair = pair_exchange_to_std(pair)
if pair not in self.l2_book:
await self._snapshot(exchange_pair)
skip_update, forced = self._check_update_id(pair, msg)
if skip_update:
return
delta = {BID: [], ASK: []}
ts = msg['E']
for s, side in (('b', BID), ('a', ASK)):
for update in msg[s]:
price = Decimal(update[0])
amount = Decimal(update[1])
'ab': 'BID', // 'BID' or 'ASK'
'cp': 64000.0, // Change of price
'pcp': 6823000.0, // Previous closing price
'sid': 1584257228000000, // Sequential ID
'st': 'SNAPSHOT', // 'SNAPSHOT' or 'REALTIME'
'td': '2020-03-15', // Trade date utc
'ttm': '07:27:08', // Trade time utc
'c': 'FALL', // Change - 'FALL' / 'RISE' / 'EVEN'
}
"""
price = Decimal(msg['tp'])
amount = Decimal(msg['tv'])
await self.callback(TRADES, feed=self.id,
order_id=msg['sid'],
pair=pair_exchange_to_std(msg['cd']),
side=BUY if msg['ab'] == 'BID' else SELL,
amount=amount,
price=price,
timestamp=timestamp_normalize(self.id, msg['ttms']),
receipt_timestamp=timestamp)
data = self._post_private('/private/TradesHistory', params)
if len(data['error']) != 0:
return data
ret = []
for trade_id, trade in data['result']['trades'].items():
sym = trade['pair']
sym = sym.replace('XX', 'X')
sym = sym.replace('ZUSD', 'USD')
sym = sym.replace('ZCAD', 'CAD')
sym = sym.replace('ZEUR', 'EUR')
sym = sym.replace('ZGBP', 'GBP')
sym = sym.replace('ZJPY', 'JPY')
if pair_exchange_to_std(sym) != symbol:
continue
ret.append({
'price': Decimal(trade['price']),
'amount': Decimal(trade['vol']),
'timestamp': trade['time'],
'side': SELL if trade['type'] == 'sell' else BUY,
'fee_currency': symbol.split('-')[1],
'fee_amount': Decimal(trade['fee']),
'trade_id': trade_id,
'order_id': trade['ordertxid']
})
return ret
async def _trade(self, msg: dict, timestamp: float):
"""
{
'ch': 'market.btcusd.trade.detail',
'ts': 1549773923965,
'tick': {
'id': 100065340982,
'ts': 1549757127140,
'data': [{'id': '10006534098224147003732', 'amount': Decimal('0.0777'), 'price': Decimal('3669.69'), 'direction': 'buy', 'ts': 1549757127140}]}}
"""
for trade in msg['tick']['data']:
await self.callback(TRADES,
feed=self.id,
pair=pair_exchange_to_std(msg['ch'].split('.')[1]),
order_id=trade['id'],
side=BUY if trade['direction'] == 'buy' else SELL,
amount=Decimal(trade['amount']),
price=Decimal(trade['price']),
timestamp=timestamp_normalize(self.id, trade['ts']),
receipt_timestamp=timestamp)
def __reset(self, pairs):
for pair in pairs:
self.l2_book[pair_exchange_to_std(pair)] = {BID: sd(), ASK: sd()}
def _trade_normalization(self, symbol: str, trade: list) -> dict:
if symbol[0] == 'f':
# period is in days, from 2 to 30
trade_id, timestamp, amount, price, period = trade
else:
trade_id, timestamp, amount, price = trade
period = None
ret = {
'timestamp': timestamp_normalize(self.ID, timestamp),
'pair': pair_exchange_to_std(symbol),
'id': trade_id,
'feed': self.ID,
'side': SELL if amount < 0 else BUY,
'amount': abs(amount),
'price': price,
}
if period:
ret['period'] = period
return ret
async def _book(self, msg: dict, timestamp: float):
if msg['action'] == 'partial':
# snapshot
for update in msg['data']:
pair = pair_exchange_to_std(update['instrument_id'])
self.l2_book[pair] = {
BID: sd({
Decimal(price): Decimal(amount) for price, amount, *_ in update['bids']
}),
ASK: sd({
Decimal(price): Decimal(amount) for price, amount, *_ in update['asks']
})
}
await self.book_callback(self.l2_book[pair], L2_BOOK, pair, True, None, timestamp_normalize(self.id, update['timestamp']), timestamp)
else:
# update
for update in msg['data']:
delta = {BID: [], ASK: []}
pair = pair_exchange_to_std(update['instrument_id'])
for side in ('bids', 'asks'):
s = BID if side == 'bids' else ASK
def _order_status(order_id: str, order: dict):
if order['status'] == 'canceled':
status = CANCELLED
if order['status'] == 'open':
status = OPEN
if order['status'] == 'closed':
status = FILLED
return {
'order_id': order_id,
'symbol': pair_exchange_to_std(order['descr']['pair']),
'side': SELL if order['descr']['type'] == 'sell' else BUY,
'order_type': LIMIT if order['descr']['ordertype'] == 'limit' else MARKET,
'price': Decimal(order['descr']['price']),
'total': Decimal(order['vol']),
'executed': Decimal(order['vol_exec']),
'pending': Decimal(order['vol']) - Decimal(order['vol_exec']),
'timestamp': order['opentm'],
'order_status': status
}
def _trade_normalize(self, trade, symbol):
return {
'timestamp': pd.Timestamp(trade['date']).timestamp(),
'pair': pair_exchange_to_std(symbol),
'id': trade['tradeID'],
'feed': self.ID,
'side': BUY if trade['type'] == 'buy' else SELL,
'amount': Decimal(trade['amount']),
'price': Decimal(trade['rate'])
}
def get_active_symbols():
load_exchange_pair_mapping(Upbit.id)
symbols = []
for data in Upbit.get_active_symbols_info():
symbols.append(pair_exchange_to_std(data['market']))
return symbols