@@ -17,146 +17,11 @@ import (
17
17
18
18
exchange2 "github.com/c9s/bbgo/pkg/exchange"
19
19
"github.com/c9s/bbgo/pkg/fixedpoint"
20
- "github.com/c9s/bbgo/pkg/indicator"
21
20
"github.com/c9s/bbgo/pkg/service"
22
21
"github.com/c9s/bbgo/pkg/types"
23
22
"github.com/c9s/bbgo/pkg/util"
24
23
)
25
24
26
- var (
27
- debugEWMA = false
28
- debugSMA = false
29
- )
30
-
31
- func init () {
32
- // when using --dotenv option, the dotenv is loaded from command.PersistentPreRunE, not init.
33
- // hence here the env var won't enable the debug flag
34
- util .SetEnvVarBool ("DEBUG_EWMA" , & debugEWMA )
35
- util .SetEnvVarBool ("DEBUG_SMA" , & debugSMA )
36
- }
37
-
38
- type StandardIndicatorSet struct {
39
- Symbol string
40
- // Standard indicators
41
- // interval -> window
42
- sma map [types.IntervalWindow ]* indicator.SMA
43
- ewma map [types.IntervalWindow ]* indicator.EWMA
44
- boll map [types.IntervalWindowBandWidth ]* indicator.BOLL
45
- stoch map [types.IntervalWindow ]* indicator.STOCH
46
- volatility map [types.IntervalWindow ]* indicator.Volatility
47
-
48
- store * MarketDataStore
49
- }
50
-
51
- func NewStandardIndicatorSet (symbol string , store * MarketDataStore ) * StandardIndicatorSet {
52
- set := & StandardIndicatorSet {
53
- Symbol : symbol ,
54
- sma : make (map [types.IntervalWindow ]* indicator.SMA ),
55
- ewma : make (map [types.IntervalWindow ]* indicator.EWMA ),
56
- boll : make (map [types.IntervalWindowBandWidth ]* indicator.BOLL ),
57
- stoch : make (map [types.IntervalWindow ]* indicator.STOCH ),
58
- volatility : make (map [types.IntervalWindow ]* indicator.Volatility ),
59
- store : store ,
60
- }
61
-
62
- // let us pre-defined commonly used intervals
63
- for interval := range types .SupportedIntervals {
64
- for _ , window := range []int {7 , 25 , 99 } {
65
- iw := types.IntervalWindow {Interval : interval , Window : window }
66
- set .sma [iw ] = & indicator.SMA {IntervalWindow : iw }
67
- set .sma [iw ].Bind (store )
68
- if debugSMA {
69
- set .sma [iw ].OnUpdate (func (value float64 ) {
70
- log .Infof ("%s SMA %s: %f" , symbol , iw .String (), value )
71
- })
72
- }
73
-
74
- set .ewma [iw ] = & indicator.EWMA {IntervalWindow : iw }
75
- set .ewma [iw ].Bind (store )
76
-
77
- // if debug EWMA is enabled, we add the debug handler
78
- if debugEWMA {
79
- set .ewma [iw ].OnUpdate (func (value float64 ) {
80
- log .Infof ("%s EWMA %s: %f" , symbol , iw .String (), value )
81
- })
82
- }
83
-
84
- }
85
-
86
- // setup boll indicator, we may refactor boll indicator by subscribing SMA indicator,
87
- // however, since general used BOLLINGER band use window 21, which is not in the existing SMA indicator sets.
88
- // Pull out the bandwidth configuration as the boll Key
89
- iw := types.IntervalWindow {Interval : interval , Window : 21 }
90
-
91
- // set efault band width to 2.0
92
- iwb := types.IntervalWindowBandWidth {IntervalWindow : iw , BandWidth : 2.0 }
93
- set .boll [iwb ] = & indicator.BOLL {IntervalWindow : iw , K : iwb .BandWidth }
94
- set .boll [iwb ].Bind (store )
95
- }
96
-
97
- return set
98
- }
99
-
100
- // BOLL returns the bollinger band indicator of the given interval, the window and bandwidth
101
- func (set * StandardIndicatorSet ) BOLL (iw types.IntervalWindow , bandWidth float64 ) * indicator.BOLL {
102
- iwb := types.IntervalWindowBandWidth {IntervalWindow : iw , BandWidth : bandWidth }
103
- inc , ok := set .boll [iwb ]
104
- if ! ok {
105
- inc = & indicator.BOLL {IntervalWindow : iw , K : bandWidth }
106
- inc .Bind (set .store )
107
- set .boll [iwb ] = inc
108
- }
109
-
110
- return inc
111
- }
112
-
113
- // SMA returns the simple moving average indicator of the given interval and the window size.
114
- func (set * StandardIndicatorSet ) SMA (iw types.IntervalWindow ) * indicator.SMA {
115
- inc , ok := set .sma [iw ]
116
- if ! ok {
117
- inc = & indicator.SMA {IntervalWindow : iw }
118
- inc .Bind (set .store )
119
- set .sma [iw ] = inc
120
- }
121
-
122
- return inc
123
- }
124
-
125
- // EWMA returns the exponential weighed moving average indicator of the given interval and the window size.
126
- func (set * StandardIndicatorSet ) EWMA (iw types.IntervalWindow ) * indicator.EWMA {
127
- inc , ok := set .ewma [iw ]
128
- if ! ok {
129
- inc = & indicator.EWMA {IntervalWindow : iw }
130
- inc .Bind (set .store )
131
- set .ewma [iw ] = inc
132
- }
133
-
134
- return inc
135
- }
136
-
137
- func (set * StandardIndicatorSet ) STOCH (iw types.IntervalWindow ) * indicator.STOCH {
138
- inc , ok := set .stoch [iw ]
139
- if ! ok {
140
- inc = & indicator.STOCH {IntervalWindow : iw }
141
- inc .Bind (set .store )
142
- set .stoch [iw ] = inc
143
- }
144
-
145
- return inc
146
- }
147
-
148
- // VOLATILITY returns the volatility(stddev) indicator of the given interval and the window size.
149
- func (set * StandardIndicatorSet ) VOLATILITY (iw types.IntervalWindow ) * indicator.Volatility {
150
- inc , ok := set .volatility [iw ]
151
- if ! ok {
152
- inc = & indicator.Volatility {IntervalWindow : iw }
153
- inc .Bind (set .store )
154
- set .volatility [iw ] = inc
155
- }
156
-
157
- return inc
158
- }
159
-
160
25
// ExchangeSession presents the exchange connection Session
161
26
// It also maintains and collects the data returned from the stream.
162
27
type ExchangeSession struct {
@@ -504,7 +369,7 @@ func (session *ExchangeSession) initSymbol(ctx context.Context, environ *Environ
504
369
marketDataStore .BindStream (session .MarketDataStream )
505
370
session .marketDataStores [symbol ] = marketDataStore
506
371
507
- standardIndicatorSet := NewStandardIndicatorSet (symbol , marketDataStore )
372
+ standardIndicatorSet := NewStandardIndicatorSet (symbol , session . MarketDataStream , marketDataStore )
508
373
session .standardIndicatorSets [symbol ] = standardIndicatorSet
509
374
510
375
// used kline intervals by the given symbol
0 commit comments