feat: 添加网格监控并修复程序卡死与打包启动
This commit is contained in:
@@ -9,7 +9,16 @@ from grid_trading.domain.calculations import (
|
||||
calculate_positions,
|
||||
estimate_fees,
|
||||
)
|
||||
from grid_trading.domain.models import Account, FeeRules, Instrument, QuoteSnapshot, Trade, TradeGroup, TradeSide
|
||||
from grid_trading.domain.models import (
|
||||
Account,
|
||||
FeeRules,
|
||||
GridDirection,
|
||||
Instrument,
|
||||
QuoteSnapshot,
|
||||
Trade,
|
||||
TradeGroup,
|
||||
TradeSide,
|
||||
)
|
||||
|
||||
|
||||
def make_trade(
|
||||
@@ -24,6 +33,7 @@ def make_trade(
|
||||
stamp_tax: str = "0",
|
||||
transfer_fee: str = "0",
|
||||
trade_group: TradeGroup = TradeGroup.GRID,
|
||||
grid_direction: GridDirection | None = None,
|
||||
) -> Trade:
|
||||
return Trade(
|
||||
id=trade_id,
|
||||
@@ -37,6 +47,7 @@ def make_trade(
|
||||
stamp_tax=Decimal(stamp_tax),
|
||||
transfer_fee=Decimal(transfer_fee),
|
||||
trade_group=trade_group,
|
||||
grid_direction=grid_direction,
|
||||
)
|
||||
|
||||
|
||||
@@ -100,7 +111,7 @@ def test_buy_sell_grid_profit_and_breakeven():
|
||||
assert summary.floating_pnl == Decimal("49.00")
|
||||
|
||||
|
||||
def test_t_plus_one_available_quantity_excludes_today_buys():
|
||||
def test_available_quantity_includes_today_buys():
|
||||
today = date(2026, 7, 8)
|
||||
instrument = Instrument(id=1, code="600000", name="浦发银行", manual_price=Decimal("10"))
|
||||
trades = [
|
||||
@@ -125,7 +136,48 @@ def test_t_plus_one_available_quantity_excludes_today_buys():
|
||||
[summary] = calculate_positions([instrument], trades, as_of=today)
|
||||
|
||||
assert summary.total_quantity == 300
|
||||
assert summary.available_quantity == 200
|
||||
assert summary.available_quantity == 300
|
||||
|
||||
|
||||
def test_reverse_grid_profit_is_realized_when_sell_is_bought_back():
|
||||
today = date(2026, 7, 8)
|
||||
instrument = Instrument(id=1, code="510300", name="沪深300ETF")
|
||||
trades = [
|
||||
make_trade(
|
||||
trade_id=1,
|
||||
trade_date=today - timedelta(days=1),
|
||||
side=TradeSide.BUY,
|
||||
price="9.00",
|
||||
quantity=100,
|
||||
trade_group=TradeGroup.BASE,
|
||||
),
|
||||
make_trade(
|
||||
trade_id=2,
|
||||
trade_date=today,
|
||||
side=TradeSide.SELL,
|
||||
price="10.30",
|
||||
quantity=100,
|
||||
commission="1",
|
||||
trade_group=TradeGroup.BASE,
|
||||
grid_direction=GridDirection.REVERSE,
|
||||
),
|
||||
make_trade(
|
||||
trade_id=3,
|
||||
trade_date=today,
|
||||
side=TradeSide.BUY,
|
||||
price="10.00",
|
||||
quantity=100,
|
||||
commission="1",
|
||||
trade_group=TradeGroup.BASE,
|
||||
grid_direction=GridDirection.REVERSE,
|
||||
),
|
||||
]
|
||||
|
||||
[summary] = calculate_positions([instrument], trades, as_of=today)
|
||||
|
||||
assert summary.total_quantity == 100
|
||||
assert summary.available_quantity == 100
|
||||
assert summary.grid_profit == Decimal("28.00")
|
||||
|
||||
|
||||
def test_quote_snapshot_supplies_current_price_for_position_value():
|
||||
@@ -213,7 +265,7 @@ def test_account_summary_caps_capital_usage_when_cash_is_negative():
|
||||
assert summary.capital_usage_rate == Decimal("1.0000")
|
||||
|
||||
|
||||
def test_sell_more_than_group_position_raises():
|
||||
def test_sell_more_than_total_position_raises():
|
||||
today = date(2026, 7, 8)
|
||||
instrument = Instrument(id=1, code="159915", name="创业板ETF")
|
||||
trades = [
|
||||
@@ -235,10 +287,39 @@ def test_sell_more_than_group_position_raises():
|
||||
),
|
||||
]
|
||||
|
||||
with pytest.raises(CalculationError, match="Insufficient grid position"):
|
||||
with pytest.raises(CalculationError, match="总持仓不足"):
|
||||
calculate_positions([instrument], trades, as_of=today)
|
||||
|
||||
|
||||
def test_sell_can_use_holding_recorded_in_a_different_legacy_group():
|
||||
today = date(2026, 7, 8)
|
||||
instrument = Instrument(id=1, code="600588", name="用友网络")
|
||||
trades = [
|
||||
make_trade(
|
||||
trade_id=1,
|
||||
trade_date=today - timedelta(days=1),
|
||||
side=TradeSide.BUY,
|
||||
price="9.00",
|
||||
quantity=9400,
|
||||
trade_group=TradeGroup.BASE,
|
||||
),
|
||||
make_trade(
|
||||
trade_id=2,
|
||||
trade_date=today,
|
||||
side=TradeSide.SELL,
|
||||
price="9.56",
|
||||
quantity=9400,
|
||||
trade_group=TradeGroup.GRID,
|
||||
grid_direction=GridDirection.REVERSE,
|
||||
),
|
||||
]
|
||||
|
||||
[summary] = calculate_positions([instrument], trades, as_of=today)
|
||||
|
||||
assert summary.total_quantity == 0
|
||||
assert summary.realized_pnl == Decimal("5264.00")
|
||||
|
||||
|
||||
def test_estimate_fees_uses_min_commission_and_sell_tax():
|
||||
rules = FeeRules(
|
||||
commission_rate=Decimal("0.00025"),
|
||||
|
||||
Reference in New Issue
Block a user