tudorelu / pyjuque

⚡ Open Source Algorithmic Trading Bot for Python.
MIT License
463 stars 88 forks source link

[FIX] Not enough data for computing profit on backtesting #13

Closed gabriel-milan closed 3 years ago

gabriel-milan commented 3 years ago

I've noticed that, sometimes, if neither your short signal nor stop_loss triggers, there's not an exit from positions. For that particular reason, you may bump into something like this

Traceback (most recent call last):
  File "backtesting.py", line 53, in <module>
    Main()
  File "backtesting.py", line 29, in Main
    bt.backtest(df)
  File "/home/gabriel-milan/.local/lib/python3.7/site-packages/pyjuque/Engine/BacktesterSundayTheQuant.py", line 291, in backtest
    last_exit = self.exits[len(self.exits) - 1]
IndexError: list index out of range

For it to be fixed, I've implemented a flag on the bot config called sell_on_end (defaults to False for backwards compatibility) which tells the backtester to sell positions on data end. Beside that, I've also added a friendly message for this kind of error, just in case it still happens:

Traceback (most recent call last):
  File "backtesting.py", line 53, in <module>
    Main()
  File "backtesting.py", line 29, in Main
    bt.backtest(df)
  File "/home/gabriel-milan/.local/lib/python3.7/site-packages/pyjuque/Engine/BacktesterSundayTheQuant.py", line 300, in backtest
    (Total entries: {} / Total exits: {})""".format(len(self.entries), len(self.exits)))
ValueError: 
            Not enough data for computing profits. Either your long or short signal wasn't triggered.
            Please check your strategies and try again. If you have at least one entry but no exits,
            you can force a full sell on the end by setting 'sell_on_end' to True on your bot config.
            (Total entries: 1 / Total exits: 0)
tudorelu commented 3 years ago

Hey! Good idea man - thank you again for contributing. Could you put this sell_on_end inside exit_settings in params ?

gabriel-milan commented 3 years ago

Sure, there you go.

I'm actually thinking about starting a code refactor for pyjuque as I'm using it for evolving my genetic algorithms and there are a few other things not working as expected on it and some other stuff I'd probably change. Is that of interest for you?

tudorelu commented 3 years ago

Absolutely of interest in principle, in practice we have to agree on the best way to do it. But yes it is something of interest!