brighter-trading/tests/test_trade2.py

175 lines
6.7 KiB
Python

from trade import Trade
def test_get_position_size():
opening_price = 1000
print('%s %d' % ('opening_price', opening_price))
quantity = 0.01
print('%s %d' % ('quantity', quantity))
trade_obj = Trade(target='backtester', symbol='BTCUSD', side='BUY', order_price=opening_price,
base_order_qty=quantity, order_type='MARKET', time_in_force='GTC',
unique_id=None, status=None, stats=None, order=None)
print('trade_obj')
print(trade_obj.__dict__)
# Position size after construction.
position_size = trade_obj.get_position_size()
print('\nPosition size after construction.')
print(f'Trade for {quantity} BTC @ {opening_price} of the quote currency. Price has not changed.')
print(f'Position size: {position_size}')
assert position_size == opening_price * quantity
# Position size after current price changes on unopened trade.
print('\nPosition size after current price changes on unopened trade.')
current_price = 50
result = trade_obj.update(current_price)
print(f'the result of the update {result}')
position_size = trade_obj.get_position_size()
print(f'{quantity} BTC bought at {opening_price} is current priced at {current_price}')
print(f'Position size: {position_size}')
assert position_size != opening_price * quantity
print('\nPosition size after trade fills at opening price.')
# Position size after trade fills
trade_obj.trade_filled(quantity, opening_price)
position_size = trade_obj.get_position_size()
print(f'{quantity} BTC bought at {opening_price} is current priced at {current_price}')
print(f'Position size: {position_size}')
assert position_size == current_price * quantity
print('\nPosition size after current price changes on live trade.')
# Position size after current price changes on open trade
current_price = 50
result = trade_obj.update(current_price)
print(f'the result of the update {result}')
position_size = trade_obj.get_position_size()
print(f'{quantity} BTC bought at {opening_price} is current priced at {current_price}')
print(f'Position size: {position_size}')
assert position_size == current_price * quantity
def test_update_values():
# Create a test trade.
opening_price = 100
quantity = 0.1
trade_obj = Trade(target='backtester', symbol='BTCUSD', side='BUY', order_price=opening_price,
base_order_qty=quantity, order_type='MARKET', time_in_force='GTC',
unique_id=None, status=None, stats=None, order=None)
trade_obj.trade_filled(quantity, opening_price)
print(f'\nopening price is {opening_price}')
# Output the pl for unchanged trade.
print('\n Live trade price has not changed.')
position_size = trade_obj.get_position_size()
print(f'Position size: {position_size}')
assert position_size == 10
pl = trade_obj.get_pl()
print(f'PL reported: {pl}')
assert pl == 0
pl_pct = trade_obj.get_pl_pct()
print(f'PL% reported: {pl_pct}')
assert pl_pct == 0
# Divide the price of the quote symbol by 2.
current_price = 50
trade_obj.update_values(current_price)
# Output PL for adjusted trade.
print(f'\n Live trade price has changed to {current_price}.')
position_size = trade_obj.get_position_size()
print(f'Position size: {position_size}')
assert position_size == 5
pl = trade_obj.get_pl()
print(f'PL reported: {pl}')
# Should be: - 1/2 * opening value - opening value * fee - closing value * fee
# 5 - 1 - 0.5 = -6.5
assert pl == -6.5
pl_pct = trade_obj.get_pl_pct()
print(f'PL% reported: {pl_pct}')
# Should be -6.5/10 = -65%
assert pl_pct == -65
# Add 1/2 to the price of the quote symbol.
current_price = 150
trade_obj.update_values(current_price)
# Output PL for adjusted trade.
print(f'\n Live trade price has changed to {current_price}.')
position_size = trade_obj.get_position_size()
print(f'Position size: {position_size}')
assert position_size == 15
pl = trade_obj.get_pl()
print(f'PL reported: {pl}')
# Should be 5 - opening fee - closing fee
# fee should be (10 * .1) + (15 * .1) = 2.5
assert pl == 2.5
pl_pct = trade_obj.get_pl_pct()
print(f'PL% reported: {pl_pct}')
# should be 2.5/10 = 25%
assert pl_pct == 25
def test_update():
# Create test trade.
opening_price = 1000
quantity = 0.01
trade_obj = Trade(target='backtester', symbol='BTCUSD', side='BUY', order_price=opening_price,
base_order_qty=quantity, order_type='MARKET', time_in_force='GTC',
unique_id=None, status=None, stats=None, order=None)
print('\nUpdate price of un-placed trade.')
# Use Update to change the price of the inactive trade.
current_price = 50
result = trade_obj.update(current_price)
print(f'The result {result}')
assert result == 'inactive'
# Simulate a placed trade.
trade_obj.trade_filled(0.01, 1000)
print('\nUpdate price of live trade.')
# Use Update to change the price.
current_price = 50
result = trade_obj.update(current_price)
print(f'The result {result}')
assert result == 'filled'
position_size = trade_obj.get_position_size()
print(f'\n{quantity} BTC bought at {opening_price} is current priced at {current_price}')
print(f'Position size: {position_size}')
assert position_size == current_price * quantity
def test_trade_filled():
# Create a test trade.
opening_price = 100
quantity = 0.1
trade_obj = Trade(target='backtester', symbol='BTCUSD', side='BUY', order_price=opening_price,
base_order_qty=quantity, order_type='MARKET', time_in_force='GTC',
unique_id=None, status=None, stats=None, order=None)
trade_obj.trade_filled(qty=0.05, price=100)
status = trade_obj.get_status()
print(f'\n Status after trade_filled() called: {status}')
assert status == 'part_filled'
trade_obj.trade_filled(qty=0.05, price=100)
status = trade_obj.get_status()
print(f'\n Status after trade_filled() called: {status}')
assert status == 'filled'
def test_settle():
# Create a test trade.
opening_price = 100
quantity = 0.1
trade_obj = Trade(target='backtester', symbol='BTCUSD', side='BUY', order_price=opening_price,
base_order_qty=quantity, order_type='MARKET', time_in_force='GTC',
unique_id=None, status=None, stats=None, order=None)
trade_obj.trade_filled(qty=quantity, price=opening_price)
print(f'\ninitial status: {trade_obj.get_status()}')
print('settle the trade at 120')
trade_obj.settle(qty=0.1, price=120)
status = trade_obj.get_status()
print(f'final status: {status}')
assert status == 'closed'