Skip to content

Commit

Permalink
Update Phemex, add sandbox endpoint, correct rounding errors, use cor…
Browse files Browse the repository at this point in the history
…rect volume
  • Loading branch information
bmoscon committed Nov 28, 2021
1 parent 2e9d850 commit fec6686
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions cryptofeed/exchanges/phemex.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Phemex(Feed):
id = PHEMEX
symbol_endpoint = 'https://api.phemex.com/exchange/public/cfg/v2/products'
websocket_endpoint = 'wss://phemex.com/ws'
sandbox_endpoint = 'wss://testnet.phemex.com/ws'
price_scale = {}
valid_candle_intervals = ('1m', '5m', '15m', '30m', '1h', '4h', '1d', '1M', '1Q', '1Y')
candle_interval_map = {interval: second for interval, second in zip(valid_candle_intervals, [60, 300, 900, 1800, 3600, 14400, 86400, 604800, 2592000, 7776000, 31104000])}
Expand Down Expand Up @@ -96,11 +97,11 @@ async def _book(self, msg: dict, timestamp: float):

if msg['type'] == 'snapshot':
delta = None
self._l2_book[symbol] = OrderBook(self.id, symbol, max_depth=self.max_depth, bids={Decimal(entry[0] / self.price_scale[symbol]): Decimal(entry[1]) for entry in msg['book']['bids']}, asks={Decimal(entry[0] / self.price_scale[symbol]): Decimal(entry[1]) for entry in msg['book']['asks']})
self._l2_book[symbol] = OrderBook(self.id, symbol, max_depth=self.max_depth, bids={Decimal(entry[0]) / Decimal(self.price_scale[symbol]): Decimal(entry[1]) for entry in msg['book']['bids']}, asks={Decimal(entry[0]) / Decimal(self.price_scale[symbol]): Decimal(entry[1]) for entry in msg['book']['asks']})
else:
for key, side in (('asks', ASK), ('bids', BID)):
for price, amount in msg['book'][key]:
price = Decimal(price / self.price_scale[symbol])
price = Decimal(price) / Decimal(self.price_scale[symbol])
amount = Decimal(amount)
delta[side].append((price, amount))
if amount == 0:
Expand Down Expand Up @@ -130,7 +131,7 @@ async def _trade(self, msg: dict, timestamp: float):
symbol,
BUY if side == 'Buy' else SELL,
Decimal(amount),
Decimal(price / self.price_scale[symbol]),
Decimal(price) / Decimal(self.price_scale[symbol]),
self.timestamp_normalize(ts),
raw=msg
)
Expand All @@ -150,18 +151,18 @@ async def _candle(self, msg: dict, timestamp: float):
symbol = self.exchange_symbol_to_std_symbol(msg['symbol'])

for entry in msg['kline']:
ts, _, _, open, high, low, close, _, volume = entry
ts, _, _, open, high, low, close, volume, _ = entry
c = Candle(
self.id,
symbol,
ts,
ts + self.candle_interval_map[self.candle_interval],
self.candle_interval,
None,
Decimal(open / self.price_scale[symbol]),
Decimal(close / self.price_scale[symbol]),
Decimal(high / self.price_scale[symbol]),
Decimal(low / self.price_scale[symbol]),
Decimal(open) / Decimal(self.price_scale[symbol]),
Decimal(close) / Decimal(self.price_scale[symbol]),
Decimal(high) / Decimal(self.price_scale[symbol]),
Decimal(low) / Decimal(self.price_scale[symbol]),
Decimal(volume),
None,
None
Expand Down Expand Up @@ -624,7 +625,6 @@ def connect(self) -> List[Tuple[AsyncConnection, Callable[[None], None], Callabl
return ret

async def message_handler(self, msg: str, conn: AsyncConnection, timestamp: float):

msg = json.loads(msg, parse_float=Decimal)

if 'id' in msg and msg['id'] == 100:
Expand Down

0 comments on commit fec6686

Please sign in to comment.