From c2b7d6e9cbd28dc8c88966164e4ced0743f67b33 Mon Sep 17 00:00:00 2001 From: sebastienverreault Date: Fri, 6 May 2022 17:52:21 +0900 Subject: [PATCH] Fix: annualized funding yield (#95) * fix: annualize funding yield * chore: add supplemental annualized funding yield * chore: use annualized funding yield in exporter Co-authored-by: Sebastien Verreault --- dealer/src/Dealer.ts | 4 ++-- .../repositories/FundingRatesRepository.ts | 22 +++++++++++++++++++ .../sql/funding_rates/get_funding_yield.sql | 4 ++-- dealer/src/servers/exporter/exporter.ts | 2 +- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/dealer/src/Dealer.ts b/dealer/src/Dealer.ts index dc275e90..61959244 100644 --- a/dealer/src/Dealer.ts +++ b/dealer/src/Dealer.ts @@ -542,7 +542,7 @@ export class Dealer { return result.value } - public async getFundingYieldMetrics(): Promise { + public async getAnnualFundingYieldMetrics(): Promise { const ret: FundingYieldMetrics = { fundingYield1d: NaN, fundingYield1W: NaN, @@ -572,7 +572,7 @@ export class Dealer { { tenor: "5Y", numberOfDays: 5 * 365 }, ] for (const tenor of tenors) { - const result = await database.fundingRates.getFundingYield( + const result = await database.fundingRates.getAnnualFundingYield( ExchangeNames.Okex, tenor.numberOfDays, ) diff --git a/dealer/src/database/repositories/FundingRatesRepository.ts b/dealer/src/database/repositories/FundingRatesRepository.ts index 0a8e0716..fa55fbc5 100644 --- a/dealer/src/database/repositories/FundingRatesRepository.ts +++ b/dealer/src/database/repositories/FundingRatesRepository.ts @@ -69,6 +69,28 @@ export class FundingRatesRepository { } } + public async getAnnualFundingYield( + exchangeName: ExchangeNames, + numberOfDays: number, + ): Promise> { + try { + const annualFundingYield = await this.db.one( + sql.get_funding_yield, + { exchangeName, numberOfDays }, + (a: { annualFundingYield: number }) => a && a.annualFundingYield, + ) + this.logger.info( + { annualFundingYield }, + "getAnnualFundingYield() returned: {result}.", + ) + + return { ok: true, value: annualFundingYield } + } catch (error) { + this.logger.error({ error }, "Error: getAnnualFundingYield() failed.") + return { ok: false, error: error } + } + } + public async getLastFundingTime(): Promise> { try { const fundingTime = await this.db.oneOrNone( diff --git a/dealer/src/database/sql/funding_rates/get_funding_yield.sql b/dealer/src/database/sql/funding_rates/get_funding_yield.sql index 297e4ecb..96ac45e1 100644 --- a/dealer/src/database/sql/funding_rates/get_funding_yield.sql +++ b/dealer/src/database/sql/funding_rates/get_funding_yield.sql @@ -1,7 +1,7 @@ SELECT - ROUND(COALESCE(dealer.mul( COALESCE(1 + funding_rate, 1) ) - 1, 0), 15) as funding_yield + ROUND(COALESCE(dealer.mul( COALESCE(1 + funding_rate, 1) ) - 1, 0), 15) as funding_yield, + ((1. + ROUND(COALESCE(dealer.mul( COALESCE(1 + funding_rate, 1) ) - 1, 0), 15))^(365.25 / COALESCE(NULLIF(${numberOfDays}, 0), 0.0000001))) - 1. as annual_funding_yield FROM dealer.funding_rates WHERE funding_rate IS NOT NULL AND exchange_id = (SELECT id FROM dealer.exchanges where exchange_name = lower(${exchangeName})) AND timestamp >= current_timestamp - make_interval(days => ${numberOfDays}); - diff --git a/dealer/src/servers/exporter/exporter.ts b/dealer/src/servers/exporter/exporter.ts index 3925d8b0..4c2cf3a3 100644 --- a/dealer/src/servers/exporter/exporter.ts +++ b/dealer/src/servers/exporter/exporter.ts @@ -640,7 +640,7 @@ export async function exporter() { ) // Funding Yields - const fundingYieldMetrics = await dealer.getFundingYieldMetrics() + const fundingYieldMetrics = await dealer.getAnnualFundingYieldMetrics() Metrics.set(metrics["fundingYield1d"], fundingYieldMetrics.fundingYield1d) Metrics.set(metrics["fundingYield1W"], fundingYieldMetrics.fundingYield1W) Metrics.set(metrics["fundingYield2W"], fundingYieldMetrics.fundingYield2W)