forked from cinar/indicator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compound_strategies.go
74 lines (62 loc) · 2.04 KB
/
compound_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
// Copyright (c) 2021 Onur Cinar. All Rights Reserved.
// The source code is provided under MIT License.
//
// https://github.com/cinar/indicator
package indicator
// The AllStrategies function takes one or more StrategyFunction and
// provides a StrategyFunction that will return a BUY or SELL action
// if all strategies are returning the same action, otherwise it
// will return a HOLD action.
func AllStrategies(strategies ...StrategyFunction) StrategyFunction {
return func(asset *Asset) []Action {
actions := RunStrategies(asset, strategies...)
for i := 1; i < len(actions); i++ {
for j := 0; j < len(actions[0]); j++ {
if actions[0][j] != actions[i][j] {
actions[0][j] = HOLD
}
}
}
return actions[0]
}
}
// The SeparateStrategies function takes a buy strategy and a sell strategy.
//
// It returns a BUY action if the buy strategy returns a BUY action and
// the the sell strategy returns a HOLD action.
//
// It returns a SELL action if the sell strategy returns a SELL action
// and the buy strategy returns a HOLD action.
//
// It returns HOLD otherwise.
func SeparateStategies(buyStrategy, sellStrategy StrategyFunction) StrategyFunction {
return func(asset *Asset) []Action {
actions := make([]Action, len(asset.Date))
buyActions := buyStrategy(asset)
sellActions := sellStrategy(asset)
for i := 0; i < len(actions); i++ {
if buyActions[i] == BUY && sellActions[i] == HOLD {
actions[i] = BUY
} else if sellActions[i] == SELL && buyActions[i] == HOLD {
actions[i] = SELL
} else {
actions[i] = HOLD
}
}
return actions
}
}
// The RunStrategies takes one or more StrategyFunction and returns
// the acitions for each.
func RunStrategies(asset *Asset, strategies ...StrategyFunction) [][]Action {
actions := make([][]Action, len(strategies))
for i := 0; i < len(strategies); i++ {
actions[i] = strategies[i](asset)
}
return actions
}
// MACD and RSI strategy.
func MacdAndRsiStrategy(asset *Asset) []Action {
strategy := AllStrategies(MacdStrategy, DefaultRsiStrategy)
return strategy(asset)
}