first version

This commit is contained in:
2026-06-07 21:01:40 +00:00
parent ee7167accf
commit f19bab1100
79 changed files with 10355 additions and 145 deletions
@@ -0,0 +1,13 @@
DROP TABLE IF EXISTS reconciliations;
DROP TABLE IF EXISTS daily_reports;
DROP TABLE IF EXISTS system_state;
DROP TABLE IF EXISTS free_order_counters;
DROP TABLE IF EXISTS risk_events;
DROP TABLE IF EXISTS positions;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS signals;
DROP TABLE IF EXISTS features;
DROP TABLE IF EXISTS candles_minute;
DROP TABLE IF EXISTS candles_daily;
DROP TABLE IF EXISTS instruments;
DROP TABLE IF EXISTS schema_meta;
@@ -0,0 +1,181 @@
CREATE TABLE IF NOT EXISTS schema_meta (
meta_key VARCHAR(64) PRIMARY KEY,
meta_value VARCHAR(255) NOT NULL,
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS instruments (
instrument_uid VARCHAR(128) PRIMARY KEY,
figi VARCHAR(64),
ticker VARCHAR(32) NOT NULL,
class_code VARCHAR(32) NOT NULL DEFAULT 'TQTF',
name VARCHAR(255) NOT NULL DEFAULT '',
lot BIGINT NOT NULL DEFAULT 1,
min_price_increment DECIMAL(20,8) NOT NULL DEFAULT 0,
currency VARCHAR(8) NOT NULL DEFAULT 'RUB',
enabled TINYINT(1) NOT NULL DEFAULT 1,
fund_type VARCHAR(64) NOT NULL DEFAULT '',
expected_commission_bps_per_side DECIMAL(12,4) NOT NULL DEFAULT 0,
free_order_limit_per_day INT NOT NULL DEFAULT 0 COMMENT '0 means no configured free-order cap',
quarantine TINYINT(1) NOT NULL DEFAULT 0,
quarantine_reason TEXT,
exclude_reason TEXT,
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
UNIQUE KEY ux_instruments_ticker_class (ticker, class_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS candles_daily (
instrument_uid VARCHAR(128) NOT NULL,
trade_date DATE NOT NULL,
open DECIMAL(20,8) NOT NULL,
high DECIMAL(20,8) NOT NULL,
low DECIMAL(20,8) NOT NULL,
close DECIMAL(20,8) NOT NULL,
volume_lots DECIMAL(20,8) NOT NULL DEFAULT 0,
source VARCHAR(32) NOT NULL,
loaded_at DATETIME(3) NOT NULL,
PRIMARY KEY (instrument_uid, trade_date),
CONSTRAINT fk_candles_daily_instrument FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS candles_minute (
instrument_uid VARCHAR(128) NOT NULL,
ts DATETIME(3) NOT NULL,
open DECIMAL(20,8) NOT NULL,
high DECIMAL(20,8) NOT NULL,
low DECIMAL(20,8) NOT NULL,
close DECIMAL(20,8) NOT NULL,
volume_lots DECIMAL(20,8) NOT NULL DEFAULT 0,
source VARCHAR(32) NOT NULL,
loaded_at DATETIME(3) NOT NULL,
PRIMARY KEY (instrument_uid, ts),
CONSTRAINT fk_candles_minute_instrument FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS features (
instrument_uid VARCHAR(128) NOT NULL,
trade_date DATE NOT NULL,
r_on DECIMAL(20,10) NOT NULL DEFAULT 0,
r_day DECIMAL(20,10) NOT NULL DEFAULT 0,
mu_on_60 DECIMAL(20,10) NOT NULL DEFAULT 0,
mu_on_252 DECIMAL(20,10) NOT NULL DEFAULT 0,
sigma_on_60 DECIMAL(20,10) NOT NULL DEFAULT 0,
tstat_on_60 DECIMAL(20,10) NOT NULL DEFAULT 0,
win_on_60 DECIMAL(20,10) NOT NULL DEFAULT 0,
ewma_on DECIMAL(20,10) NOT NULL DEFAULT 0,
spread_bps DECIMAL(12,4) NOT NULL DEFAULT 0,
half_spread_bps DECIMAL(12,4) NOT NULL DEFAULT 0,
tick_bps DECIMAL(12,4) NOT NULL DEFAULT 0,
adv_20 DECIMAL(20,8) NOT NULL DEFAULT 0,
expected_cost_bps DECIMAL(12,4) NOT NULL DEFAULT 0,
net_edge_bps DECIMAL(12,4) NOT NULL DEFAULT 0,
entry_interval_volume DECIMAL(20,8) NOT NULL DEFAULT 0,
exit_interval_volume DECIMAL(20,8) NOT NULL DEFAULT 0,
calculated_at DATETIME(3) NOT NULL,
PRIMARY KEY (instrument_uid, trade_date),
CONSTRAINT fk_features_instrument FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS signals (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
trade_date DATE NOT NULL,
instrument_uid VARCHAR(128) NOT NULL,
decision ENUM('ENTER','SKIP','REJECT') NOT NULL,
score DECIMAL(20,10) NOT NULL DEFAULT 0,
net_edge_bps DECIMAL(12,4) NOT NULL DEFAULT 0,
target_notional DECIMAL(20,8) NOT NULL DEFAULT 0,
target_lots BIGINT NOT NULL DEFAULT 0,
reject_reason VARCHAR(128),
context_json JSON,
created_at DATETIME(3) NOT NULL,
UNIQUE KEY ux_signals_date_instr (trade_date, instrument_uid),
CONSTRAINT fk_signals_instrument FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS orders (
client_order_id VARCHAR(128) PRIMARY KEY,
broker_order_id VARCHAR(128),
account_id_hash VARCHAR(128) NOT NULL,
instrument_uid VARCHAR(128) NOT NULL,
trade_date DATE NOT NULL,
side ENUM('BUY','SELL') NOT NULL,
order_type ENUM('LIMIT') NOT NULL,
limit_price DECIMAL(20,8) NOT NULL DEFAULT 0,
quantity_lots BIGINT NOT NULL,
filled_lots BIGINT NOT NULL DEFAULT 0,
avg_fill_price DECIMAL(20,8) NOT NULL DEFAULT 0,
status ENUM('NEW','SENT','PARTIALLY_FILLED','FILLED','CANCELLED','REJECTED','EXPIRED','FAILED') NOT NULL,
commission DECIMAL(20,8) NOT NULL DEFAULT 0,
attempt_no INT NOT NULL DEFAULT 1,
raw_state_json JSON,
created_at DATETIME(3) NOT NULL,
updated_at DATETIME(3) NOT NULL,
UNIQUE KEY ux_orders_broker_order_id (broker_order_id),
KEY ix_orders_active (account_id_hash, status),
CONSTRAINT fk_orders_instrument FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS positions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
account_id_hash VARCHAR(128) NOT NULL,
instrument_uid VARCHAR(128) NOT NULL,
open_trade_date DATE NOT NULL,
lots BIGINT NOT NULL,
avg_buy_price DECIMAL(20,8) NOT NULL DEFAULT 0,
avg_sell_price DECIMAL(20,8) NOT NULL DEFAULT 0,
status ENUM('NO_POSITION','ENTRY_SIGNALLED','ENTRY_ORDER_SENT','ENTRY_PARTIALLY_FILLED','ENTRY_FILLED','HOLDING_OVERNIGHT','EXIT_ORDER_SENT','EXIT_PARTIALLY_FILLED','EXIT_FILLED','EXIT_FAILED','QUARANTINE') NOT NULL,
gross_pnl DECIMAL(20,8) NOT NULL DEFAULT 0,
net_pnl DECIMAL(20,8) NOT NULL DEFAULT 0,
commission_total DECIMAL(20,8) NOT NULL DEFAULT 0,
realized_edge_bps DECIMAL(12,4) NOT NULL DEFAULT 0,
opened_at DATETIME(3),
closed_at DATETIME(3),
updated_at DATETIME(3) NOT NULL,
KEY ix_positions_open (account_id_hash, status),
CONSTRAINT fk_positions_instrument FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS risk_events (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
ts DATETIME(3) NOT NULL,
severity ENUM('INFO','WARN','ALERT','CRITICAL') NOT NULL,
event_type VARCHAR(128) NOT NULL,
instrument_uid VARCHAR(128),
message TEXT NOT NULL,
raw_context_json JSON,
KEY ix_risk_events_ts (ts)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS free_order_counters (
trade_date DATE NOT NULL,
instrument_uid VARCHAR(128) NOT NULL,
orders_sent INT NOT NULL DEFAULT 0,
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (trade_date, instrument_uid),
CONSTRAINT fk_free_orders_instrument FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS system_state (
id TINYINT NOT NULL PRIMARY KEY,
state ENUM('INIT','SYNC_INSTRUMENTS','SYNC_MARKET_DATA','GENERATE_SIGNALS','WAIT_ENTRY_WINDOW','PLACE_ENTRY_ORDERS','MONITOR_ENTRY_ORDERS','HOLD_OVERNIGHT','WAIT_EXIT_WINDOW','PLACE_EXIT_ORDERS','MONITOR_EXIT_ORDERS','RECONCILE','REPORT','SLEEP','HALTED') NOT NULL,
mode ENUM('backtest','paper','sandbox','live_readonly','live_trade') NOT NULL,
halted TINYINT(1) NOT NULL DEFAULT 0,
halt_reason TEXT,
last_heartbeat DATETIME(3) NOT NULL,
context_json JSON
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS reconciliations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
ts DATETIME(3) NOT NULL,
has_diff TINYINT(1) NOT NULL,
diff_json JSON,
KEY ix_reconciliations_ts (ts)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO schema_meta(meta_key, meta_value) VALUES ('schema_version', '0001')
ON DUPLICATE KEY UPDATE meta_value=VALUES(meta_value);
INSERT INTO system_state(id, state, mode, halted, last_heartbeat, context_json)
VALUES (1, 'INIT', 'paper', 0, UTC_TIMESTAMP(3), JSON_OBJECT())
ON DUPLICATE KEY UPDATE id=id;
@@ -0,0 +1,7 @@
DELETE FROM instruments WHERE instrument_uid IN (
'PENDING:TRUR','PENDING:TGLD','PENDING:TBRU','PENDING:TDIV','PENDING:TMON',
'PENDING:TOFZ','PENDING:TLCB','PENDING:TITR','PENDING:TRND','PENDING:TMOS'
);
UPDATE schema_meta SET meta_value='0001' WHERE meta_key='schema_version';
@@ -0,0 +1,24 @@
INSERT INTO instruments (
instrument_uid, ticker, class_code, name, lot, min_price_increment, currency,
enabled, fund_type, expected_commission_bps_per_side, free_order_limit_per_day,
quarantine, exclude_reason, updated_at
) VALUES
('PENDING:TRUR', 'TRUR', 'TQTF', 'TRUR', 1, 0.0001, 'RUB', 1, 'mixed', 0, 15, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TGLD', 'TGLD', 'TQTF', 'TGLD', 1, 0.0001, 'RUB', 1, 'commodity', 0, 15, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TBRU', 'TBRU', 'TQTF', 'TBRU', 1, 0.0001, 'RUB', 1, 'bonds', 0, 0, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TDIV', 'TDIV', 'TQTF', 'TDIV', 1, 0.0001, 'RUB', 1, 'equity_income', 0, 0, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TMON', 'TMON', 'TQTF', 'TMON', 1, 0.0001, 'RUB', 1, 'money_market', 0, 0, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TOFZ', 'TOFZ', 'TQTF', 'TOFZ', 1, 0.0001, 'RUB', 1, 'bonds', 0, 0, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TLCB', 'TLCB', 'TQTF', 'TLCB', 1, 0.0001, 'RUB', 1, 'corporate_bonds', 0, 0, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TITR', 'TITR', 'TQTF', 'TITR', 1, 0.0001, 'RUB', 1, 'equity', 0, 0, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TRND', 'TRND', 'TQTF', 'TRND', 1, 0.0001, 'RUB', 1, 'equity', 0, 0, 0, NULL, UTC_TIMESTAMP(3)),
('PENDING:TMOS', 'TMOS', 'TQTF', 'TMOS', 1, 0.0001, 'RUB', 0, 'equity', 0, 0, 0, 'Excluded by default due to possible non-zero sell-side fee', UTC_TIMESTAMP(3))
ON DUPLICATE KEY UPDATE
enabled=VALUES(enabled),
fund_type=VALUES(fund_type),
expected_commission_bps_per_side=VALUES(expected_commission_bps_per_side),
free_order_limit_per_day=VALUES(free_order_limit_per_day),
exclude_reason=VALUES(exclude_reason),
updated_at=UTC_TIMESTAMP(3);
UPDATE schema_meta SET meta_value='0002' WHERE meta_key='schema_version';
@@ -0,0 +1,29 @@
ALTER TABLE free_order_counters DROP FOREIGN KEY fk_free_orders_instrument;
ALTER TABLE free_order_counters ADD CONSTRAINT fk_free_orders_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid);
ALTER TABLE positions DROP FOREIGN KEY fk_positions_instrument;
ALTER TABLE positions ADD CONSTRAINT fk_positions_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid);
ALTER TABLE orders DROP FOREIGN KEY fk_orders_instrument;
ALTER TABLE orders ADD CONSTRAINT fk_orders_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid);
ALTER TABLE signals DROP FOREIGN KEY fk_signals_instrument;
ALTER TABLE signals ADD CONSTRAINT fk_signals_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid);
ALTER TABLE features DROP FOREIGN KEY fk_features_instrument;
ALTER TABLE features ADD CONSTRAINT fk_features_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid);
ALTER TABLE candles_minute DROP FOREIGN KEY fk_candles_minute_instrument;
ALTER TABLE candles_minute ADD CONSTRAINT fk_candles_minute_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid);
ALTER TABLE candles_daily DROP FOREIGN KEY fk_candles_daily_instrument;
ALTER TABLE candles_daily ADD CONSTRAINT fk_candles_daily_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid);
UPDATE schema_meta SET meta_value='0002' WHERE meta_key='schema_version';
@@ -0,0 +1,29 @@
ALTER TABLE candles_daily DROP FOREIGN KEY fk_candles_daily_instrument;
ALTER TABLE candles_daily ADD CONSTRAINT fk_candles_daily_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE;
ALTER TABLE candles_minute DROP FOREIGN KEY fk_candles_minute_instrument;
ALTER TABLE candles_minute ADD CONSTRAINT fk_candles_minute_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE;
ALTER TABLE features DROP FOREIGN KEY fk_features_instrument;
ALTER TABLE features ADD CONSTRAINT fk_features_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE;
ALTER TABLE signals DROP FOREIGN KEY fk_signals_instrument;
ALTER TABLE signals ADD CONSTRAINT fk_signals_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE;
ALTER TABLE orders DROP FOREIGN KEY fk_orders_instrument;
ALTER TABLE orders ADD CONSTRAINT fk_orders_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE;
ALTER TABLE positions DROP FOREIGN KEY fk_positions_instrument;
ALTER TABLE positions ADD CONSTRAINT fk_positions_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE;
ALTER TABLE free_order_counters DROP FOREIGN KEY fk_free_orders_instrument;
ALTER TABLE free_order_counters ADD CONSTRAINT fk_free_orders_instrument
FOREIGN KEY (instrument_uid) REFERENCES instruments(instrument_uid) ON UPDATE CASCADE;
UPDATE schema_meta SET meta_value='0003' WHERE meta_key='schema_version';
@@ -0,0 +1,5 @@
DROP TABLE IF EXISTS daily_reports;
ALTER TABLE positions DROP INDEX ux_positions_trade;
ALTER TABLE positions DROP COLUMN exit_filled_lots;
UPDATE schema_meta SET meta_value='0003' WHERE meta_key='schema_version';
@@ -0,0 +1,11 @@
ALTER TABLE positions ADD COLUMN exit_filled_lots BIGINT NOT NULL DEFAULT 0 AFTER lots;
ALTER TABLE positions ADD UNIQUE KEY ux_positions_trade (account_id_hash, instrument_uid, open_trade_date);
CREATE TABLE IF NOT EXISTS daily_reports (
report_date DATE NOT NULL,
account_id_hash VARCHAR(128) NOT NULL,
sent_at DATETIME(3) NOT NULL,
PRIMARY KEY (report_date, account_id_hash)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
UPDATE schema_meta SET meta_value='0004' WHERE meta_key='schema_version';
@@ -0,0 +1,7 @@
ALTER TABLE risk_events
MODIFY severity ENUM('INFO','WARN','ALERT','CRITICAL','REPORT') NOT NULL;
ALTER TABLE instruments
MODIFY free_order_limit_per_day INT NOT NULL DEFAULT 0;
UPDATE schema_meta SET meta_value='0004' WHERE meta_key='schema_version';
@@ -0,0 +1,20 @@
UPDATE instruments
SET free_order_limit_per_day=0
WHERE ticker NOT IN ('TRUR', 'TGLD') AND free_order_limit_per_day=15;
ALTER TABLE instruments
MODIFY free_order_limit_per_day INT NOT NULL DEFAULT 0 COMMENT '0 means no configured free-order cap';
UPDATE risk_events
SET
severity='INFO',
event_type=CASE
WHEN event_type LIKE 'report_%' THEN event_type
ELSE CONCAT('report_', event_type)
END
WHERE severity='REPORT';
ALTER TABLE risk_events
MODIFY severity ENUM('INFO','WARN','ALERT','CRITICAL') NOT NULL;
UPDATE schema_meta SET meta_value='0005' WHERE meta_key='schema_version';
@@ -0,0 +1,3 @@
ALTER TABLE positions DROP COLUMN lot_size;
UPDATE schema_meta SET meta_value='0005' WHERE meta_key='schema_version';
@@ -0,0 +1,8 @@
ALTER TABLE positions ADD COLUMN lot_size BIGINT NOT NULL DEFAULT 1 AFTER lots;
UPDATE positions p
JOIN instruments i ON i.instrument_uid = p.instrument_uid
SET p.lot_size = i.lot
WHERE p.lot_size = 1 AND i.lot > 1;
UPDATE schema_meta SET meta_value='0006' WHERE meta_key='schema_version';
@@ -0,0 +1,8 @@
package migrations
import "embed"
// FS contains SQL migrations used by both the daemon and cmd/migrate.
//
//go:embed *.sql
var FS embed.FS