Bar number of a signal formation


Category:
0
0

Hi Pete,

I am looking for the logic to get the bar number of a signal formation and keep that number till the next signal forming bar. It seems simple but I cannot get my head around it. I’ve attached a screenshot to explain.

Thanks in advance to help me out.

Stock

Attachments:
Marked as spam
Posted by (Questions: 6, Answers: 3)
Asked on October 4, 2020 2:41 am
69 views
0
Private answer

You did not provide the code you are using so my solution is going to be of my own application. You will have to figure out how to apply the technique your own code.

input maLengthOne = 8;
input maTypeOne = AverageType.EXPONENTIAL;
input maPriceOne = close;
input maLengthTwo = 21;
input maTypeTwo = AverageType.EXPONENTIAL;
input maPriceTwo = close;
def maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
def maTwo = MovingAverage(maTypeTwo, maPriceTwo, maLengthTwo);
def signal = maOne[1] < maTwo[1] and maOne > maTwo;
rec barNumberOfSignal = if signal then BarNumber() else barNumberOfSignal[1];
plot data = barNumberOfSignal;

The solution creates two moving averages. The signal is for the slow moving average crossing above the slow moving average. The code captures the bar number of the crossover event and retains it until the next crossover event. The technique required to achieve this is known as recursion. Which is when the current value of a variable is derived from a previous value of the same variable (in part or in whole).

If you need the bar number to reset when the other signal occurs you will need to add new logic to the recursive variable to reset the bar number when the other signal occurs. Depending on the complexity of your signals it might as simple as this:

rec barNumberOfSignal = if signalOne or signalTwo then BarNumber() else barNumberOfSignal[1];

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on October 4, 2020 8:47 am
0
Thanks Pete. My code is a basic CCI plot. I am looking for the bar number for crossabove and crossbelow. #-- Code starts here declare lower; input cciLength = 14; input cciAvgLength = 9; input over_sold = -100; input over_bought = 100; DEF CCI = CCI(length = cciLength); DEF CCIAvg = Average(CCI, cciAvgLength); def Cross_above = if (CCI()."CCI" crosses above CCIAverage()."CCIAvg", 1, 0); def Cross_below = if (CCI()."CCI" crosses below CCIAverage()."CCIAvg", 1, 0); plot crossabove = Cross_above; plot crossbelow = Cross_below; AddLabel(yes, IF Crossabove then "CCI_BUY" else if Crossbelow then "CCI_SELL" else "Hold"); #-- Code ends here
( at October 4, 2020 10:13 am)
0
Ok then the line of code to keep track of the bar numbers and reset that value for each signal is just like the example I provided. The only thing you need to do is replace the signals I created in my solution with the signals from your code: rec barNumberOfSignal = if Crossabove or Crossbelow then BarNumber() else barNumberOfSignal[1];
( at October 4, 2020 11:33 am)