Skip to content

Commit

Permalink
Add script to print candle time boundaries (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill authored Mar 1, 2024
1 parent da21726 commit 9850cb4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions indexer/services/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pnpm run validate-pnl -- \

KAFKA_BROKER_URLS=...kafka.ap-northeast-1.amazonaws.com:9092 \
SERVICE_NAME=scripts pnpm run print-block -- --h 9265388

SERVICE_NAME=script pnpm run print-candle-time-boundaries -- \
--t 2024-02-28T10:01:36.17+00:00
```

### EnvVars
Expand Down
1 change: 1 addition & 0 deletions indexer/services/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build:watch": "pnpm run build -- --watch",
"validate-pnl": "ts-node src/validate-pnl.ts",
"print-block": "ts-node src/print-block.ts",
"print-candle-time-boundaries": "ts-node src/print-candle-time-boundaries.ts",
"coverage": "pnpm test -- --coverage",
"lint": "eslint --ext .ts,.js .",
"lint:fix": "eslint --ext .ts,.js . --fix",
Expand Down
48 changes: 48 additions & 0 deletions indexer/services/scripts/src/print-candle-time-boundaries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { CandleResolution, NUM_SECONDS_IN_CANDLE_RESOLUTIONS } from '@dydxprotocol-indexer/postgres';
import { DateTime } from 'luxon';
import yargs from 'yargs';

export function calculateNormalizedCandleStartTime(
time: DateTime,
resolution: CandleResolution,
): DateTime {
const epochSeconds: number = Math.floor(time.toUTC().toSeconds());
const normalizedTimeSeconds: number = epochSeconds - (
epochSeconds % NUM_SECONDS_IN_CANDLE_RESOLUTIONS[resolution]
);

return DateTime.fromSeconds(normalizedTimeSeconds).toUTC();
}

// Get normalized boundaries for a given time and resolutions
function getNormalizedBoundaries(time: string, resolutions: CandleResolution[]): void {
const date = DateTime.fromISO(time);

resolutions.forEach((resolution: CandleResolution) => {
const startTime = calculateNormalizedCandleStartTime(date, resolution);
const endTime = startTime.plus({ seconds: NUM_SECONDS_IN_CANDLE_RESOLUTIONS[resolution] });

console.log(`Resolution: ${resolution}, Start Time: ${startTime.toISO()}, End Time: ${endTime.toISO()}`);
});
}

const resolutions: CandleResolution[] = [
CandleResolution.ONE_DAY,
CandleResolution.FOUR_HOURS,
CandleResolution.ONE_HOUR,
CandleResolution.THIRTY_MINUTES,
CandleResolution.FIFTEEN_MINUTES,
CandleResolution.FIVE_MINUTES,
CandleResolution.ONE_MINUTE,
];

const args = yargs.options({
time: {
type: 'string',
alias: 't',
description: 'Time to compute normalized boundaries for, e.g. 2024-02-28T10:01:36.17+00:00',
required: true,
},
}).argv;

getNormalizedBoundaries(args.time, resolutions);

0 comments on commit 9850cb4

Please sign in to comment.