letianzj / quanttrader

Backtest and live trading in Python
Apache License 2.0
476 stars 104 forks source link

Is it supports to add/remove options DataFrame dynamic during engine running? #7

Closed GF-Huang closed 2 years ago

GF-Huang commented 2 years ago

Hello, I have looked for many python backtesting frameworks, none of them support option backtesting well.

All the df (DataFrame) mentioned below are with 1 minute precision (1 minute bars), since I'm backtesting for day trading.

Because for example like SPY there will be 3 expiring options every week (Monday, Wednesday, Friday). I will follow the steps below:

  1. Add SPY 1 year 1 minute bar df to engine. (2021-06-30 ~ 2022-06-30 minutes bar).
  2. Add a few SPY options expires at 2021-06-30 (For the sake of explanation, let's assume that 2021-06-30 is a Monday).
  3. Start engine to start backtesting.
  4. Until the 2021-06-30 last bar reach (2021-06-03 15:59, suppose my df is from 09:30 ~ 15:59 instead of 09:31 ~ 16:00), remove the Monday expiration options from engine.
  5. Add SPY 2021-07-02 (Wednesday) expiration options df to engine. The engine is going on and reach 2021-07-01 09:30. So I can access the new options for backtesting.
  6. Repeat 4~5 until reach 2022-06-30 15:59, all SPY minute bars depleted.

Is quanttrader supports my idea/strategy?

letianzj commented 2 years ago

Hello, you may check out comdty roll. Basically the backtest engine loops through the SPY 1min timestamp. With the current timestamp, you are able to get the price/iv/greeks dataframe available up to then, either from the dataframes you load locally from strategy init, or use the self._data_board class. Also note that looking into future timestamps e.g. to get dte does not count as look forward bias.

GF-Huang commented 2 years ago

Thanks, got it.