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

RE: [amibroker] Simple code problem



PureBytes Links

Trading Reference Links

Title: Simple code problem

Hi Tomasz,

 

Thanks for that explanation.

 

So what seems to happen is that the price is drawn on the chart.  Then the results in the results array is drawn.  So every time I code something like this, the result needs to be in an array?  If that is correct, how can you say plot a bar or candlestick which is a moving average?  i.e the open, high, low, and close are all a moving average and then plotted as a price bar.  Maybe there is a function that will draw the bar.

 

Thanks

 

Rush

 


From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of Tomasz Janeczko
Sent: 26 March 2006 11:49
To: amibroker@xxxxxxxxxxxxxxx
Subject: Re: [amibroker] Simple code problem

 

Hello,

 

You are trying to write loop code but you are using array functions (i.e. Ref). This does not make too much sense.

If you are writing looping code, you should reference previous values using subscript operators ( [] )

 

Proper way to code simple moving average from the scratch looks like this:

 

 

Periods = Param("Periods", 10, 2, 200, 1, 1 );  

result = Null;
// initialize with null

// we start calculations from 'Periods' bar
// previous bars are set to null

for ( i = Periods; i < BarCount; i++ )
{
  
// in the inner- loop we calculate average for last 'Periods' of bars
   tempsum =
0;
  
for ( j = 0; j < Periods; j++ )
   {
      tempsum = tempsum + Close[ i - j ];
   }

   
// store sum/periods in appropriate array entry
   result[ i ] = tempsum / Periods;
}

Plot( result, "MA", ParamColor( "Color", colorCycle ), ParamStyle("Style"));


Best regards,
Tomasz Janeczko
amibroker.com

----- Original Message -----

From: Rush

Sent: Sunday, March 26, 2006 9:18 AM

Subject: [amibroker] Simple code problem

 

Hi,

This is my first attempt at using the built in language to program.  I wrote the code below, a simple moving average, which looks correct but only works correctly when “Periods” has a value of 3.  Any other value and the resulting line has a much greater value than the closing prices.  I know there are functions to do this, but I’m just trying to make an example to teach myself.

        Periods = Param("Periods", 10, 2, 200, 1, 1 ); 

        n = 0;

        for ( i = 0; i < BarCount; i++ ){

            if( i > Periods ){

                for ( j = i - (Periods-1); j <= Periods; j++ ){

                    n = n + Ref(Close,-j);

                }

            }

        }

        n = n / Periods;

        Plot( n, "MA", ParamColor( "Color", colorCycle ), ParamStyle("Style"));

However, it seems more sense to me to code it the following way, which slows the software down and also ends up with the wrong values.  Am I missing something with the way the flow of the code works in AmiBroker.

        Periods = Param("Periods", 10, 2, 200, 1, 1 );

        for ( i = 0; i < BarCount; i++ ){

            if( i > Periods ){

                n = 0;

                for ( j = Periods; j >= 0; j-- ){

                    n = n + Ref(Close,-j);

        }

                n = n / Periods;

                Plot( n, "MA", ParamColor( "Color", colorCycle ), ParamStyle("Style"));

            }

        }

Thanks





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