Thanks for the input. At last I finally have some direction as to where to go next. Will plug away Rick
Rick Osborn ----- Original Message ---- From: gp_sydney <[EMAIL PROTECTED]> To: amibroker@yahoogroups.com Sent: Tuesday, November 20, 2007 12:44:13 AM Subject: [amibroker] Re: Struggling to Learn >From a very quick look, one issue I can see is that you're trying to use the Trend array and Period variable before you've defined their values. Your values Uptrend, Downtrend, Crossup, Crossdown, HV, and LV are all generated using only the initial values of Trend and Period. You then go and generate the Trend array data in the loop, incrementing Period as you go, but all those other variables you're using only have their initial values. To get what you want, you're going to have to do more for yourself in the loop. Since you want the HHV and LLV values over varying periods as you go along, you can't just call those functions once at the start for some initial value of Period. Similarly, you can't start checking for trend crossovers until you've actually generated the Trend array data up to that point. Essentially, all those array variables you've defined before the loop need to be filled iteratively inside the loop, with the values at each bar calculated from other values calculated at earlier bars. Once basic rule of programming: you can't use values before you've calculated them. Regards, GP --- In [EMAIL PROTECTED] ps.com, "ricko8294_98" <[EMAIL PROTECTED]> wrote: > > 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",colorYell ow,1); > Plot(C,"Close" ,colorYellow, 64); >