Using bt.Strategy class for both algorithm deployment and backtesting
-
Hello everyone I am new the community! I had a general question about the use of backtrader framework. I would ideally like to have a single strategy class for each of my strategies where I can both backtest and then with the needed modifications use the same class to deploy my strategy with my broker i.e., Alpaca. But whenever I initialize
MyStrategy
class it calls on the__call__
method fromMetaBase
class and it fails. I am assuming that is because I am initializing the class without Cerebro, and thus is has no owner. Does anyone have an idea if there is simple way to achieve this? My current solution is to just overload the function calls, but I want to do this as elegant as possible. Thank you very much! -
@clxyder Are you going to deploy the strategy to Alpaca outside of the Backtrader framework? How do you envision this will work technically?
-
Hey @vladisld thanks for the quick response. I envision breaking up my strategy into key functions that can be called as needed in
next()
for the backtrader framework and then another function saydeploy()
that would run through every thing needed to send my orders out with Alpaca. I was thinking something along the lines of:strat = MyStrategy(deployment=True) strat.deploy()
to deploy with Alpaca. Where setting
deployment = True
would disable the backtesting functionality. And it would by default be set to False so I can pass theMyStrategy
toCerebro
without a problem.The main reason I ask this is because I figured if I am already backtesting the strategy can't I just augment the class a little and make it work with Alpaca.
But if this seem a round about way of approaching I can just make two separate classes.
-
You may want to check live trading sections in
bt
docs. I am not sure why do you want to get strategy from cerebro. Replacing backtest broker with the live broker will do the live trading with the same strategy class. And I believe alpaca broker is already implemented forbt
. Search the forum.