216 lines
6.8 KiB
Python
216 lines
6.8 KiB
Python
from datetime import date
|
|
from decimal import Decimal
|
|
import sqlite3
|
|
|
|
from grid_trading.domain.models import (
|
|
Account,
|
|
GridAlertSource,
|
|
GridAlertState,
|
|
GridAlertTarget,
|
|
GridDirection,
|
|
GridMode,
|
|
Instrument,
|
|
StrategyTemplate,
|
|
Trade,
|
|
TradeGroup,
|
|
TradeSide,
|
|
)
|
|
from grid_trading.storage.repositories import Repository
|
|
|
|
|
|
def test_database_round_trips_account_instrument_template_and_trade(tmp_path):
|
|
db_path = tmp_path / "grid.db"
|
|
repo = Repository(db_path)
|
|
repo.initialize()
|
|
|
|
account = repo.save_account(Account(id=None, name="主账户", initial_cash=Decimal("100000"), notes="first"))
|
|
template = repo.save_strategy_template(
|
|
StrategyTemplate(
|
|
id=None,
|
|
name="默认模板",
|
|
grid_spacing_pct=Decimal("0.03"),
|
|
amount_per_grid=Decimal("5000"),
|
|
base_target_amount=Decimal("20000"),
|
|
max_position_amount=Decimal("80000"),
|
|
min_lot=100,
|
|
is_default=True,
|
|
)
|
|
)
|
|
instrument = repo.save_instrument(
|
|
Instrument(
|
|
id=None,
|
|
code="510300",
|
|
name="沪深300ETF",
|
|
market="ETF",
|
|
lot_size=100,
|
|
manual_price=Decimal("3.95"),
|
|
)
|
|
)
|
|
trade = repo.save_trade(
|
|
Trade(
|
|
id=None,
|
|
account_id=account.id,
|
|
instrument_id=instrument.id,
|
|
trade_date=date(2026, 7, 8),
|
|
side=TradeSide.BUY,
|
|
price=Decimal("3.90"),
|
|
quantity=1000,
|
|
commission=Decimal("5"),
|
|
transfer_fee=Decimal("0.04"),
|
|
trade_group=TradeGroup.GRID,
|
|
notes="first buy",
|
|
)
|
|
)
|
|
repo.close()
|
|
|
|
reopened = Repository(db_path)
|
|
reopened.initialize()
|
|
|
|
assert reopened.list_accounts() == [account]
|
|
assert reopened.get_default_strategy_template() == template
|
|
assert reopened.list_instruments() == [instrument]
|
|
assert reopened.list_trades(account_id=account.id) == [trade]
|
|
|
|
|
|
def test_trade_update_and_delete_are_persistent(tmp_path):
|
|
repo = Repository(tmp_path / "grid.db")
|
|
repo.initialize()
|
|
account = repo.save_account(Account(id=None, name="主账户", initial_cash=Decimal("50000")))
|
|
instrument = repo.save_instrument(Instrument(id=None, code="600000", name="浦发银行"))
|
|
trade = repo.save_trade(
|
|
Trade(
|
|
id=None,
|
|
account_id=account.id,
|
|
instrument_id=instrument.id,
|
|
trade_date=date(2026, 7, 7),
|
|
side=TradeSide.BUY,
|
|
price=Decimal("10"),
|
|
quantity=100,
|
|
commission=Decimal("5"),
|
|
trade_group=TradeGroup.BASE,
|
|
)
|
|
)
|
|
|
|
updated = repo.save_trade(
|
|
Trade(
|
|
id=trade.id,
|
|
account_id=account.id,
|
|
instrument_id=instrument.id,
|
|
trade_date=date(2026, 7, 7),
|
|
side=TradeSide.BUY,
|
|
price=Decimal("9.8"),
|
|
quantity=200,
|
|
commission=Decimal("5"),
|
|
trade_group=TradeGroup.BASE,
|
|
notes="corrected",
|
|
)
|
|
)
|
|
|
|
assert repo.get_trade(trade.id) == updated
|
|
|
|
repo.close()
|
|
reopened = Repository(tmp_path / "grid.db")
|
|
reopened.initialize()
|
|
assert reopened.get_trade(trade.id) == updated
|
|
|
|
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
|