feat: 添加网格监控并修复程序卡死与打包启动
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
from datetime import date, timedelta
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
|
||||
from grid_trading.domain.models import Instrument, QuoteSnapshot, Trade, TradeGroup, TradeSide
|
||||
from grid_trading.domain.models import (
|
||||
GridAlertSource,
|
||||
GridAlertState,
|
||||
GridDirection,
|
||||
GridMode,
|
||||
Instrument,
|
||||
QuoteSnapshot,
|
||||
Trade,
|
||||
TradeGroup,
|
||||
TradeSide,
|
||||
)
|
||||
from grid_trading.services.trading_service import TradingService
|
||||
|
||||
|
||||
@@ -21,6 +31,40 @@ class FakeQuoteProvider:
|
||||
}
|
||||
|
||||
|
||||
def test_service_deletes_instrument_without_trades(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
instrument = service.add_instrument(Instrument(id=None, code="600000", name="浦发银行"))
|
||||
|
||||
service.delete_instrument(instrument.id)
|
||||
|
||||
assert service.list_instruments() == []
|
||||
service.close()
|
||||
|
||||
|
||||
def test_service_refuses_to_delete_instrument_with_trades(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
service.ensure_defaults()
|
||||
account = service.get_active_account()
|
||||
instrument = service.add_instrument(Instrument(id=None, code="600000", name="浦发银行"))
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date(2026, 7, 28),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("10"),
|
||||
quantity=100,
|
||||
)
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="已有成交记录"):
|
||||
service.delete_instrument(instrument.id)
|
||||
|
||||
assert service.list_instruments() == [instrument]
|
||||
service.close()
|
||||
|
||||
|
||||
def test_service_creates_default_account_and_computes_summary(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
service.ensure_defaults()
|
||||
@@ -72,7 +116,7 @@ def test_service_creates_default_account_and_computes_summary(tmp_path):
|
||||
assert summary.total_assets == Decimal("98138.97")
|
||||
|
||||
|
||||
def test_service_validates_lot_size_and_available_sell_quantity(tmp_path):
|
||||
def test_service_validates_lot_size_and_allows_same_day_sell(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
service.ensure_defaults()
|
||||
account = service.get_active_account()
|
||||
@@ -105,32 +149,6 @@ def test_service_validates_lot_size_and_available_sell_quantity(tmp_path):
|
||||
)
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="T\\+1"):
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date(2026, 7, 8),
|
||||
side=TradeSide.SELL,
|
||||
price=Decimal("10.1"),
|
||||
quantity=100,
|
||||
trade_group=TradeGroup.BASE,
|
||||
)
|
||||
)
|
||||
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date(2026, 7, 8) - timedelta(days=1),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("9.9"),
|
||||
quantity=100,
|
||||
trade_group=TradeGroup.BASE,
|
||||
)
|
||||
)
|
||||
saved_sell = service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
@@ -143,9 +161,22 @@ def test_service_validates_lot_size_and_available_sell_quantity(tmp_path):
|
||||
trade_group=TradeGroup.BASE,
|
||||
)
|
||||
)
|
||||
|
||||
assert saved_sell.id is not None
|
||||
|
||||
with pytest.raises(ValueError, match="总持仓不足"):
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date(2026, 7, 8),
|
||||
side=TradeSide.SELL,
|
||||
price=Decimal("10.2"),
|
||||
quantity=100,
|
||||
trade_group=TradeGroup.BASE,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_service_rejects_historical_changes_that_break_future_sells(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
@@ -260,6 +291,39 @@ def test_service_returns_empty_grid_levels_without_realtime_price(tmp_path):
|
||||
assert service.get_grid_level_suggestions(instrument.id, levels=10) == []
|
||||
|
||||
|
||||
def test_service_generates_reverse_levels_capped_by_current_holding(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db", quote_provider=FakeQuoteProvider())
|
||||
service.ensure_defaults()
|
||||
account = service.get_active_account()
|
||||
instrument = service.add_instrument(
|
||||
Instrument(id=None, code="510300", name="沪深300ETF", market="ETF")
|
||||
)
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date.today(),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("4.00"),
|
||||
quantity=500,
|
||||
trade_group=TradeGroup.BASE,
|
||||
)
|
||||
)
|
||||
service.refresh_quotes()
|
||||
|
||||
[level] = service.get_grid_level_suggestions(
|
||||
instrument.id,
|
||||
levels=1,
|
||||
mode=GridMode.REVERSE,
|
||||
)
|
||||
|
||||
assert level.direction is GridDirection.REVERSE
|
||||
assert level.sell_price == Decimal("4.24")
|
||||
assert level.buy_price == Decimal("4.12")
|
||||
assert level.suggested_quantity == 500
|
||||
|
||||
|
||||
def test_service_returns_open_grid_lots_with_suggested_sell_price(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db", quote_provider=FakeQuoteProvider())
|
||||
service.ensure_defaults()
|
||||
@@ -350,3 +414,112 @@ def test_service_returns_grid_trade_matches(tmp_path):
|
||||
assert matches[0].gross_profit == Decimal("90.00")
|
||||
assert matches[1].matched_quantity == 100
|
||||
assert matches[1].gross_profit == Decimal("80.00")
|
||||
|
||||
|
||||
def test_service_persists_buy_alert_and_synchronizes_open_sell_alert(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db", quote_provider=FakeQuoteProvider())
|
||||
service.ensure_defaults()
|
||||
account = service.get_active_account()
|
||||
instrument = service.add_instrument(
|
||||
Instrument(id=None, code="510300", name="沪深300ETF", market="ETF")
|
||||
)
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date.today(),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("4.00"),
|
||||
quantity=1000,
|
||||
trade_group=TradeGroup.GRID,
|
||||
)
|
||||
)
|
||||
buy_target = service.add_buy_grid_alert(
|
||||
instrument.id,
|
||||
target_price=Decimal("4.10"),
|
||||
quantity=1200,
|
||||
)
|
||||
service.refresh_quotes()
|
||||
|
||||
first = service.evaluate_grid_alerts()
|
||||
second = service.evaluate_grid_alerts()
|
||||
|
||||
assert buy_target.source is GridAlertSource.MANUAL
|
||||
assert {item.side for item in first.items} == {TradeSide.BUY, TradeSide.SELL}
|
||||
assert any(item.side is TradeSide.SELL and item.state is GridAlertState.REACHED for item in first.items)
|
||||
assert len(first.newly_reached) == 1
|
||||
assert second.newly_reached == []
|
||||
|
||||
|
||||
def test_service_rearms_alert_only_after_price_leaves_near_zone(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
service.ensure_defaults()
|
||||
instrument = service.add_instrument(Instrument(id=None, code="510300", name="沪深300ETF"))
|
||||
service.add_buy_grid_alert(
|
||||
instrument.id,
|
||||
target_price=Decimal("4.00"),
|
||||
quantity=100,
|
||||
)
|
||||
|
||||
def apply(price):
|
||||
service.apply_quote_snapshots(
|
||||
{
|
||||
"510300": QuoteSnapshot(
|
||||
symbol="sh510300",
|
||||
code="510300",
|
||||
name="沪深300ETF",
|
||||
price=Decimal(price),
|
||||
source="test",
|
||||
)
|
||||
}
|
||||
)
|
||||
return service.evaluate_grid_alerts()
|
||||
|
||||
assert len(apply("4.00").newly_reached) == 1
|
||||
assert apply("4.01").newly_reached == []
|
||||
assert apply("4.00").newly_reached == []
|
||||
assert apply("4.10").newly_reached == []
|
||||
assert len(apply("4.00").newly_reached) == 1
|
||||
|
||||
|
||||
def test_reverse_sell_automatically_creates_buyback_alert(tmp_path):
|
||||
service = TradingService(tmp_path / "grid.db", quote_provider=FakeQuoteProvider())
|
||||
service.ensure_defaults()
|
||||
account = service.get_active_account()
|
||||
instrument = service.add_instrument(
|
||||
Instrument(id=None, code="510300", name="沪深300ETF", market="ETF")
|
||||
)
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date.today(),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("4.00"),
|
||||
quantity=500,
|
||||
trade_group=TradeGroup.BASE,
|
||||
)
|
||||
)
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date.today(),
|
||||
side=TradeSide.SELL,
|
||||
price=Decimal("4.24"),
|
||||
quantity=100,
|
||||
trade_group=TradeGroup.OTHER,
|
||||
grid_direction=GridDirection.REVERSE,
|
||||
)
|
||||
)
|
||||
service.refresh_quotes()
|
||||
|
||||
snapshot = service.evaluate_grid_alerts()
|
||||
|
||||
[target] = snapshot.items
|
||||
assert target.side is TradeSide.BUY
|
||||
assert target.target_price == Decimal("4.12")
|
||||
assert target.state is GridAlertState.REACHED
|
||||
|
||||
Reference in New Issue
Block a user