comparing indicator lines with strategy parameters
-
I created 2 buy conditions in the strategy init function.
self.buyCondition1 = bt.ind.CrossOver(self.kst, self.p.zeroLine) self.buyCondition2 = bt.ind.CrossOver(self.kst, self.p.rsignal)
self.kst is just a line in an indicator, so it's like an sma line, so this code is working for me. I use only one of these buy conditions, but have tested both and they both work. From what I read in the docs, it's better to keep this sort of thing in init() rather than next() for better performance.
So I'm trying to do a 3rd condition to test out, but it involves comparing self.kst to a strategy parameter, self.p.overSold, or self.p.overBought. This doesn't seem to work. I imagine I can get it working in next(), but my question is, is there a proper way to do this in Init() ? Should I use a parameter from kst instead, or should I just do all of this in the kst indicator file? I'm sure if I continue to mess around with it I'll get it working, but I am trying to learn to make everything run as fast as possible. This is the code I tried for the 3rd buy condition:
if self.kst <= self.p.overSold: self.buyCondition3 = 1 elif self.kst >= self.p.overBought: self.buyCondition3 = -1 else: self.buyCondition3 = 0
self.p.overBought and self.p.overSold are just 50 and -50
-
on the 2nd code block the indentation looks odd but it's correct in the actual project
-
Create line object using
bt.LineNum
and than you can compare it with theself.kst
usingbt.If
.
Links -
Blogs - Crossing over numbers
Docs - Platform Concepts - Some non-overriden operators/functions -
@ab_trader Looks like the "if" was the problem. Didn't realize I had to use bt.If in init(). Used lineNum too but not sure if I had to, I'll just leave it though. Thanks!