forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
347 lines (322 loc) · 9.78 KB
/
main.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"github.com/thrasher-corp/gocryptotrader/backtester/common"
"github.com/thrasher-corp/gocryptotrader/backtester/config"
backtest "github.com/thrasher-corp/gocryptotrader/backtester/engine"
"github.com/thrasher-corp/gocryptotrader/backtester/plugins/strategies"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/engine"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/gocryptotrader/signaler"
)
var singleTaskStrategyPath, templatePath, outputPath, btConfigDir, strategyPluginPath, pprofURL string
var printLogo, generateReport, darkReport, colourOutput, logSubHeader, enablePProf bool
func main() {
wd, err := os.Getwd()
if err != nil {
fmt.Printf("Could not get working directory. Error: %v\n", err)
os.Exit(1)
}
flags := parseFlags(wd)
var btCfg *config.BacktesterConfig
if btConfigDir == "" {
btConfigDir = config.DefaultBTConfigDir
log.Infof(log.Global, "Blank config received, using default path '%v'", btConfigDir)
}
if enablePProf {
go func() {
server := &http.Server{
Addr: pprofURL,
ReadHeaderTimeout: time.Minute,
ReadTimeout: time.Minute,
}
fmt.Println(server.ListenAndServe())
}()
}
fe := file.Exists(btConfigDir)
switch {
case fe:
btCfg, err = config.ReadBacktesterConfigFromPath(btConfigDir)
if err != nil {
fmt.Printf("Could not read config. Error: %v\n", err)
os.Exit(1)
}
case !fe && btConfigDir == config.DefaultBTConfigDir:
btCfg, err = config.GenerateDefaultConfig()
if err != nil {
fmt.Printf("Could not generate config. Error: %v\n", err)
os.Exit(1)
}
var btCfgJSON []byte
btCfgJSON, err = json.MarshalIndent(btCfg, "", " ")
if err != nil {
fmt.Printf("Could not generate config. Error: %v\n", err)
os.Exit(1)
}
err = os.MkdirAll(config.DefaultBTDir, file.DefaultPermissionOctal)
if err != nil {
fmt.Printf("Could not generate config. Error: %v\n", err)
os.Exit(1)
}
err = os.WriteFile(btConfigDir, btCfgJSON, file.DefaultPermissionOctal)
if err != nil {
fmt.Printf("Could not generate config. Error: %v\n", err)
os.Exit(1)
}
default:
log.Errorf(log.Global, "Non-standard config '%v' does not exist. Exiting...", btConfigDir)
return
}
flagSet := engine.FlagSet(flags)
flagSet.WithBool("printlogo", &printLogo, btCfg.PrintLogo)
flagSet.WithBool("darkreport", &darkReport, btCfg.Report.DarkMode)
flagSet.WithBool("generatereport", &generateReport, btCfg.Report.GenerateReport)
flagSet.WithBool("logsubheaders", &logSubHeader, btCfg.LogSubheaders)
flagSet.WithBool("colouroutput", &colourOutput, btCfg.UseCMDColours)
if singleTaskStrategyPath != "" && !file.Exists(singleTaskStrategyPath) {
fmt.Printf("Strategy config path not found '%v'", singleTaskStrategyPath)
os.Exit(1)
}
defaultTemplate := filepath.Join(
wd,
"report",
"tpl.gohtml")
defaultReportOutput := filepath.Join(
wd,
"results")
if templatePath != defaultTemplate {
btCfg.Report.TemplatePath = templatePath
}
if !file.Exists(btCfg.Report.TemplatePath) {
fmt.Printf("Report template path not found '%v'", btCfg.Report.TemplatePath)
os.Exit(1)
}
if outputPath != defaultReportOutput {
btCfg.Report.OutputPath = outputPath
}
if !file.Exists(btCfg.Report.OutputPath) {
fmt.Printf("Report output path not found '%v'", btCfg.Report.OutputPath)
os.Exit(1)
}
if colourOutput {
common.SetColours(&btCfg.Colours)
} else {
common.PurgeColours()
}
defaultLogSettings := log.GenDefaultSettings()
defaultLogSettings.AdvancedSettings.ShowLogSystemName = convert.BoolPtr(logSubHeader)
defaultLogSettings.AdvancedSettings.Headers.Info = common.CMDColours.Info + "[INFO]" + common.CMDColours.Default
defaultLogSettings.AdvancedSettings.Headers.Warn = common.CMDColours.Warn + "[WARN]" + common.CMDColours.Default
defaultLogSettings.AdvancedSettings.Headers.Debug = common.CMDColours.Debug + "[DEBUG]" + common.CMDColours.Default
defaultLogSettings.AdvancedSettings.Headers.Error = common.CMDColours.Error + "[ERROR]" + common.CMDColours.Default
err = log.SetGlobalLogConfig(defaultLogSettings)
if err != nil {
fmt.Printf("Could not setup global logger. Error: %v\n", err)
os.Exit(1)
}
err = log.SetupGlobalLogger()
if err != nil {
fmt.Printf("Could not setup global logger. Error: %v\n", err)
os.Exit(1)
}
err = common.RegisterBacktesterSubLoggers()
if err != nil {
fmt.Printf("Could not register subloggers. Error: %v\n", err)
os.Exit(1)
}
if printLogo {
fmt.Println(common.Logo())
}
if strategyPluginPath == "" && btCfg.PluginPath != "" {
strategyPluginPath = btCfg.PluginPath
}
if strategyPluginPath != "" {
err = strategies.LoadCustomStrategies(strategyPluginPath)
if err != nil {
fmt.Printf("Could not load custom strategies. Error: %v\n", err)
os.Exit(1)
}
log.Infof(common.Backtester, "Loaded plugin %v\n", strategyPluginPath)
}
if singleTaskStrategyPath != "" {
dir := singleTaskStrategyPath
var cfg *config.Config
cfg, err = config.ReadStrategyConfigFromFile(dir)
if err != nil {
fmt.Printf("Could not read strategy config. Error: %v\n", err)
os.Exit(1)
}
var bt *backtest.BackTest
bt, err = backtest.NewBacktesterFromConfigs(cfg, &config.BacktesterConfig{
Report: config.Report{
GenerateReport: generateReport,
TemplatePath: btCfg.Report.TemplatePath,
OutputPath: btCfg.Report.OutputPath,
DarkMode: darkReport,
},
})
if err != nil {
fmt.Printf("Could not execute strategy. Error: %v\n", err)
os.Exit(1)
}
if bt.MetaData.LiveTesting {
err = bt.ExecuteStrategy(false)
if err != nil {
fmt.Printf("Could not stop task %v %v. Error: %v\n", bt.MetaData.ID, bt.MetaData.Strategy, err)
os.Exit(1)
}
interrupt := signaler.WaitForInterrupt()
log.Infof(log.Global, "Captured %v, shutdown requested\n", interrupt)
log.Infoln(log.Global, "Exiting.")
err = bt.Stop()
if err != nil {
fmt.Printf("Could not stop task %v %v. Error: %v\n", bt.MetaData.ID, bt.MetaData.Strategy, err)
os.Exit(1)
}
} else {
err = bt.ExecuteStrategy(true)
if err != nil {
fmt.Printf("Could not stop task %v %v. Error: %v\n", bt.MetaData.ID, bt.MetaData.Strategy, err)
os.Exit(1)
}
}
return
}
// grpc server mode
btCfg.Report.DarkMode = darkReport
btCfg.Report.GenerateReport = generateReport
runManager := backtest.NewTaskManager()
go func(c *config.BacktesterConfig) {
log.Info(log.GRPCSys, "Starting RPC server")
var s *backtest.GRPCServer
s, err = backtest.SetupRPCServer(c, runManager)
err = backtest.StartRPCServer(s)
if err != nil {
fmt.Printf("Could not start RPC server. Error: %v\n", err)
os.Exit(1)
}
log.Info(log.GRPCSys, "Ready to receive commands")
}(btCfg)
interrupt := signaler.WaitForInterrupt()
log.Infof(log.Global, "Captured %v, shutdown requested\n", interrupt)
if btCfg.StopAllTasksOnClose {
log.Infoln(log.Global, "Stopping all running tasks on close")
var stopped []*backtest.TaskSummary
stopped, err = runManager.StopAllTasks()
if err != nil {
log.Error(common.Backtester, err)
}
for i := range stopped {
log.Infof(common.Backtester, "Task %v %v was stopped", stopped[i].MetaData.ID, stopped[i].MetaData.Strategy)
}
} else {
var tasks []*backtest.TaskSummary
tasks, err = runManager.List()
if err != nil {
log.Error(common.Backtester, err)
}
for i := range tasks {
if tasks[i].MetaData.ClosePositionsOnStop && !tasks[i].MetaData.Closed {
err = runManager.StopTask(tasks[i].MetaData.ID)
if err != nil {
log.Error(common.Backtester, err)
continue
}
log.Infof(common.Backtester, "Task %v %v was stopped", tasks[i].MetaData.ID, tasks[i].MetaData.Strategy)
}
}
}
log.Infoln(log.Global, "Exiting. Have a nice day")
err = log.CloseLogger()
if err != nil {
fmt.Println(err)
}
}
func parseFlags(wd string) map[string]bool {
defaultStrategy := filepath.Join(
wd,
"config",
"strategyexamples",
"dca-api-candles.strat")
defaultTemplate := filepath.Join(
wd,
"report",
"tpl.gohtml")
defaultReportOutput := filepath.Join(
wd,
"results")
flag.StringVar(
&singleTaskStrategyPath,
"singlerunstrategypath",
"",
fmt.Sprintf("path to a strategy file. Will execute strategy and exit, instead of creating a GRPC server. Example %v", defaultStrategy))
flag.StringVar(
&btConfigDir,
"backtesterconfigpath",
config.DefaultBTConfigDir,
"the location of the backtester config")
flag.StringVar(
&templatePath,
"templatepath",
defaultTemplate,
"the report template to use")
flag.BoolVar(
&generateReport,
"generatereport",
true,
"whether to generate the report file")
flag.StringVar(
&outputPath,
"outputpath",
defaultReportOutput,
"the path where to output results")
flag.BoolVar(
&darkReport,
"darkreport",
false,
"sets the output report to use a dark theme by default")
flag.BoolVar(
&colourOutput,
"colouroutput",
true,
"if enabled, will print in colours, if your terminal supports \033[38;5;99m[colours like this]\u001b[0m")
flag.BoolVar(
&logSubHeader,
"logsubheader",
true,
"displays logging subheader to track where activity originates")
flag.BoolVar(
&printLogo,
"printlogo",
true,
"shows the stunning, profit inducing logo on startup")
flag.StringVar(
&strategyPluginPath,
"strategypluginpath",
"",
"example path: "+filepath.Join(wd, "plugins", "strategies", "example", "example.so"))
flag.BoolVar(
&enablePProf,
"enablepprof",
false,
"if enabled, runs a pprof server for debugging")
flag.StringVar(
&pprofURL,
"pprofurl",
"http://localhost:6060",
"")
flag.Parse()
// collect flags
flags := make(map[string]bool)
// Stores the set flags
flag.Visit(func(f *flag.Flag) { flags[f.Name] = true })
return flags
}