-
Notifications
You must be signed in to change notification settings - Fork 4
/
future.go
346 lines (327 loc) · 11.2 KB
/
future.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
package trade
import (
"github.com/beaquant/utils"
"github.com/nntaoli-project/GoEx"
"github.com/sirupsen/logrus"
"time"
)
type FutureTradeManager struct {
exchange goex.FutureRestAPI //交易所
pair goex.CurrencyPair //货币对
contractType string //合约类型
initAccount *Account //初始账户
opMode OpMode //下单方式:吃单|挂单
slidePrice float64 //下单滑动价
slideGrowthRate float64 //下单滑动价增长率
openPositionSlideGrowthRateMax float64 //下单滑动价最大增长率
coverPositionSlideGrowthRateMax float64 //下单滑动价最大增长率
retryDelayMs time.Duration //失败重试(毫秒)
logger *logrus.Logger //logger
priceDot int //价格小数精度
amountDot int //数量小数精度
marginLevel int //杆杠大小
//maxSpace float64 //挂单失效距离
//maxAmount float64 //开仓最大单次下单量
//minStocks float64 //最小交易数量
}
type SummaryPosition struct {
Amount float64 //持仓量, OKCoin表示合约的份数(整数且大于1)
Price float64 //持仓均价
Position *Position //
}
type Position struct {
MarginLevel int //杆杠大小, OKCoin为10或者20。
Amount float64 //持仓量, OKCoin表示合约的份数(整数且大于1)
FrozenAmount float64 //仓位冻结量
Price float64 //持仓均价
Profit float64 //持仓浮动盈亏(数据货币单位:BTC/LTC, 传统期货单位:RMB, 股票不支持此字段, 注: OKCoin期货全仓情况下指实现盈余, 并非持仓盈亏, 逐仓下指持仓盈亏)
Type int //PD_LONG为多头仓位(CTP中用closebuy_today平仓), PD_SHORT为空头仓位(CTP用closesell_today)平仓, (CTP期货中)PD_LONG_YD为咋日多头仓位(用closebuy平), PD_SHORT_YD为咋日空头仓位(用closesell平)
ContractType string //商品期货为合约代码
}
func NewFutureTradeManager(
exchange goex.FutureRestAPI,
pair goex.CurrencyPair,
contractType string,
opMode OpMode,
//maxSpace float64,
slidePrice float64,
slideGrowthRate float64,
openPositionSlideGrowthRateMax float64,
coverPositionSlideGrowthRateMax float64,
//maxAmount float64,
//minStocks float64,
retryDelayMs int,
logger *logrus.Logger,
priceDot int,
amountDot int,
) *FutureTradeManager {
if logger == nil {
logger = logrus.New()
}
utils.SetDelay(retryDelayMs)
mgr := &FutureTradeManager{
exchange: exchange,
pair: pair,
contractType: contractType,
initAccount: nil,
opMode: opMode,
slidePrice: slidePrice,
slideGrowthRate: slideGrowthRate,
openPositionSlideGrowthRateMax: openPositionSlideGrowthRateMax,
coverPositionSlideGrowthRateMax: coverPositionSlideGrowthRateMax,
retryDelayMs: time.Duration(retryDelayMs) * time.Millisecond,
logger: logger,
priceDot: priceDot,
amountDot: amountDot,
//maxAmount: maxAmount,
//maxSpace: maxSpace,
//minStocks: minStocks,
}
mgr.initAccount = mgr.GetAccount()
return mgr
}
func (future *FutureTradeManager) getPosition(direction int) *Position {
var allCost = 0.0
var allAmount = 0.0
var allProfit = 0.0
var allFrozen = 0.0
var posMargin = 0
var positions = utils.RE(future.exchange.GetFuturePosition, future.pair, future.contractType).([]goex.FuturePosition)
if len(positions) == 0 {
return nil
}
for i := 0; i < len(positions); i++ {
if positions[i].ContractType == future.contractType {
posMargin = positions[i].LeverRate
if direction == goex.OPEN_BUY {
allCost += positions[i].BuyPriceAvg * positions[i].BuyAmount
allAmount += positions[i].BuyAmount
allProfit += positions[i].BuyProfitReal
allFrozen += positions[i].BuyAmount - positions[i].BuyAvailable
} else if direction == goex.OPEN_SELL {
allCost += positions[i].BuyPriceAvg * positions[i].SellAmount
allAmount += positions[i].SellAmount
allProfit += positions[i].SellProfitReal
allFrozen += positions[i].SellAmount - positions[i].SellAvailable
}
}
}
if allAmount == 0 {
return nil
}
return &Position{
MarginLevel: posMargin,
FrozenAmount: allFrozen,
Price: utils.Float64Round(allCost/allAmount, future.priceDot),
Amount: allAmount,
Profit: allProfit,
Type: direction,
ContractType: future.contractType,
}
}
// direction : goex.OPEN_BUY, goex.OPEN_SELL
func (future *FutureTradeManager) open(direction int, price, opAmount float64) *SummaryPosition {
var initPosition = future.getPosition(direction)
var isFirst = true
var initAmount = 0.0
var positionNow = initPosition
var step = 0.0
if initPosition != nil {
initAmount = initPosition.Amount
}
for {
var needOpen = opAmount
if isFirst {
isFirst = false
} else {
positionNow = future.getPosition(direction)
if positionNow != nil {
needOpen = opAmount - (positionNow.Amount - initAmount)
}
}
if needOpen < 1 {
break
}
if step > future.openPositionSlideGrowthRateMax {
break
}
var amount = needOpen
//var orderId string
if direction == goex.OPEN_BUY {
future.exchange.PlaceFutureOrder(
future.pair,
future.contractType,
utils.Float64RoundString(price+future.slidePrice*(1+step), future.priceDot),
utils.Float64RoundString(amount, future.amountDot),
goex.OPEN_BUY,
0,
future.marginLevel,
)
} else if direction == goex.OPEN_SELL {
future.exchange.PlaceFutureOrder(
future.pair,
future.contractType,
utils.Float64RoundString(price-future.slidePrice*(1+step), future.priceDot),
utils.Float64RoundString(amount, future.amountDot),
goex.OPEN_SELL,
0,
future.marginLevel,
)
}
for {
var orders = utils.RE(future.exchange.GetUnfinishFutureOrders, future.pair, future.contractType).([]goex.FutureOrder)
if len(orders) == 0 {
break
}
time.Sleep(future.retryDelayMs)
for j := 0; j < len(orders); j++ {
future.exchange.FutureCancelOrder(future.pair, future.contractType, orders[j].OrderID2)
if j < (len(orders) - 1) {
time.Sleep(future.retryDelayMs)
}
}
}
step += future.slideGrowthRate
}
var pos = &SummaryPosition{
Price: 0,
Amount: 0,
Position: positionNow,
}
if positionNow == nil {
return pos
}
if initPosition == nil {
pos.Price = positionNow.Price
pos.Amount = positionNow.Amount
} else {
pos.Amount = positionNow.Amount - initPosition.Amount
pos.Price = utils.Float64Round(((positionNow.Price*positionNow.Amount)-(initPosition.Price*initPosition.Amount))/pos.Amount, future.priceDot)
}
return pos
}
//
// direction : goex.CLOSE_BUY, goex.CLOSE_SELL
func (future *FutureTradeManager) cover(direction int, opAmount, price float64) float64 {
var initP = make([]goex.FuturePosition, 0)
var positions = make([]goex.FuturePosition, 0)
var isFirst = true
var orderId string
//var err error
var step = 0.0
var index = 0
for {
var n = 0
positions = utils.RE(future.exchange.GetFuturePosition, future.pair, future.contractType).([]goex.FuturePosition)
if isFirst == true {
if len(positions) > 1 || (direction != goex.CLOSE_BUY && direction != goex.CLOSE_SELL) {
future.logger.Fatalln("有多,空双向持仓,并且参数direction未明确方向!或 direction 参数异常:", direction)
}
copy(initP, positions)
isFirst = false
}
for i := 0; i < len(positions); i++ {
if positions[i].ContractType != future.contractType ||
(positions[i].BuyAmount == 0 && direction == goex.CLOSE_BUY) ||
(positions[i].SellAmount == 0 && direction == goex.CLOSE_SELL) {
continue
}
var amount = 0.0
if direction == goex.CLOSE_BUY {
amount = opAmount - (initP[i].BuyAmount - positions[i].BuyAmount)
} else if direction == goex.CLOSE_BUY {
amount = opAmount - (initP[i].SellAmount - positions[i].SellAmount)
}
if amount == 0 {
continue
}
if direction == goex.CLOSE_BUY {
orderId, _ = future.exchange.PlaceFutureOrder(
future.pair,
future.contractType,
utils.Float64RoundString(price-future.slidePrice*(1+step), future.priceDot),
utils.Float64RoundString(amount, future.amountDot),
goex.CLOSE_BUY,
0,
future.marginLevel,
)
n++
} else if direction == goex.CLOSE_SELL {
orderId, _ = future.exchange.PlaceFutureOrder(
future.pair,
future.contractType,
utils.Float64RoundString(price+future.slidePrice*(1+step), future.priceDot),
utils.Float64RoundString(amount, future.amountDot),
goex.CLOSE_SELL,
0,
future.marginLevel,
)
n++
}
index = i
}
if n == 0 {
break
}
time.Sleep(future.retryDelayMs)
future.exchange.FutureCancelOrder(future.pair, future.contractType, orderId)
step += future.slideGrowthRate
if step > future.coverPositionSlideGrowthRateMax {
break
}
}
var nowP = utils.RE(future.exchange.GetFuturePosition, future.pair, future.contractType).([]goex.FuturePosition)
if len(nowP)-1 < index ||
(nowP[index].BuyAmount != initP[index].BuyAmount && direction == goex.CLOSE_BUY) ||
(nowP[index].SellAmount != initP[index].SellAmount && direction == goex.CLOSE_SELL) {
if len(initP) == 0 {
return 0
} else {
if direction == goex.CLOSE_BUY {
return initP[index].BuyAmount
} else if direction == goex.CLOSE_SELL {
return initP[index].SellAmount
}
}
} else {
if direction == goex.CLOSE_BUY {
return initP[index].BuyAmount - nowP[index].BuyAmount
} else if direction == goex.CLOSE_SELL {
return initP[index].SellAmount - nowP[index].SellAmount
}
}
return 0
}
func (future *FutureTradeManager) GetAccount() *Account {
var account = new(Account)
for {
acc := utils.RE(future.exchange.GetFutureUserinfo).(*goex.FutureAccount)
for _, v := range acc.FutureSubAccounts {
if v.Currency == future.pair.CurrencyB {
account.Balance = v.KeepDeposit
account.FrozenBalance = v.KeepDeposit - v.AccountRights
} else if v.Currency == future.pair.CurrencyA {
account.Balance = v.KeepDeposit
account.FrozenBalance = v.KeepDeposit - v.AccountRights
}
}
}
return account
}
func (future *FutureTradeManager) OpenLong(price, opAmount float64) *SummaryPosition {
return future.open(goex.OPEN_BUY, opAmount, price)
}
func (future *FutureTradeManager) OpenShort(price, opAmount float64) *SummaryPosition {
return future.open(goex.OPEN_SELL, opAmount, price)
}
func (future *FutureTradeManager) CloseLong(price, opAmount float64) float64 {
return future.cover(goex.CLOSE_BUY, opAmount, price)
}
func (future *FutureTradeManager) CloseShort(price, opAmount float64) float64 {
return future.cover(goex.CLOSE_SELL, opAmount, price)
}
func (future *FutureTradeManager) Profit(price, opAmount float64) float64 {
var accountNow = future.GetAccount()
future.logger.Infoln("NOW:", accountNow, "--account:", future.initAccount)
return utils.Float64Round(accountNow.Balance - future.initAccount.Balance)
}