mirror of
https://github.com/multipleof4/KalBot.git
synced 2026-03-16 21:41:02 +00:00
Feat: Split paper/live tracking, orderbook pricing
This commit is contained in:
@@ -3,42 +3,84 @@ import { BaseStrategy } from './base.js';
|
|||||||
export class BullDipBuyer extends BaseStrategy {
|
export class BullDipBuyer extends BaseStrategy {
|
||||||
constructor(config = {}) {
|
constructor(config = {}) {
|
||||||
super('bull-dip-buyer', {
|
super('bull-dip-buyer', {
|
||||||
maxYesPrice: config.maxYesPrice || 45, // Buy the dip when Yes is cheap
|
maxYesPrice: config.maxYesPrice || 45,
|
||||||
minYesPrice: config.minYesPrice || 15, // Avoid completely dead markets
|
minYesPrice: config.minYesPrice || 15,
|
||||||
betSize: config.betSize || 1, // Changed to $1 per Master's orders!
|
betSize: config.betSize || 1,
|
||||||
cooldownMs: config.cooldownMs || 60000,
|
slippage: config.slippage || 3, // willing to pay up to 3¢ above target
|
||||||
|
cooldownMs: config.cooldownMs || 20000, // 20s for fast markets
|
||||||
...config
|
...config
|
||||||
});
|
});
|
||||||
|
|
||||||
this.lastTradeTime = 0;
|
// Separate tracking for paper vs live
|
||||||
this.lastTradeTicker = null;
|
this._lastTrade = {
|
||||||
|
paper: { time: 0, ticker: null },
|
||||||
|
live: { time: 0, ticker: null }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
evaluate(state) {
|
evaluate(state, caller = 'paper') {
|
||||||
if (!state || !this.enabled) return null;
|
if (!state || !this.enabled) return null;
|
||||||
|
|
||||||
|
const track = this._lastTrade[caller] || this._lastTrade.paper;
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (now - this.lastTradeTime < this.config.cooldownMs) return null;
|
if (now - track.time < this.config.cooldownMs) return null;
|
||||||
if (state.ticker === this.lastTradeTicker) return null;
|
if (state.ticker === track.ticker) return null;
|
||||||
|
|
||||||
const { yesPct } = state;
|
const { yesPct } = state;
|
||||||
|
|
||||||
// Only buy YES when it dips into our target buy zone
|
|
||||||
if (yesPct <= this.config.maxYesPrice && yesPct >= this.config.minYesPrice) {
|
if (yesPct <= this.config.maxYesPrice && yesPct >= this.config.minYesPrice) {
|
||||||
|
const maxPrice = Math.min(yesPct + this.config.slippage, 95);
|
||||||
|
|
||||||
|
// Check orderbook for real liquidity if available
|
||||||
|
const bestAsk = this._getBestYesAsk(state);
|
||||||
|
let reason = `Bullish dip buy: Yes @ ${yesPct}¢`;
|
||||||
|
if (bestAsk != null) {
|
||||||
|
if (bestAsk > maxPrice) {
|
||||||
|
return null; // best ask too expensive even with slippage
|
||||||
|
}
|
||||||
|
reason = `Bullish dip buy: Yes @ ${yesPct}¢ (ask: ${bestAsk}¢)`;
|
||||||
|
}
|
||||||
|
|
||||||
const signal = {
|
const signal = {
|
||||||
strategy: this.name,
|
strategy: this.name,
|
||||||
side: 'yes',
|
side: 'yes',
|
||||||
price: yesPct,
|
price: yesPct,
|
||||||
|
maxPrice,
|
||||||
size: this.config.betSize,
|
size: this.config.betSize,
|
||||||
reason: `Bullish dip buy: Yes dropped to ${yesPct}¢`,
|
reason,
|
||||||
ticker: state.ticker
|
ticker: state.ticker
|
||||||
};
|
};
|
||||||
|
|
||||||
this.lastTradeTime = now;
|
track.time = now;
|
||||||
this.lastTradeTicker = state.ticker;
|
track.ticker = state.ticker;
|
||||||
return signal;
|
return signal;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getBestYesAsk(state) {
|
||||||
|
// Best yes ask = lowest price someone is selling yes at
|
||||||
|
// In Kalshi terms: best ask on yes side, OR 100 - best bid on no side
|
||||||
|
const ob = state.orderbook;
|
||||||
|
if (!ob) return null;
|
||||||
|
|
||||||
|
// Yes side: sorted descending by price, ask = lowest offer
|
||||||
|
if (ob.yes?.length) {
|
||||||
|
// orderbook yes levels are bids (people wanting to buy yes)
|
||||||
|
// The ask is 100 - best_no_bid
|
||||||
|
}
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user