First off, a big thanks to your efforts on this forum, without your responses to the various questions my automated strategy development wouldn't have got off the ground, so thanks! Compared to the likes of metatrader, you are seriously good!
So to my issue: I'm trying to write a custom indicator, and whenever I attach it to a chart I get the error: "Error on calling 'OnBarUpdate' method for indicator 'CustomRSI' on bar 0: Object reference not set to an instance of an object."
The code is simply declaring a two-dimensional array, and then filling it with OHLC data. If this is causing the error, I'd love to know a better way to do this. I'm a relatively experienced C++ programmer, and have done a course in C#, but it's fair to say I'm new to ninjatrader.
I've tried a 'try catch' setup but don't seem to get anything out of it when it runs.
Code posted below:
namespace NinjaTrader.Indicator
{
/// <summary>
/// CustomRSI indicator to feed master indicator
/// </summary>
[Description("CustomRSI indicator to feed master indicator")]
public class CustomRSI : Indicator
{
#region Variables
// Wizard generated variables
private int rSI_Buy_Variable = 1; // Default setting for RSI_Buy_Variable
double[,] afMultiVarCalculationArray = new double[1000,221];
int CalculationStartingPoint= 1000;
int NumberOfMultiCalcArrayCols = 221;
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
Overlay = false;
for(int i=0;i<CalculationStartingPoint;i++)
{
for(int j=0;j<NumberOfMultiCalcArrayCols;j++)
{
afMultiVarCalculationArray[i,j] = 0;
}
}
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if(CurrentBar < CalculationStartingPoint)
{
Print("Not enough bars to calculate");
return;
}
int first;
first=CurrentBar - CalculationStartingPoint -1; // start index for all the bar, the "-1" converts to array position
for(int i=0;i<CalculationStartingPoint;i++)
{
afMultiVarCalculationArray[i,1]=(float)Close[first+i];
afMultiVarCalculationArray[i,2]=(float)High[first+i];
afMultiVarCalculationArray[i,3]=(float)Low[first+i];
afMultiVarCalculationArray[i,4]=(float)Open[first+i];
afMultiVarCalculationArray[i,5]=(float)Volume[first+i];
}
for(int i=0;i<CalculationStartingPoint;i++)
for(int j=7;j<NumberOfMultiCalcArrayCols;j++)
afMultiVarCalculationArray[i,j]=0;
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
Plot0.Set(Close[0]);
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot0
{
get { return Values[0]; }
}
[Description("")]
[GridCategory("Parameters")]
public int RSI_Buy_Variable
{
get { return rSI_Buy_Variable; }
set { rSI_Buy_Variable = Math.Max(1, value); }
}
#endregion
}
}
Comment