-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (142 loc) · 4.02 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
package main
import (
"flag"
"fmt"
"github.com/lukehollenback/goose/exchange/binance"
"github.com/lukehollenback/goose/trader/algos/movingaverages"
"github.com/lukehollenback/goose/trader/broker"
"github.com/lukehollenback/goose/trader/candle"
"github.com/lukehollenback/goose/trader/monitor"
"github.com/lukehollenback/goose/trader/writer"
"github.com/shopspring/decimal"
"log"
"os"
"os/signal"
)
func main() {
//
// Register a kill signal handler with the operating system so that we can gracefully shutdown if
// necessary.
//
osInterrupt := make(chan os.Signal, 1)
signal.Notify(osInterrupt, os.Interrupt)
//
// Register and parse global flags.
//
// NOTE ~> Individual services and algorithms might register their own flags in their package
// initialization functions. This, however, is the only place where they are parsed.
//
cfgBinanceAPIKey := flag.String(
"binance-key",
"none",
fmt.Sprintf("The API key to use when interacting with the Binance.US API."),
)
cfgBinanceAPISecret := flag.String(
"binance-secret",
"none",
fmt.Sprintf("The secret to use for signing requests when interacting with the Binance.US API."),
)
cfgAsset := flag.String(
"asset",
"BTC",
fmt.Sprintf("The asset that should be traded."),
)
cfgMock := flag.Bool(
"mock",
false,
fmt.Sprintf("Whether or not mock trading should be enabled."),
)
cfgMockAmt := flag.Int64(
"mock-amount",
1000,
fmt.Sprintf("The initial amount of USD to fund the mock trader with."),
)
cfgMockFee := flag.Float64(
"mock-fee",
0.00075,
fmt.Sprintf("The maker/taker fee that each mock trade costs to execute."),
)
flag.Parse()
//
// Instantiate and authenticate with a financial exchange's REST API.
//
client := binance.NewClient()
_, _ = client.Auth(*cfgBinanceAPIKey, *cfgBinanceAPISecret)
//
// Start the desired algorithm(s).
//
movingaverages.Init()
//
// Start up the Writer Service.
//
chWriterStarted, err := writer.Instance().Start()
if err != nil {
log.Fatalf("Failed to start the writer service. (Error: %s)", err)
}
//
// Start up the Candle Service
//
chCandleStarted, err := candle.Instance().Start()
if err != nil {
log.Fatalf("Failed to start the match monitor service. (Error: %s)", err)
}
//
// Start up the Broker Service.
//
broker.Instance().SetAsset(*cfgAsset)
if *cfgMock {
broker.Instance().EnableMockTrading(decimal.NewFromInt(*cfgMockAmt), decimal.NewFromFloat(*cfgMockFee))
}
chBrokerStarted, err := broker.Instance().Start()
if err != nil {
log.Fatalf("Failed to start the broker service. (Error: %s)", err)
}
//
// Start up the Monitor Service.
//
monitor.Instance().SetClient(client)
monitor.Instance().SetAsset(*cfgAsset)
chMonitorStarted, err := monitor.Instance().Start()
if err != nil {
log.Fatalf("Failed to start the match monitor service. (Error: %s)", err)
}
//
// Wait for all services to finish starting up.
//
<-chWriterStarted
<-chCandleStarted
<-chBrokerStarted
<-chMonitorStarted
//
// Block until we are shut down by the operating system.
//
<-osInterrupt
log.Print("An operating system interrupt has been received. Shutting down all services...")
//
// Stop all running services.
//
chMonitorStopped, err := monitor.Instance().Stop()
if err != nil {
log.Fatalf("Failed to stop the match monitor service. (Error: %s)", err)
}
chBrokerStopped, err := broker.Instance().Stop()
if err != nil {
log.Fatalf("Failed to stop the broker service. (Error: %s)", err)
}
chCandleStopped, err := candle.Instance().Stop()
if err != nil {
log.Fatalf("Failed to stop the match monitor service. (Error: %s)", err)
}
chWriterStopped, err := writer.Instance().Stop()
if err != nil {
log.Fatalf("Failed to stop the writer service. (Error: %s)", err)
}
<-chMonitorStopped
<-chBrokerStopped
<-chCandleStopped
<-chWriterStopped
//
// Wrap everything up.
//
log.Print("Goodbye.")
}