-
Notifications
You must be signed in to change notification settings - Fork 9
/
trade.go
420 lines (372 loc) · 9.8 KB
/
trade.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package trade
import (
"fmt"
"github.com/markcheno/go-quote"
"github.com/markcheno/go-talib"
anko_core "github.com/mattn/anko/builtins"
"github.com/mattn/anko/vm"
"math"
"os"
"time"
)
// Trade - info for a single trade
type Trade struct {
Symbol string
Kind string
Units int
EntryDate time.Time
EntryPrice float64
ExitDate time.Time
ExitPrice float64
Profit float64
}
// Strategy - main struct to hold trading system info
type Strategy struct {
Price quote.Quote
Script string
Runtime time.Duration
Startbar int
Units float64
Roundlot int
Startcash float64
Skidfrac float64
Trades []Trade
Balance []float64
Barcount int
Bar int
maxdd float64
peak float64
valley float64
position string
Equity []float64
openprofit []float64
drawdown []float64
buystop []float64
sellstop []float64
shortstop []float64
coverstop []float64
env *vm.Env
}
// NewStrategy -
func NewStrategy(p quote.Quote, script string) Strategy {
s := Strategy{}
s.Script = script
s.Price = p
s.Barcount = len(p.Close)
s.Equity = make([]float64, s.Barcount)
s.Balance = make([]float64, s.Barcount)
s.openprofit = make([]float64, s.Barcount)
s.drawdown = make([]float64, s.Barcount)
s.buystop = make([]float64, s.Barcount)
s.sellstop = make([]float64, s.Barcount)
s.shortstop = make([]float64, s.Barcount)
s.coverstop = make([]float64, s.Barcount)
s.Startcash = 1000000.0
s.Balance[0] = s.Startcash
s.Skidfrac = 0.5
s.Startbar = 0
s.maxdd = 0.99
s.Roundlot = 250
s.position = "flat"
return s
}
// SetUnits -
func (s *Strategy) SetUnits(units float64) {
s.Units = units
}
// SetBar -
func (s *Strategy) SetBar(bar int) {
s.Bar = bar
}
// Backtest -
func (s *Strategy) Backtest(params []float64) float64 {
starttime := time.Now()
s.env = vm.NewEnv()
anko_core.Import(s.env)
s.env.Define("Bar", s.Bar)
s.env.Define("Open", s.Price.Open)
s.env.Define("High", s.Price.High)
s.env.Define("Low", s.Price.Low)
s.env.Define("Close", s.Price.Close)
s.env.Define("Volume", s.Price.Volume)
s.env.Define("Date", s.Price.Date)
s.env.Define("BuyOpen", s.BuyOpen)
s.env.Define("SellOpen", s.SellOpen)
s.env.Define("ShortOpen", s.ShortOpen)
s.env.Define("CoverOpen", s.CoverOpen)
s.env.Define("Ema", talib.Ema)
s.env.Define("Atr", talib.Atr)
s.env.Define("Balance", s.Balance)
s.env.Define("Equity", s.Equity)
s.env.Define("StartCash", s.Startcash)
s.env.Define("StarBar", s.Startbar)
s.env.Define("Units", s.Units)
s.env.Define("SetUnits", s.SetUnits)
s.env.Define("Params", params)
s.env.Define("Max", math.Max)
_, err := s.env.Execute(s.Script)
if err != nil {
panic(err)
}
v, _ := s.env.Get("StartCash")
s.Startcash = v.Float()
s.Balance[0] = s.Startcash
v, _ = s.env.Get("StartBar")
s.Startbar = int(v.Int())
for s.Bar = 0; s.Bar < s.Barcount-1; s.Bar++ {
s.Evaluate()
if s.Bar >= s.Startbar {
s.env.Set("Bar", s.Bar)
_, err := s.env.Execute("run()")
if err != nil {
fmt.Println(err.Error()[9:])
os.Exit(0)
}
}
}
//s.ExitTrade(s.Price.Date[s.Barcount-1], s.Price.Close[s.Barcount-1])
s.ClosePosition()
s.Runtime = time.Since(starttime)
return s.Bliss()
}
// Evaluate -
func (s *Strategy) Evaluate() {
bar := s.Bar
if s.Bar > 0 {
s.Balance[s.Bar] = s.Balance[s.Bar-1]
}
// check if long protective stop was hit
if s.position == "long" && s.Price.Low[bar] < s.sellstop[bar] {
bestprice := math.Min(s.Price.Open[bar], s.sellstop[bar])
fillprice := s.skidfunction(bestprice, s.Price.Low[bar])
s.ExitTrade(s.Price.Date[bar], fillprice)
}
// check if short protective stop was hit
if s.position == "short" && s.coverstop[bar] > 0 && s.Price.High[bar] > s.coverstop[bar] {
bestprice := math.Max(s.Price.Open[bar], s.coverstop[bar])
fillprice := s.skidfunction(bestprice, s.Price.High[bar])
s.ExitTrade(s.Price.Date[bar], fillprice)
}
// check if entry long stop order was hit
if s.position == "flat" && s.buystop[bar] > 0 && s.Price.High[bar] > s.buystop[bar] {
bestprice := math.Max(s.Price.Open[bar], s.buystop[bar])
bestprice = math.Max(bestprice, s.Price.Low[bar])
fillprice := s.skidfunction(s.Price.High[bar], bestprice)
s.EnterTrade("long", s.Price.Symbol, s.Units, s.Price.Date[bar], fillprice)
}
// check if entry short stop order was hit
if s.position == "flat" && s.Price.Low[bar] < s.shortstop[bar] {
bestprice := math.Min(s.Price.Open[bar], s.shortstop[bar])
bestprice = math.Min(bestprice, s.Price.High[bar])
fillprice := s.skidfunction(s.Price.Low[bar], bestprice)
s.EnterTrade("short", s.Price.Symbol, s.Units, s.Price.Date[bar], fillprice)
}
// calculate open profit and equity
if len(s.Trades) > 0 {
t := s.Trades[len(s.Trades)-1]
if s.position == "short" {
s.openprofit[s.Bar] = (t.EntryPrice - s.Price.Close[s.Bar]) * float64(t.Units)
} else if s.position == "long" {
s.openprofit[bar] = (s.Price.Close[bar] - t.EntryPrice) * float64(t.Units)
}
}
s.Equity[s.Bar] = s.Balance[s.Bar] + s.openprofit[s.Bar]
// calculate drawdown
if s.Bar == 0 {
s.peak = s.Balance[0]
s.valley = s.Balance[0]
}
if s.Equity[s.Bar] > s.peak {
s.peak = s.Equity[s.Bar]
}
if s.Equity[s.Bar] < s.valley {
s.valley = s.Equity[s.Bar]
}
retrace := s.peak - s.valley
if retrace > 0 {
s.drawdown[s.Bar] = retrace / s.peak
} else if s.Bar > 0 {
s.drawdown[s.Bar] = s.drawdown[s.Bar-1]
}
}
func (s *Strategy) skidfunction(price1, price2 float64) float64 {
return price1 + s.Skidfrac*(price2-price1)
}
// ExitTrade -
func (s *Strategy) ExitTrade(date time.Time, exitprice float64) {
profit := 0.0
if len(s.Trades) == 0 {
return
}
t := &s.Trades[len(s.Trades)-1]
if s.position == "long" {
profit = (exitprice - t.EntryPrice) * float64(t.Units)
} else if s.position == "short" {
profit = (t.EntryPrice - exitprice) * float64(t.Units)
}
s.Balance[s.Bar] = s.Balance[s.Bar-1] + profit
if s.Bar < s.Barcount-1 {
s.openprofit[s.Bar+1] = 0
}
t.ExitDate = date
t.ExitPrice = exitprice
t.Profit = profit
s.position = "flat"
}
// EnterTrade -
func (s *Strategy) EnterTrade(kind string, symbol string, units float64, entrydate time.Time, entryprice float64) {
t := Trade{
Symbol: symbol,
Kind: kind,
Units: s.roundunits(units),
EntryDate: entrydate,
EntryPrice: entryprice,
ExitDate: entrydate,
ExitPrice: 0,
Profit: 0,
}
s.Trades = append(s.Trades, t)
s.position = kind
}
func (s *Strategy) roundunits(units float64) int {
return s.Roundlot * int((units+0.0001)/float64(s.Roundlot)+0.5)
}
// BuyOpen -
func (s *Strategy) BuyOpen() {
if s.position == "flat" {
fillprice := s.skidfunction(s.Price.Open[s.Bar+1], s.Price.High[s.Bar+1])
s.EnterTrade("long", s.Price.Symbol, s.Units, s.Price.Date[s.Bar+1], fillprice)
}
}
// SellOpen -
func (s *Strategy) SellOpen() {
if s.position == "long" {
fillprice := s.skidfunction(s.Price.Open[s.Bar+1], s.Price.Low[s.Bar+1])
s.ExitTrade(s.Price.Date[s.Bar+1], fillprice)
}
}
// ShortOpen -
func (s *Strategy) ShortOpen() {
if s.position == "flat" {
fillprice := s.skidfunction(s.Price.Open[s.Bar+1], s.Price.Low[s.Bar+1])
s.EnterTrade("short", s.Price.Symbol, s.Units, s.Price.Date[s.Bar+1], fillprice)
}
}
// CoverOpen -
func (s *Strategy) CoverOpen() {
if s.position == "short" {
fillprice := s.skidfunction(s.Price.Open[s.Bar+1], s.Price.High[s.Bar+1])
s.ExitTrade(s.Price.Date[s.Bar+1], fillprice)
}
}
// BuyStop -
func (s *Strategy) BuyStop(price float64) {
if s.position == "flat" {
s.buystop[s.Bar+1] = price
}
}
// SellStop -
func (s *Strategy) SellStop(price float64) {
if s.position == "long" {
s.sellstop[s.Bar+1] = price
}
}
// ShortStop -
func (s *Strategy) ShortStop(price float64) {
if s.position == "flat" {
s.shortstop[s.Bar+1] = price
}
}
// CoverStop -
func (s *Strategy) CoverStop(price float64) {
if s.position == "short" {
s.coverstop[s.Bar+1] = price
}
}
// ClosePosition -
func (s *Strategy) ClosePosition() {
fillprice := 0.0
if s.position == "long" {
fillprice = s.skidfunction(s.Price.Close[s.Bar], s.Price.Low[s.Bar])
} else if s.position == "short" {
fillprice = s.skidfunction(s.Price.Close[s.Bar], s.Price.High[s.Bar])
} else if s.position == "flat" {
return
}
s.ExitTrade(s.Price.Date[s.Bar], fillprice)
s.openprofit[s.Bar] = 0
s.Equity[s.Bar] = s.Balance[s.Bar]
}
// Icagr -
func (s *Strategy) Icagr() float64 {
icagr := 0.0
end := len(s.Balance) - 1
if s.Balance[end] > s.Balance[0] {
ratio := s.Balance[end] / s.Balance[0]
daterangeinyears := (s.Price.Date[end].Sub(s.Price.Date[0])).Hours() / 24.0 / 365.25
icagr = math.Log(ratio) / daterangeinyears
}
return icagr
}
// DrawDown -
func (s *Strategy) DrawDown() float64 {
max := -1.0
for x := 0; x < len(s.drawdown); x++ {
if s.drawdown[x] > max {
max = s.drawdown[x]
}
}
return max
}
// Bliss -
func (s *Strategy) Bliss() float64 {
if s.DrawDown() > s.maxdd {
return 0
}
if s.DrawDown() > 0 {
return s.Icagr() / s.DrawDown()
}
return 0
}
// Summary -
func (s *Strategy) Summary() {
//s.EquityLog()
s.TradeLog()
//s.PriceLog()
fmt.Printf("Bliss=%f, Icagr=%f, DD=%f\n", s.Bliss(), s.Icagr(), s.DrawDown())
}
// PriceLog -
func (s *Strategy) PriceLog() {
for n := 0; n < s.Barcount; n++ {
fmt.Printf("%s OHLC:[ %6.2f %6.2f %6.2f %6.2f ]\n",
s.Price.Date[n].Format("2006-01-02"),
s.Price.Open[n],
s.Price.High[n],
s.Price.Low[n],
s.Price.Close[n])
}
}
// EquityLog -
func (s *Strategy) EquityLog() {
for n := 0; n < s.Barcount; n++ {
fmt.Printf("%s %10.2f %10.2f %10.2f\n",
s.Price.Date[n].Format("2006-01-02"),
s.Balance[n],
s.openprofit[n],
s.Equity[n])
}
}
// TradeLog -
func (s *Strategy) TradeLog() {
for _, t := range s.Trades {
fmt.Printf("%s %s %d %s %7.3f %s %7.3f %6.2f\n",
t.Symbol,
t.Kind,
t.Units,
t.EntryDate.Format("2006-01-02"),
t.EntryPrice,
t.ExitDate.Format("2006-01-02"),
t.ExitPrice,
t.Profit)
}
}