Skip to content

Commit

Permalink
Use weighted median by default
Browse files Browse the repository at this point in the history
  • Loading branch information
adg-flare committed Nov 26, 2024
1 parent e189965 commit 98b7a03
Showing 1 changed file with 7 additions and 29 deletions.
36 changes: 7 additions & 29 deletions src/data-feeds/ccxt-provider-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ interface PriceInfo {
}

const usdtToUsdFeedId: FeedId = { category: FeedCategory.Crypto.valueOf(), name: "USDT/USD" };
const lambda = process.env.MEDIAN_DECAY ? parseFloat(process.env.MEDIAN_DECAY) : undefined;
// Parameter for exponential decay in time-weighted median price calculation
const lambda = process.env.MEDIAN_DECAY ? parseFloat(process.env.MEDIAN_DECAY) : 0.00005;

export class CcxtFeed implements BaseDataFeed {
private readonly logger = new Logger(CcxtFeed.name);
Expand Down Expand Up @@ -132,7 +133,7 @@ export class CcxtFeed implements BaseDataFeed {
continue;
}

this.setPrice(exchangeName, ticker.symbol, ticker.last, ticker.timestamp ?? 0);
this.setPrice(exchangeName, ticker.symbol, ticker.last, ticker.timestamp);
}
} else {
throw new Error("Exchange does not support fetchTickers");
Expand All @@ -152,7 +153,7 @@ export class CcxtFeed implements BaseDataFeed {
continue;
}

this.setPrice(exchangeName, ticker.symbol, ticker.last, ticker.timestamp ?? 0);
this.setPrice(exchangeName, ticker.symbol, ticker.last, ticker.timestamp);
}
}
}
Expand Down Expand Up @@ -196,15 +197,15 @@ export class CcxtFeed implements BaseDataFeed {

private processTrades(trades: Trade[], exchangeName: string) {
trades.forEach(trade => {
this.setPrice(exchangeName, trade.symbol, trade.price, trade.timestamp ?? Date.now());
this.setPrice(exchangeName, trade.symbol, trade.price, trade.timestamp);
});
}

private setPrice(exchangeName: string, symbol: string, price: number, timestamp: number) {
const prices = this.prices.get(symbol) || new Map<string, PriceInfo>();
prices.set(exchangeName, {
value: price,
time: timestamp,
time: timestamp ?? Date.now(),
exchange: exchangeName,
});
this.prices.set(symbol, prices);
Expand Down Expand Up @@ -254,30 +255,7 @@ export class CcxtFeed implements BaseDataFeed {
}

this.logger.debug(`Calculating results for ${JSON.stringify(feedId)}`);
if (lambda === undefined) {
return this.median(prices);
} else {
return this.weightedMedian(prices);
}
}

private median(prices: PriceInfo[]): number {
// If single price found, return price
if (prices.length === 1) {
return prices[0].value;
}

// Sort the prices in ascending order
prices.sort((a, b) => a.value - b.value);

// Calculate the median
const mid = Math.floor(prices.length / 2);
const median =
prices.length % 2 !== 0
? prices[mid].value // Odd number of elements, take the middle one
: (prices[mid - 1].value + prices[mid].value) / 2; // Even number of elements, average the two middle ones

return median;
return this.weightedMedian(prices);
}

private weightedMedian(prices: PriceInfo[]): number {
Expand Down

0 comments on commit 98b7a03

Please sign in to comment.