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 ;