Calling strategy from strategy leads to maximum recursion depth error
-
Hi, i am currently struggling with a problem I can't find the solution too.
The basic idea is the following:
I have a BaseStrategy which inherits from backtrader.Strategy
I have a PivotStrategy class which inherits from BaseStrategy
I have a LevelStrategy class which inherits from BaseStrategyThe LevelStrategy contains code to analyse some candlestick patterns around key levels
Now, in PivotStrategy I want to call an analysis method of LevelStrategy if the current price is around a pivot level to see if the price action gives any indication of breakout or reversal.
If I use the following code I get the error message shown below:
class BaseStrategy(backtrader.Strategy): @abstractmethod def analyse_ticker(self): pass
from trader.executor import BaseStrategy from trader.strategies.LevelStrategy import LevelsStrategy class PivotStrategy(BaseStrategy): def __init__(self, alert_only: bool = False): super().__init__() def analyse_ticker(self): # Do stuff ls = LevelsStrategy() return_value = ls.apply_strategy() return return_value def next(self): self.analyse_ticker()
from trader.executor import BaseStrategy class LevelsStrategy(BaseStrategy): def analyse_ticker(self): return 0.0
import backtrader as bt from trader.strategies.PivotStrategy import PivotStrategy if __name__ == "__main__": cerebro = bt.Cerebro() data = bt.feeds.GenericCSVData(dataname='data.csv', headers=True, dtformat=('%Y-%m-%dT%H:%M:%S%z'), timeframe=bt.TimeFrame.Minutes, datetime=1, open=2, high=3, low=4, close=5, volume=6, tz=pytz.timezone('UTC')) cerebro.adddata(data) cerebro.addstrategy(PivotStrategy) cerebro.addsizer(bt.sizers.SizerFix, stake=3) cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='mysharpe') cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="mytrades") start_portfolio_value = cerebro.broker.getvalue() thestrats = cerebro.run()
The stacktrace:
Traceback (most recent call last): File ".../pycharm/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_trace_dispatch_regular.py", line 422, in __call__ if not is_stepping and frame_cache_key in cache_skips: RecursionError: maximum recursion depth exceeded in comparison Fatal Python error: Cannot recover from stack overflow. Python runtime state: initialized Thread 0x00007feba7b38640 (most recent call first): File "/usr/lib/python3.8/threading.py", line 306 in wait File "/usr/lib/python3.8/threading.py", line 558 in wait File ".../pycharm/plugins/python/helpers/pydev/pydevd.py", line 150 in _on_run File ".../pycharm/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 218 in run File "/usr/lib/python3.8/threading.py", line 932 in _bootstrap_inner File "/usr/lib/python3.8/threading.py", line 890 in _bootstrap Current thread 0x00007febc7a67740 (most recent call first): File ".../pycharm/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_trace_dispatch_regular.py", line 422 in __call__ File ".../venv/lib/python3.8/site-packages/backtrader/lineroot.py", line 304 in _stage1 File ".../venv/lib/python3.8/site-packages/backtrader/lineiterator.py", line 189 in _stage1 File ".../venv/lib/python3.8/site-packages/backtrader/lineiterator.py", line 196 in _stage1 ....
I have already identified that the problem is the code 'ls = LevelsStrategy()'. If I remove that line and the following one, the execution runs through.
However, currently I can't think of a solution.
Any ideas on how to implement this?
Thank you.
-
Cupcake made at home are a lot more than just a delicious dessert. They can be an interesting and fun activity that you and your family can enjoy together to make Cupcake Delivery Sydney. Cupcakes make great party favors, teach children about kitchen hygiene, help with math skills, and much more!