from exchange import Exchange from trade import Trades def test_connect_exchange(): exchange = Exchange() test_trades_obj = Trades() test_trades_obj.connect_exchange(exchange) print(f'\nConnected to exchange: {test_trades_obj.exchange_connected()}') account_info = exchange.get_precision(symbol='ETHUSDT') print(account_info) assert test_trades_obj.exchange_connected() def test_get_trades_by_status(): # Connect to the exchange exchange = Exchange() test_trades_obj = Trades() test_trades_obj.connect_exchange(exchange) print(f'\nConnected to exchange: {test_trades_obj.exchange_connected()}') assert test_trades_obj.exchange_connected() # create a trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 1.5, price=100, offset=None) print('trade 0 created.') print(test_trades_obj.active_trades[0].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[0].status is 'inactive' # create a 2nd trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.4, price=2100, offset=None) print('trade 1 created.') print(test_trades_obj.active_trades[1].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[1].status is 'inactive' # create a 3rd trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.4, price=2100, offset=None) print('trade 2 created.') print(test_trades_obj.active_trades[2].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[1].status is 'inactive' # should be three trades in this list print(f'Expecting 3 trades in list: Actual:{len(test_trades_obj.active_trades)}') assert len(test_trades_obj.active_trades) is 3 print(test_trades_obj.active_trades[0].status) print(test_trades_obj.active_trades[1].status) print(test_trades_obj.active_trades[2].status) # fill trade one test_trades_obj.active_trades[1].trade_filled(0.4, 2100) print(f'trade 1 filled. status:') print(test_trades_obj.active_trades[1].status) # Search for all inactive trades result = test_trades_obj.get_trades_by_status('inactive') print(f'search for all inactive trades. The result: {result}') assert len(result) is 2 # Search for all filled trades result = test_trades_obj.get_trades_by_status('filled') print(f'search for all filled trades. The result: {result}') assert len(result) is 1 def test_get_trade_by_id(): # Connect to the exchange exchange = Exchange() test_trades_obj = Trades() test_trades_obj.connect_exchange(exchange) print(f'\nConnected to exchange: {test_trades_obj.exchange_connected()}') assert test_trades_obj.exchange_connected() # create a trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 1.5, price=100, offset=None) print('trade 0 created.') print(test_trades_obj.active_trades[0].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[0].status is 'inactive' # create a 2nd trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.4, price=2100, offset=None) print('trade 1 created.') print(test_trades_obj.active_trades[1].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[1].status is 'inactive' id = test_trades_obj.active_trades[0].unique_id print(f'the id of trade 0 is{id}') result = test_trades_obj.get_trade_by_id(id) print(f'here is the result after searching for the id:{result}') assert result.unique_id is id def test_load_trades(): assert False def test_place_order(): # Connect to the exchange exchange = Exchange() test_trades_obj = Trades() test_trades_obj.connect_exchange(exchange) print(f'\nConnected to exchange: {test_trades_obj.exchange_connected()}') # Create a new treade on the exchange. test_trades_obj.new_trade('exchange', 'BTCUSDT', 'BUY', 'MARKET', 0.1, price=100, offset=None) print(test_trades_obj.active_trades[0].__dict__) # If the status of the trade is unfilled the order is placed. assert test_trades_obj.active_trades[0].status is 'unfilled' def test_update(): # Connect to the exchange exchange = Exchange() test_trades_obj = Trades() test_trades_obj.connect_exchange(exchange) print(f'\nConnected to exchange: {test_trades_obj.exchange_connected()}') # Create a trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.4, price=100, offset=None) print(test_trades_obj.active_trades[0].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[0].status is 'inactive' # create a 2nd trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.2, price=100, offset=None) print(test_trades_obj.active_trades[1].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[1].status is 'inactive' test_trades_obj.active_trades[0].trade_filled(0.4, 100) test_trades_obj.active_trades[1].trade_filled(0.2, 100) test_trades_obj.update(200) print(test_trades_obj.active_trades[0].__dict__) print(test_trades_obj.active_trades[1].__dict__) def test_new_trade(): # Connect to the exchange exchange = Exchange() test_trades_obj = Trades() test_trades_obj.connect_exchange(exchange) print(f'\nConnected to exchange: {test_trades_obj.exchange_connected()}') # create an trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.1, price=100, offset=None) print(test_trades_obj.active_trades[0].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[0].status is 'inactive' # create a 2nd trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.4, price=2100, offset=None) print(test_trades_obj.active_trades[1].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[1].status is 'inactive' def test_close_trade(): # Connect to the exchange exchange = Exchange() test_trades_obj = Trades() test_trades_obj.connect_exchange(exchange) print(f'\nConnected to exchange: {test_trades_obj.exchange_connected()}') # create a trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.1, price=100, offset=None) print(test_trades_obj.active_trades[0].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[0].status is 'inactive' # create a 2nd trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.4, price=2100, offset=None) print(test_trades_obj.active_trades[1].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[1].status is 'inactive' # should be two trades in this list print(len(test_trades_obj.active_trades)) assert len(test_trades_obj.active_trades) > 1 trade_id = test_trades_obj.active_trades[0].unique_id test_trades_obj.close_trade(trade_id) # should be 1 trade in this list print(len(test_trades_obj.active_trades)) assert len(test_trades_obj.active_trades) == 1 def test_reduce_trade(): # Connect to the exchange exchange = Exchange() test_trades_obj = Trades() test_trades_obj.connect_exchange(exchange) print(f'\nConnected to exchange: {test_trades_obj.exchange_connected()}') assert test_trades_obj.exchange_connected() # create a trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 1.5, price=100, offset=None) print('trade 0 created.') print(test_trades_obj.active_trades[0].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[0].status is 'inactive' # create a 2nd trade but not on the exchange test_trades_obj.new_trade('backtestor', 'BTCUSDT', 'BUY', 'MARKET', 0.4, price=2100, offset=None) print('trade 1 created.') print(test_trades_obj.active_trades[1].__dict__) # If the status of the trade is inactive the trade is created but the order isn't placed. assert test_trades_obj.active_trades[1].status is 'inactive' # should be two trades in this list print(f'Expecting 2 trades in list: Actual:{len(test_trades_obj.active_trades)}') assert len(test_trades_obj.active_trades) > 1 # Grab inactive trade 0 trade_id = test_trades_obj.active_trades[0].unique_id # reduce the trade by 0.5 remaining_qty = test_trades_obj.reduce_trade(trade_id, 0.5) # The trade should be 1 (1.5 - 0.5) print(f'The remaining quantity of the trade should be 1: Actual: {remaining_qty}') assert remaining_qty == 1 print('trade 0:') print(test_trades_obj.active_trades[0].__dict__) test_trades_obj.active_trades[1].trade_filled(0.4, 2100) # Grab filled trade 1 trade_id = test_trades_obj.active_trades[1].unique_id # reduce the trade by 0.1 remaining_qty = float(test_trades_obj.reduce_trade(trade_id, 0.1)) # The trade should be 0.3 (0.4 - 0.1) print(f'\nThe remaining quantity of trade 1 should be 0.3: Actual: {remaining_qty}') assert remaining_qty == 0.3 print('trade 1:') print(test_trades_obj.active_trades[1].__dict__)