Why doesn't this work ???

Questions about MultiCharts and user contributed studies.
NW27
Posts: 177
Joined: 25 Dec 2010
Has thanked: 40 times
Been thanked: 85 times

Why doesn't this work ???

Postby NW27 » 01 Mar 2012

Hi,

I'm having a issue with this code, basically it crashes but it shouldn't.
What I've done is create a single indicator that has the capability of displaying 5 seperate moving averages. Each with it's own adjustable parameters - Type ie SMA, EMA, WMA and Price and with their own lengths. So much easier to change MA types and lengths to find best fits. and save your own prefered defaults. Setting a MA?_Length to 0 turns off this plot.

Any how, the problem -
If the length is set to 0 then the MA is not displayed. There in lies the problem. I have a 4th moving average AverageFC () and when the length is set to 0, it crashes the whole indicator and the indicator status is turned OFF.

In the following line -
If MA2_Length > 0 then // I do a check to see if the inputted length is > 0, if so, do the MA calcs.
Yet, if MA2_Length is = 0, the below line somehow still gets processed ???
If MA2_Type = "F" or MA2_Type = "f" then MA2 = AverageFC ( MA2_Price, MA2_Length ) ;
Provided you uncomment it, it will generate the error. Even though the code should not enter this area because of the "If MA2_Length > 0 then" statement.

Why is it so?
Am I just having a bad night :)

Neil.

P.S. Anybody have some other MA types to add? ie XMA? (Need the code as well)

Code: Select all

{
Type - Indicator
Name - NWT Mov Avg S,E,W
Desc - My own version of Moving Averages that allows easy changing from Simple to Exponential to Weighted and FC what ever that is

Written by Neil Wrightson

Version Date Reason
1 01/03/2012 Start
}inputs:
MA1_Type("E"), MA1_Price( Close ), MA1_Length( 8 ),
MA2_Type("E"), MA2_Price( Close ), MA2_Length( 0 ),
MA3_Type("E"), MA3_Price( Close ), MA3_Length( 0 ),
MA4_Type("E"), MA4_Price( Close ), MA4_Length( 0 ),
MA5_Type("E"), MA5_Price( Close ), MA5_Length( 0 ),
Displace( 0 ) ;

variables:
MA1( 0 ), MA2( 0 ), MA3( 0 ), MA4( 0 ), MA5( 0 );

If MA1_Length > 0 then
Begin
MA1 = XAverage( MA1_Price, MA1_Length ) ;
If MA1_Type = "S" or MA1_Type = "s" then MA1 = Average( MA1_Price, MA1_Length ) ;
If MA1_Type = "E" or MA1_Type = "e" then MA1 = XAverage( MA1_Price, MA1_Length ) ;
If MA1_Type = "W" or MA1_Type = "e" then MA1 = WAverage( MA1_Price, MA1_Length ) ;
// If MA1_Type = "F" or MA1_Type = "f" then MA1 = AverageFC( MA1_Price, MA1_Length ) ;
End;

If MA2_Length > 0 then
begin
MA2 = XAverage( MA2_Price, MA2_Length ) ;
If MA2_Type = "S" or MA2_Type = "s" then MA2 = Average( MA2_Price, MA2_Length ) ;
If MA2_Type = "E" or MA2_Type = "e" then MA2 = XAverage( MA2_Price, MA2_Length ) ;
If MA2_Type = "W" or MA2_Type = "w" then MA2 = WAverage( MA2_Price, MA2_Length ) ;
// If MA2_Type = "F" or MA2_Type = "f" then MA2 = AverageFC ( MA2_Price, MA2_Length ) ;
End;

If MA3_Length > 0 then
Begin
MA3 = XAverage( MA3_Price, MA3_Length ) ;
If MA3_Type = "S" or MA3_Type = "s" then MA3 = Average( MA3_Price, MA3_Length ) ;
If MA3_Type = "E" or MA3_Type = "e" then MA3 = XAverage( MA3_Price, MA3_Length ) ;
If MA3_Type = "W" or MA3_Type = "w" then MA3 = WAverage( MA3_Price, MA3_Length ) ;
// If MA3_Type = "F" or MA3_Type = "f" then MA3 = AverageFC ( MA3_Price, MA3_Length ) ;
End;

If MA4_Length > 0 then
Begin
MA4 = XAverage( MA4_Price, MA4_Length ) ;
If MA4_Type = "S" or MA4_Type = "s" then MA4 = Average( MA4_Price, MA4_Length ) ;
If MA4_Type = "E" or MA4_Type = "e" then MA4 = XAverage( MA4_Price, MA4_Length ) ;
If MA4_Type = "W" or MA4_Type = "w" then MA4 = WAverage( MA4_Price, MA4_Length ) ;
// If MA4_Type = "F" or MA4_Type = "f" then MA4 = AverageFC ( MA4_Price, MA4_Length ) ;
End;

If MA5_Length > 0 then
Begin
MA5 = XAverage( MA5_Price, MA5_Length ) ;
If MA5_Type = "S" or MA5_Type = "s" then MA5 = Average( MA5_Price, MA5_Length ) ;
If MA5_Type = "E" or MA5_Type = "e" then MA5 = XAverage( MA5_Price, MA5_Length ) ;
If MA5_Type = "W" or MA5_Type = "w" then MA5 = WAverage( MA5_Price, MA5_Length ) ;
// If MA5_Type = "F" or MA5_Type = "f" then MA5 = AverageFC ( MA5_Price, MA5_Length ) ;
End;

condition1 = Displace >= 0 or CurrentBar > AbsValue( Displace ) ;
if condition1 then
begin
If MA1_Length > 0 then Plot1[Displace]( MA1, "MA1" ) else NoPlot(1);
If MA2_Length > 0 then Plot2[Displace]( MA2, "MA2" ) else NoPlot(2);
If MA3_Length > 0 then Plot3[Displace]( MA3, "MA3" ) else NoPlot(3);
If MA4_Length > 0 then Plot4[Displace]( MA4, "MA4" ) else NoPlot(4);
If MA5_Length > 0 then Plot5[Displace]( MA5, "MA5" ) else NoPlot(5);
end ;

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Why doesn't this work ???

Postby furytrader » 01 Mar 2012

I am getting the same error, which is odd because it shouldn't be trying to calculate AverageFC when it is not enabled to do so. It's like MultiCharts is calculating it no matter what.

One thing to note - I don't know whether there's much point in using AverageFC since it generates the same result as Average - which you already have in your code. It's not a different value, just a different way of calculating that same value. Given the speed of today's computers, I'm not sure if there is much difference performance-wise between the two, but I may be wrong.

That being said, it would be helpful to hear from MC why this is happening.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Why doesn't this work ???

Postby Henry MultiСharts » 02 Mar 2012

AverageFC is a series function, it is calculated outside of the script body.
As you are using Len=0 input then division by zero inside the function causes floating point exception.
You need to replace the AverageFC function with a custom one that will be ready to avoid division by zero.
For example:

Code: Select all

inputs:
PriceValue( numericseries ),
Len( numericsimple ) ;
if(len <> 0) then begin
AverageFC2 = SummationFC( PriceValue, Len ) / Len ;
end
else AverageFC2 = 0;
You need to specify the value that the function will return in this case:

Code: Select all

else AverageFC2 = 0;

NW27
Posts: 177
Joined: 25 Dec 2010
Has thanked: 40 times
Been thanked: 85 times

Re: Why doesn't this work ???

Postby NW27 » 02 Mar 2012

AverageFC is a series function, it is calculated outside of the script body.
As you are using Len=0 input then division by zero inside the function causes floating point exception.
You need to replace the AverageFC function with a custom one that will be ready to avoid division by zero.
For example:

Code: Select all

inputs:
PriceValue( numericseries ),
Len( numericsimple ) ;
if(len <> 0) then begin
AverageFC2 = SummationFC( PriceValue, Len ) / Len ;
end
else AverageFC2 = 0;
You need to specify the value that the function will return in this case:

Code: Select all

else AverageFC2 = 0;
But Henry,
It should not be executed at all. The test in the main indicator "If MA2_Length > 0 then "
Stops it from going to this part of the code. Or at least it should ???
Why is it not ???

Neil.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Why doesn't this work ???

Postby Henry MultiСharts » 05 Mar 2012

AverageFC is a series function.
The nature of the series function is to be calculated on each bar of the data series.
If the condition is not met, the function is forced to be calculated outside of the script body.


Return to “MultiCharts”