/
bot.go
223 lines (203 loc) · 5.48 KB
/
bot.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2021. Quirino Gervacio
// MIT License. All Rights Reserved
package cbg
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"sync"
"time"
)
type BotSvc struct {
SpecSvc *SpecSvc
TaapiSvc *TaapiSvc
EmailSvc *EmailSvc
BiapiSvc *BiapiSvc
StratSvc *StratSvc
SpotSvc *SpotSvc
EndSvc *EndSvc
}
func NewBotSvc(ss *SpecSvc) *BotSvc {
taapiSvc := NewTaapiSvc(ss.Spec.TaapiSpec, ss.Spec.CredSpec.TaapiSk)
emailSvc := NewEmailSvc(ss.Spec.EmailSpec, ss.Spec.CredSpec.EmUser, ss.Spec.CredSpec.EmPass)
biapiSvc := NewBiapiSvc(ss.Spec.BiapiSpec, ss.Spec.CredSpec.BiapiAk, ss.Spec.CredSpec.BiapiSk)
stratSvc := NewStratSvc(ss.Spec.StratSpec, taapiSvc)
spotSvc := NewSpotSvc(ss.Spec.SpotSpec, biapiSvc)
endSvc := NewEndSvc(ss.Spec.EndSpec, biapiSvc)
return &BotSvc{
SpecSvc: ss,
TaapiSvc: taapiSvc,
EmailSvc: emailSvc,
BiapiSvc: biapiSvc,
StratSvc: stratSvc,
SpotSvc: spotSvc,
EndSvc: endSvc,
}
}
func (s *BotSvc) Run() {
if s.SpecSvc.Spec.Delayed {
t := SleepUntil(s.SpecSvc.Spec.SpotSpec.PeriodMin, time.Minute)
log.Infof("Sleeping until %s", t)
time.Sleep(time.Until(t))
}
log.Infof("Bot %s started", s.SpecSvc.Spec.Name)
log.Infof("Trading on %s%s at %dm",
s.SpecSvc.Spec.SpotSpec.Coin,
s.SpecSvc.Spec.SpotSpec.Market,
s.SpecSvc.Spec.SpotSpec.PeriodMin)
log.Infof("Backtrack at %d", s.SpecSvc.Spec.TaapiSpec.Backtrack)
ns := make(chan []string)
s.notifier(ns)
s.ender(ns)
ts := make(chan Signal)
s.trader(ns, ts)
s.looker(ns, ts)
}
func (s *BotSvc) trader(ns chan<- []string, ts <-chan Signal) {
go func() {
sig := <-ts
for {
symbol := fmt.Sprintf("%s%s", s.SpotSvc.SpotSpec.Coin, s.SpotSvc.SpotSpec.Market)
if sig == SignalBuy {
res, err := s.SpotSvc.BuyMarketMT("") // all in
if err != nil {
ns <- []string{fmt.Sprintf("Failed to buy %s", symbol), err.Error()}
} else {
ns <- []string{fmt.Sprintf("Bought %s (%s)", symbol, res.Status), ""}
}
} else {
res, err := s.SpotSvc.SellCoinMT("") // all in
if err != nil {
ns <- []string{fmt.Sprintf("Failed to sell %s", symbol), err.Error()}
} else {
ns <- []string{fmt.Sprintf("Sold %s (%s)", symbol, res.Status), ""}
}
}
}
}()
}
func (s *BotSvc) looker(ns chan<- []string, ts chan<- Signal) {
go func() {
for {
var wg sync.WaitGroup
interval := fmt.Sprintf("%dm",
s.SpecSvc.Spec.SpotSpec.PeriodMin) // TODO always minutes?
t := SleepUntil(s.SpecSvc.Spec.SpotSpec.PeriodMin, time.Minute)
// Determine if there is a trend
wg.Add(1)
adxSig := SignalNone
go func(wg *sync.WaitGroup) {
defer wg.Done()
adxSig = s.StratSvc.Adx(
s.SpecSvc.Spec.SpotSpec.Coin,
s.SpecSvc.Spec.SpotSpec.Market,
interval,
s.SpecSvc.Spec.StratSpec.Adx)
}(&wg)
// Determine the direction of the trend
wg.Add(1)
macdSig := SignalNone
go func(wg *sync.WaitGroup) {
defer wg.Done()
macdSig = s.StratSvc.Macd(
s.SpecSvc.Spec.SpotSpec.Coin,
s.SpecSvc.Spec.SpotSpec.Market,
interval,
s.SpecSvc.Spec.StratSpec.Macd)
}(&wg)
// Determine entry/exit point
wg.Add(1)
superTrendSig := SignalNone
go func(wg *sync.WaitGroup) {
defer wg.Done()
superTrendSig = s.StratSvc.SuperTrend(
s.SpecSvc.Spec.SpotSpec.Coin,
s.SpecSvc.Spec.SpotSpec.Market,
interval,
s.SpecSvc.Spec.StratSpec.SuperTrend)
}(&wg)
wg.Wait()
log.Infof("ADX Signal: %s", adxSig)
log.Infof("MACD Signal: %s", macdSig)
log.Infof("SuperTrend Signal: %s", superTrendSig)
position := SignalError
if adxSig == SignalError ||
macdSig == SignalError ||
superTrendSig == SignalError {
log.Error("Some signal failed")
time.Sleep(time.Until(t))
continue
}
if adxSig == SignalNone ||
macdSig == SignalNone ||
superTrendSig == SignalNone {
log.Warn("Incomplete strategy")
time.Sleep(time.Until(t))
continue
}
if adxSig == SignalHasTrend {
if macdSig == SignalUptrend {
if superTrendSig == SignalLong {
position = SignalBuy
} else if superTrendSig == SignalShort {
position = SignalSell
} else {
position = SignalHodl
}
} else {
position = SignalHodl
}
} else {
position = SignalHodl
}
position = SignalBuy
if position == SignalBuy || position == SignalSell {
if s.SpecSvc.Spec.DryRun {
ns <- []string{string(position), ""}
} else {
ts <- position
}
} else {
log.Infof("No action for now...")
}
log.Infof("Next position inquiry is %sm", t)
time.Sleep(time.Until(t))
}
}()
}
func (s *BotSvc) ender(ns chan<- []string) {
go func() {
for {
log.Debugf("Looking for dummy order...")
shouldCancel, err := s.EndSvc.EndByDummyOrder()
if err != nil {
ns <- []string{"Ender Triggered with Error", err.Error()}
}
if shouldCancel {
ns <- []string{"Ender Triggered", "None"}
log.Infof("Ender triggered. Sleeping for 5s before terminating bot")
time.Sleep(5 * time.Second) // TODO externalize?
os.Exit(0)
}
is := int(s.SpecSvc.Spec.EndSpec.DummyOrder[2])
log.Debugf("Ender sleeping for %ds", is)
time.Sleep(time.Duration(is) * time.Second)
}
}()
}
func (s *BotSvc) notifier(ns <-chan []string) {
go func() {
for {
msg := <-ns
log.Debugf("Notifying...")
if err := s.EmailSvc.Send(
msg[0], msg[1],
[]string{s.SpecSvc.Spec.CredSpec.NotEm},
[]string{}, []string{}); err != nil {
log.Errorf("failed to send email notification [%v]", err)
continue
}
log.Debugf("Done notifying...")
}
}()
}