mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-06 12:30:59 +08:00
- check CancelAllOrders errors in okx/kucoin/bitget/gate open/close paths (aligns with existing binance/hyperliquid/aster/bybit pattern) - log saveDecision failures in auto_trader_loop instead of discarding - remove dead MustNormalizeTimeframe that panicked in market package - web: npm audit fix resolves react-router HIGH CVEs (GHSA-49rj-9fvp-4h2h, GHSA-2j2x-hqr9-3h42, GHSA-8x6r-g9mw-2r78, GHSA-rxv8-25v2-qmq8)
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package market
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// supportedTimeframes defines supported timeframes and their corresponding durations.
|
|
var supportedTimeframes = map[string]time.Duration{
|
|
"1m": time.Minute,
|
|
"3m": 3 * time.Minute,
|
|
"5m": 5 * time.Minute,
|
|
"15m": 15 * time.Minute,
|
|
"30m": 30 * time.Minute,
|
|
"1h": time.Hour,
|
|
"2h": 2 * time.Hour,
|
|
"4h": 4 * time.Hour,
|
|
"6h": 6 * time.Hour,
|
|
"12h": 12 * time.Hour,
|
|
"1d": 24 * time.Hour,
|
|
}
|
|
|
|
// NormalizeTimeframe normalizes the incoming timeframe string (case-insensitive, no spaces), and validates if it's supported.
|
|
func NormalizeTimeframe(tf string) (string, error) {
|
|
trimmed := strings.TrimSpace(strings.ToLower(tf))
|
|
if trimmed == "" {
|
|
return "", fmt.Errorf("timeframe cannot be empty")
|
|
}
|
|
if _, ok := supportedTimeframes[trimmed]; !ok {
|
|
return "", fmt.Errorf("unsupported timeframe '%s'", tf)
|
|
}
|
|
return trimmed, nil
|
|
}
|
|
|
|
// TFDuration returns the time duration corresponding to the given timeframe.
|
|
func TFDuration(tf string) (time.Duration, error) {
|
|
norm, err := NormalizeTimeframe(tf)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return supportedTimeframes[norm], nil
|
|
}
|
|
|
|
// SupportedTimeframes returns all supported timeframes (sorted slice).
|
|
func SupportedTimeframes() []string {
|
|
keys := make([]string, 0, len(supportedTimeframes))
|
|
for k := range supportedTimeframes {
|
|
keys = append(keys, k)
|
|
}
|
|
slices.Sort(keys)
|
|
return keys
|
|
}
|