Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/decorator/testdata/cost_basis_exit_strategy.csv
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ Action
0
0
0
0
-1
0
0
Expand All @@ -234,6 +233,7 @@ Action
0
0
0
1
0
0
0
Expand Down
12 changes: 6 additions & 6 deletions examples/decorator/testdata/stop_loss_strategy.csv
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Action
0
0
0
-1
0
0
0
Expand All @@ -62,16 +63,14 @@ Action
0
0
0
1
0
0
-1
1
0
0
0
0
0
0
0
-1
0
0
0
Expand Down Expand Up @@ -234,6 +233,8 @@ Action
0
0
0
1
0
0
0
0
Expand All @@ -249,4 +250,3 @@ Action
0
0
0
0
6 changes: 3 additions & 3 deletions examples/trend/aroon_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (a *AroonStrategy) ComputeWithContext(ctx context.Context, c <-chan *asset.
})

// Aroon starts only after a full period.
actions = helper.ShiftWithContext(ctx, actions, a.Aroon.Period-1, strategy.Hold)
actions = helper.ShiftWithContext(ctx, actions, a.Aroon.IdlePeriod(), strategy.Hold)

return actions
}
Expand All @@ -80,8 +80,8 @@ func (a *AroonStrategy) Report(c <-chan *asset.Snapshot) *helper.Report {
closings := asset.SnapshotsAsClosings(snapshots[3])

ups, downs := a.Aroon.Compute(highs, lows)
ups = helper.Shift(ups, a.Aroon.Period-1, 0)
downs = helper.Shift(downs, a.Aroon.Period-1, 0)
ups = helper.Shift(ups, a.Aroon.IdlePeriod(), 0)
downs = helper.Shift(downs, a.Aroon.IdlePeriod(), 0)

actions, outcomes := strategy.ComputeWithOutcome(a, snapshots[4])
annotations := strategy.ActionsToAnnotations(actions)
Expand Down
72 changes: 36 additions & 36 deletions examples/trend/testdata/aroon_strategy.csv
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,6 @@ Action
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
-1
-1
-1
Expand All @@ -83,6 +63,25 @@ Action
-1
-1
-1
1
1
1
1
1
1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
Expand Down Expand Up @@ -234,19 +233,20 @@ Action
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
10 changes: 5 additions & 5 deletions helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ func MapWithPreviousWithContext[F, T any](ctx context.Context, c <-chan F, f fun
MapWithPreviousWithContext applies a transformation function to each element in an input channel, creating a new channel with the transformed values, supporting context cancellation.

<a name="MaxSince"></a>
## func [MaxSince](<https://github.com/cinar/indicator/blob/master/helper/max_since.go#L37>)
## func [MaxSince](<https://github.com/cinar/indicator/blob/master/helper/max_since.go#L21>)

```go
func MaxSince[T Number](c <-chan T, w int) <-chan T
Expand All @@ -1113,13 +1113,13 @@ MaxSince wraps MaxSinceWithContext for backwards compatibility.
Deprecated: Use MaxSinceWithContext instead.

<a name="MaxSinceWithContext"></a>
## func [MaxSinceWithContext](<https://github.com/cinar/indicator/blob/master/helper/max_since.go#L15>)
## func [MaxSinceWithContext](<https://github.com/cinar/indicator/blob/master/helper/max_since.go#L14>)

```go
func MaxSinceWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan T
```

MaxSinceWithContext returns a channel of T indicating since when \(number of previous values\) the respective value was the maximum within the window of size w.
MaxSinceWithContext returns a channel of T indicating since when \(number of previous values\) the maximum within the window of size w was seen. When the maximum occurs more than once, the most recent one counts. NaN values are ignored, and a window of only NaN values yields NaN.

<a name="Mean"></a>
## func [Mean](<https://github.com/cinar/indicator/blob/master/helper/mean.go#L13>)
Expand All @@ -1138,7 +1138,7 @@ fmt.Println(n) // 2.5
```

<a name="MinSince"></a>
## func [MinSince](<https://github.com/cinar/indicator/blob/master/helper/min_since.go#L36>)
## func [MinSince](<https://github.com/cinar/indicator/blob/master/helper/min_since.go#L21>)

```go
func MinSince[T Number](c <-chan T, w int) <-chan T
Expand All @@ -1155,7 +1155,7 @@ Deprecated: Use MinSinceWithContext instead.
func MinSinceWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan T
```

MinSinceWithContext returns a channel of T indicating since when \(number of previous values\) the respective value was the minimum.
MinSinceWithContext returns a channel of T indicating since when \(number of previous values\) the minimum within the window of size w was seen. When the minimum occurs more than once, the most recent one counts. NaN values are ignored, and a window of only NaN values yields NaN.

<a name="Multiply"></a>
## func [Multiply](<https://github.com/cinar/indicator/blob/master/helper/multiply.go#L33>)
Expand Down
84 changes: 84 additions & 0 deletions helper/extreme_since.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2021-2026 The Indicator Authors.
// The source code is provided under GNU AGPLv3 License.
// https://github.com/cinar/indicator

package helper

import "context"

// extremeSinceWithContext returns a channel of T indicating how many values
// ago the extreme value within the sliding window of size w was, where better
// reports whether a is strictly more extreme than b. On ties the most recent
// value counts, as in TA-Lib. NaN values are never taken as the extreme; a
// window that holds only NaN values yields NaN. While the window fills, the
// values seen so far are used.
//
// The position of the extreme is kept between values, and the window is
// scanned again only when that position leaves it.
func extremeSinceWithContext[T Number](ctx context.Context, c <-chan T, w int, better func(a, b T) bool) <-chan T {
r := make(chan T)

if w <= 0 {
close(r)
return r
}

go func() {
defer close(r)

// h is a ring buffer where the value at index t is h[t%w].
h := make([]T, w)

// best is the index of the extreme value, or -1 if there is none.
best := -1

for t := 0; ; t++ {
var n T
var ok bool

select {
case <-ctx.Done():
return
case n, ok = <-c:
if !ok {
return
}
}

h[t%w] = n
oldest := max(t-w+1, 0)

switch {
case n != n: // NaN
if best < oldest {
best = -1
}
case best < oldest:
// The extreme left the window. Scan it from the newest value
// to the oldest, so that on ties the newest one is kept.
best = -1
for k := t; k >= oldest; k-- {
v := h[k%w]
if v == v && (best == -1 || better(v, h[best%w])) {
best = k
}
}
case !better(h[best%w], n):
best = t
}

out := n
if best != -1 {
out = T(t - best)
}

select {
case <-ctx.Done():
return
case r <- out:
}
}
}()

return r
}
28 changes: 6 additions & 22 deletions helper/max_since.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,15 @@

package helper

import (
"context"
"slices"
)
import "context"

// MaxSinceWithContext returns a channel of T indicating since when
// (number of previous values) the respective value was the maximum
// within the window of size w.
// (number of previous values) the maximum within the window of size w
// was seen. When the maximum occurs more than once, the most recent one
// counts. NaN values are ignored, and a window of only NaN values yields
// NaN.
func MaxSinceWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan T {
return WindowWithContext(ctx, c, func(w []T, i int) T {
since := 0
found := false
m := slices.Max(w)
SlicesReverse(w, i, func(n T) bool {
if found && n < m {
return false
}
since++
if n == m {
found = true
}
return true
})
return T(since - 1)
}, w)
return extremeSinceWithContext(ctx, c, w, func(a, b T) bool { return a > b })
}

// MaxSince wraps MaxSinceWithContext for backwards compatibility.
Expand Down
Loading
Loading