1Branch0Tags
GL
glucryptoReserve only trade targets in price trigger profil...
4a63a6312 days ago20Commits
typescript
import type { BacktestResolution } from "opentool/backtest"; import type { PriceTriggerConfig } from "../config"; const DEFAULT_PRICE_TRIGGER_SCHEDULE_MINUTES = 60; function resolveCronFields(cron: string | undefined) { if (!cron) return null; const fields = cron.trim().split(/\s+/).filter(Boolean); if (fields.length === 5) return fields; if (fields.length === 6) return fields.slice(1); return null; } export function resolvePriceTriggerScheduleMinutes( config: PriceTriggerConfig, ): number { const cronFields = resolveCronFields(config.schedule?.cron); if (!cronFields) { return DEFAULT_PRICE_TRIGGER_SCHEDULE_MINUTES; } const [minute, hour, dayOfMonth, month, dayOfWeek] = cronFields; if ( minute && hour && dayOfMonth && month && dayOfWeek && /^\*\/(\d+)$/.test(minute) && hour === "*" && dayOfMonth === "*" && month === "*" && dayOfWeek === "*" ) { const intervalMinutes = Number.parseInt(minute.slice(2), 10); if (Number.isFinite(intervalMinutes) && intervalMinutes > 0) { return intervalMinutes; } } if ( minute && hour && dayOfMonth && month && dayOfWeek && /^\d+$/.test(minute) && hour === "*" && dayOfMonth === "*" && month === "*" && dayOfWeek === "*" ) { return 60; } if ( minute && hour && dayOfMonth && month && dayOfWeek && /^\d+$/.test(minute) && /^\*\/(\d+)$/.test(hour) && dayOfMonth === "*" && month === "*" && dayOfWeek === "*" ) { const intervalHours = Number.parseInt(hour.slice(2), 10); if (Number.isFinite(intervalHours) && intervalHours > 0) { return intervalHours * 60; } } if ( minute && hour && dayOfMonth && month && dayOfWeek && /^\d+$/.test(minute) && /^\d+$/.test(hour) && dayOfMonth === "*" && month === "*" && dayOfWeek === "*" ) { return 24 * 60; } if ( minute && hour && dayOfMonth && month && dayOfWeek && /^\d+$/.test(minute) && /^\d+$/.test(hour) && dayOfMonth === "*" && month === "*" && dayOfWeek !== "*" ) { const weeklyRuns = dayOfWeek .split(",") .map((value) => value.trim()) .filter((value) => value.length > 0).length; return weeklyRuns > 1 ? Math.round((7 * 24 * 60) / weeklyRuns) : 7 * 24 * 60; } if ( minute && hour && dayOfMonth && month && dayOfWeek && /^\d+$/.test(minute) && /^\d+$/.test(hour) && dayOfMonth !== "*" && month === "*" && dayOfWeek === "*" ) { return 30 * 24 * 60; } return DEFAULT_PRICE_TRIGGER_SCHEDULE_MINUTES; } export function resolvePriceTriggerBacktestResolution( scheduleMinutes: number, ): BacktestResolution { if (scheduleMinutes <= 5) return "1"; if (scheduleMinutes <= 15) return "5"; if (scheduleMinutes <= 60) return "15"; if (scheduleMinutes <= 240) return "60"; if (scheduleMinutes <= 24 * 60) return "240"; return "1D"; }