feat: 添加网格监控并修复程序卡死与打包启动
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from datetime import date, timedelta
|
||||
from decimal import Decimal
|
||||
|
||||
from grid_trading.domain.models import Trade, TradeGroup, TradeSide
|
||||
from grid_trading.domain.models import GridDirection, Trade, TradeGroup, TradeSide
|
||||
from grid_trading.domain.open_grid_lots import calculate_grid_trade_matches, calculate_open_grid_lots
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ def make_trade(
|
||||
price: str,
|
||||
quantity: int,
|
||||
trade_group: TradeGroup = TradeGroup.GRID,
|
||||
grid_direction: GridDirection | None = None,
|
||||
commission: str = "0",
|
||||
) -> Trade:
|
||||
return Trade(
|
||||
id=trade_id,
|
||||
@@ -23,6 +25,8 @@ def make_trade(
|
||||
price=Decimal(price),
|
||||
quantity=quantity,
|
||||
trade_group=trade_group,
|
||||
grid_direction=grid_direction,
|
||||
commission=Decimal(commission),
|
||||
)
|
||||
|
||||
|
||||
@@ -149,3 +153,49 @@ def test_calculate_open_grid_lots_reports_not_reached_and_missing_quote_statuses
|
||||
assert not_reached.status == "未到价"
|
||||
assert missing_quote.current_price is None
|
||||
assert missing_quote.status == "未刷新行情"
|
||||
|
||||
|
||||
def test_reverse_grid_sell_opens_lot_and_buy_closes_it_fifo():
|
||||
today = date(2026, 7, 9)
|
||||
trades = [
|
||||
make_trade(
|
||||
trade_id=1,
|
||||
trade_date=today - timedelta(days=2),
|
||||
side=TradeSide.SELL,
|
||||
price="10.30",
|
||||
quantity=300,
|
||||
trade_group=TradeGroup.BASE,
|
||||
grid_direction=GridDirection.REVERSE,
|
||||
commission="3",
|
||||
),
|
||||
make_trade(
|
||||
trade_id=2,
|
||||
trade_date=today - timedelta(days=1),
|
||||
side=TradeSide.BUY,
|
||||
price="10.00",
|
||||
quantity=100,
|
||||
trade_group=TradeGroup.BASE,
|
||||
grid_direction=GridDirection.REVERSE,
|
||||
commission="1",
|
||||
),
|
||||
]
|
||||
|
||||
[lot] = calculate_open_grid_lots(
|
||||
trades,
|
||||
spacing=Decimal("0.03"),
|
||||
current_price=Decimal("9.99"),
|
||||
as_of=today,
|
||||
)
|
||||
[match] = calculate_grid_trade_matches(trades, as_of=today)
|
||||
|
||||
assert lot.direction is GridDirection.REVERSE
|
||||
assert lot.open_trade_id == 1
|
||||
assert lot.remaining_quantity == 200
|
||||
assert lot.open_price == Decimal("10.30")
|
||||
assert lot.target_price == Decimal("10.00")
|
||||
assert lot.status == "可买回"
|
||||
assert match.direction is GridDirection.REVERSE
|
||||
assert match.sell_trade_id == 1
|
||||
assert match.buy_trade_id == 2
|
||||
assert match.gross_profit == Decimal("30.00")
|
||||
assert match.net_profit == Decimal("28.00")
|
||||
|
||||
Reference in New Issue
Block a user