update api,libs

This commit is contained in:
2026-08-30 00:34:27 +08:00
parent 9a43aaba23
commit cdccc48d8c
21 changed files with 2616 additions and 179 deletions

View File

@@ -6,11 +6,11 @@ from types import SimpleNamespace
from unittest.mock import patch
from libs.grid_take_profit import GridState, GridTrailingTracker
from sdk import Assets, Position, Tick
from sdk import Assets, PositionItem, Tick
from strategy.trend.order import OrderBook, PlaceOrderRequest
from strategy.trend.positions import LOSS_TIERS, handle_loss, manage_positions
from strategy.trend.boot import RunOnce
from strategy.trend.state import STATUS_OK, State, StateItem
from strategy.trend.state import STATUS_OK, STATUS_UNKNOWN, State, StateItem
class FakeClient:
@@ -42,7 +42,7 @@ class TrendTests(unittest.TestCase):
def test_position_dataclasses_execute_without_type_error(self):
with TemporaryDirectory() as directory:
state = State.for_strategy(directory, "trend", "A")
position = Position(
position = PositionItem(
stock_code="000001.SZ", volume=100, can_use_volume=100,
open_price=10, market_value=1000,
)
@@ -64,7 +64,7 @@ class TrendTests(unittest.TestCase):
self.assertEqual(len(LOSS_TIERS), 2)
with TemporaryDirectory() as directory:
state = State.for_strategy(directory, "trend", "A")
position = Position(stock_code="A", volume=100, open_price=10, market_value=1000)
position = PositionItem(stock_code="A", volume=100, open_price=10, market_value=1000)
state.sync_positions([position])
item = state.get("A")
item.added_num = len(LOSS_TIERS)
@@ -80,7 +80,7 @@ class TrendTests(unittest.TestCase):
def test_loss_tiers_zero_and_one(self):
with TemporaryDirectory() as directory:
state = State.for_strategy(directory, "trend", "A")
position = Position(stock_code="A", volume=100, open_price=10, market_value=1000)
position = PositionItem(stock_code="A", volume=100, open_price=10, market_value=1000)
state.sync_positions([position])
runtime = SimpleNamespace(
state=state, account_cfg=SimpleNamespace(buy_value=5000, strategy="trend"),
@@ -100,7 +100,7 @@ class TrendTests(unittest.TestCase):
def test_reconcile_ing_order_from_deal(self):
with TemporaryDirectory() as directory:
state = State.for_strategy(directory, "trend", "A")
position = Position(stock_code="A", volume=100, open_price=10)
position = PositionItem(stock_code="A", volume=100, open_price=10)
state.set(StateItem("A", base_order_id="local-1", base_status="ING"))
state.reconcile(
[position],
@@ -112,7 +112,9 @@ class TrendTests(unittest.TestCase):
def test_low_cash_still_runs_position_management(self):
client = SimpleNamespace(
assets=lambda: Assets(total=10000, available=10),
positions=lambda: (["A"], [Position(stock_code="A", volume=100, open_price=10)]),
positions=lambda: (["A"], [PositionItem(stock_code="A", volume=100, open_price=10)]),
trade_detail_data=lambda _datatype: [],
deals=lambda: [],
full_tick=lambda _codes: {"A": Tick(last_price=11)},
)
runtime = SimpleNamespace(
@@ -120,7 +122,11 @@ class TrendTests(unittest.TestCase):
account_cfg=SimpleNamespace(min_cash_ratio=0.1),
global_cfg=SimpleNamespace(api_host="http://example"),
orders=SimpleNamespace(cancel_expired=lambda _client: None),
state=SimpleNamespace(codes=["A"]),
state=SimpleNamespace(
codes=["A"],
unresolved_codes=[],
reconcile=lambda *_args: None,
),
)
with (
patch("strategy.trend.boot.trading_time", return_value=True),
@@ -132,6 +138,44 @@ class TrendTests(unittest.TestCase):
open_mock.assert_not_called()
manage_mock.assert_called_once()
def test_unknown_order_without_position_blocks_reopen(self):
with TemporaryDirectory() as directory:
state = State.for_strategy(directory, "trend", "A")
state.set(StateItem(
"A",
base_order_id="missing-order",
base_qty=100,
base_status=STATUS_UNKNOWN,
))
state.save()
client = SimpleNamespace(
assets=lambda: Assets(total=10000, available=5000),
positions=lambda: ([], []),
trade_detail_data=lambda _datatype: [],
deals=lambda: [],
full_tick=lambda _codes: {"A": Tick(last_price=10)},
)
runtime = SimpleNamespace(
client=client,
account_cfg=SimpleNamespace(min_cash_ratio=0.1),
global_cfg=SimpleNamespace(api_host="http://example"),
orders=OrderBook(),
state=state,
open_watch=SimpleNamespace(forget=lambda _code: None),
add_watch=SimpleNamespace(forget=lambda _code: None),
)
signal = SimpleNamespace(code="A", signal_key="morning")
with (
patch("strategy.trend.boot.trading_time", return_value=True),
patch("strategy.trend.boot.market_allow_open", return_value=True),
patch("strategy.trend.boot.open_signal") as open_mock,
patch("strategy.trend.boot.manage_positions"),
):
RunOnce(runtime, [signal])
open_mock.assert_not_called()
self.assertTrue(state.has_unresolved_order("A"))
if __name__ == "__main__":
unittest.main()