Repository navigation
Expand file tree
/
Copy pathtriple_moving_average_crossover_strategy.go
More file actions
173 lines (137 loc) · 5.92 KB
/
Copy pathtriple_moving_average_crossover_strategy.go
File metadata and controls
173 lines (137 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2021-2026 The Indicator Authors.
// The source code is provided under GNU AGPLv3 License.
// https://github.com/cinar/indicator
package trend
import (
"context"
"github.com/cinar/indicator/v2/asset"
"github.com/cinar/indicator/v2/helper"
"github.com/cinar/indicator/v2/strategy"
"github.com/cinar/indicator/v2/trend"
)
const (
// DefaultTripleMovingAverageCrossoverStrategyFastPeriod is the default triple moving average crossover strategy fast period.
DefaultTripleMovingAverageCrossoverStrategyFastPeriod = 21
// DefaultTripleMovingAverageCrossoverStrategyMediumPeriod is the default triple moving average crossover strategy medium period.
DefaultTripleMovingAverageCrossoverStrategyMediumPeriod = 50
// DefaultTripleMovingAverageCrossoverStrategySlowPeriod is the default triple moving average crossover strategy slow period.
DefaultTripleMovingAverageCrossoverStrategySlowPeriod = 200
)
// TripleMovingAverageCrossoverStrategy demonstrates how to compose three Exponential
// Moving Averages (EMAs) with different periods into an illustrative triple crossover strategy.
type TripleMovingAverageCrossoverStrategy struct {
// FastEma is the fastest EMA.
FastEma *trend.Ema[float64]
// MediumEma is the meium EMA.
MediumEma *trend.Ema[float64]
// SlowEma is the slowest EMA.
SlowEma *trend.Ema[float64]
}
// NewTripleMovingAverageCrossoverStrategy initializes an example TripleMovingAverageCrossoverStrategy instance with default parameters.
func NewTripleMovingAverageCrossoverStrategy() *TripleMovingAverageCrossoverStrategy {
return NewTripleMovingAverageCrossoverStrategyWith(
DefaultTripleMovingAverageCrossoverStrategyFastPeriod,
DefaultTripleMovingAverageCrossoverStrategyMediumPeriod,
DefaultTripleMovingAverageCrossoverStrategySlowPeriod,
)
}
// NewTripleMovingAverageCrossoverStrategyWith initializes an example TripleMovingAverageCrossoverStrategyWith instance with default parameters.
func NewTripleMovingAverageCrossoverStrategyWith(fastPeriod, mediumPeriod, slowPeriod int) *TripleMovingAverageCrossoverStrategy {
return &TripleMovingAverageCrossoverStrategy{
FastEma: trend.NewEmaWithPeriod[float64](fastPeriod),
MediumEma: trend.NewEmaWithPeriod[float64](mediumPeriod),
SlowEma: trend.NewEmaWithPeriod[float64](slowPeriod),
}
}
// Name returns the name of the example strategy.
func (*TripleMovingAverageCrossoverStrategy) Name() string {
return "Triple Moving Average Crossover Strategy"
}
// ComputeWithContext processes the provided asset snapshots and generates an illustrative stream of actions.
func (t *TripleMovingAverageCrossoverStrategy) ComputeWithContext(ctx context.Context, c <-chan *asset.Snapshot) <-chan strategy.Action {
fastEmas, mediumEmas, slowEmas := t.calculateEmas(c)
actions := helper.Operate3WithContext(ctx, fastEmas, mediumEmas, slowEmas, func(fastEma, mediumEma, slowEma float64) strategy.Action {
// A buy signal is generated when the **fastest** EMA crosses above both the **medium** and **slowest** EMAs.
if (fastEma > mediumEma) && (fastEma > slowEma) {
return strategy.Buy
}
// A sell signal is generated when the fastest EMA crosses below both the medium and slowest EMAs.
if (fastEma < mediumEma) && (fastEma < slowEma) {
return strategy.Sell
}
// Otherwise, the strategy recommends holding the asset.
return strategy.Hold
})
// Generate a Hold signal during the idle period.
actions = helper.ShiftWithContext(ctx, actions, t.SlowEma.IdlePeriod(), strategy.Hold)
return actions
}
// Report processes the provided asset snapshots and generates an
// illustrative report annotated with example actions.
func (t *TripleMovingAverageCrossoverStrategy) Report(c <-chan *asset.Snapshot) *helper.Report {
//
// snapshots[0] -> dates
// snapshots[1] -> closings
// snapshots[2] -> fastEmas
// mediumEmas
// slowEmas
// snapshots[3] -> actions -> annotations
// -> outcomes
//
snapshots := helper.Duplicate(c, 4)
dates := helper.Skip(
asset.SnapshotsAsDates(snapshots[0]),
t.SlowEma.IdlePeriod(),
)
closingsSplice := helper.Duplicate(
helper.Skip(
asset.SnapshotsAsClosings(snapshots[1]),
t.SlowEma.IdlePeriod(),
),
2,
)
fastEmas, mediumEmas, slowEmas := t.calculateEmas(snapshots[2])
actions, outcomes := strategy.ComputeWithOutcome(t, snapshots[3])
annotations := helper.Skip(
strategy.ActionsToAnnotations(actions),
t.SlowEma.IdlePeriod(),
)
outcomes = helper.MultiplyBy(
helper.Skip(
outcomes,
t.SlowEma.IdlePeriod(),
),
100,
)
report := helper.NewReport(t.Name(), dates)
report.AddChart()
report.AddChart()
report.AddColumn(helper.NewNumericReportColumn("Close", closingsSplice[0]))
report.AddColumn(helper.NewNumericReportColumn("Close", closingsSplice[1]), 1)
report.AddColumn(helper.NewNumericReportColumn("Fast", fastEmas), 1)
report.AddColumn(helper.NewNumericReportColumn("Medium", mediumEmas), 1)
report.AddColumn(helper.NewNumericReportColumn("Slow", slowEmas), 1)
report.AddColumn(helper.NewAnnotationReportColumn(annotations), 0, 1)
report.AddColumn(helper.NewNumericReportColumn("Outcome", outcomes), 2)
return report
}
// calculateEmas calculates the fast, medium, and slow EMAs.
func (t *TripleMovingAverageCrossoverStrategy) calculateEmas(c <-chan *asset.Snapshot) (<-chan float64, <-chan float64, <-chan float64) {
closings := helper.Duplicate(asset.SnapshotsAsClosings(c), 3)
fastEmas := helper.Skip(
t.FastEma.Compute(closings[0]),
t.SlowEma.IdlePeriod()-t.FastEma.IdlePeriod(),
)
mediumEmas := helper.Skip(
t.MediumEma.Compute(closings[1]),
t.SlowEma.IdlePeriod()-t.MediumEma.IdlePeriod(),
)
slowEmas := t.SlowEma.Compute(closings[2])
return fastEmas, mediumEmas, slowEmas
}
// Compute wraps ComputeWithContext for backwards compatibility.
//
// Deprecated: Use ComputeWithContext instead.
func (t *TripleMovingAverageCrossoverStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action {
return t.ComputeWithContext(context.Background(), c)
}