Can we create Strategies with Dynamic Indicators with 100% declarative in the __init__ method?
-
I am a bt newbie and I follow the examples in https://www.backtrader.com/blog/posts/2018-02-06-dynamic-indicator/dynamic-indicator/ .
I really appreciate the beauty and elegance of being 100% declarative in the init method mentioned here https://www.backtrader.com/blog/2019-07-08-canonical-or-not/canonical-or-not/ . But something go wrong when I include the dynamic variable.
I try to rewrite
case1:into
case2:
Case1 was successfully run but the running result was wrong in case2 even I have already set runonce = False . I don't know what's happening. And I am looking for a way that we can minimize the logic checking in next( ) method .any help is appreciated. Thank you.
-
sorry, I made some mistakes .The code should be something like this.
#case1 class MyStrategy(bt.Strategy): def __init__(self): self.dyn = MyDynamicIndicator() def notify_trade(self, trade): xxxxxx def next(self): if self.dyn: print('ABOUT TO DO SOMETHING') #case2 class MyStrategy(bt.Strategy): def __init__(self): self.dyn = MyDynamicIndicator() > 100 def notify_trade(self, trade): xxxxx def next(self): if self.dyn: print('ABOUT TO DO SOMETHING')
-
You are not creating dynamic indicator, since it doesn't depend on the strategy execution. You just compare two lines:
self.dyn
line and line which has 100 as a value. This can be done like this:def __init__(self): self.dyn = MyDynamicIndicator() self.line100 = bt.LineNum(100.0) self.compare = self.dyn.indicator_line_which_is_unknown_but_missed_here > self.line100
If you want to develop an indicator which depends on the strategy execution results like order execution, trade open/closed, than you will need
next()
.