amor71 / LiuAlgoTrader

Framework for algorithmic trading
MIT License
784 stars 129 forks source link

Backtesting on a market closed day #404

Closed aivisol closed 9 months ago

aivisol commented 1 year ago

Describe the bug Running backtesting on day when markets are closed, throws following error:

[backtest()][49032]2023-10-08 13:18:41.303132:backtest() - exception of type IndexError with args ('list index out of range',)
 ...
  File "/opt/homebrew/lib/python3.11/site-packages/liualgotrader/trading/alpaca.py", line 58, in __init__
    calendar = self.alpaca_rest_client.get_calendar(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
IndexError: list index out of range

Most likely caused by this: https://github.com/amor71/LiuAlgoTrader/blob/3df70067cf7a419115259956c64b50c5c07931db/liualgotrader/trading/alpaca.py#L58

Suggesting to change it to:

        try:
            calendar = self.alpaca_rest_client.get_calendar(
                start=now.strftime("%Y-%m-%d"), end=now.strftime("%Y-%m-%d")
            )[0]
        except IndexError:
            self.market_open = self.market_close = None
            super().__init__(qm)
            return

Let me know if this makes sense, I can send a PR.

thanks.

github-actions[bot] commented 1 year ago

Thank you for joining LiuAlgoTrader community and submitting your first issue. Feel free to check out Liu community: https://gitter.im/LiuAlgoTrader/community

github-actions[bot] commented 9 months ago

Stale issue message

oaimtiaz commented 3 months ago

I had this issue too. Not sure if a fix is planned for this, so for a quick workaround, you can directly modify the executable script. Here's some steps on how to do it:

  1. Find where the script is installed
    
    import liualgotrader.trading.alpaca

print(liualgotrader.trading.alpaca.file)


2. Now, open this file in your editor and replace with @aivisol's suggested code above. Until a fix is implemented/merged in, you should be able to run the backtester!