Files
KalBot/lib/strategies/threshold.js

63 lines
1.5 KiB
JavaScript

import { BaseStrategy } from './base.js';
export class ThresholdStrategy extends BaseStrategy {
constructor(config = {}) {
super('threshold', {
triggerPct: config.triggerPct || 65,
betSize: config.betSize || 1,
cooldownMs: config.cooldownMs || 90000,
...config
});
this.lastTradeTime = 0;
this.lastTradeTicker = null;
}
evaluate(state) {
if (!state || !this.enabled) return null;
const now = Date.now();
if (now - this.lastTradeTime < this.config.cooldownMs) return null;
if (state.ticker === this.lastTradeTicker) return null;
const { yesPct, noPct } = state;
const trigger = this.config.triggerPct;
let signal = null;
if (yesPct >= trigger && noPct < 99) {
signal = {
strategy: this.name,
side: 'no',
price: noPct,
size: this.config.betSize,
reason: `Yes at ${yesPct}% (≥${trigger}%), contrarian No at ${noPct}¢`,
ticker: state.ticker
};
} else if (noPct >= trigger && yesPct < 99) {
signal = {
strategy: this.name,
side: 'yes',
price: yesPct,
size: this.config.betSize,
reason: `No at ${noPct}% (≥${trigger}%), contrarian Yes at ${yesPct}¢`,
ticker: state.ticker
};
}
if (signal) {
this.lastTradeTime = now;
this.lastTradeTicker = state.ticker;
}
return signal;
}
toJSON() {
return {
...super.toJSON(),
lastTradeTicker: this.lastTradeTicker
};
}
}