-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
195 lines (166 loc) · 3.8 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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"time"
"github.com/lvhuat/textformatter"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v2"
)
var (
log = logrus.WithFields(logrus.Fields{})
)
var gridFile = flag.String("grid", "grid.csv", "网格文件")
var cfgFile = flag.String("cfg", "config.json", "基本配置文件")
var testMode = flag.Bool("test", false, "仅打印不会下单,不会执行网格")
var mf = flag.Bool("mf", false, "仅监控保证金率")
type EventRejectOrder struct {
ClientId string
Side string
}
type GridPersistItem struct {
Time time.Time
Ask float64
Bid float64
Symbol string
ProfitTotal float64
Grids []*TradeGrid
}
func persistGrids() {
d, err := yaml.Marshal(&GridPersistItem{
Grids: grids,
Time: time.Now(),
Symbol: perpName,
Ask: ask1,
Bid: bid1,
ProfitTotal: profitTotal,
})
if err != nil {
log.Fatalf("error: %v", err)
}
ioutil.WriteFile("save.yaml", d, 0666)
}
func main() {
logrus.SetFormatter(&textformatter.TextFormatter{})
flag.Parse()
if *cfgFile != "" {
loadBaseConfigAndAssign(*cfgFile)
}
eventChan := make(chan interface{}, 1000)
go func() {
for {
wsclient := WebsocketClient{
apiKey: apiKey,
secret: []byte(secretKey),
subAccount: subAccount,
quit: make(chan interface{}),
}
if err := wsclient.dial(false); err != nil {
logrus.WithError(err).Errorln("DialWebsocketFailed")
time.Sleep(time.Second)
continue
}
wsclient.ping()
wsclient.login()
wsclient.subOrder()
wsclient.onOrderChange = func(body []byte) {
order := &Order{}
raw := gjson.GetBytes(body, "data").Raw
json.Unmarshal([]byte(raw), &order)
if order.ClientID == "" {
return
}
eventChan <- order
}
wsclient.waitFinished()
logrus.Errorln("WebsocketStop")
time.Sleep(time.Second)
}
}()
RejectOrder = func(clientId, side string) {
eventChan <- &EventRejectOrder{
ClientId: clientId,
Side: side,
}
}
if *mf {
mfLoop()
return
}
if *gridFile != "" {
loadGridConfigAndAssign(*gridFile)
}
if _, err := os.Stat("./save.yaml"); os.IsNotExist(err) {
if *gridFile != "" {
loadGridConfigAndAssign(*gridFile)
}
} else {
loadFromSaveFile("save.yaml")
}
// 打印网格配置
debugGrid()
// 打印持仓
debugPositions()
writeGridCurrent()
for i := 3; i > 0; i-- {
log.Infoln("Counting ", i)
time.Sleep(time.Second)
}
log.Infoln("Good luck!")
// go monitorPosition()
// go mfLoop()
// 执行网格
wait := checkInterval
lastSyncOrderTime := time.Now()
for {
persistGrids()
select {
case <-time.After(wait):
check()
writeGridCurrent()
wait = quickRecheckInterval
case event := <-eventChan:
switch event.(type) {
case *Order:
onOrderChange(event.(*Order))
case *EventRejectOrder:
data := event.(*EventRejectOrder)
onRejectOrder(data.ClientId, data.Side)
}
continue
}
if time.Now().Sub(lastSyncOrderTime) < time.Second*5 {
time.Sleep(wait)
continue
}
// 定时刷新订单状态
orders, err := client.getOrders(perpName)
if err != nil {
logrus.WithError(err).Errorln("GetOpenOrders")
continue
}
for _, order := range orders {
onOrderChange(order)
}
// 未能及时同步的订单,将采用单个同步的方式同步
orderMap.RangeOver(func(order *GridOrder) bool {
if time.Now().Sub(order.UpdateTime) < time.Second*3 {
return true
}
ftxOrder, err := client.getOrderByClient(order.ClientId)
if err != nil {
switch err.Error() {
case "Order not found":
RejectOrder(order.ClientId, order.Side)
}
logrus.WithError(err).Errorln("GetOrder", order.ClientId)
return true
}
onOrderChange(ftxOrder)
return true
})
}
}