Code - Loop Error?

Questions about MultiCharts and user contributed studies.
Laurent
Posts: 159
Joined: 20 Nov 2010
Location: France
Has thanked: 76 times
Been thanked: 32 times

Code - Loop Error?

Postby Laurent » 28 May 2011

Here are 2 pieces of code that should plot moving averages on a chart (from Avg1 to Avg20)

I have a little problem.. These codes do not produce the same results/values on a chart? I'm doing something wrong with my loop???

Code: Select all

Inputs:
   Series ( Close );

Variables:
   Avg1 ( 1 ),
   Avg2 ( 2 ),
   Avg3 ( 3 ),
   Avg4 ( 4 ),
   Avg5 ( 5 ),
   Avg6 ( 6 ),
   Avg7 ( 7 ),
   Avg8 ( 8 ),
   Avg9 ( 9 ),
   Avg10 ( 10 ),
   Avg11 ( 11 ),
   Avg12 ( 12 ),
   Avg13 ( 13 ),
   Avg14 ( 14 ),
   Avg15 ( 15 ),
   Avg16 ( 16 ),
   Avg17 ( 17 ),
   Avg18 ( 18 ),
   Avg19 ( 19 ),
   Avg20 ( 20 );
      
Avg1  = XAverage(Series, 1);
Avg2  = XAverage(Series, 2);
Avg3  = XAverage(Series, 3);
Avg4  = XAverage(Series, 4);
Avg5  = XAverage(Series, 5);
Avg6  = XAverage(Series, 6);
Avg7  = XAverage(Series, 7);
Avg8  = XAverage(Series, 8);
Avg9  = XAverage(Series, 9);
Avg10 = XAverage(Series, 10);
Avg11 = XAverage(Series, 11);
Avg12 = XAverage(Series, 12);
Avg13 = XAverage(Series, 13);
Avg14 = XAverage(Series, 14);
Avg15 = XAverage(Series, 15);
Avg16 = XAverage(Series, 16);
Avg17 = XAverage(Series, 17);
Avg18 = XAverage(Series, 18);
Avg19 = XAverage(Series, 19);
Avg20 = XAverage(Series, 20);

Plot1(Avg1, "Avg1");
Plot2(Avg2, "Avg2");
Plot3(Avg3, "Avg3");
Plot4(Avg4, "Avg4");
Plot5(Avg5, "Avg5");
Plot6(Avg6, "Avg6");
Plot7(Avg7, "Avg7");
Plot8(Avg8, "Avg8");
Plot9(Avg9, "Avg9");
Plot10(Avg10, "Avg10");
Plot11(Avg11, "Avg11");
Plot12(Avg12, "Avg12");
Plot13(Avg13, "Avg13");
Plot14(Avg14, "Avg14");
Plot15(Avg15, "Avg15");
Plot16(Avg16, "Avg16");
Plot17(Avg17, "Avg17");
Plot18(Avg18, "Avg18");
Plot19(Avg19, "Avg19");
Plot20(Avg20, "Avg20");


Code: Select all

Inputs:
   Series ( Close );

Variables:
   Index ( 0 ),
   ShortColor ( Green ),
   LongColor ( Red );

Arrays:
   AvgArr[20]( 0 ),
   ColorArr[20]( 0 );

For Index = 1 to 20
Begin
   AvgArr[Index] = XAverage(Series, Index);
   ColorArr[Index] = GradientColor(Index, 1, 20, ShortColor, LongColor);
End ;

Plot1(AvgArr[1], "AvgArr[1]", ColorArr[1]);
Plot2(AvgArr[2], "AvgArr[2]", ColorArr[2]);
Plot3(AvgArr[3], "AvgArr[3]", ColorArr[3]);
Plot4(AvgArr[4], "AvgArr[4]", ColorArr[4]);
Plot5(AvgArr[5], "AvgArr[5]", ColorArr[5]);
Plot6(AvgArr[6], "AvgArr[6]", ColorArr[6]);
Plot7(AvgArr[7], "AvgArr[7]", ColorArr[7]);
Plot8(AvgArr[8], "AvgArr[8]", ColorArr[8]);
Plot9(AvgArr[9], "AvgArr[9]", ColorArr[9]);
Plot10(AvgArr[10], "AvgArr[10]", ColorArr[10]);
Plot11(AvgArr[11], "AvgArr[11]", ColorArr[11]);
Plot12(AvgArr[12], "AvgArr[12]", ColorArr[12]);
Plot13(AvgArr[13], "AvgArr[13]", ColorArr[13]);
Plot14(AvgArr[14], "AvgArr[14]", ColorArr[14]);
Plot15(AvgArr[15], "AvgArr[15]", ColorArr[15]);
Plot16(AvgArr[16], "AvgArr[16]", ColorArr[16]);
Plot17(AvgArr[17], "AvgArr[17]", ColorArr[17]);
Plot18(AvgArr[18], "AvgArr[18]", ColorArr[18]);
Plot19(AvgArr[19], "AvgArr[19]", ColorArr[19]);
Plot20(AvgArr[20], "AvgArr[20]", ColorArr[20]);

User avatar
TJ
Posts: 6553
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1893 times

Re: Code - Loop Error?

Postby TJ » 28 May 2011

Laurent wrote:Here are 2 pieces of code that should plot moving averages on a chart (from Avg1 to Avg20)

I have a little problem.. These codes do not produce the same results/values on a chart? I'm doing something wrong with my loop???
...


XAverage uses short cut to derive its calculation.

as such, it should not be used inside a function, or anything that can cause it to lose its internal reference.

ie. Use it as is.


ps. try simple moving average, it does not use short cut, so it will work fine.
but if you use averageFC, which uses short cut like Xaverage, you will notice the same problem as above.

Laurent
Posts: 159
Joined: 20 Nov 2010
Location: France
Has thanked: 76 times
Been thanked: 32 times

Re: Code - Loop Error?

Postby Laurent » 29 May 2011

How to know which function is "safe"?

I'm surprised by what you say, there is a lot of functions that use XAverage in them?
(Ex: Zero Lag EMA). They're all weak?

SP
Posts: 444
Joined: 06 Feb 2006
Has thanked: 36 times
Been thanked: 280 times

Re: Code - Loop Error?

Postby SP » 29 May 2011

The general rule is:
Dont use series functions into loops ( for....begin, while ....begin, repeat....until), all series functions return wrong values if used in loops.

To see if a built-in function is simple or series, you can open the function and right click on Properties in the PLE Editor and take a look what is marked under Function Storage.

If it is series you need to put the original formula into the loop, see like the loop in the Mov Avg Exp Ribbon is calculated.
These users thanked the author SP for the post (total 2):
LaurentStan Bokov

Laurent
Posts: 159
Joined: 20 Nov 2010
Location: France
Has thanked: 76 times
Been thanked: 32 times

Re: Code - Loop Error?

Postby Laurent » 29 May 2011

Ok thanks SP! :)

User avatar
TJ
Posts: 6553
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1893 times

Re: Code - Loop Error?

Postby TJ » 29 May 2011

Laurent wrote:How to know which function is "safe"?

I'm surprised by what you say, there is a lot of functions that use XAverage in them?
(Ex: Zero Lag EMA). They're all weak?



Zero Lag EMA does not have XAverage in a function,
study the code again to see the difference.


Return to “MultiCharts”