def test_position_empty(self, bybit_mock, ws_mock): ex = bot.BybitExchange() # mock_bybit.bybit().Positions.Positions_myPosition().result().__getitem__.return_value = ( bybit_mock.bybit().Positions.Positions_myPosition().result = lambda: ( { "ret_code": 0, "ret_msg": "OK", "ext_code": "", "ext_info": "", "result": { "id": 0, "position_idx": 0, "mode": 0, "user_id": 2681267, "risk_id": 1, "symbol": "BTCUSD", "side": "None", "size": 0, "position_value": "0", "entry_price": "0", "is_isolated": False, "auto_add_margin": 1, "leverage": "100", "effective_leverage": "100", "position_margin": "0", "liq_price": "0", "bust_price": "0", "occ_closing_fee": "0", "occ_funding_fee": "0", "take_profit": "0", "stop_loss": "0", "trailing_stop": "0", "position_status": "Normal", "deleverage_indicator": 0, "oc_calc_data": '{"blq":0,"slq":0,"bmp":0,"smp":0,"bv2c":0.0115075,"sv2c":0.0114925}', "order_margin": "0", "wallet_balance": "0.00698913", "realised_pnl": "0", "unrealised_pnl": 0, "cum_realised_pnl": "-0.00025533", "cross_seq": 6028527302, "position_seq": 0, "created_at": "2021-04-12T10:29:40.395983654Z", "updated_at": "2021-04-21T00:00:02.116270109Z", }, "time_now": "1619009282.617507", "rate_limit_status": 119, "rate_limit_reset_ms": 1619009282614, "rate_limit": 120, }, None, ) with self.assertRaises(bot.NotInCycle): ex.position
def test_short(self, bybit_mock, ws_mock): ex = bot.BybitExchange() ex.short(0, 0) bybit_mock.bybit().Order.Order_new.assert_called_with( side="Sell", symbol="BTCUSD", order_type="Limit", qty=0, price=0, time_in_force="PostOnly", )
def test_cancel(self, bybit_mock, ws_mock): ex = bot.BybitExchange() ex.cancel("88d6be9c-c6f4-4c2b-87c4-05cb67d2bfc4") bybit_mock.bybit().Order.Order_cancel.assert_called_with( symbol="BTCUSD", order_id="88d6be9c-c6f4-4c2b-87c4-05cb67d2bfc4")
def test_cancel_all(self, bybit_mock, ws_mock): ex = bot.BybitExchange() ex.cancel_all() bybit_mock.bybit().Order.Order_cancelAll.assert_called_with( symbol="BTCUSD")
def test_ask(self, bybit_mock, ws_mock): ex = bot.BybitExchange() ex.ask bybit_mock.bybit().Market.Market_symbolInfo.assert_called_once()
def test_orders(self, bybit_mock, ws_mock): ex = bot.BybitExchange() ws_mock.BybitWebsocket().get_data.return_value = [ { "user_id": 2681267, "position_idx": 0, "order_status": "New", "symbol": "BTCUSD", "side": "Buy", "order_type": "Limit", "price": "55600", "qty": "2", "time_in_force": "PostOnly", "order_link_id": "", "order_id": "d0aa620e-bcbd-41c6-9315-f1be7570bfe3", "created_at": "2021-04-20T12:35:28.941Z", "updated_at": "2021-04-20T12:35:28.941Z", "leaves_qty": "2", "leaves_value": "0.00003597", "cum_exec_qty": "0", "cum_exec_value": "0", "cum_exec_fee": "0", "reject_reason": "EC_NoError", }, { "user_id": 2681267, "position_idx": 0, "order_status": "New", "symbol": "BTCUSD", "side": "Buy", "order_type": "Limit", "price": "55600", "qty": "1", "time_in_force": "PostOnly", "order_link_id": "", "order_id": "d3aa620e-bcbd-41c6-9315-f1be7570bfe3", "created_at": "2021-04-20T12:35:28.941Z", "updated_at": "2021-04-20T12:35:28.941Z", "leaves_qty": "1", "leaves_value": "0.00003597", "cum_exec_qty": "0", "cum_exec_value": "0", "cum_exec_fee": "0", "reject_reason": "EC_NoError", }, { "user_id": 2681267, "position_idx": 0, "order_status": "New", "symbol": "BTCUSD", "side": "Sell", "order_type": "Limit", "price": "56300", "qty": "1", "time_in_force": "PostOnly", "order_link_id": "", "order_id": "5b7eebcf-c43c-4396-8aea-07c6d9dad76e", "created_at": "2021-04-20T12:35:09.135Z", "updated_at": "2021-04-20T12:35:09.135Z", "leaves_qty": "1", "leaves_value": "0.00001776", "cum_exec_qty": "0", "cum_exec_value": "0", "cum_exec_fee": "0", "reject_reason": "EC_NoError", }, { "user_id": 2681267, "position_idx": 0, "order_status": "New", "symbol": "BTCUSD", "side": "Sell", "order_type": "Limit", "price": "56500", "qty": "1", "time_in_force": "PostOnly", "order_link_id": "", "order_id": "88d6be9c-c6f4-4c2b-87c4-05cb67d2bfc4", "created_at": "2021-04-20T12:35:00.350Z", "updated_at": "2021-04-20T12:35:00.350Z", "leaves_qty": "1", "leaves_value": "0.00001769", "cum_exec_qty": "0", "cum_exec_value": "0", "cum_exec_fee": "0", "reject_reason": "EC_NoError", }, ] self.assertEqual( ex.orders, bot.Orders( longs={ "d3aa620e-bcbd-41c6-9315-f1be7570bfe3": bot.Order( order_id="d3aa620e-bcbd-41c6-9315-f1be7570bfe3", side="Buy", price=55600.0, quantity=1, order_status="New", ), "d0aa620e-bcbd-41c6-9315-f1be7570bfe3": bot.Order( order_id="d0aa620e-bcbd-41c6-9315-f1be7570bfe3", side="Buy", price=55600.0, quantity=2, order_status="New", ), }, shorts={ "5b7eebcf-c43c-4396-8aea-07c6d9dad76e": bot.Order( order_id="5b7eebcf-c43c-4396-8aea-07c6d9dad76e", side="Sell", price=56300.0, quantity=1, order_status="New", ), "88d6be9c-c6f4-4c2b-87c4-05cb67d2bfc4": bot.Order( order_id="88d6be9c-c6f4-4c2b-87c4-05cb67d2bfc4", side="Sell", price=56500.0, quantity=1, order_status="New", ), }, ), )
def test_orders_empty(self, bybit_mock, ws_mock): ex = bot.BybitExchange() ws_mock.get_data.return_value = [] self.assertEqual(ex.orders, bot.Orders(longs={}, shorts={}))
def test_rest_orders(self, bybit_mock, ws_mock): ex = bot.BybitExchange() ex.orders bybit_mock.bybit().Order.Order_getOrders.assert_called_with( symbol="BTCUSD", order_status="New")
def test_position_value(self, mock_bybit, ws_mock): ex = bot.BybitExchange() # mock_bybit.bybit().Positions.Positions_myPosition().result().__getitem__.return_value = ( mock_bybit.bybit().Positions.Positions_myPosition().result = lambda: ( { "ret_code": 0, "ret_msg": "OK", "ext_code": "", "ext_info": "", "result": { "id": 0, "position_idx": 0, "mode": 0, "user_id": 153179, "risk_id": 1, "symbol": "BTCUSD", "side": "Buy", "size": 1, "position_value": "0.00001791", "entry_price": "55834.72920156", "is_isolated": False, "auto_add_margin": 0, "leverage": "100", "effective_leverage": "100", "position_margin": "0.00000021", "liq_price": "2.5", "bust_price": "2.5", "occ_closing_fee": "0.0003", "occ_funding_fee": "0", "take_profit": "0", "stop_loss": "0", "trailing_stop": "0", "position_status": "Normal", "deleverage_indicator": 1, "oc_calc_data": '{"blq":0,"slq":0,"bmp":0,"smp":0,"fq":-1,"bv2c":0.0115075,"sv2c":0.0114925}', "order_margin": "0", "wallet_balance": "0.5", "realised_pnl": "0", "unrealised_pnl": -1e-08, "cum_realised_pnl": "0", "cross_seq": 2898033321, "position_seq": 0, "created_at": "2021-04-21T16:27:37.947905736Z", "updated_at": "2021-04-21T16:53:29.651160082Z", "tp_sl_mode": "Full", }, "time_now": "1619024009.787388", "rate_limit_status": 119, "rate_limit_reset_ms": 1619024009784, "rate_limit": 120, }, None, ) self.assertEqual( ex.position, bot.Position( entry_price=55834.5, real_entry_price=55834.72920156, quantity=1, rate_limit_status=119, rate_limit_reset="2021-04-21 18:53:29.784000", rate_limit=120, unrealised_pnl=-1e-08, liq_price=2.5, ), )
def test_position(self, bybit_mock, ws_mock): ex = bot.BybitExchange() ex.position bybit_mock.bybit().Positions.Positions_myPosition.assert_called_with( symbol="BTCUSD")