-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
156 lines (136 loc) · 3.32 KB
/
controller.go
File metadata and controls
156 lines (136 loc) · 3.32 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
package main
import (
"github.com/toorop/go-bittrex"
"log"
"strings"
"time"
)
type Controller struct {
Config *ControllerConfig
Markets map[string]*Market
Client *bittrex.Bittrex
stop chan interface{}
signals chan Signal
}
func NewController(cfg ControllerConfig) *Controller {
return &Controller{
Config: &cfg,
Client: bittrex.New(cfg.BittrexApiKey, cfg.BittrexApiSecret),
Markets: make(map[string]*Market),
signals: make(chan Signal),
}
}
func (ctrl *Controller) NewMarket(market bittrex.Market, cfg MarketConfig) *Market {
return NewMarket(market, cfg, ctrl.Client)
}
func (ctrl *Controller) InitMarkets(mcfg MarketConfig, vpcicfg VPCIConfig) error {
markets, err := ctrl.Client.GetMarkets()
if err != nil {
return err
}
for _, market := range markets {
name := market.MarketName
if !market.IsActive {
// only monitor active markets
continue
}
if ctrl.Config.Pairs != nil && len(ctrl.Config.Pairs) > 0 {
// only track this market if it is in Config.Pairs filter
trackMarket := false
for _, pair := range ctrl.Config.Pairs {
if pair == market.MarketName {
trackMarket = true
break
}
}
if !trackMarket {
continue
}
} else {
if strings.Index(name, "BTC") != 0 {
// only monitor btc markets
continue
}
summary, err := ctrl.Client.GetMarketSummary(name)
if err != nil {
log.Printf("error retreiving market history for %s: %s", name, err)
continue
}
bv, _ := summary[0].BaseVolume.Float64()
if bv < ctrl.Config.MinBtcVolumeDaily {
// filter out low volume markets
log.Printf("filtering out low volume market %s (base vol: %5f)", name, bv)
continue
}
}
m := ctrl.NewMarket(market, mcfg)
err = m.PrefillCandles()
if err != nil {
log.Printf("error filling candles for %s: %s", market.MarketName, err)
continue
}
// add desired indicators
ind := NewVPCI(market.MarketName, vpcicfg, []chan<- Signal{chan<- Signal(ctrl.signals)})
m.AddIndicator(ind)
ctrl.Markets[name] = m
log.Println("tracking market", name)
}
return nil
}
func (ctrl *Controller) Stop() {
if ctrl.stop == nil {
return
}
ctrl.stop <- nil
ctrl.stop = nil
}
func (ctrl *Controller) run() {
ctrl.stop = make(chan interface{})
for {
select {
case sig := <-ctrl.signals:
log.Println(sig)
case <-ctrl.stop:
for _, market := range ctrl.Markets {
market.Stop()
}
return
}
}
}
func (ctrl *Controller) Start(vpci VPCIConfig) error {
log.Printf("%d tracked markets", len(ctrl.Markets))
go ctrl.run()
for _, market := range ctrl.Markets {
go market.StartPolling()
}
return nil
}
func (ctrl *Controller) Analyze(marketName string, cfg MarketConfig, from, to time.Time) error {
log.Printf("starting market analysis for \"%s\"", marketName)
var bFrom, bTo bool
if !from.Equal(time.Time{}) {
bFrom = true
log.Printf("from: %s", from)
}
if !to.Equal(time.Time{}) {
bTo = true
log.Printf(" to: %s", to)
}
m := ctrl.NewMarket(bittrex.Market{MarketName: marketName}, cfg)
candles, err := ctrl.Client.GetTicks(marketName, string(cfg.Interval))
if err != nil {
return err
}
for _, c := range candles {
// candles should come in historical order..
if bFrom && c.TimeStamp.Time.Before(from) {
continue
}
if bTo && c.TimeStamp.Time.After(to) {
break
}
_ = m.AddTick(c, false)
}
return nil
}