polakowo / vectorbt

Find your trading edge, using the fastest engine for backtesting, algorithmic trading, and research.
https://vectorbt.dev
Other
4.41k stars 621 forks source link

Simulate case where you're using pre/after market data but only trading during market hours #44

Closed NowBothWhiteAndRed closed 8 months ago

NowBothWhiteAndRed commented 4 years ago

HI! I am exploring the following case and would just like to check with you if I am missing something obvious.

  1. I need to simulate trading activities when the market is open and NOT during the pre/after hour periods. (I am using minutely data so it’s between 9:30 and 4 pm EST)
  2. I still want to use the data of pre/after hours in my entry conditions. a. So: https://nbviewer.jupyter.org/github/polakowo/vectorbt/blob/master/examples/Trading-Sessions.ipynb does not seem to apply, since I don’t want to cut that data out, I just don’t want to simulate a trade then.

So far all I can think of is:

  1. Make a Boolean series which has Falses where you’re out of your desired time range and Trues when you’re in it.
  2. Use bitwise AND with entry/exits to make it so that your entry/exit conditions don’t occur during the times you don’t want them to.

Two issues with this:

  1. If I am running a bunch of combinations, I now need to do this to every single column in the dataframe.
  2. This doesn’t work on profit and profit_or_loss algos since they’re iterative. (My workaround right now for simple algorithms which go in on an indicator but go out at profit or loss is: a. FOR ENTRY: use bitwise and with a boolean series which maps to the trading interval I want. b. FOR EXIT: pass in data without that interval for signal generation. Then reindex the exit and new entries objects to fill in the missing pieces so that entries/exits and the dataset can all be broadcast to the same shape.

Both 1 and 2 carry significant performance impacts, of course.

I am assuming that this is another case for a custom numba function but it seems like then I would have to have one for every single indicator.

polakowo commented 4 years ago

I need to clarify a couple of things:

In any case, there is currently no other way than masking signals after running an indicator. Maybe you've got a simplified example? We can then figure out how to do it efficiently.