forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtest.go
848 lines (802 loc) · 23.1 KB
/
backtest.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
package engine
import (
"errors"
"fmt"
"time"
"github.com/gofrs/uuid"
"github.com/aaabigfish/gocryptotrader/backtester/common"
"github.com/aaabigfish/gocryptotrader/backtester/data"
"github.com/aaabigfish/gocryptotrader/backtester/eventhandlers/exchange"
"github.com/aaabigfish/gocryptotrader/backtester/eventhandlers/portfolio"
"github.com/aaabigfish/gocryptotrader/backtester/eventhandlers/statistics"
"github.com/aaabigfish/gocryptotrader/backtester/eventhandlers/strategies/base"
"github.com/aaabigfish/gocryptotrader/backtester/eventtypes/fill"
"github.com/aaabigfish/gocryptotrader/backtester/eventtypes/kline"
"github.com/aaabigfish/gocryptotrader/backtester/eventtypes/order"
"github.com/aaabigfish/gocryptotrader/backtester/eventtypes/signal"
"github.com/aaabigfish/gocryptotrader/backtester/funding"
gctcommon "github.com/aaabigfish/gocryptotrader/common"
"github.com/aaabigfish/gocryptotrader/currency"
gctexchange "github.com/aaabigfish/gocryptotrader/exchanges"
"github.com/aaabigfish/gocryptotrader/exchanges/asset"
"github.com/aaabigfish/gocryptotrader/exchanges/futures"
gctorder "github.com/aaabigfish/gocryptotrader/exchanges/order"
"github.com/aaabigfish/gocryptotrader/log"
)
// Reset BackTest values to default
func (bt *BackTest) Reset() error {
if bt == nil {
return gctcommon.ErrNilPointer
}
var err error
if bt.orderManager != nil {
err = bt.orderManager.Stop()
if err != nil {
return err
}
}
if bt.databaseManager != nil {
err = bt.databaseManager.Stop()
if err != nil {
return err
}
}
err = bt.EventQueue.Reset()
if err != nil {
return err
}
err = bt.DataHolder.Reset()
if err != nil {
return err
}
err = bt.Portfolio.Reset()
if err != nil {
return err
}
err = bt.Statistic.Reset()
if err != nil {
return err
}
err = bt.Exchange.Reset()
if err != nil {
return err
}
err = bt.Funding.Reset()
if err != nil {
return err
}
bt.exchangeManager = nil
bt.orderManager = nil
bt.databaseManager = nil
return nil
}
// RunLive is a proof of concept function that does not yet support multi currency usage
// It tasks by constantly checking for new live datas and running through the list of events
// once new data is processed. It will run until application close event has been received
func (bt *BackTest) RunLive() error {
if bt.LiveDataHandler == nil {
return errLiveOnly
}
var err error
if bt.LiveDataHandler.IsRealOrders() {
err = bt.LiveDataHandler.UpdateFunding(false)
if err != nil {
return err
}
}
err = bt.LiveDataHandler.Start()
if err != nil {
return err
}
bt.wg.Add(1)
go func() {
err = bt.liveCheck()
if err != nil {
log.Errorln(common.LiveStrategy, err)
}
bt.wg.Done()
}()
return nil
}
func (bt *BackTest) liveCheck() error {
for {
select {
case <-bt.shutdown:
return bt.LiveDataHandler.Stop()
case <-bt.LiveDataHandler.HasShutdownFromError():
return bt.Stop()
case <-bt.LiveDataHandler.HasShutdown():
return nil
case <-bt.LiveDataHandler.Updated():
err := bt.Run()
if err != nil {
return err
}
}
}
}
// ExecuteStrategy executes the strategy using the provided configs
func (bt *BackTest) ExecuteStrategy(waitForOfflineCompletion bool) error {
if bt == nil {
return gctcommon.ErrNilPointer
}
bt.m.Lock()
if bt.MetaData.DateLoaded.IsZero() {
bt.m.Unlock()
return errNotSetup
}
if !bt.MetaData.Closed && !bt.MetaData.DateStarted.IsZero() {
bt.m.Unlock()
return fmt.Errorf("%w %v %v", errTaskIsRunning, bt.MetaData.ID, bt.MetaData.Strategy)
}
if bt.MetaData.Closed {
bt.m.Unlock()
return fmt.Errorf("%w %v %v", errAlreadyRan, bt.MetaData.ID, bt.MetaData.Strategy)
}
if waitForOfflineCompletion && bt.MetaData.LiveTesting {
bt.m.Unlock()
return fmt.Errorf("%w cannot wait for a live task to finish", errCannotHandleRequest)
}
bt.MetaData.DateStarted = time.Now()
liveTesting := bt.MetaData.LiveTesting
bt.m.Unlock()
var err error
switch {
case waitForOfflineCompletion && !liveTesting:
err = bt.Run()
if err != nil {
log.Errorln(common.Backtester, err)
}
return bt.Stop()
case !waitForOfflineCompletion && liveTesting:
return bt.RunLive()
case !waitForOfflineCompletion && !liveTesting:
go func() {
err = bt.Run()
if err != nil {
log.Errorln(common.Backtester, err)
}
err = bt.Stop()
if err != nil {
log.Errorln(common.Backtester, err)
}
}()
}
return nil
}
// Run will iterate over loaded data events
// save them and then handle the event based on its type
func (bt *BackTest) Run() error {
// doubleNil allows the run function to exit if no new data is detected on a live run
var doubleNil bool
if bt.MetaData.DateLoaded.IsZero() {
return errNotSetup
}
for ev := bt.EventQueue.NextEvent(); ; ev = bt.EventQueue.NextEvent() {
if ev == nil {
if bt.hasShutdown {
return nil
}
if doubleNil {
if bt.verbose {
log.Infoln(common.Backtester, "No new data on second check")
}
return nil
}
doubleNil = true
dataHandlers, err := bt.DataHolder.GetAllData()
if err != nil {
return err
}
for i := range dataHandlers {
var e data.Event
e, err = dataHandlers[i].Next()
if err != nil {
if errors.Is(err, data.ErrEndOfData) {
return nil
}
return err
}
if e == nil {
if !bt.hasProcessedAnEvent && bt.LiveDataHandler == nil {
var (
exch string
assetItem asset.Item
cp currency.Pair
)
exch, assetItem, cp, err = dataHandlers[i].GetDetails()
if err != nil {
return err
}
log.Errorf(common.Backtester, "Unable to perform `Next` for %v %v %v", exch, assetItem, cp)
}
return nil
}
o := e.GetOffset()
if bt.Strategy.UsingSimultaneousProcessing() && bt.hasProcessedDataAtOffset[o] {
// only append one event, as simultaneous processing
// will retrieve all relevant events to process under
// processSimultaneousDataEvents()
continue
}
bt.EventQueue.AppendEvent(e)
if !bt.hasProcessedDataAtOffset[o] {
bt.hasProcessedDataAtOffset[o] = true
}
}
} else {
doubleNil = false
err := bt.handleEvent(ev)
if err != nil {
log.Errorln(common.Backtester, err)
}
if !bt.hasProcessedAnEvent {
bt.hasProcessedAnEvent = true
}
}
}
}
// handleEvent is the main processor of data for the backtester
// after data has been loaded and Run has appended a data event to the queue,
// handle event will process events and add further events to the queue if they
// are required
func (bt *BackTest) handleEvent(ev common.Event) error {
if ev == nil {
return fmt.Errorf("cannot handle event %w", errNilData)
}
funds, err := bt.Funding.GetFundingForEvent(ev)
if err != nil {
return err
}
switch eType := ev.(type) {
case kline.Event:
// using kline.Event as signal.Event also matches data.Event
if bt.Strategy.UsingSimultaneousProcessing() {
err = bt.processSimultaneousDataEvents()
} else {
err = bt.processSingleDataEvent(eType, funds.FundReleaser())
}
case signal.Event:
err = bt.processSignalEvent(eType, funds.FundReserver())
case order.Event:
err = bt.processOrderEvent(eType, funds.FundReleaser())
case fill.Event:
err = bt.processFillEvent(eType, funds.FundReleaser())
if bt.LiveDataHandler != nil {
// output log data per interval instead of at the end
result, logErr := bt.Statistic.CreateLog(eType)
if logErr != nil {
return logErr
}
if err != nil {
return err
}
log.Infoln(common.LiveStrategy, result)
}
default:
err = fmt.Errorf("handleEvent %w %T received, could not process",
errUnhandledDatatype,
ev)
}
if err != nil {
return err
}
return bt.Funding.CreateSnapshot(ev.GetTime())
}
// processSingleDataEvent will pass the event to the strategy and determine how it should be handled
func (bt *BackTest) processSingleDataEvent(ev data.Event, funds funding.IFundReleaser) error {
err := bt.updateStatsForDataEvent(ev, funds)
if err != nil {
return err
}
d, err := bt.DataHolder.GetDataForCurrency(ev)
if err != nil {
return err
}
s, err := bt.Strategy.OnSignal(d, bt.Funding, bt.Portfolio)
if err != nil {
if errors.Is(err, base.ErrTooMuchBadData) {
// too much bad data is a severe error and backtesting must cease
return err
}
log.Errorf(common.Backtester, "OnSignal %v", err)
return nil
}
err = bt.Statistic.SetEventForOffset(s)
if err != nil {
log.Errorf(common.Backtester, "SetEventForOffset %v", err)
}
bt.EventQueue.AppendEvent(s)
return nil
}
// processSimultaneousDataEvents determines what signal events are generated and appended
// to the event queue. It will pass all currency events to the strategy to determine what
// currencies to act upon
func (bt *BackTest) processSimultaneousDataEvents() error {
dataHolders, err := bt.DataHolder.GetAllData()
if err != nil {
return err
}
dataEvents := make([]data.Handler, 0, len(dataHolders))
for i := range dataHolders {
var latestData data.Event
latestData, err = dataHolders[i].Latest()
if err != nil {
return err
}
var funds funding.IFundingPair
funds, err = bt.Funding.GetFundingForEvent(latestData)
if err != nil {
return err
}
err = bt.updateStatsForDataEvent(latestData, funds.FundReleaser())
if err != nil {
switch {
case errors.Is(err, statistics.ErrAlreadyProcessed):
if !bt.MetaData.Closed || !bt.MetaData.ClosePositionsOnStop {
// Closing positions on close reuses existing events and doesn't need to be logged
// any other scenario, this should be logged
log.Warnf(common.LiveStrategy, "%v %v", latestData.GetOffset(), err)
}
continue
case errors.Is(err, futures.ErrPositionLiquidated):
return nil
default:
log.Errorln(common.Backtester, err)
}
}
dataEvents = append(dataEvents, dataHolders[i])
}
signals, err := bt.Strategy.OnSimultaneousSignals(dataEvents, bt.Funding, bt.Portfolio)
if err != nil {
switch {
case errors.Is(err, base.ErrTooMuchBadData):
// too much bad data is a severe error and backtesting must cease
return err
case errors.Is(err, base.ErrNoDataToProcess) && bt.MetaData.Closed && bt.MetaData.ClosePositionsOnStop:
// event queue is being cleared with no data events to process
return nil
default:
log.Errorf(common.Backtester, "OnSimultaneousSignals %v", err)
return nil
}
}
for i := range signals {
err = bt.Statistic.SetEventForOffset(signals[i])
if err != nil {
log.Errorf(common.Backtester, "SetEventForOffset %v %v %v %v", signals[i].GetExchange(), signals[i].GetAssetType(), signals[i].Pair(), err)
}
bt.EventQueue.AppendEvent(signals[i])
}
return nil
}
// updateStatsForDataEvent makes various systems aware of price movements from
// data events
func (bt *BackTest) updateStatsForDataEvent(ev data.Event, funds funding.IFundReleaser) error {
if ev == nil {
return common.ErrNilEvent
}
if funds == nil {
return fmt.Errorf("%v %v %v %w missing fund releaser", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), gctcommon.ErrNilPointer)
}
// update statistics with the latest price
err := bt.Statistic.SetEventForOffset(ev)
if err != nil {
if errors.Is(err, statistics.ErrAlreadyProcessed) {
return err
}
log.Errorf(common.Backtester, "SetEventForOffset %v", err)
}
// update portfolio manager with the latest price
err = bt.Portfolio.UpdateHoldings(ev, funds)
if err != nil {
log.Errorf(common.Backtester, "UpdateHoldings %v", err)
}
if ev.GetAssetType().IsFutures() {
var cr funding.ICollateralReleaser
cr, err = funds.CollateralReleaser()
if err != nil {
return err
}
err = bt.Portfolio.UpdatePNL(ev, ev.GetClosePrice())
if err != nil {
if errors.Is(err, futures.ErrPositionNotFound) {
// if there is no position yet, there's nothing to update
return nil
}
if !errors.Is(err, futures.ErrPositionLiquidated) {
return fmt.Errorf("UpdatePNL %v", err)
}
}
var pnl *portfolio.PNLSummary
pnl, err = bt.Portfolio.GetLatestPNLForEvent(ev)
if err != nil {
return err
}
if pnl.Result.IsLiquidated {
return nil
}
if bt.LiveDataHandler == nil || (bt.LiveDataHandler != nil && !bt.LiveDataHandler.IsRealOrders()) {
err = bt.Portfolio.CheckLiquidationStatus(ev, cr, pnl)
if err != nil {
if errors.Is(err, futures.ErrPositionLiquidated) {
liquidErr := bt.triggerLiquidationsForExchange(ev, pnl)
if liquidErr != nil {
return liquidErr
}
}
return err
}
}
return bt.Statistic.AddPNLForTime(pnl)
}
return nil
}
// processSignalEvent receives an event from the strategy for processing under the portfolio
func (bt *BackTest) processSignalEvent(ev signal.Event, funds funding.IFundReserver) error {
if ev == nil {
return common.ErrNilEvent
}
if funds == nil {
return fmt.Errorf("%w funds", gctcommon.ErrNilPointer)
}
cs, err := bt.Exchange.GetCurrencySettings(ev.GetExchange(), ev.GetAssetType(), ev.Pair())
if err != nil {
log.Errorf(common.Backtester, "GetCurrencySettings %v", err)
return fmt.Errorf("GetCurrencySettings %v", err)
}
var o *order.Order
o, err = bt.Portfolio.OnSignal(ev, &cs, funds)
if err != nil {
log.Errorf(common.Backtester, "OnSignal %v", err)
return fmt.Errorf("OnSignal %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
err = bt.Statistic.SetEventForOffset(o)
if err != nil {
return fmt.Errorf("SetEventForOffset %v", err)
}
bt.EventQueue.AppendEvent(o)
return nil
}
func (bt *BackTest) processOrderEvent(ev order.Event, funds funding.IFundReleaser) error {
if ev == nil {
return common.ErrNilEvent
}
if funds == nil {
return fmt.Errorf("%w funds", gctcommon.ErrNilPointer)
}
d, err := bt.DataHolder.GetDataForCurrency(ev)
if err != nil {
return err
}
f, err := bt.Exchange.ExecuteOrder(ev, d, bt.orderManager, funds)
if err != nil {
if f == nil {
log.Errorf(common.Backtester, "ExecuteOrder fill event should always be returned, please fix, %v", err)
return fmt.Errorf("ExecuteOrder fill event should always be returned, please fix, %v", err)
}
if !errors.Is(err, exchange.ErrCannotTransact) {
log.Errorf(common.Backtester, "ExecuteOrder %v %v %v %v", f.GetExchange(), f.GetAssetType(), f.Pair(), err)
}
}
err = bt.Statistic.SetEventForOffset(f)
if err != nil {
log.Errorf(common.Backtester, "SetEventForOffset %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
bt.EventQueue.AppendEvent(f)
return nil
}
func (bt *BackTest) processFillEvent(ev fill.Event, funds funding.IFundReleaser) error {
_, err := bt.Portfolio.OnFill(ev, funds)
if err != nil {
return fmt.Errorf("OnFill %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
err = bt.Funding.UpdateCollateralForEvent(ev, false)
if err != nil {
return fmt.Errorf("UpdateCollateralForEvent %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
holding, err := bt.Portfolio.ViewHoldingAtTimePeriod(ev)
if err != nil {
log.Errorln(common.Backtester, err)
}
err = bt.Statistic.AddHoldingsForTime(holding)
if err != nil {
log.Errorf(common.Backtester, "AddHoldingsForTime %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
snap, err := bt.Portfolio.GetLatestComplianceSnapshot(ev.GetExchange(), ev.GetAssetType(), ev.Pair())
if err != nil {
log.Errorf(common.Backtester, "GetLatestComplianceSnapshot %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
err = bt.Statistic.AddComplianceSnapshotForTime(snap, ev)
if err != nil {
log.Errorf(common.Backtester, "AddComplianceSnapshotForTime %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
fde := ev.GetFillDependentEvent()
if fde != nil && !fde.IsNil() {
// some events can only be triggered on a successful fill event
fde.SetOffset(ev.GetOffset())
err = bt.Statistic.SetEventForOffset(fde)
if err != nil {
log.Errorf(common.Backtester, "SetEventForOffset %v %v %v %v", fde.GetExchange(), fde.GetAssetType(), fde.Pair(), err)
}
od := ev.GetOrder()
if fde.MatchOrderAmount() && od != nil {
fde.SetAmount(ev.GetAmount())
}
fde.AppendReasonf("raising event after %v %v %v fill", ev.GetExchange(), ev.GetAssetType(), ev.Pair())
bt.EventQueue.AppendEvent(fde)
}
if ev.GetAssetType().IsFutures() {
return bt.processFuturesFillEvent(ev, funds)
}
return nil
}
func (bt *BackTest) processFuturesFillEvent(ev fill.Event, funds funding.IFundReleaser) error {
if ev.GetOrder() == nil {
return nil
}
pnl, err := bt.Portfolio.TrackFuturesOrder(ev, funds)
if err != nil && !errors.Is(err, gctorder.ErrSubmissionIsNil) {
return fmt.Errorf("TrackFuturesOrder %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
var exch gctexchange.IBotExchange
exch, err = bt.exchangeManager.GetExchangeByName(ev.GetExchange())
if err != nil {
return fmt.Errorf("GetExchangeByName %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
rPNL := pnl.GetRealisedPNL()
if !rPNL.PNL.IsZero() {
var receivingCurrency currency.Code
var receivingAsset asset.Item
receivingCurrency, receivingAsset, err = exch.GetCurrencyForRealisedPNL(ev.GetAssetType(), ev.Pair())
if err != nil {
return fmt.Errorf("GetCurrencyForRealisedPNL %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
err = bt.Funding.RealisePNL(ev.GetExchange(), receivingAsset, receivingCurrency, rPNL.PNL)
if err != nil {
return fmt.Errorf("RealisePNL %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
}
err = bt.Statistic.AddPNLForTime(pnl)
if err != nil {
return fmt.Errorf("AddPNLForTime %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
err = bt.Funding.UpdateCollateralForEvent(ev, false)
if err != nil {
return fmt.Errorf("UpdateCollateralForEvent %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
return nil
}
// Stop shuts down the live data loop
func (bt *BackTest) Stop() error {
if bt == nil {
return gctcommon.ErrNilPointer
}
bt.m.Lock()
defer bt.m.Unlock()
if bt.MetaData.Closed {
return errAlreadyRan
}
close(bt.shutdown)
bt.MetaData.Closed = true
bt.MetaData.DateEnded = time.Now()
if bt.MetaData.ClosePositionsOnStop {
err := bt.CloseAllPositions()
if err != nil {
log.Errorf(common.Backtester, "Could not close all positions on stop: %s", err)
}
}
if !bt.hasProcessedAnEvent {
return nil
}
err := bt.Statistic.CalculateAllResults()
if err != nil {
return err
}
err = bt.Reports.GenerateReport()
if err != nil {
return err
}
return nil
}
func (bt *BackTest) triggerLiquidationsForExchange(ev data.Event, pnl *portfolio.PNLSummary) error {
if ev == nil {
return common.ErrNilEvent
}
if pnl == nil {
return fmt.Errorf("%w pnl summary", gctcommon.ErrNilPointer)
}
orders, err := bt.Portfolio.CreateLiquidationOrdersForExchange(ev, bt.Funding)
if err != nil {
return err
}
for i := range orders {
// these orders are raising events for event offsets
// which may not have been processed yet
// this will create and store stats for each order
// then liquidate it at the funding level
var datas data.Handler
datas, err = bt.DataHolder.GetDataForCurrency(orders[i])
if err != nil {
return err
}
var latest data.Event
latest, err = datas.Latest()
if err != nil {
return err
}
err = bt.Statistic.SetEventForOffset(latest)
if err != nil && !errors.Is(err, statistics.ErrAlreadyProcessed) {
return err
}
bt.EventQueue.AppendEvent(orders[i])
err = bt.Statistic.SetEventForOffset(orders[i])
if err != nil {
log.Errorf(common.Backtester, "SetEventForOffset %v %v %v %v", ev.GetExchange(), ev.GetAssetType(), ev.Pair(), err)
}
err = bt.Funding.Liquidate(orders[i])
if err != nil {
return err
}
}
pnl.Result.IsLiquidated = true
pnl.Result.Status = gctorder.Liquidated
return bt.Statistic.AddPNLForTime(pnl)
}
// CloseAllPositions will close sell any positions held on closure
// can only be with live testing and where a strategy supports it
func (bt *BackTest) CloseAllPositions() error {
if bt.LiveDataHandler == nil {
return errLiveOnly
}
err := bt.LiveDataHandler.UpdateFunding(true)
if err != nil {
return err
}
dataHolders, err := bt.DataHolder.GetAllData()
if err != nil {
return err
}
latestPrices := make([]data.Event, len(dataHolders))
for i := range dataHolders {
var latest data.Event
latest, err = dataHolders[i].Latest()
if err != nil {
return err
}
latestPrices[i] = latest
}
events, err := bt.Strategy.CloseAllPositions(bt.Portfolio.GetLatestHoldingsForAllCurrencies(), latestPrices)
if err != nil {
if errors.Is(err, gctcommon.ErrFunctionNotSupported) {
log.Warnf(common.LiveStrategy, "Closing all positions is not supported by strategy %v", bt.Strategy.Name())
return nil
}
return err
}
if len(events) == 0 {
return nil
}
err = bt.LiveDataHandler.SetDataForClosingAllPositions(events...)
if err != nil {
return err
}
for i := range events {
k := events[i].ToKline()
err = bt.Statistic.SetEventForOffset(k)
if err != nil {
return err
}
bt.EventQueue.AppendEvent(events[i])
}
err = bt.Run()
if err != nil {
return err
}
err = bt.LiveDataHandler.UpdateFunding(true)
if err != nil {
return err
}
err = bt.Funding.CreateSnapshot(events[0].GetTime())
if err != nil {
return err
}
for i := range events {
var funds funding.IFundingPair
funds, err = bt.Funding.GetFundingForEvent(events[i])
if err != nil {
return err
}
err = bt.Portfolio.SetHoldingsForEvent(funds.FundReader(), events[i])
if err != nil {
return err
}
}
her := bt.Portfolio.GetLatestHoldingsForAllCurrencies()
for i := range her {
err = bt.Statistic.AddHoldingsForTime(&her[i])
if err != nil {
return err
}
}
return nil
}
// GenerateSummary creates a summary of a strategy task
// this summary contains many details of a task
func (bt *BackTest) GenerateSummary() (*TaskSummary, error) {
if bt == nil {
return nil, gctcommon.ErrNilPointer
}
bt.m.Lock()
defer bt.m.Unlock()
return &TaskSummary{
MetaData: bt.MetaData,
}, nil
}
// SetupMetaData will populate metadata fields
func (bt *BackTest) SetupMetaData() error {
if bt == nil {
return gctcommon.ErrNilPointer
}
bt.m.Lock()
defer bt.m.Unlock()
if !bt.MetaData.ID.IsNil() && !bt.MetaData.DateLoaded.IsZero() {
// already setup
return nil
}
id, err := uuid.NewV4()
if err != nil {
return err
}
bt.MetaData.ID = id
bt.MetaData.DateLoaded = time.Now()
return nil
}
// IsRunning checks if the task is running
func (bt *BackTest) IsRunning() bool {
if bt == nil {
return false
}
bt.m.Lock()
defer bt.m.Unlock()
return !bt.MetaData.DateStarted.IsZero() && !bt.MetaData.Closed
}
// HasRan checks if the task has been executed
func (bt *BackTest) HasRan() bool {
if bt == nil {
return false
}
bt.m.Lock()
defer bt.m.Unlock()
return bt.MetaData.Closed
}
// Equal checks if the incoming task matches
func (bt *BackTest) Equal(bt2 *BackTest) bool {
if bt == nil || bt2 == nil {
return false
}
bt.m.Lock()
btM := bt.MetaData
bt.m.Unlock()
// if they are actually the same pointer
// locks must be handled separately
bt2.m.Lock()
btM2 := bt2.MetaData
bt2.m.Unlock()
return btM == btM2
}
// MatchesID checks if the backtesting run's ID matches the supplied
func (bt *BackTest) MatchesID(id uuid.UUID) bool {
if bt == nil {
return false
}
if id.IsNil() {
return false
}
bt.m.Lock()
defer bt.m.Unlock()
if bt.MetaData.ID.IsNil() {
return false
}
return bt.MetaData.ID == id
}