feat: add autonomous paper-trading and calibration pipeline

This commit is contained in:
ImBenji
2026-08-03 14:03:27 +01:00
parent 5a9a2e4c6d
commit c4028cc394
46 changed files with 2246 additions and 115 deletions
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env node
const path = require('path');
const Database = require('better-sqlite3');
const { initAutonomySchema } = require('../src/autonomy/schema');
const sourcePath = process.env.LEGACY_INTELLIGENCE_DB || path.resolve(process.cwd(), 'intelligence.sqlite');
const db = new Database(sourcePath);
db.pragma('journal_mode = WAL');
initAutonomySchema(db);
const insert = db.prepare(`
INSERT OR IGNORE INTO autonomy_legacy_records(source_table, source_id, payload)
VALUES (?, ?, ?)
`);
const tx = db.transaction(() => {
for (const table of ['event_predictions', 'trade_signals', 'company_facts', 'company_relationships']) {
const exists = db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?").get(table);
if (!exists) continue;
const rows = db.prepare(`SELECT * FROM ${table}`).all();
for (const row of rows) insert.run(table, row.id, JSON.stringify(row));
console.log(`${table}: ${rows.length}`);
}
});
tx();
db.close();
+24
View File
@@ -0,0 +1,24 @@
#!/usr/bin/env node
// Initializes the additive autonomy schema and creates one reconciliation job.
// It deliberately does not rewrite legacy intelligence or enqueue millions of
// historical records; reconciliation workers discover those in bounded batches.
const path = require('path');
const Database = require('better-sqlite3');
const { initAutonomySchema } = require('../src/autonomy/schema');
const { enqueueJob } = require('../src/autonomy/jobs');
const intelligencePath = process.env.INTELLIGENCE_DB || path.resolve(process.cwd(), 'intelligence.sqlite');
const db = new Database(intelligencePath);
db.pragma('journal_mode = WAL');
initAutonomySchema(db);
const result = enqueueJob(db, {
jobType: 'reconcile_archive',
lane: 'maintenance',
priority: 100,
entityType: 'archive',
entityId: 'archive',
idempotencyKey: 'reconcile_archive:v1',
});
console.log(JSON.stringify({ intelligencePath, reconciliationJobInserted: result.inserted }));
db.close();
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env node
const Database = require('better-sqlite3');
const path = require('path');
const { initAutonomySchema } = require('../src/autonomy/schema');
const symbol = String(process.argv[2] || '').trim().toUpperCase();
if (!/^[A-Z0-9._/-]+$/.test(symbol)) {
console.error('usage: node scripts/set-autonomy-instrument.js SYMBOL [broker]');
process.exit(2);
}
const broker = String(process.argv[3] || 'simulator');
const db = new Database(process.env.INTELLIGENCE_DB || path.resolve(process.cwd(), 'intelligence.sqlite'));
initAutonomySchema(db);
db.prepare(`
INSERT INTO autonomy_instruments(symbol, broker, active, tradable)
VALUES (?, ?, 1, 1)
ON CONFLICT(symbol) DO UPDATE SET broker=excluded.broker, active=1, tradable=1, updated_at=datetime('now')
`).run(symbol, broker);
console.log(JSON.stringify({ symbol, broker, active: true, tradable: true }));
db.close();
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env node
const path = require('path');
const Database = require('better-sqlite3');
const { initAutonomySchema } = require('../src/autonomy/schema');
const { createAlpacaPaperClient } = require('../src/brokers/alpacaPaper');
async function main() {
const client = createAlpacaPaperClient({
keyId: process.env.ALPACA_PAPER_KEY_ID,
secretKey: process.env.ALPACA_PAPER_SECRET_KEY,
});
const assets = await client.getAssets();
const db = new Database(process.env.INTELLIGENCE_DB || path.resolve(process.cwd(), 'intelligence.sqlite'));
initAutonomySchema(db);
const upsert = db.prepare(`
INSERT INTO autonomy_instruments(symbol, broker, asset_class, active, tradable, shortable, fractionable, updated_at)
VALUES (?, 'alpaca-paper', ?, ?, ?, ?, ?, datetime('now'))
ON CONFLICT(symbol) DO UPDATE SET
broker=excluded.broker, asset_class=excluded.asset_class, active=excluded.active,
tradable=excluded.tradable, shortable=excluded.shortable, fractionable=excluded.fractionable,
updated_at=datetime('now')
`);
const tx = db.transaction(() => assets.forEach((asset) => upsert.run(
asset.symbol, asset.class || 'us_equity', asset.status === 'active' ? 1 : 0,
asset.tradable ? 1 : 0, asset.shortable ? 1 : 0, asset.fractionable ? 1 : 0
)));
tx();
console.log(JSON.stringify({ synced: assets.length }));
db.close();
}
main().catch((error) => { console.error(error.message); process.exit(1); });