Beispiel #1
0
    def test_update_from_exchange_sell(self):
        owa = ActionOrder("ADA/ETH", 1000, 0.00032485131, "sell")
        resp = {"status": "open", "filled": 500, "cost": 0.32485131 / 2}
        owa.update_from_exchange(resp)
        self.assertEqual(owa.filled_start_amount, 500)
        self.assertEqual(owa.filled, 500)
        self.assertEqual(owa.status, "open")
        self.assertEqual(owa.state, "fill")

        self.assertEqual(
            "fill", owa.active_trade_order.supplementary["parent_action_order"]
            ["state"])

        self.assertEqual(owa.order_command, "hold")
        self.assertEqual(owa.filled_price, owa.active_trade_order.price)
Beispiel #2
0
    def test_fill(self):

        owa = ActionOrder.create_from_start_amount("ADA/ETH", "ADA", 1000,
                                                   "ETH", 0.00032485131)

        # update 1 - partial fill
        resp = {"status": "open", "filled": 500, "cost": 0.32485131 / 2}
        owa.update_from_exchange(resp)
        self.assertEqual(owa.filled_start_amount, 500)
        self.assertEqual(owa.filled, 500)
        self.assertEqual(owa.status, "open")
        self.assertEqual(owa.state, "fill")
        self.assertEqual(
            "fill", owa.active_trade_order.supplementary["parent_action_order"]
            ["state"])
        self.assertEqual(owa.order_command, "hold")
        self.assertEqual(owa.filled_price, owa.active_trade_order.price)

        # update 2 - complete fill
        resp = {"status": "closed", "filled": 1000, "cost": 0.32485131}
        owa.update_from_exchange(resp)

        self.assertEqual(owa.filled_start_amount, 1000)
        self.assertEqual(owa.filled, 1000)
        self.assertEqual(owa.status, "closed")
        self.assertEqual(owa.order_command, "")
        self.assertEqual(owa.state, "fill")
        self.assertEqual(
            "fill", owa.orders_history[0].supplementary["parent_action_order"]
            ["state"])

        self.assertEqual(1, len(owa.orders_history))

        self.assertListEqual(owa.tags, list())
Beispiel #3
0
    def test_create_next_order_for_remained_amount(self):
        ao = ActionOrder.create_from_start_amount("ADA/ETH", "ADA", 1000,
                                                  "ETH", 0.32485131)
        to = ao._create_next_trade_order_for_remained_amount(1)

        self.assertEqual(to.dest_currency, "ETH")
        self.assertEqual(to.amount, 1000)
        self.assertEqual(to.side, "sell")
        self.assertEqual(to.price, 1)
        self.assertEqual("fill",
                         to.supplementary["parent_action_order"]["state"])
        self.assertEqual(ao.amount, 1000)

        ao.filled_start_amount = 400

        to = ao._create_next_trade_order_for_remained_amount(666)

        self.assertEqual(to.dest_currency, "ETH")
        self.assertEqual(to.amount, 600)
        self.assertEqual(to.price, 666)

        with self.assertRaises(errors.OrderError) as e:
            ao.filled_start_amount = 1001
            to = ao._create_next_trade_order_for_remained_amount(666)

        self.assertEqual("Bad new order amount -1", e.exception.args[0])
Beispiel #4
0
 def test_create_trade_order(self):
     owa_order = ActionOrder("ADA/ETH", 1000, 0.32485131 / 1000, "sell")
     order = owa_order.active_trade_order
     self.assertEqual(order.dest_currency, "ETH")
     self.assertEqual(order.amount, 1000)
     self.assertEqual(order.side, "sell")
     self.assertEqual(owa_order.amount, 1000)
Beispiel #5
0
    def test_report(self):

        owa = ActionOrder.create_from_start_amount("ADA/ETH", "ADA", 1000,
                                                   "ETH", 0.00032485131)

        # update 1 - partial fill
        resp = {"status": "open", "filled": 500, "cost": 0.32485131 / 2}
        owa.update_from_exchange(resp)

        # update 2 - complete fill
        resp = {"status": "closed", "filled": 1000, "cost": 0.32485131}
        owa.update_from_exchange(resp)

        self.assertListEqual(owa.tags, list())

        report = owa.report()

        self.assertEqual(1000, report["filled"])
        self.assertEqual("closed", report["status"])
        self.assertNotIn("orders_history", report)
        self.assertEqual(None, report["market_data"])
        self.assertNotIn("tags", report)

        owa.tags.append("#test #data")

        report2 = owa.report()

        self.assertEqual(1000, report2["filled"])
        self.assertEqual("closed", report2["status"])
        self.assertNotIn("orders_history", report2)
        self.assertEqual(None, report2["market_data"])
        self.assertEqual("#test #data", report2["tags"])
Beispiel #6
0
    def test_snapshots(self):
        ao = ActionOrder.create_from_start_amount("ADA/ETH", "ADA", 1000,
                                                  "ETH", 0.32485131)

        self.assertTrue(ao.changed_from_last_update)

        snapshot = ao._snapshot()
        self.assertEqual(snapshot.symbol, "ADA/ETH")
        self.assertEqual(snapshot.filled, 0.0)
        self.assertEqual("", snapshot.active_trade_order_id)
        self.assertEqual("", snapshot.active_trade_order_status)

        resp = {"status": "open", "filled": 10, "cost": 0}

        ao.update_from_exchange(resp)
        self.assertTrue(ao.changed_from_last_update)
        self.assertEqual(00, ao.previous_snapshot.filled)

        resp = {"status": "open", "filled": 20, "cost": 0}

        ao.update_from_exchange(resp)
        self.assertTrue(ao.changed_from_last_update)
        self.assertEqual(10, ao.previous_snapshot.filled)

        resp = {"status": "open", "filled": 20, "cost": 0}

        ao.update_from_exchange(resp)
        self.assertFalse(ao.changed_from_last_update)
        self.assertEqual(10, ao.previous_snapshot.filled)

        resp = {"status": "open", "filled": 30, "cost": 0}

        ao.update_from_exchange(resp)
        self.assertTrue(ao.changed_from_last_update)
        self.assertEqual(20, ao.previous_snapshot.filled)
Beispiel #7
0
 def test_create_from_start_amount_buy(self):
     owa_order = ActionOrder.create_from_start_amount(
         "ADA/ETH", "ETH", 0.32485131, "ADA", 0.00032485131)
     self.assertEqual("ADA", owa_order.dest_currency)
     self.assertEqual("ETH", owa_order.start_currency)
     self.assertAlmostEqual(1000, owa_order.amount, 5)
     self.assertAlmostEqual(1000, owa_order.dest_amount, 5)
     self.assertEqual(owa_order.start_amount, 0.32485131)
     self.assertEqual(owa_order.side, "buy")
Beispiel #8
0
    def test_create_from_start_amount_sell(self):
        owa_order = ActionOrder.create_from_start_amount(
            "ADA/ETH", "ADA", 1000, "ETH", 0.00032485131)

        self.assertEqual(owa_order.dest_currency, "ETH")
        self.assertEqual(owa_order.amount, 1000)
        self.assertEqual(owa_order.start_currency, "ADA")
        self.assertEqual(owa_order.dest_amount, 0.00032485131 * 1000)
        self.assertEqual(owa_order.side, "sell")
        self.assertEqual(owa_order.amount, 1000)
Beispiel #9
0
    def test_trade_orders_report_no_order_history(self):

        owa = ActionOrder.create_from_start_amount("ADA/ETH", "ADA", 1000,
                                                   "ETH", 0.00032485131)
        orders_report = owa.closed_trade_orders_report()

        if orders_report:
            self.assertEqual(1, 1)

        self.assertEqual(False, bool(orders_report))
Beispiel #10
0
def log_scalp_order(_bot, _scalp, _scalp_order: ActionOrder):
    _bot.log(_bot.LOG_INFO, "######################################################################################")
    _bot.log(_bot.LOG_INFO, "Scalp ID: {}".format(_scalp.id))
    _bot.log(_bot.LOG_INFO,
             "State: {}. Order {}->{} status {} filled {}/{} (upd {}/{}).".
             format(_scalp.state,
                    _scalp_order.start_currency,
                    _scalp_order.dest_currency,
                    _scalp_order.status,
                    _scalp_order.filled,
                    _scalp_order.amount,
                    _scalp_order.get_active_order().update_requests_count,
                    _scalp_order.max_order_updates))
Beispiel #11
0
    def test_trade_order_report_from_trade_order_after_fill(self):

        order = ActionOrder.create_from_start_amount("ETH/BTC", "BTC", 1,
                                                     "ETH", 0.01)

        ew = ccxtExchangeWrapper.load_from_id(
            "binace")  # type: ccxtExchangeWrapper
        ew.set_offline_mode("test_data/markets.json", "test_data/tickers.csv")
        om = ActionOrderManager(ew)

        om.add_order(order)
        while len(om.get_open_orders()) > 0:
            om.proceed_orders()

        trade_order = om.get_closed_orders()[0].orders_history[
            0]  # type: TradeOrder

        order_report = TradeOrderReport.from_trade_order(
            trade_order,
            datetime.datetime.now(tz=pytz.timezone("UTC")),
            supplementary={"order_no": 1})

        self.assertEqual(order_report.symbol, trade_order.symbol)
        self.assertEqual(order_report.side, trade_order.side)
        self.assertEqual(order_report.amount, trade_order.amount)
        self.assertEqual(order_report.filled, trade_order.filled)
        self.assertEqual(order_report.cost, trade_order.cost)

        self.assertListEqual(order_report.trades, trade_order.trades)

        self.assertDictEqual(order_report.timestamp_closed,
                             trade_order.timestamp_closed)
        self.assertDictEqual(order_report.timestamp_open,
                             trade_order.timestamp_open)

        self.assertEqual(order_report.start_currency,
                         trade_order.start_currency)
        self.assertEqual(order_report.start_currency,
                         trade_order.start_currency)

        self.assertEqual(
            "fill", order_report.supplementary["parent_action_order"]["state"])
        self.assertEqual(1, order_report.supplementary["order_no"])
Beispiel #12
0
    def test_force_close(self):
        owa = ActionOrder.create_from_start_amount("ADA/ETH", "ADA", 1000,
                                                   "ETH", 0.00032485131)

        for i in range(1, 10):
            resp = {"status": "open", "filled": 0, "cost": 0}
            owa.update_from_exchange(resp)
            self.assertEqual(owa.filled, 0)
            self.assertEqual(owa.status, "open")

        self.assertEqual(False, owa._force_close)
        owa.force_close()
        self.assertEqual(True, owa._force_close)

        resp = {"status": "open", "filled": 0, "cost": 0}
        command = owa.update_from_exchange(resp)
        self.assertEqual("cancel", command)

        resp = {"status": "canceled", "filled": 0, "cost": 0}
        owa.update_from_exchange(resp)

        self.assertEqual("closed", owa.status)
        self.assertIn("#force_close", owa.tags)
Beispiel #13
0
    def test_comparison_eq(self):
        rm1 = ActionOrder("ADA/ETH", 1000, 0.32485131 / 1000, "buy")
        rm2 = ActionOrder("ADA/ETH", 1000, 0.32485131 / 1000, "sell")

        self.assertEqual(rm1, rm1)
        self.assertNotEqual(rm1, rm2)

        rm2.id = rm1.id
        self.assertEqual(rm1, rm1)

        rm2 = copy.copy(rm1)
        self.assertEqual(rm1, rm2)

        rm2.status = "closed"
        self.assertNotEqual(rm1, rm2)

        rm2 = copy.copy(rm1)
        rm2.filled = 1
        self.assertNotEqual(rm1, rm2)
Beispiel #14
0
    def test_om_data_requests_from_orders_commands(self):
        ex = zt.ccxtExchangeWrapper.load_from_id("binance")
        ex.set_offline_mode("test_data/markets.json", "test_data/tickers.csv")
        ex.load_markets()

        om = zt.ActionOrderManager(ex)

        order = ActionOrder("USD/RUR", 1, 70, "buy")
        order2 = ActionOrder("EUR/RUR", 1, 75, "buy")

        om.add_order(order)

        om.add_order(order2)

        order.order_command = "new tickers"
        order2.order_command = "new ma"

        # check for number of actionsv- should be 2 actions
        self.assertEqual(2, om.pending_actions_number())

        om.data_for_orders = {"ma": 5}
        om.proceed_orders(
        )  # proceed "new command with data request from exchange

        self.assertDictEqual(om.exchange.tickers, order.market_data[0])
        self.assertEqual(5, order2.market_data[0])
        self.assertDictEqual(
            om.data_for_orders,
            dict())  # check that data_for_orders was cleaned by order manager

        self.assertEqual(0, om.pending_actions_number())

        order.order_command = "hold tickers ETH/BTC ask;ma 5"  # will be second ticker from tickers.csv file and ma 5
        order2.order_command = "hold ma 5"
        om.data_for_orders = {"ma": {"5": 71}}  # let's add some more data
        om.proceed_orders()

        self.assertEqual(0.082975, order.market_data[0])
        self.assertEqual(71, order.market_data[1])
        self.assertEqual(71, order2.market_data[0])

        self.assertDictEqual(
            om.data_for_orders,
            dict())  # check that data_for_orders was cleaned by order manager

        # no market data
        order.order_command = "hold"
        order2.order_command = "hold ma"
        # om.data_for_orders = {"ma": {"5": 71}}  # let's add some more data
        om.proceed_orders()
        self.assertEqual(None, order.market_data)
        self.assertEqual(
            None, order2.market_data[0])  # no data was found with the request

        self.assertDictEqual(
            om.data_for_orders,
            dict())  # check that data_for_orders was cleaned by order manager
        self.assertEqual(None, order2.market_data[0])

        # 3rd request
        order.order_command = "hold tickers ETH/BTC bid"
        om.proceed_orders()
        self.assertEqual(0.082921, order.market_data[0])

        # 4rth request - no data available
        order.order_command = "cancel tickers ETH/BTC bid; ma 5"
        order2.order_command = "cancel tickers ETH/BTC bid; ma 5"

        om.data_for_orders = {"ma": {"5": 71}}  # let's add some more data
        om.proceed_orders()
        self.assertEqual(None, order.market_data[0])
        self.assertEqual(None, order2.market_data[0])

        self.assertEqual(71, order.market_data[1])
        self.assertEqual(71, order2.market_data[1])