Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
Fix: annualized funding yield (#95)
Browse files Browse the repository at this point in the history
* fix: annualize funding yield
* chore: add supplemental annualized funding yield
* chore: use annualized funding yield in exporter
Co-authored-by: Sebastien Verreault <[email protected]>
  • Loading branch information
sebastienverreault authored May 6, 2022
1 parent 0c554c4 commit c2b7d6e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dealer/src/Dealer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ export class Dealer {
return result.value
}

public async getFundingYieldMetrics(): Promise<FundingYieldMetrics> {
public async getAnnualFundingYieldMetrics(): Promise<FundingYieldMetrics> {
const ret: FundingYieldMetrics = {
fundingYield1d: NaN,
fundingYield1W: NaN,
Expand Down Expand Up @@ -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,
)
Expand Down
22 changes: 22 additions & 0 deletions dealer/src/database/repositories/FundingRatesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ export class FundingRatesRepository {
}
}

public async getAnnualFundingYield(
exchangeName: ExchangeNames,
numberOfDays: number,
): Promise<Result<number>> {
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<Result<number | null>> {
try {
const fundingTime = await this.db.oneOrNone(
Expand Down
4 changes: 2 additions & 2 deletions dealer/src/database/sql/funding_rates/get_funding_yield.sql
Original file line number Diff line number Diff line change
@@ -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});

2 changes: 1 addition & 1 deletion dealer/src/servers/exporter/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c2b7d6e

Please sign in to comment.