|
| 1 | +// Copyright (c) 2021-2026 Onur Cinar. |
| 2 | +// The source code is provided under GNU AGPLv3 License. |
| 3 | +// https://github.com/cinar/indicator |
| 4 | + |
| 5 | +package volatility_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/cinar/indicator/v2/helper" |
| 11 | + "github.com/cinar/indicator/v2/volatility" |
| 12 | +) |
| 13 | + |
| 14 | +func TestChop(t *testing.T) { |
| 15 | + type Data struct { |
| 16 | + High float64 `header:"High"` |
| 17 | + Low float64 `header:"Low"` |
| 18 | + Close float64 `header:"Close"` |
| 19 | + Expected float64 `header:"Expected"` |
| 20 | + } |
| 21 | + |
| 22 | + input, err := helper.ReadFromCsvFile[Data]("testdata/chop.csv") |
| 23 | + if err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + |
| 27 | + inputs := helper.Duplicate(input, 4) |
| 28 | + |
| 29 | + highs := helper.Map(inputs[0], func(d *Data) float64 { return d.High }) |
| 30 | + lows := helper.Map(inputs[1], func(d *Data) float64 { return d.Low }) |
| 31 | + closings := helper.Map(inputs[2], func(d *Data) float64 { return d.Close }) |
| 32 | + |
| 33 | + chop := volatility.NewChop[float64]() |
| 34 | + actuals := chop.Compute(highs, lows, closings) |
| 35 | + actuals = helper.RoundDigits(actuals, 6) |
| 36 | + |
| 37 | + inputs[3] = helper.Skip(inputs[3], chop.IdlePeriod()) |
| 38 | + |
| 39 | + for data := range inputs[3] { |
| 40 | + actual, ok := <-actuals |
| 41 | + if !ok { |
| 42 | + t.Fatal("actuals channel closed early") |
| 43 | + } |
| 44 | + if actual != data.Expected { |
| 45 | + t.Fatalf("actual %v expected %v for High=%v Low=%v Close=%v", actual, data.Expected, data.High, data.Low, data.Close) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if _, ok := <-actuals; ok { |
| 50 | + t.Fatal("actuals channel should be closed") |
| 51 | + } |
| 52 | + |
| 53 | + if chop.String() != "CHOP(14)" { |
| 54 | + t.Fatalf("expected CHOP(14) but got %s", chop.String()) |
| 55 | + } |
| 56 | + |
| 57 | + if chop.IdlePeriod() != 14 { |
| 58 | + t.Fatalf("expected 14 but got %d", chop.IdlePeriod()) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestNewChop(t *testing.T) { |
| 63 | + chop := volatility.NewChop[float64]() |
| 64 | + if chop == nil { |
| 65 | + t.Fatal("NewChop should not be nil") |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestNewChopWithPeriod(t *testing.T) { |
| 70 | + chop := volatility.NewChopWithPeriod[float64](10) |
| 71 | + if chop.Period != 10 { |
| 72 | + t.Fatalf("expected 10 but got %d", chop.Period) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestChopDiffZero(t *testing.T) { |
| 77 | + highs := helper.SliceToChan([]float64{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}) |
| 78 | + lows := helper.SliceToChan([]float64{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}) |
| 79 | + closings := helper.SliceToChan([]float64{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}) |
| 80 | + |
| 81 | + chop := volatility.NewChopWithPeriod[float64](2) |
| 82 | + actuals := chop.Compute(highs, lows, closings) |
| 83 | + |
| 84 | + for actual := range actuals { |
| 85 | + if actual != 0 { |
| 86 | + t.Fatalf("expected 0 but got %v", actual) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments