From 72f0090fb83974f857ef6c1273adaa13479d4bd3 Mon Sep 17 00:00:00 2001 From: dekun Date: Sun, 7 Jun 2026 17:52:12 +0800 Subject: [PATCH] fix(trend): pass empty dict to ccxt create_order for DCA adds Gate build_gate_order_params returns {} which is falsy; params or None broke trend_market_add with NoneType is not iterable while first-leg place_exchange_order worked. Co-authored-by: Cursor --- strategy_config.py | 4 +++- strategy_trend_exchange.py | 3 ++- tests/test_trend_market_add_params.py | 28 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/test_trend_market_add_params.py diff --git a/strategy_config.py b/strategy_config.py index b749059..3f0c12f 100644 --- a/strategy_config.py +++ b/strategy_config.py @@ -105,7 +105,9 @@ def build_strategy_config( params = m.build_gate_order_params(direction, reduce_only=False) else: params = {} - return m.exchange.create_order(ex_sym, "limit", side, float(amount), float(price), params or None) + return m.exchange.create_order( + ex_sym, "limit", side, float(amount), float(price), params if params is not None else {} + ) def replace_tpsl(ex_sym, direction, sl, tp, order_row): row = order_row or {"symbol": ex_sym, "exchange_symbol": ex_sym, "direction": direction} diff --git a/strategy_trend_exchange.py b/strategy_trend_exchange.py index 20de0d4..ec55693 100644 --- a/strategy_trend_exchange.py +++ b/strategy_trend_exchange.py @@ -39,7 +39,8 @@ def trend_market_add(cfg: dict, exchange_symbol: str, direction: str, contracts: params = m.build_okx_order_params(direction, reduce_only=False) else: params = {} - return ex.create_order(exchange_symbol, "market", side, float(contracts), None, params or None) + order_params = params if params is not None else {} + return ex.create_order(exchange_symbol, "market", side, float(contracts), None, order_params) def trend_market_close(cfg: dict, exchange_symbol: str, direction: str, pos_qty: float, leverage: int): diff --git a/tests/test_trend_market_add_params.py b/tests/test_trend_market_add_params.py new file mode 100644 index 0000000..badc9f2 --- /dev/null +++ b/tests/test_trend_market_add_params.py @@ -0,0 +1,28 @@ +"""趋势补仓下单:空 params 不得变成 None(ccxt 会报 not iterable)。""" +from __future__ import annotations + +import unittest +from unittest.mock import MagicMock + +from strategy_trend_exchange import trend_market_add + + +class TestTrendMarketAddParams(unittest.TestCase): + def test_empty_gate_params_not_passed_as_none(self): + ex = MagicMock() + ex.create_order.return_value = {"id": "1", "average": 0.34} + app = MagicMock() + app.exchange = ex + app.ensure_markets_loaded = MagicMock() + app.build_gate_order_params = MagicMock(return_value={}) + cfg = {"app_module": app} + + trend_market_add(cfg, "ONDO/USDT:USDT", "long", 23, 10) + + args = ex.create_order.call_args + self.assertEqual(args[0][5], {}) + self.assertIsNotNone(args[0][5]) + + +if __name__ == "__main__": + unittest.main()