I am trying to move my methods to a global class so I can reuse them.
I have structured it as per an example by NT support.
I've run into 2 problems:
1. If I declare the method (CheckMACD) as static, the compiler complains that MACD needs to be called from an object reference.
2. So I declare it as a regular public int. The problem is now that on calling MACD, a runtime error occurs: System.NullReferenceException: 'Object reference not set to an instance of an object.'
Note that CurrentBar for some reason has a value of -1. I'm thinking that is the problem as I'm trying to access index of [0]?
However if I code and wait until CurrentBar > 2, the code never runs as CurrentBar is always -1.
The method is called like this:
test = cmsGlobals.CheckMACD(Close, MACDFastLength, MACDSlowLength, MACDLength, MACDThreshold);
Many Thanks, Caesar.
The code is as follows (Note that there is no issue in calling CalculateDelta):
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class cmsGlobals : Indicator
{
// This double can be accessed from within an indicator with MySharedMethods.CalculateDelta()
// or can be accessed from any script using NinjaTrader.NinjaScript.Indicators.MySharedMethods.CalculateDelta()
public static double CalculateDelta(double firstPrice, double secondPrice)
{
return Math.Abs(firstPrice - secondPrice);
}
public int CheckMACD(ISeries<double> Price, int FastLength, int SlowLength, int MACDLength, double MACDThreshold)
{
int retval = 0;
double MyMACD = 0;
double MACDAvg = 0;
double MACDDiff0 = 0;
double MACDDiff1 = 0;
int myCurrentBar = 0;
myCurrentBar = CurrentBar;
MACDDiff0 = MACD(Price, FastLength, SlowLength, MACDLength).Diff[0];
MACDDiff1 = MACD(Price, FastLength, SlowLength, MACDLength).Diff[1];
if (Math.Abs(MACDDiff1) < Math.Abs(MACDDiff0))
{
if (MACDDiff0 > MACDThreshold)
retval = 1;
else
if (MACDDiff0 < -(1 * MACDThreshold))
retval = -1;
else
retval = 0;
}
return retval;
}

Comment