This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
logic.mm.basic.go
172 lines (147 loc) · 3.01 KB
/
logic.mm.basic.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
package bitflyer
import (
"fmt"
"time"
v1 "github.com/go-numb/go-bitflyer/v1"
)
type LogicByBasic struct {
Name string
}
// IgniteBasic ignition to Logic
func (p *Client) IgniteBasic() {
p.Logic <- &LogicByBasic{
Name: "mm_basic",
}
}
// GetSide return sideInt for this logic
func (logic *LogicByBasic) getSide(isBuy bool) int {
if isBuy {
return 1
}
return -1
}
// Order do order and execution for MM Basic
func (logic *LogicByBasic) Order(p *Client) {
if !p.Controllers.Basic.IsOK() {
return
}
defer p.Controllers.Basic.Close()
if p.isDelay() {
return
}
var (
isMarket bool
)
side := logic.getSide(p.E.IsBuy)
if side == 0 {
return
}
price, ok := p.setLimitPrice(side)
if !ok {
return
}
isFull, size := p.O.GetOrderSize(side, p.Setting.Size)
if isFull {
return
}
if p.Setting.IsTestNet { // simple backtest
if err := logic.BackTest(p, side, price, size); err != nil {
p.Logger.Error(err)
}
return
}
o, err := p.OrderBySimple(isMarket, side, price, size, nil)
if err != nil {
p.SetError(true, err)
return
}
p.O.Set(*o)
// 注文のタイムアウト
go func() {
timeout := time.After(time.Duration(CHECKRESOUCEPERIOD*MMBASICCANCELORDER) * time.Second)
for {
select {
case <-timeout:
p.CancelByID(o.OrderID)
goto EXIT
}
}
EXIT:
return
}()
// 約定監視
if !p.isExecution(side, logic.Name, o) {
if !p.O.IsCancel(o) {
time.Sleep(p.E.Delay + o.OnAccept)
}
}
return
}
var (
BUYPRICE, SELLPRICE float64
)
// setLimitPrice 指値価格の決定
func (p *Client) setLimitPrice(side int) (price float64, ok bool) {
base := p.E.Price * 0.0002
ok = true
if 0 < side {
price = p.E.BestBid - p.E.Spread()
if BUYPRICE+base > price && BUYPRICE-base < price {
ok = false // 同価格帯なら拒否
}
BUYPRICE = price
} else if side < 0 {
price = p.E.BestAsk + p.E.Spread()
if SELLPRICE+base > price && SELLPRICE-base < price {
ok = false // 同価格帯なら拒否
}
SELLPRICE = price
}
return price, ok
}
// BackTest check execution, and set database
func (logic *LogicByBasic) BackTest(p *Client, side int, price, size float64) error {
o := &Order{
Side: v1.ToSide(side),
Price: price,
Size: size,
IsDone: false,
}
func() {
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
/*
Timeout: 9sec
order length: 366, exec rate: 34.699%
BUY 866702.6470588231 0.6800000000000004
SELL 867249.6949152538 0.5900000000000003
CreateAt: 2020/1/10 3:02:32 JST
*/
timeout := time.After(time.Duration(CHECKRESOUCEPERIOD*MMBASICCANCELORDER) * time.Second)
for {
select {
case <-ticker.C:
if 0 < side {
if price > p.E.Price {
fmt.Println("is done")
o.IsDone = true
return
}
} else if side < 0 {
if price < p.E.Price {
fmt.Println("is done")
o.IsDone = true
return
}
}
case <-timeout:
return
}
}
}()
// BackTest用table
if err := p.DB.Set(DBTABLEORDERSFORTEST, o); err != nil {
return err
}
return nil
}