fix(market): handle invalid period in calculateDonchian

This commit is contained in:
tinkle-community
2026-01-17 21:43:14 +08:00
parent 5c79aa451e
commit cbe753b9e6
2 changed files with 19 additions and 1 deletions

View File

@@ -1213,7 +1213,7 @@ func ExportCalculateBOLL(klines []Kline, period int, multiplier float64) (upper,
// calculateDonchian calculates Donchian channel (highest high, lowest low) for given period
func calculateDonchian(klines []Kline, period int) (upper, lower float64) {
if len(klines) == 0 {
if len(klines) == 0 || period <= 0 {
return 0, 0
}

View File

@@ -537,3 +537,21 @@ func TestCalculateDonchian_PartialPeriod(t *testing.T) {
t.Errorf("Expected lower = 88, got %v", lower)
}
}
func TestCalculateDonchian_InvalidPeriod(t *testing.T) {
klines := []Kline{
{High: 100, Low: 90},
}
// Zero period should return (0, 0)
upper, lower := ExportCalculateDonchian(klines, 0)
if upper != 0 || lower != 0 {
t.Errorf("Expected (0, 0) for zero period, got (%v, %v)", upper, lower)
}
// Negative period should return (0, 0)
upper, lower = ExportCalculateDonchian(klines, -1)
if upper != 0 || lower != 0 {
t.Errorf("Expected (0, 0) for negative period, got (%v, %v)", upper, lower)
}
}