-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
If I have a model that is trained using multiple indicators, how can I best pass that to the strategy and keep it in sync with the backtesting expanding window? also making sure that I am not getting any Lookahead bias ... the library is conceived to only get as input the OCHL... of a single indicator but in ML we look at many indicators. It would be a good design to separate the traded instrument from the indicators (which may also include the traded instrument but not necessarily).
what I would do for now is to use the UD strategy constructor to pass all the data as context and then time filter it accordingly while running the strategy in the expanding window.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 2 replies
-
how can I best pass that to the strategy and keep it in sync with the backtesting expanding window?
Does the approach in #39 work for you?
Beta Was this translation helpful? Give feedback.
All reactions
-
@kernc thanks a lot for your answer. Yes I saw that, but it is unfortunately still not ideal ... I would like to be able pass additional context to the Strategy, for example: pre-trained ML models, backtesting start date (which may not coincide with the OCHL input data time index at iloc 0), etc. Otherwise, I saw that instead of filling the state of the Strategy in OOP, Strategy implementations need to end up consuming global variables as alternative which compromises the design quality and maintainability. The type or complexity of data needed by a Strategy may not be always cast as additional columns in the dataframe passed (for example, pre-trained models or path location where to load them from). I get it to keep the Strategy simple as much as possible but being able have a provision for more sophisticated use-cases would be invaluable.
It would be great including an additional kvargs
argument to the Backtest
to be piped to the Strategy's constructor and then it will work, or even just allowing to pass a context dictionary so that the Strategy can consume that context data internally as it sees fit.
For example, here:
https://kernc.github.io/backtesting.py/doc/examples/Trading%20with%20Machine%20Learning.html
I would like to instead of passing or reading this parameter from a "magic number" global variable, to instead pass from configuration to my CLI or WebApp a date where to start the backtest or the retraining frequency for my ML models etc:
N_TRAIN = 400
Beta Was this translation helpful? Give feedback.
All reactions
-
Ah, I understand. You'd like to additionally pass kwargs
. This makes sense. Indeed, global scope was currently the showcase way.
Note, since this is Python, you can always do the equivalent of:
from .complex_strategy import OurMostComplexStrategy def some_locals(**kwargs): class StrategyKwargsWrapper(OurMostComplexStrategy): def init(self): self.model = Path(kwargs['model_path']).read() # OurMostComplexStrategy can use `self.model` ... super().init() local_backtest = Backtest(GOOG, StrategyKwargsWrapper, ...) ... some_locals(model_path=...)
But passing kwargs is a valid feature. Please freely open an issue for it.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1