def test_failed_leg1(self): ex = ccxtExchangeWrapper.load_from_id("binance") #type: ccxtExchangeWrapper ex.set_offline_mode("test_data/markets.json", "test_data/tickers_maker.csv") ex.load_markets() ex.fetch_tickers() tickers = ex.fetch_tickers() # second fetch contains good triangle om = ActionOrderManager(ex) om.offline_order_updates = 10 good_triangle = self.good_triangle maker = SingleTriArbMakerDeal(currency1=good_triangle["cur1"], currency2=good_triangle["cur2"], currency3=good_triangle["cur3"], price1=good_triangle["leg1-price"], price2=good_triangle["leg2-price"], price3=good_triangle["leg3-price"], start_amount=0.01, min_amount_currency1=0.003, symbol1=good_triangle["symbol1"], symbol2=good_triangle["symbol2"], symbol3=good_triangle["symbol3"], commission=0.00075, commission_maker=0.0006, threshold=1.001, max_order1_updates=2000, max_order2_updates=2000, max_order3_updates=2000, uuid="test" ) maker.update_state(tickers) om.add_order(maker.order1) om.proceed_orders() maker.update_state(tickers) maker.order1.force_close() om.proceed_orders() maker.update_state(tickers) self.assertEqual("Failed", maker.status) self.assertEqual(0, maker.leg2_recovery_amount) self.assertEqual(0, maker.leg2_recovery_target) self.assertEqual(0, maker.leg3_recovery_amount) self.assertEqual(0, maker.leg3_recovery_target) self.assertEqual(0, maker.filled_start_amount) self.assertEqual(0, maker.result_amount) self.assertEqual(0, maker.gross_profit)
def test_recovery(self): ex = ccxtExchangeWrapper.load_from_id("binance") #type: ccxtExchangeWrapper ex.set_offline_mode("test_data/markets.json", "test_data/tickers_maker.csv") ex.load_markets() ex.fetch_tickers() tickers = ex.fetch_tickers() # second fetch contains good triangle om = ActionOrderManager(ex) om.offline_order_updates = 10 good_triangle = self.good_triangle maker = SingleTriArbMakerDeal(currency1=good_triangle["cur1"], currency2=good_triangle["cur2"], currency3=good_triangle["cur3"], price1=good_triangle["leg1-price"], price2=good_triangle["leg2-price"], price3=good_triangle["leg3-price"], start_amount=0.01, min_amount_currency1=0.003, symbol1=good_triangle["symbol1"], symbol2=good_triangle["symbol2"], symbol3=good_triangle["symbol3"], commission=0.00075, commission_maker=0.0006, threshold=1.001, max_order1_updates=2000, max_order2_updates=2000, max_order3_updates=2000, recover_factor_order2=1.2, recover_factor_order3=1.3, uuid="test" ) maker.update_state(tickers) om.add_order(maker.order1) while len(om.get_open_orders()) > 0: # !!!! # first we proceed the orders and we update the deal manager with new updated orders om.proceed_orders() maker.update_state(tickers) om.add_order(maker.order2) om.proceed_orders() # this will create order while maker.order2.get_active_order().update_requests_count < 6: om.proceed_orders() # this will fill order once for 1/10 of amount maker.update_state(tickers) print("") print("Force Cancel") print("") # let's force to close the order maker.order2.force_close() om.proceed_orders() maker.update_state(tickers) self.assertEqual("closed", maker.order2.status) self.assertLess(maker.order2.filled, maker.order2.amount) self.assertLess(0, maker.leg2_recovery_amount) self.assertLess(0, maker.leg2_recovery_target) self.assertEqual(maker.leg2_recovery_amount, maker.order2.start_amount - maker.order2.filled_start_amount) self.assertAlmostEqual(0.5 * maker.order2.amount, maker.leg2_recovery_amount, 6) self.assertAlmostEqual(1/2, maker.order2.filled / maker.order2.amount, 6) self.assertAlmostEqual(0.005*1.2, maker.leg2_recovery_target, 6) # proceed to leg3 self.assertEqual("order3_create", maker.state) om.add_order(maker.order3) while maker.order3.get_active_order().update_requests_count < 6: om.proceed_orders() # this will fill order once for 1/10 of amount maker.update_state(tickers) # let's force to close the order maker.order3.force_close() om.proceed_orders() maker.update_state(tickers) self.assertEqual(maker.leg3_recovery_amount, maker.order3.start_amount - maker.order3.filled_start_amount) self.assertAlmostEqual(1 / 2, maker.order3.filled / maker.order3.amount, 6) self.assertAlmostEqual(0.0025*1.3, maker.leg3_recovery_target, 6) self.assertAlmostEqual(0.5 * maker.order3.amount, maker.leg3_recovery_amount, 6) self.assertEqual("finished", maker.state) self.assertEqual("InRecovery", maker.status) self.assertEqual(0.01, maker.filled_start_amount) self.assertAlmostEqual((0.01/4) * good_triangle["result"], maker.result_amount, 4) self.assertAlmostEqual((0.01 / 4) * good_triangle["result"] - 0.01, maker.gross_profit, 4)