How to know whether 2 lines are crossing?
-
How to find out when 2 lines are crossing?
Let's say I have a Strategy with indicator 2 lines, I want to execute an order once the to 2 indicator lines are crossing. I tried if (self.l.indicator1[0]-self.l.indicator2[0]) == 0 but this is not working of course, especially if the timeframe is not 1 Minute. -
@crypto_trader Crossing mean one line was above the other on previous period, and is under on current period.
This logic could be written like that :if (self.l.indicator1[1]>self.l.indicator2[1]) and (self.l.indicator1[0]<self.l.indicator2[0]):
You may want to read below links, for a more efficient implementation :
https://community.backtrader.com/topic/231/how-to-generate-crossover-signal-with-slowk-and-slowd-from-talib-and-or-bt-stochastic-fonction
https://www.backtrader.com/blog/posts/2017-02-04-crossing-over-numbers/crossing-over-numbers/
and have a look at crossover.py indicator. -
unfortunately there is a error message:
return self.array[self.idx + ago]
IndexError: array index out of range -
@crypto_trader With crossover indicator ? with the if <...> and <...> logic ? You may get a faster and more accurate help if you post the part of your code which cause this error.
-
@crypto_trader try crossover indicator, or :
if (self.l.indicator1[-1]>self.l.indicator2[-1]) and (self.l.indicator1[0]<self.l.indicator2[0]):
-
@crypto_trader I think you should consider CrossUp and CrossDown