Files
Duriin-API/scripts/sync-paper-assets.js
T

34 lines
1.4 KiB
JavaScript

#!/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); });