forked from cinar/indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrend_strategies.go
157 lines (127 loc) · 3.48 KB
/
trend_strategies.go
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
// Copyright (c) 2021 Onur Cinar. All Rights Reserved.
// The source code is provided under MIT License.
//
// https://github.com/cinar/indicator
package indicator
// Chande forecast oscillator strategy.
func ChandeForecastOscillatorStrategy(asset Asset) []Action {
actions := make([]Action, len(asset.Date))
cfo := ChandeForecastOscillator(asset.Closing)
for i := 0; i < len(actions); i++ {
if cfo[i] < 0 {
actions[i] = BUY
} else if cfo[i] > 0 {
actions[i] = SELL
} else {
actions[i] = HOLD
}
}
return actions
}
// Moving chande forecast oscillator strategy function.
func MovingChandeForecastOscillatorStrategy(period int, asset Asset) []Action {
actions := make([]Action, len(asset.Date))
cfo := MovingChandeForecastOscillator(period, asset.Closing)
for i := 0; i < len(actions); i++ {
if cfo[i] < 0 {
actions[i] = BUY
} else if cfo[i] > 0 {
actions[i] = SELL
} else {
actions[i] = HOLD
}
}
return actions
}
// Make moving chande forecast oscillator strategy.
func MakeMovingChandeForecastOscillatorStrategy(period int) StrategyFunction {
return func(asset Asset) []Action {
return MovingChandeForecastOscillatorStrategy(period, asset)
}
}
// The KdjStrategy function uses the k, d, j values that are generated by
// the Kdj indicator function to provide a BUY action when k crosses
// above d and j. It is stronger when below 20%. Also the SELL
// action is when k crosses below d and j. It is strong when
// above 80%.
//
// Returns actions.
func KdjStrategy(rPeriod, kPeriod, dPeriod int, asset Asset) []Action {
actions := make([]Action, len(asset.Date))
k, d, j := Kdj(rPeriod, kPeriod, dPeriod, asset.High, asset.Low, asset.Closing)
for i := 0; i < len(actions); i++ {
if (k[i] > d[i]) && (k[i] > j[i]) && (k[i] <= 20) {
actions[i] = BUY
} else if (k[i] < d[i]) && (k[i] < j[i]) && (k[i] >= 80) {
actions[i] = SELL
} else {
actions[i] = HOLD
}
}
return actions
}
// Make KDJ strategy function.
func MakeKdjStrategy(rPeriod, kPeriod, dPeriod int) StrategyFunction {
return func(asset Asset) []Action {
return KdjStrategy(rPeriod, kPeriod, dPeriod, asset)
}
}
// Default KDJ strategy function.
func DefaultKdjStrategy(asset Asset) []Action {
return KdjStrategy(9, 3, 3, asset)
}
// MACD strategy.
func MacdStrategy(asset Asset) []Action {
actions := make([]Action, len(asset.Date))
macd, signal := Macd(asset.Closing)
for i := 0; i < len(actions); i++ {
if macd[i] > signal[i] {
actions[i] = BUY
} else if macd[i] < signal[i] {
actions[i] = SELL
} else {
actions[i] = HOLD
}
}
return actions
}
// Trend strategy. Buy when trending up for count times,
// sell when trending down for count times.
func TrendStrategy(asset Asset, count uint) []Action {
actions := make([]Action, len(asset.Date))
if len(actions) == 0 {
return actions
}
lastClosing := asset.Closing[0]
trendCount := uint(1)
trendUp := false
actions[0] = HOLD
for i := 1; i < len(actions); i++ {
closing := asset.Closing[i]
if trendUp && (lastClosing <= closing) {
trendCount++
} else if !trendUp && (lastClosing >= closing) {
trendCount++
} else {
trendUp = !trendUp
trendCount = 1
}
lastClosing = closing
if trendCount >= count {
if trendUp {
actions[i] = BUY
} else {
actions[i] = SELL
}
} else {
actions[i] = HOLD
}
}
return actions
}
// Make trend strategy function.
func MakeTrendStrategy(count uint) StrategyFunction {
return func(asset Asset) []Action {
return TrendStrategy(asset, count)
}
}