-
Hello everyone, Firstly, please let me thank you for such an amazing package and its great developers! With reference to issues #20 and #104, how would you go about implementing a multi-portfolio back tester? Some BackgroundMy (mostly-implemented) system is training a DL model on the features of historical data using Meta-Labelling. So, I am training it on an individual instrument which returns pretty good backtest stats. I repeat this procedure on about 12 different data frames (each in 15 minutes timeframe). However, the next step is rebalancing/ optimizing the portfolio among multiple instruments (fx currencies in Oanda in my case). Even if I somehow discard the RL and hard-code the logic behind the money management system (which I consider to be the most essential pillar of trading), I still need a way of backtesting multi-symbols. Falling in love with the flexibility, plotting, statistics, and the API, I am trying to make it work with backtesting.py. Even if it means changing some of the source code. Any help is appreciated. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@kernc Any idea how to handle this? |
Beta Was this translation helpful? Give feedback.
-
If backtesting.py/backtesting/backtesting.py Lines 1151 to 1165 in 73e1534 were on line 1166 continued with: if yielding:
yield strategy then you could provide a manager such as the following: class MetaManager:
model: RLModel # Size/direction model
def __init__(self, backtests):
self.backtests = backtests
def run(self):
# List of generators/"coroutines"
running_backtests = [bt.run(yielding=True, manager=self)
for bt in self.backtests]
while True:
try:
strategy_instances = [next(bt) for bt in running_backtests]
except StopIteration:
# shortest backtest finished
...
break
else:
# Collect signals and confidences, re-fit model
self.update_model(strategy_instances)
def get_signal(self, strategy):
X = self.get_params(strategy)
direction, size = self.model.predict(X)
return direction, size
class PortfolioStrategy(Strategy):
manager = None # Passed in .run()
def next(self):
direction, size = self.manager.get_signal(self)
...
individual_backtests: List[Backtest] = ...
manager = MetaManager(individual_backtests)
manager.run() That But above should be the gist of it, and it seems a fairly good idea. Probably can be simplified some more. Let me know how it works for you. |
Beta Was this translation helpful? Give feedback.
If
Backtest.run()
method got a new parameteryielding=False
(TBD), and this main backtest loop:backtesting.py/backtesting/backtesting.py
Lines 1151 to 1165 in 73e1534
were on line 1166 continued with: