request help for this afl code from seniors

#1
Hi, I have a problem with the code below:

Code:
MA1 = MA(C, 32);
MA2 = MA(C, 16);

trig = Cross(MA2, MA1);
vLINE = Ref(trig, 16);

LLBars= LLVBars(L, 16 + 5);
pd = ValueWhen(trig,LLBars,0);
vLINELow = Ref(trig, pd);


//Plot(MA1, "MA1", colorYellow, styleLine);
//Plot(MA2, "MA1", colorBlue, styleLine);

Plot(vLINElow, "", colorGrey50 , styleArea|styleOwnScale);
It basically draws a vertical line to the lowest low over the previous 16 days preceding the fast MA crossing the slow MA. The main goal of this formula is to detect past cycles.

However, the way the formula is written, it won't be able to skip whipsaws.

I maen, if the MA(16) crosses MA(32) from below, the formula will go search the lowest low over the preceding 16 days, to detect the starting point of the cycle. Yet, if then the MA(16) soon crosses back the MA(32) from above the there's no point in drawing the line. Because that is a whipsaw.

So, as a general rule, if the MA(16) crosses MA(32) from below, then the cycle will be labeled by the vertical line (at the lowest low over the preceding 16 days the crossover) as long as the MA(16) does not cross back MA(32) from above over the next 16 days at least.

How can I add this rule to the formula?

Thanks

NB: picture below, you get to see at the very left of the chart the yellow crossing the orange from below. The formula is wrong in drawing the vertical line, because then the yellow shortly after crosses the orange from above...only 3/4 days later. So, the first vertical line should not be drawn.

Later on, you see one more crossover. The yellow crosses the orange from below and the formula draws a vertical line to the lowest low over the past 16 days. The formula is right, in fact, next time yellow crosses back orange from above is more then 16 days later...

Hopefully I made my self understood.

Thanks

 

KelvinHand

Well-Known Member
#2
maybe i am wrong about your algorithm, but here for you to try:

Code:
MA1 = MA(C, 32);
MA2 = MA(C, 16);

trig = Cross(MA2, MA1);

//-- Reset the lowest low
iLlv = Null;

//---------------------------------------
// Find the LLV before the Cross Trig
//---------------------------------------

for(i=0; i<BarCount; i++)
 if (trig[i])  //at the MA cross.
 {
   ii=i; 
   iMin=9999;
   
  //Loop back 16 bars to find the lowest low
   for(j=0; j<16; j++) 
   {
      if (L[i-j]<iMin)
      {
        ii= i-j;
        iMin=L[ii];
      
      }

   } 
      illv[ii] = iMin;  // set the array of lowest low

 }



//Plot(MA1, "MA1", colorYellow, styleLine);
//Plot(MA2, "MA1", colorBlue, styleLine);

Plot(illv, "", colorGrey50 , styleArea|styleOwnScale);
 
Last edited:

Similar threads