[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Looping Problem using Array Variables



PureBytes Links

Trading Reference Links

Graham,
I have tried the different loop statements and they all give the same 
error when trying to use an Array in the statement line.

Here is the original code for the AmiBroker formula (found in the 
Members Area of the AmiBroker website).

//--------------------------------------------------
Price = ( H + L ) /2; 
Threshold = 0.002; 

Smooth = (Price + 2 * Ref( Price, -1 ) + 2 * Ref( Price, -2 ) + Ref( 
Price, -3 ) )/6; 

Length = 39; 
Value3 = 0.2; 

AvgLength = 39; 

for( Length = 39; Length >= 3; Length = Length - 2 ) 
{ 
   alpha = 2 / ( Length + 1 ); 

   Value1 = Median( Smooth, Length ); 
   Value2 = AMA( Smooth, alpha ); 

   Value3 = Nz( abs( Value1 - Value2 )/Value1 ); 

   AvgLength = IIf( Value3 > Threshold, Length, AvgLength );   
} 

alpha = 2 / (AvgLength + 1); 
Filt = AMA( Smooth, alpha ); 
Plot( C, "Price", colorBlack, styleCandle ); 
Plot( Filt, "Filt", colorRed ); 
//----------------------------------------------------

Below is the EasyLanguage code from Ehlers.  You will notice that the 
Ehlers' loop exits on the first occurence of Value3 <= Threshold.

The AmiBroker code continues to loop through the entire range of 
Length = 39 down to 3, and records the shortest Length value that 
meets the condition of Value3 > Threshold.  Reading the magazine 
article, it seems that this is what Ehlers had intended, but the 
EasyLanguage code tells a different story.

Upon closer investigation, I found that Value3 can cross under the 
Threshold more than once over the Length range in the loop. 
Therefore, the AmiBroker formula will sometimes select a lower Length 
value (always picks the shortest Length that stays above Threshold) 
than Ehlers' formula (always picks the first cross under Threshold).

As to which is better, I am not really sure, but Ehlers' AMA seems to 
flatten out more during periods of sideways price movement.


EasyLanguage Code to Compute the Median-Average Adaptive Filter
{***************************************************
         Median-Average Adaptive Filter
                  John Ehlers
****************************************************}

Inputs: Price((H+L)/2),
        Threshold(.002);

Vars: Smooth(0),
      Length(30),
      alpha(0),
      Filt(0);

Smooth = (Price + 2*Price[1] + 2*Price[2] + Price[3]) / 6;      

Length = 39;
Value3 = .2;
While Value3 > Threshold begin  
       alpha = 2 / (Length + 1);
       Value1 = Median(Smooth, Length);
       Value2 = alpha*Smooth + (1 - alpha)*Value2[1];
       If Value1 <> 0 then Value3 = AbsValue(Value1 - Value2) / 
Value1;
       Length = Length - 2;
End;                        
If Length < 3 then Length = 3;
alpha = 2 / (Length + 1);

Filt = alpha*Smooth + (1 - alpha)*Filt[1];

Plot1(Filt);


-John Ehlers

 


--- In amibroker@xxxxxxxxxxxxxxx, Graham <kavemanperth@xxxx> wrote:
> Not familiar iwth the original code you refer to so can only make
> rough statements and assistance. Best if you go back to original AB
> code, can you refer that to me. I don't know other chart pacage
> languages so not much use converting unless they are obvious. If in
> members only area of AB site, then just send the link address to me
> 
> sometimes you should use the While function/statement. Unfortunately
> not real knowledgeable on this, but maybe something
> 
> 
> While(Value3<0.002)
> {
> etc
> 
> On 7/4/05, pakaypakay <pakaypakay@xxxx> wrote:
> > Graham,
> > Thank you for trying to help with this.  I guess I didn't get far
> > enough to discover the mixed variable issue in the for statement.
> > However, I think that can be easily remedied.  The main problem 
seems
> > to be how to get any loop statement to work using this criteria 
for
> > exiting the loop: Value3 <= 0.002, where Value3 is calculated from
> > Array values.
> > 
> > I actually rewrote the code using the "for( i = Start; i < 
BarCount;
> > i++)" approach, but having this loop nested within the main loop
> > resulted in a inordinate amount of calculation time (although it 
did
> > produce the desired result).  So, I was hoping that someone has a
> > better solution.
> > 
> > The original code that I modified was taken from the S&C Trader's
> > Tips - Issue 3, 2005 (the AmiBroker code for Ehler's Median-
Average
> > Adaptive Filter).  However, that original code deviates slightly 
from
> > the algorithm presented in the S&C magazine article, shown in Easy
> > Language format.  The Amibroker code results in a substantially
> > different adaptive moving average, so I was hoping to get closer 
to
> > Ehler's version, which determines the Length value used to 
calculate
> > alpha by exiting this loop when Value3 is less than or equal to 
the
> > specified threshold of 0.002.
> > 
> > pakay
> > 
> > 
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, Graham <kavemanperth@xxxx> 
wrote:
> > > to start with compare the basic statement construction with 
those in
> > > the help files
> > >
> > > for( Value3 = 0.2; Value3 > 0.002; Length = Length - 2 )
> > > This should have the same variable in each part
> > > you cannot mix Value3 and length, so which one should it be?
> > >
> > > Then insode the loop the variables need to be identified 
against the
> > > basic for variable
> > >
> > > Perhaps you should explain what exactly you are trying to 
achieve
> > with
> > > the code so we can help with creating it
> > >
> > > On 7/4/05, pakaypakay <pakaypakay@xxxx> wrote:
> > > > Does anyone know how to work around this problem? Since the 
loop
> > > > statement does not allow array type variables (Value3), this 
code
> > > > will not work.
> > > >
> > > >
> > > > Price = ( H + L ) /2;
> > > > Smooth = (Price + 2 * Ref( Price, -1 ) + 2 * Ref( Price, -2 ) 
+
> > Ref(
> > > > Price, -3 ) )/6;
> > > >
> > > >
> > > > Length = 39;
> > > >
> > > > for( Value3 = 0.2; Value3 > 0.002; Length = Length - 2 )
> > > > {
> > > > alpha = 2 / ( Length + 1 );
> > > >
> > > > Value1 = Median( Smooth, Length );
> > > > Value2 = AMA( Smooth, alpha );
> > > >
> > > > Value3 = Nz( abs( Value1 - Value2 )/Value1 );
> > > >
> > > > }
> > > >
> > > > AvgLength = IIf( Length < 3, 3, Length );
> > > > AvgAlpha = 2 / (AvgLength + 1);
> > > >
> > > > Filt = AMA( Smooth, AvgAlpha);
> > > >
> > > > Plot( C, "Price", colorBlack, styleBar );
> > > > Plot( Filt, "Filt", colorRed, styleThick );[/code]
> > > >
> > > >
> > > >
> > > >
> > > > Please note that this group is for discussion between users 
only.
> > > >
> > > > To get support from AmiBroker please send an e-mail directly 
to
> > > > SUPPORT {at} amibroker.com
> > > >
> > > > For other support material please check also:
> > > > http://www.amibroker.com/support.html
> > > >
> > > >
> > > > Yahoo! Groups Links
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> > > --
> > > Cheers
> > > Graham
> > > http://e-wire.net.au/~eb_kavan/
> > 
> > 
> > 
> > 
> > 
> > Please note that this group is for discussion between users only.
> > 
> > To get support from AmiBroker please send an e-mail directly to
> > SUPPORT {at} amibroker.com
> > 
> > For other support material please check also:
> > http://www.amibroker.com/support.html
> > 
> > 
> > Yahoo! Groups Links
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> 
> 
> -- 
> Cheers
> Graham
> http://e-wire.net.au/~eb_kavan/




Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/