mirror of
https://github.com/multipleof4/KalBot.git
synced 2026-03-16 21:41:02 +00:00
Fix: Settle orphans on startup, reset flag, norm result
This commit is contained in:
35
worker.js
35
worker.js
@@ -3,6 +3,7 @@ import { PaperEngine } from './lib/paper/engine.js';
|
|||||||
import { MartingaleStrategy } from './lib/strategies/martingale.js';
|
import { MartingaleStrategy } from './lib/strategies/martingale.js';
|
||||||
import { MartingaleAlphaStrategy } from './lib/strategies/martingale-alpha.js';
|
import { MartingaleAlphaStrategy } from './lib/strategies/martingale-alpha.js';
|
||||||
import { ThresholdStrategy } from './lib/strategies/threshold.js';
|
import { ThresholdStrategy } from './lib/strategies/threshold.js';
|
||||||
|
import { getMarket } from './lib/kalshi/rest.js';
|
||||||
import { db } from './lib/db.js';
|
import { db } from './lib/db.js';
|
||||||
import { notify } from './lib/notify.js';
|
import { notify } from './lib/notify.js';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
@@ -31,6 +32,13 @@ async function main() {
|
|||||||
|
|
||||||
console.log(`[Worker] Loaded ${strategies.length} strategies: ${strategies.map((s) => s.name).join(', ')}`);
|
console.log(`[Worker] Loaded ${strategies.length} strategies: ${strategies.map((s) => s.name).join(', ')}`);
|
||||||
|
|
||||||
|
// Settle any orphaned positions from previous runs before starting
|
||||||
|
try {
|
||||||
|
await paper.settleOrphans(getMarket);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[Worker] Orphan settlement error:', e.message);
|
||||||
|
}
|
||||||
|
|
||||||
const tracker = new MarketTracker();
|
const tracker = new MarketTracker();
|
||||||
let latestMarketState = null;
|
let latestMarketState = null;
|
||||||
let heartbeatTimer = null;
|
let heartbeatTimer = null;
|
||||||
@@ -67,7 +75,7 @@ async function main() {
|
|||||||
for (const strategy of strategies) {
|
for (const strategy of strategies) {
|
||||||
if (!settledPositions) continue;
|
if (!settledPositions) continue;
|
||||||
for (const trade of settledPositions) {
|
for (const trade of settledPositions) {
|
||||||
strategy.onSettlement(result, trade);
|
strategy.onSettlement(trade.result, trade);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +96,31 @@ async function main() {
|
|||||||
writeState(latestMarketState, paper, strategies);
|
writeState(latestMarketState, paper, strategies);
|
||||||
}, HEARTBEAT_MS);
|
}, HEARTBEAT_MS);
|
||||||
|
|
||||||
|
// Check for reset flag periodically
|
||||||
|
setInterval(async () => {
|
||||||
|
try {
|
||||||
|
if (fs.existsSync('/tmp/kalbot-reset-flag')) {
|
||||||
|
fs.unlinkSync('/tmp/kalbot-reset-flag');
|
||||||
|
console.log('[Worker] Reset flag detected — resetting all paper data');
|
||||||
|
await paper.resetAll();
|
||||||
|
for (const s of strategies) {
|
||||||
|
if (s.consecutiveLosses !== undefined) s.consecutiveLosses = 0;
|
||||||
|
if (s.currentBetSize !== undefined) s.currentBetSize = s.config.baseBet;
|
||||||
|
if (s.round !== undefined) s.round = 0;
|
||||||
|
if (s.cycleWins !== undefined) s.cycleWins = 0;
|
||||||
|
if (s.cycleLosses !== undefined) s.cycleLosses = 0;
|
||||||
|
if (s.totalCycles !== undefined) s.totalCycles = 0;
|
||||||
|
s.lastTradeTicker = null;
|
||||||
|
s.lastTradeTime = 0;
|
||||||
|
}
|
||||||
|
writeState(latestMarketState, paper, strategies);
|
||||||
|
await notify('🔄 Paper trading reset by admin', 'Kalbot Reset', 'default', 'recycle');
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
console.log('[Worker] Running. Press Ctrl+C to stop.');
|
console.log('[Worker] Running. Press Ctrl+C to stop.');
|
||||||
|
|
||||||
const shutdown = async (signal) => {
|
const shutdown = async (signal) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user