Hello

I am trying to learn AB and loops in particular.  There seems to be 
something I'm not getting - and I have been playing with this for the 
past several weeks.

I want to create a trend indicator that will track below an uptrend 
(x% below the highest high of the most recent "y" period) or track 
above a downtrend(x% above the lowest low of the most recent "y" 
period), where the period "y" increases each day for the duration of 
each trend up to a maximum level.  

When a cross of the price (C) and the trend occurs,(either up or 
down) the period "y" resets to zero.

My attepmt at coding this is below. For some reason, when a cross 
occurs the change doesn't happen.  And the value of the "trend" 
indicator doesn't seem to be the HHV or LLV value either.  I'm 
stumped and any help would be appreciated. 

Here is the code:

// Percentage Trend Indicator 
K = Param("% Coeff of Correlation",10,1,50,1);
MaxPeriod = Param("Trend Calculation Period",5,1,25,1);
Period = 1;
Trend = C[0];
Uptrend = C > Ref(Trend,-1); // Uptrend
Downtrend = NOT Uptrend;
CrossUP = Cross(C, Ref(Trend,-1)); // change to uptrend
CrossDown = Cross(Ref(Trend,-1),C); // change to downtrend

HV = HHV(H,Period );
LV = LLV(L,Period );
MP = MaxPeriod ;


for (i = 1; i < BarCount; i++) 
{
   if (CrossUP [i] OR CrossDown [i]){ 
   Period = 0; 
}
   if(Period < MP) 
   {
      if ( Uptrend[i])
      {
              Period ++;
         Trend[i] = HV[i] * (1-(K/100));

           }
           if (Downtrend[i])
      {
         Period ++;
         Trend[i] = LV[i] * (1+(K/100));
      }
    }   
    else if (Period >= MP)
         {
        if (Uptrend[i]) Trend[i] = HV[i] * (1-(K/100)) ; 
      if (Downtrend[i]) Trend[i] = LV[i] * (1+(K/100)) ;
    }
}
 
Plot(Trend,"Trend",colorYellow,1);
Plot(C,"Close",colorYellow,64);


Reply via email to