thrasher-corp / gocryptotrader

A cryptocurrency trading bot and framework supporting multiple exchanges written in Golang.
MIT License
3.03k stars 807 forks source link

Backtester panics when there is missing data #840

Closed MeanSquaredError closed 3 years ago

MeanSquaredError commented 3 years ago

New Issue

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

Expected Behavior

Backtester should run correctly and generate a results file

Current Behavior

Backtester panics with the following error message

panic: decimal division by 0

goroutine 1 [running]:
github.com/shopspring/decimal.Decimal.QuoRem({0xc00185ae20, 0x0}, {0xc0000339e0, 0x0}, 0x10)
        /root/go/pkg/mod/github.com/shopspring/decimal@v1.3.1/decimal.go:565 +0x2c5
github.com/shopspring/decimal.Decimal.DivRound({0xc00185ae20, 0x200800}, {0xc0000339e0, 0x0}, 0x10)
        /root/go/pkg/mod/github.com/shopspring/decimal@v1.3.1/decimal.go:607 +0x56
github.com/shopspring/decimal.Decimal.Div(...)
        /root/go/pkg/mod/github.com/shopspring/decimal@v1.3.1/decimal.go:552
github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics.(*CurrencyPairStatistic).CalculateResults(0xc000358b00, {0xc000032a00, 0x1f})
        /usr/local/projects/github/gocryptotrader/backtester/eventhandlers/statistics/currencystatistics.go:67 +0xab3
github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics.(*Statistic).CalculateAllResults(0xc00026a000)
        /usr/local/projects/github/gocryptotrader/backtester/eventhandlers/statistics/statistics.go:171 +0x505
main.main()
        /usr/local/projects/github/gocryptotrader/backtester/main.go:109 +0xb14
exit status 2

Failure Information (for bugs)

Please help by providing information about the failure. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. run backtester with the attached configuration file: go run . -configpath=/root/.gocryptotrader/strategy/config/rsi-api-candles-latest.strat (in my case it is placed in /root/.gocryptotrader/strategy/config/rsi-api-candles-latest.strat)

Failure Logs

No log files are generated, but I have attached the strategy configuration which causes the crash. rsi-api-candles-latest.strat.txt

It seems that the crash is caused by division by zero in backtester/eventhandlers/statistics/currencystatistics.go:66

        benchmarkRates[i] = c.Events[i].DataEvent.ClosePrice().Sub(
            c.Events[i-1].DataEvent.ClosePrice()).Div(
            c.Events[i-1].DataEvent.ClosePrice())

If c.Events[i-1] is missing data then c.Events[i-1].DataEvent.ClosePrice() is zero and division by zero occurs.

gloriousCode commented 3 years ago

Thank you very much for providing your strat config for me to verify! I can confirm the panic and I'll look into a solution

thrasher- commented 3 years ago

Thanks for reporting this @MeanSquaredError