|
|
@@ -189,6 +189,29 @@ def positions_with_zero_size_response() -> DummyResponse:
|
|
|
)
|
|
|
|
|
|
|
|
|
+def positions_with_zero_size_malformed_avg_price_response() -> DummyResponse:
|
|
|
+ return DummyResponse(
|
|
|
+ {
|
|
|
+ "code": "0",
|
|
|
+ "msg": "",
|
|
|
+ "data": [
|
|
|
+ {
|
|
|
+ "instId": "BTC-USDT-SWAP",
|
|
|
+ "posSide": "long",
|
|
|
+ "pos": "0",
|
|
|
+ "avgPx": "bad",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "instId": "BTC-USDT-SWAP",
|
|
|
+ "posSide": "short",
|
|
|
+ "pos": "3",
|
|
|
+ "avgPx": "24900",
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
def market_long_signal() -> TradeSignal:
|
|
|
return TradeSignal(
|
|
|
action="long",
|
|
|
@@ -453,6 +476,24 @@ def test_place_demo_order_rejects_invalid_leverage_before_okx():
|
|
|
assert session.request_paths == []
|
|
|
|
|
|
|
|
|
+def test_place_demo_order_rejects_unknown_action_before_okx():
|
|
|
+ session = DummySession([])
|
|
|
+ signal = TradeSignal(
|
|
|
+ action="hold",
|
|
|
+ confidence=0.9,
|
|
|
+ leverage=2,
|
|
|
+ entry_price=None,
|
|
|
+ take_profit_price=None,
|
|
|
+ stop_loss_price=None,
|
|
|
+ reason="x",
|
|
|
+ )
|
|
|
+ client = OkxClient(config=sample_config(), session=session)
|
|
|
+
|
|
|
+ with pytest.raises(ValueError, match="action is invalid"):
|
|
|
+ client.place_demo_order(symbol="BTC-USDT-SWAP", signal=signal, margin_usdt=100)
|
|
|
+ assert session.request_paths == []
|
|
|
+
|
|
|
+
|
|
|
def test_get_positions_returns_normalized_positions():
|
|
|
session = DummySession([positions_response()])
|
|
|
client = OkxClient(config=sample_config(), session=session)
|
|
|
@@ -474,3 +515,14 @@ def test_get_positions_filters_zero_size_rows():
|
|
|
assert len(positions) == 1
|
|
|
assert positions[0].pos_side == "short"
|
|
|
assert positions[0].size == 3.0
|
|
|
+
|
|
|
+
|
|
|
+def test_get_positions_ignores_malformed_fields_on_zero_size_rows():
|
|
|
+ session = DummySession([positions_with_zero_size_malformed_avg_price_response()])
|
|
|
+ client = OkxClient(config=sample_config(), session=session)
|
|
|
+
|
|
|
+ positions = client.get_positions(symbol="BTC-USDT-SWAP")
|
|
|
+
|
|
|
+ assert len(positions) == 1
|
|
|
+ assert positions[0].pos_side == "short"
|
|
|
+ assert positions[0].avg_price == 24900.0
|