fix order busy

This commit is contained in:
2026-09-05 11:32:10 +08:00
parent a53fc738cf
commit 3689a7ad86
4 changed files with 204 additions and 28 deletions

View File

@@ -0,0 +1,42 @@
import unittest
from datetime import datetime, timedelta
from unittest.mock import Mock
from sdk import OrderItem
from strategy.trend.order import OrderBook, PlaceOrderRequest
class TrendBusyTests(unittest.TestCase):
def test_snapshot_blocks_order_without_local_cache(self):
book = OrderBook()
client = Mock()
order = OrderItem("1", "A", "BUY", "", "50", None, 100)
book.refresh(client, [order])
self.assertTrue(book.busy("A", "BUY"))
self.assertFalse(book.busy("A", "SELL"))
self.assertFalse(book.place(PlaceOrderRequest(client, 23, "A", 100, "local", "trend")))
client.passorder.assert_not_called()
book.refresh(client, [])
self.assertFalse(book.busy("A", "BUY"))
def test_cancel_request_keeps_order_busy_until_terminal_snapshot(self):
book = OrderBook()
client = Mock()
order = OrderItem("1", "A", "BUY", "", "50", datetime.now() - timedelta(seconds=20), 100)
book.refresh(client, [order])
client.cancel_by_id.assert_called_once_with("1")
self.assertTrue(book.busy("A", "BUY"))
order.status = "54"
book.refresh(client, [order])
self.assertFalse(book.busy("A", "BUY"))
def test_empty_snapshot_keeps_local_cache_protection(self):
book = OrderBook()
client = Mock()
client.passorder.return_value = {"status": "success", "order_ref": "1"}
request = PlaceOrderRequest(client, 23, "A", 100, "local", "trend")
self.assertTrue(book.place(request))
book.refresh(client, [])
self.assertTrue(book.busy("A", "BUY"))
self.assertFalse(book.place(request))
client.passorder.assert_called_once()