Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async with session.get("{}Trades?pair={}".format(self.address, pair)) as response:
data = await response.json()
self.last_trade_update = data['result']['last']
else:
async with session.get("{}Trades?pair={}&since={}".format(self.address, pair, self.last_trade_update)) as response:
data = await response.json()
self.last_trade_update = data['result']['last']
if data['result'][pair] == []:
return
else:
for trade in data['result'][pair]:
# , , <time>, , ,
price, amount, timestamp, side, _, _ = trade
await self.callback(TRADES, feed=self.id,
pair=pair_exchange_to_std(pair),
side=BUY if side == 'b' else SELL,
amount=Decimal(amount),
price=Decimal(price),
order_id=None,
timestamp=timestamp)
</time>
if msg_type == 'o':
side = ASK if update[1] == 0 else BID
price = Decimal(update[2])
amount = Decimal(update[3])
if amount == 0:
delta[side].append((price, 0))
del self.l2_book[pair][side][price]
else:
delta[side].append((price, amount))
self.l2_book[pair][side][price] = amount
elif msg_type == 't':
# index 1 is trade id, 2 is side, 3 is price, 4 is amount, 5 is timestamp
_, order_id, _, price, amount, server_ts = update
price = Decimal(price)
amount = Decimal(amount)
side = BUY if update[2] == 1 else SELL
if self.__do_callback(TRADES, pair):
await self.callback(TRADES, feed=self.id,
pair=pair,
side=side,
amount=amount,
price=price,
timestamp=float(server_ts),
order_id=order_id,
receipt_timestamp=timestamp)
else:
LOG.warning("%s: Unexpected message received: %s", self.id, msg)
if self.__do_callback(L2_BOOK, pair):
await self.book_callback(self.l2_book[pair], L2_BOOK, pair, forced, delta, timestamp, timestamp)
def place_order(self, symbol: str, side: str, order_type: str, amount: Decimal, price=None, options=None):
if not price:
raise ValueError('Poloniex only supports limit orders, must specify price')
# Poloniex only supports limit orders, so check the order type
_ = normalize_trading_options(self.ID, order_type)
parameters = {}
if options:
parameters = {
normalize_trading_options(self.ID, o): 1 for o in options
}
parameters['currencyPair'] = pair_std_to_exchange(symbol, self.ID)
parameters['amount'] = str(amount)
parameters['rate'] = str(price)
endpoint = None
if side == BUY:
endpoint = 'buy'
elif side == SELL:
endpoint = 'sell'
data = self._post(endpoint, parameters)
order = self.order_status(data['orderNumber'])
if 'error' not in order:
if len(data['resultingTrades']) == 0:
return order
else:
return Poloniex._trade_status(data['resultingTrades'], symbol, data['orderNumber'], amount)
return data
async def trades(self, pair: str, msg: dict, timestamp: float):
# adding because of error
trade_q = self.config.get(TRADES, [])
if self.config and pair in trade_q or not self.config:
pair = pair_exchange_to_std(pair)
for trade in msg:
await self.callback(TRADES, feed=self.id,
order_id=trade['FI'],
pair=pair,
side=BUY if trade['OT'] == 'BUY' else SELL,
amount=trade['Q'],
price=trade['R'],
timestamp=timestamp_normalize(self.id, trade['T']),
receipt_timestamp=timestamp)
"direction": "sell",
"amount": 10
}
],
"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
)
async def _trade(self, msg: dict, timestamp: float):
"""
Trade message
['T', '1', '1547947390', 'BTC_USDT', 'bid', '3683.74440000', '0.082', '33732290']
"""
ts = float(msg[2])
pair = pair_exchange_to_std(msg[3])
side = BUY if msg[4] == 'bid' else SELL
price = Decimal(msg[5])
amount = Decimal(msg[6])
trade_id = msg[7]
await self.callback(TRADES,
feed=self.id,
pair=pair,
order_id=trade_id,
side=side,
amount=amount,
price=price,
timestamp=ts,
receipt_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)