mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-12 15:26:55 +08:00
fix(market): handle invalid period in calculateDonchian
This commit is contained in:
@@ -1213,7 +1213,7 @@ func ExportCalculateBOLL(klines []Kline, period int, multiplier float64) (upper,
|
|||||||
|
|
||||||
// calculateDonchian calculates Donchian channel (highest high, lowest low) for given period
|
// calculateDonchian calculates Donchian channel (highest high, lowest low) for given period
|
||||||
func calculateDonchian(klines []Kline, period int) (upper, lower float64) {
|
func calculateDonchian(klines []Kline, period int) (upper, lower float64) {
|
||||||
if len(klines) == 0 {
|
if len(klines) == 0 || period <= 0 {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -537,3 +537,21 @@ func TestCalculateDonchian_PartialPeriod(t *testing.T) {
|
|||||||
t.Errorf("Expected lower = 88, got %v", lower)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user