feat: 添加网格监控并修复程序卡死与打包启动
This commit is contained in:
@@ -1,7 +1,20 @@
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
import sqlite3
|
||||
|
||||
from grid_trading.domain.models import Account, Instrument, StrategyTemplate, Trade, TradeGroup, TradeSide
|
||||
from grid_trading.domain.models import (
|
||||
Account,
|
||||
GridAlertSource,
|
||||
GridAlertState,
|
||||
GridAlertTarget,
|
||||
GridDirection,
|
||||
GridMode,
|
||||
Instrument,
|
||||
StrategyTemplate,
|
||||
Trade,
|
||||
TradeGroup,
|
||||
TradeSide,
|
||||
)
|
||||
from grid_trading.storage.repositories import Repository
|
||||
|
||||
|
||||
@@ -103,3 +116,100 @@ def test_trade_update_and_delete_are_persistent(tmp_path):
|
||||
reopened.delete_trade(trade.id)
|
||||
assert reopened.get_trade(trade.id) is None
|
||||
assert reopened.list_trades(account_id=account.id) == []
|
||||
|
||||
|
||||
def test_instrument_delete_is_persistent(tmp_path):
|
||||
repo = Repository(tmp_path / "grid.db")
|
||||
repo.initialize()
|
||||
instrument = repo.save_instrument(Instrument(id=None, code="600000", name="浦发银行"))
|
||||
|
||||
repo.delete_instrument(instrument.id)
|
||||
|
||||
assert repo.get_instrument(instrument.id) is None
|
||||
assert repo.list_instruments() == []
|
||||
|
||||
|
||||
def test_grid_alert_target_round_trips_and_updates_state(tmp_path):
|
||||
repo = Repository(tmp_path / "grid.db")
|
||||
repo.initialize()
|
||||
instrument = repo.save_instrument(Instrument(id=None, code="510300", name="沪深300ETF"))
|
||||
target = repo.save_grid_alert_target(
|
||||
GridAlertTarget(
|
||||
id=None,
|
||||
target_key="manual:test",
|
||||
instrument_id=instrument.id,
|
||||
side=TradeSide.BUY,
|
||||
target_price=Decimal("3.88"),
|
||||
quantity=1200,
|
||||
source=GridAlertSource.MANUAL,
|
||||
)
|
||||
)
|
||||
|
||||
assert target.id is not None
|
||||
repo.update_grid_alert_state(
|
||||
target.target_key,
|
||||
state=GridAlertState.REACHED,
|
||||
alerted=True,
|
||||
last_alerted_at=None,
|
||||
)
|
||||
updated = repo.get_grid_alert_target(target.target_key)
|
||||
|
||||
assert updated.last_state is GridAlertState.REACHED
|
||||
assert updated.alerted
|
||||
|
||||
repo.delete_grid_alert_target(target.target_key)
|
||||
assert repo.get_grid_alert_target(target.target_key) is None
|
||||
|
||||
|
||||
def test_initialize_migrates_legacy_grid_columns(tmp_path):
|
||||
db_path = tmp_path / "legacy.db"
|
||||
connection = sqlite3.connect(db_path)
|
||||
connection.executescript(
|
||||
"""
|
||||
CREATE TABLE strategy_templates (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
grid_spacing_pct TEXT NOT NULL,
|
||||
amount_per_grid TEXT NOT NULL,
|
||||
base_target_amount TEXT NOT NULL,
|
||||
max_position_amount TEXT NOT NULL,
|
||||
min_lot INTEGER NOT NULL,
|
||||
is_default INTEGER NOT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TABLE trades (
|
||||
id INTEGER PRIMARY KEY,
|
||||
account_id INTEGER NOT NULL,
|
||||
instrument_id INTEGER NOT NULL,
|
||||
trade_date TEXT NOT NULL,
|
||||
side TEXT NOT NULL,
|
||||
price TEXT NOT NULL,
|
||||
quantity INTEGER NOT NULL,
|
||||
commission TEXT NOT NULL DEFAULT '0',
|
||||
stamp_tax TEXT NOT NULL DEFAULT '0',
|
||||
transfer_fee TEXT NOT NULL DEFAULT '0',
|
||||
trade_group TEXT NOT NULL,
|
||||
notes TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
INSERT INTO strategy_templates
|
||||
(id, name, grid_spacing_pct, amount_per_grid, base_target_amount,
|
||||
max_position_amount, min_lot, is_default)
|
||||
VALUES (1, '旧策略', '0.03', '5000', '0', '80000', 100, 1);
|
||||
INSERT INTO trades
|
||||
(id, account_id, instrument_id, trade_date, side, price, quantity, trade_group)
|
||||
VALUES (1, 1, 1, '2026-07-01', 'buy', '4.00', 100, 'grid');
|
||||
"""
|
||||
)
|
||||
connection.commit()
|
||||
connection.close()
|
||||
|
||||
repo = Repository(db_path)
|
||||
repo.initialize()
|
||||
|
||||
template = repo.get_default_strategy_template()
|
||||
trade = repo.get_trade(1)
|
||||
assert template.grid_mode is GridMode.FORWARD
|
||||
assert trade.grid_direction is GridDirection.FORWARD
|
||||
|
||||
Reference in New Issue
Block a user