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

[amibroker] Simple code problem



PureBytes Links

Trading Reference Links

Title: 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