mirror of
https://github.com/multipleof4/KalBot.git
synced 2026-03-17 05:51:02 +00:00
Feat: Expose orderbook in getState for strategies
This commit is contained in:
@@ -102,7 +102,12 @@ export class MarketTracker extends EventEmitter {
|
||||
closeTime: this.marketData.close_time || this.marketData.expiration_time,
|
||||
status: this.marketData.status,
|
||||
result: this.marketData.result,
|
||||
timestamp: Date.now()
|
||||
timestamp: Date.now(),
|
||||
// Expose raw orderbook for strategies and live engine
|
||||
orderbook: {
|
||||
yes: this.orderbook.yes,
|
||||
no: this.orderbook.no
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -133,14 +138,12 @@ export class MarketTracker extends EventEmitter {
|
||||
return null;
|
||||
};
|
||||
|
||||
// Try cents first (from REST API), then dollar-string fields (from WS ticker)
|
||||
let yesBid = pick('yes_bid', 'yesBid');
|
||||
let yesAsk = pick('yes_ask', 'yesAsk');
|
||||
let noBid = pick('no_bid', 'noBid');
|
||||
let noAsk = pick('no_ask', 'noAsk');
|
||||
let lastPrice = pick('last_price', 'lastPrice', 'yes_price', 'yesPrice');
|
||||
|
||||
// WS ticker sends dollar strings — convert to cents
|
||||
if (yesBid == null) yesBid = dollarsToCents(market?.yes_bid_dollars);
|
||||
if (yesAsk == null) yesAsk = dollarsToCents(market?.yes_ask_dollars);
|
||||
if (noBid == null) noBid = dollarsToCents(market?.no_bid_dollars);
|
||||
@@ -159,12 +162,9 @@ export class MarketTracker extends EventEmitter {
|
||||
let qty = null;
|
||||
|
||||
if (Array.isArray(level)) {
|
||||
// New API format: ["0.4200", "300.00"] (dollar strings)
|
||||
// Or old format: [42, 300] (cents integers)
|
||||
const rawPrice = level[0];
|
||||
const rawQty = level[1];
|
||||
|
||||
// Detect dollar-string format (contains a decimal point and is < 1.01)
|
||||
if (typeof rawPrice === 'string' && rawPrice.includes('.')) {
|
||||
price = dollarsToCents(rawPrice);
|
||||
qty = this._num(rawQty);
|
||||
@@ -173,7 +173,6 @@ export class MarketTracker extends EventEmitter {
|
||||
qty = this._num(rawQty);
|
||||
}
|
||||
} else if (level && typeof level === 'object') {
|
||||
// Object format
|
||||
const rawPrice = level.price ?? level.price_dollars ?? level[0];
|
||||
const rawQty = level.qty ?? level.quantity ?? level.size ?? level.count ?? level[1];
|
||||
|
||||
@@ -195,7 +194,6 @@ export class MarketTracker extends EventEmitter {
|
||||
_normalizeOrderbook(book) {
|
||||
const root = book?.orderbook && typeof book.orderbook === 'object' ? book.orderbook : book;
|
||||
return {
|
||||
// Support both old fields (yes/no) and new fields (yes_dollars_fp/no_dollars_fp)
|
||||
yes: this._normalizeBookSide(root?.yes ?? root?.yes_dollars_fp ?? root?.yes_dollars),
|
||||
no: this._normalizeBookSide(root?.no ?? root?.no_dollars_fp ?? root?.no_dollars)
|
||||
};
|
||||
@@ -360,13 +358,11 @@ export class MarketTracker extends EventEmitter {
|
||||
if (msg.market_ticker !== this.currentTicker) return;
|
||||
|
||||
if (msg.type === 'orderbook_snapshot') {
|
||||
// New format: yes_dollars_fp / no_dollars_fp
|
||||
this.orderbook = this._normalizeOrderbook(msg);
|
||||
console.log(`[Tracker] Orderbook snapshot: ${this.orderbook.yes.length} yes levels, ${this.orderbook.no.length} no levels`);
|
||||
} else if (msg.type === 'orderbook_delta') {
|
||||
const side = String(msg.side || '').toLowerCase();
|
||||
|
||||
// New format uses price_dollars + delta_fp
|
||||
let price = this._num(msg.price);
|
||||
if (price == null) price = dollarsToCents(msg.price_dollars);
|
||||
|
||||
@@ -387,7 +383,6 @@ export class MarketTracker extends EventEmitter {
|
||||
|
||||
this.orderbook[side] = [...map.entries()].sort((a, b) => b[0] - a[0]);
|
||||
} else {
|
||||
// Batch delta arrays (old format fallback)
|
||||
const yesArr = msg.yes ?? msg.yes_dollars_fp;
|
||||
const noArr = msg.no ?? msg.no_dollars_fp;
|
||||
if (Array.isArray(yesArr)) this.orderbook.yes = this._applyDelta(this.orderbook.yes, yesArr);
|
||||
@@ -402,7 +397,6 @@ export class MarketTracker extends EventEmitter {
|
||||
if (msg.market_ticker !== this.currentTicker) return;
|
||||
|
||||
if (this.marketData) {
|
||||
// New API sends dollar-string fields; store them for _extractMarketQuotes
|
||||
const fields = [
|
||||
'yes_bid', 'yes_ask', 'no_bid', 'no_ask', 'last_price', 'volume',
|
||||
'yes_bid_dollars', 'yes_ask_dollars', 'no_bid_dollars', 'no_ask_dollars',
|
||||
@@ -414,7 +408,6 @@ export class MarketTracker extends EventEmitter {
|
||||
if (msg[key] != null) this.marketData[key] = msg[key];
|
||||
}
|
||||
|
||||
// Also map dollar_volume / dollar_open_interest to standard fields
|
||||
if (msg.dollar_volume != null) this.marketData.volume = this._num(msg.dollar_volume) ?? this.marketData.volume;
|
||||
if (msg.dollar_open_interest != null) this.marketData.open_interest = this._num(msg.dollar_open_interest) ?? this.marketData.open_interest;
|
||||
if (msg.volume_fp != null && this.marketData.volume == null) this.marketData.volume = this._num(msg.volume_fp);
|
||||
|
||||
Reference in New Issue
Block a user