def trade_from_bitmex(bitmex_trade: Dict) -> MarginPosition: """Turn a bitmex trade returned from bitmex trade history to our common trade history format. This only returns margin positions as bitmex only deals in margin trading""" close_time = iso8601ts_to_timestamp(bitmex_trade['transactTime']) profit_loss = satoshis_to_btc(FVal(bitmex_trade['amount'])) currency = bitmex_to_world(bitmex_trade['currency']) notes = bitmex_trade['address'] assert currency == 'BTC', 'Bitmex trade should only deal in BTC' log.debug( 'Processing Bitmex Trade', sensitive_log=True, timestamp=close_time, profit_loss=profit_loss, currency=currency, notes=notes, ) return MarginPosition( exchange='bitmex', open_time=None, close_time=close_time, profit_loss=profit_loss, pl_currency=S_BTC, notes=notes, )
def query_trade_history( self, start_ts: typing.Timestamp, end_ts: typing.Timestamp, end_at_least_ts: typing.Timestamp, market: Optional[str] = None, count: Optional[int] = None, ) -> List: try: # We know user/walletHistory returns a list resp = self._api_query('get', 'user/walletHistory') except RemoteError as e: msg = ('Bitmex API request failed. Could not reach bitmex due ' 'to {}'.format(e)) log.error(msg) return list() log.debug('Bitmex trade history query', results_num=len(resp)) realised_pnls = [] for tx in resp: if tx['timestamp'] is None: timestamp = None else: timestamp = iso8601ts_to_timestamp(tx['timestamp']) if tx['transactType'] != 'RealisedPNL': continue if timestamp and timestamp < start_ts: continue if timestamp and timestamp > end_ts: continue realised_pnls.append(tx) return realised_pnls
def query_deposits_withdrawals( self, start_ts: typing.Timestamp, end_ts: typing.Timestamp, end_at_least_ts: typing.Timestamp, market: Optional[str] = None, count: Optional[int] = None, ) -> List: # TODO: Implement cache like in other exchange calls try: resp = self._api_query('get', 'user/walletHistory') except RemoteError as e: msg = ( 'Bitmex API request failed. Could not reach bitmex due ' 'to {}'.format(e) ) log.error(msg) return list() log.debug('Bitmex deposit/withdrawals query', results_num=len(resp)) movements = list() for movement in resp: transaction_type = movement['transactType'] if transaction_type not in ('Deposit', 'Withdrawal'): continue timestamp = iso8601ts_to_timestamp(movement['timestamp']) if timestamp < start_ts: continue if timestamp > end_ts: continue asset = bitmex_to_world(movement['currency']) amount = FVal(movement['amount']) fee = FVal(0) if movement['fee'] is not None: fee = FVal(movement['fee']) # bitmex has negative numbers for withdrawals if amount < 0: amount *= -1 if asset == 'BTC': # bitmex stores amounts in satoshis amount = satoshis_to_btc(amount) fee = satoshis_to_btc(fee) movements.append(AssetMovement( exchange='bitmex', category=transaction_type, timestamp=timestamp, asset=asset, amount=amount, fee=fee, )) return movements
def test_iso8601ts_to_timestamp(): assert iso8601ts_to_timestamp('2018-09-09T12:00:00.000Z') == 1536494400 assert iso8601ts_to_timestamp('2011-01-01T04:13:22.220Z') == 1293855202 assert iso8601ts_to_timestamp('1986-11-04T16:23:57.921Z') == 531505437