mirror of
https://github.com/multipleof4/KalBot.git
synced 2026-03-17 14:01:02 +00:00
94 lines
2.5 KiB
JavaScript
94 lines
2.5 KiB
JavaScript
import { BaseStrategy } from './base.js';
|
|
|
|
export class BullDipBuyer extends BaseStrategy {
|
|
constructor(config = {}) {
|
|
super('bull-dip-buyer', {
|
|
maxYesPrice: config.maxYesPrice || 45,
|
|
minYesPrice: config.minYesPrice || 15,
|
|
betSize: config.betSize || 1,
|
|
slippage: config.slippage || 3,
|
|
cooldownMs: config.cooldownMs || 20000,
|
|
...config
|
|
});
|
|
|
|
this._lastTrade = {
|
|
paper: { time: 0, ticker: null },
|
|
live: { time: 0, ticker: null }
|
|
};
|
|
}
|
|
|
|
evaluate(state, caller = 'paper') {
|
|
if (!state || !this.enabled) return null;
|
|
|
|
const track = this._lastTrade[caller] || this._lastTrade.paper;
|
|
const now = Date.now();
|
|
if (now - track.time < this.config.cooldownMs) return null;
|
|
if (state.ticker === track.ticker) return null;
|
|
|
|
const { yesPct } = state;
|
|
|
|
if (yesPct <= this.config.maxYesPrice && yesPct >= this.config.minYesPrice) {
|
|
// For live trading, require orderbook data — refuse to trade blind
|
|
if (caller === 'live') {
|
|
const bestAsk = this._getBestYesAsk(state);
|
|
if (bestAsk == null) {
|
|
return null;
|
|
}
|
|
// Verify the actual ask price is within our dip range + slippage
|
|
if (bestAsk > this.config.maxYesPrice + this.config.slippage) {
|
|
return null;
|
|
}
|
|
// Don't let slippage push us above 50¢ — that's not a dip
|
|
if (bestAsk > 50) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const maxPrice = Math.min(yesPct + this.config.slippage, this.config.maxYesPrice + this.config.slippage, 50);
|
|
|
|
const bestAsk = this._getBestYesAsk(state);
|
|
let reason = `Bullish dip buy: Yes @ ${yesPct}¢`;
|
|
if (bestAsk != null) {
|
|
if (bestAsk > maxPrice) {
|
|
return null;
|
|
}
|
|
reason = `Bullish dip buy: Yes @ ${yesPct}¢ (ask: ${bestAsk}¢)`;
|
|
}
|
|
|
|
const signal = {
|
|
strategy: this.name,
|
|
side: 'yes',
|
|
price: yesPct,
|
|
maxPrice,
|
|
size: this.config.betSize,
|
|
reason,
|
|
ticker: state.ticker
|
|
};
|
|
|
|
track.time = now;
|
|
track.ticker = state.ticker;
|
|
return signal;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
_getBestYesAsk(state) {
|
|
const ob = state.orderbook;
|
|
if (!ob) return null;
|
|
|
|
if (ob.no?.length) {
|
|
const bestNoBid = ob.no[0]?.[0];
|
|
if (bestNoBid != null) return 100 - bestNoBid;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
toJSON() {
|
|
return {
|
|
...super.toJSON(),
|
|
lastTradeTicker: this._lastTrade.live.ticker || this._lastTrade.paper.ticker
|
|
};
|
|
}
|
|
}
|