def test_margin_events_affect_gained_lost_amount(accountant): history = [{ "timestamp": 1476979735, "pair": "BTC_EUR", "type": "buy", "rate": 578.505, "cost": 2892.525, "cost_currency": "EUR", "fee": 0.0012, "fee_currency": "BTC", "amount": 5, "location": "kraken", }, { # 2519.62-0.02-((0.0012*578.505)/5 + 578.505) "timestamp": 1496979735, "pair": "BTC_EUR", "type": "sell", "rate": 2519.62, "cost": 2519.62, "cost_currency": "EUR", "fee": 0.02, "fee_currency": "EUR", "amount": 1, "location": "kraken", }] margin_history = [ MarginPosition( exchange='poloniex', # BTC/EUR: 810.49 open_time=1484438400, # 15/01/2017 close_time=1484629704, # 17/01/2017 profit_loss=FVal('-0.5'), pl_currency='BTC', notes='margin1', ), MarginPosition( exchange='poloniex', # BTC/EUR: 979.39 open_time=1487116800, # 15/02/2017 close_time=1487289600, # 17/02/2017 profit_loss=FVal('0.25'), pl_currency='BTC', notes='margin2', ) ] result = accounting_history_process( accountant, 1436979735, 1519693374, history, margin_list=margin_history, ) assert accountant.get_calculated_asset_amount('BTC').is_close('3.75') assert FVal(result['overview']['general_trade_profit_loss']).is_close( '1940.9561588') assert FVal(result['overview']['margin_positions_profit_loss']).is_close( '-160.3975') assert FVal(result['overview']['total_taxable_profit_loss']).is_close( '1780.5586588')
def test_margin_events_affect_gained_lost_amount(accountant): history = [{ 'timestamp': 1476979735, 'pair': 'BTC_EUR', 'trade_type': 'buy', 'rate': 578.505, 'fee': 0.0012, 'fee_currency': 'BTC', 'amount': 5, 'location': 'kraken', }, { # 2519.62-0.02-((0.0012*578.505)/5 + 578.505) 'timestamp': 1496979735, 'pair': 'BTC_EUR', 'trade_type': 'sell', 'rate': 2519.62, 'fee': 0.02, 'fee_currency': 'EUR', 'amount': 1, 'location': 'kraken', }] margin_history = [ MarginPosition( exchange='poloniex', # BTC/EUR: 810.49 open_time=1484438400, # 15/01/2017 close_time=1484629704, # 17/01/2017 profit_loss=FVal('-0.5'), pl_currency=A_BTC, notes='margin1', ), MarginPosition( exchange='poloniex', # BTC/EUR: 979.39 open_time=1487116800, # 15/02/2017 close_time=1487289600, # 17/02/2017 profit_loss=FVal('0.25'), pl_currency=A_BTC, notes='margin2', ) ] result = accounting_history_process( accountant, 1436979735, 1519693374, history, margin_list=margin_history, ) assert accountant.get_calculated_asset_amount('BTC').is_close('3.75') assert FVal(result['overview']['general_trade_profit_loss']).is_close( '1940.9561588') assert FVal(result['overview']['margin_positions_profit_loss']).is_close( '-160.3975') assert FVal(result['overview']['total_taxable_profit_loss']).is_close( '1780.5586588')
def do_read_manual_margin_positions( user_directory: FilePath) -> List[MarginPosition]: manual_margin_path = os.path.join(user_directory, MANUAL_MARGINS_LOGFILE) if os.path.isfile(manual_margin_path): with open(manual_margin_path, 'r') as f: margin_data = rlk_jsonloads(f.read()) else: margin_data = [] logger.info( 'Could not find manual margins log file at {}'.format( manual_margin_path), ) # Now turn the manual margin data to our MarginPosition format # The poloniex manual data format is: # { "open_time": unix_timestamp, "close_time": unix_timestamp, # "btc_profit_loss": floating_point_number for profit or loss, # "notes": "optional string with notes on the margin position" # } margin_positions = list() for position in margin_data: margin_positions.append( MarginPosition( exchange='poloniex', open_time=position['open_time'], close_time=position['close_time'], profit_loss=FVal(position['btc_profit_loss']), pl_currency=A_BTC, notes=position['notes'], ), ) return margin_positions
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=A_BTC, notes=notes, )
def test_trade_from_bitmex(): margin_positions = [ MarginPosition( exchange='bitmex', open_time=None, close_time=1536580800, profit_loss=FVal('0.00000003'), pl_currency=A_BTC, notes='XBTZ18', ), MarginPosition( exchange='bitmex', open_time=None, close_time=1536580800, profit_loss=FVal('0.0000004'), pl_currency=A_BTC, notes='XBTUSD', ), MarginPosition( exchange='bitmex', open_time=None, close_time=1536580800, profit_loss=FVal('0.00000183'), pl_currency=A_BTC, notes='XBTJPY', ), MarginPosition( exchange='bitmex', open_time=None, close_time=1536580800, profit_loss=FVal('0.00000683'), pl_currency=A_BTC, notes='ETHUSD', ), MarginPosition( exchange='bitmex', open_time=None, close_time=1536494400, profit_loss=FVal('0.00000002'), pl_currency=A_BTC, notes='XRPU18', ), MarginPosition( exchange='bitmex', open_time=None, close_time=1536494400, profit_loss=FVal('0.00000003'), pl_currency=A_BTC, notes='XBTUSD', ), MarginPosition( exchange='bitmex', open_time=None, close_time=1536494400, profit_loss=FVal('0.00000003'), pl_currency=A_BTC, notes='XBTJPY', ), MarginPosition( exchange='bitmex', open_time=None, close_time=1536494400, profit_loss=FVal('0.00000005'), pl_currency=A_BTC, notes='ETHUSD', ), MarginPosition( exchange='bitmex', open_time=None, close_time=1536494400, profit_loss=FVal('-0.00007992'), pl_currency=A_BTC, notes='ETHU18', ), ] assert len(BITMEX_FIRST_9_TRADES) == len(margin_positions) for idx, trade in enumerate(BITMEX_FIRST_9_TRADES): position = trade_from_bitmex(trade) assert position == margin_positions[idx] assert isinstance(position.pl_currency, Asset)
'block_number': 5417790, 'hash': DUMMY_HASH, 'from_address': DUMMY_ADDRESS, 'to_address': DUMMY_ADDRESS, 'value': 12323, 'gas': 5000000, 'gas_price': 2100000000, 'gas_used': 1900000, }, ] margin_history = [ MarginPosition( # before query period -- BTC/EUR: 422.90 exchange='poloniex', open_time=Timestamp(1463184190), # 14/05/2016 close_time=Timestamp(1464393600), # 28/05/2016 profit_loss=FVal(0.05), pl_currency=S_BTC, notes='margin1', ), MarginPosition( # before query period -- BTC/EUR: 542.87 exchange='poloniex', open_time=Timestamp(1472428800), # 29/08/2016 close_time=Timestamp(1473897600), # 15/09/2016 profit_loss=FVal('-0.042'), pl_currency=S_BTC, notes='margin2', ), MarginPosition( # BTC/EUR: 1039.935 exchange='poloniex', open_time=Timestamp(1489276800), # 12/03/2017 close_time=Timestamp(1491177600), # 03/04/2017