I have developed a custom indicator that is causing a massive problem.
When I apply the indicator to the chart template of my current workspace (25 futures, time frame daily), the memory usage increases by 100% from ~5GB to ~10GB. Also, when I re-apply the indicator to the chart or press F5 (Reload Ninja Script) on a chart, NT becomes unresponsive and takes more than 30 (up to a minute) seconds to reload the chart.
If I remove the indicator from my chart template, the template takes only seconds to reload.
Something seems to be very badly coded but I have no clue what it is.
Does anyone see anything that is really badly implemented? Any help would be greatly appreciated.
Here is the code:
public class IW_Momentum : Indicator
{
private Series<double> AccumDistSeries;
private Series<double> IWPositiveSeries;
private Series<double> IWMomentumSeries;
double accumDistTemp;
SMA sma_3;
SMA sma_10;
SMA sma_iwpos_3;
SMA sma_iwpos_10;
SMA sma_iwmomentum_15;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "IW_Momentum";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
StrokePlot = new Stroke(Brushes.RoyalBlue, DashStyleHelper.Solid, 3, 100);
StylePlot = PlotStyle.Line;
}
else if (State == State.Configure)
{
AddPlot(StrokePlot, StylePlot, "IW_Momentum");
AddLine(new Stroke(Brushes.Silver, DashStyleHelper.Solid, 1, 66), 0, "Nullline");
}
else if (State == State.DataLoaded)
{
AccumDistSeries = new Series<double>(this);
IWPositiveSeries = new Series<double>(this);
IWMomentumSeries = new Series<double>(this);
}
else if (State == State.Historical)
{
sma_3 = SMA(3);
sma_10 = SMA(10);
sma_iwpos_3 = SMA(IWPositiveSeries, 3);
sma_iwpos_10 = SMA(IWPositiveSeries, 10);
sma_iwmomentum_15 = SMA(IWMomentumSeries, 15);
}
}
private double TrueHigh()
{
return Math.Max(Close[1], High[0]);
}
private double TrueLow()
{
return Math.Min(Close[1], Low[0]);;
}
private double AccumDist()
{
if (Close[0] > Close[1])
{
accumDistTemp = Close[0] - TrueLow();
}
else if (Close[0] < Close[1])
{
accumDistTemp = Close[0] - TrueHigh();
}
else
{
accumDistTemp = 0;
}
return accumDistTemp;
}
private double IWPositive()
{
return AccumDistSeries[0] + (100 - MIN(AccumDistSeries, CurrentBar)[0]);
}
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
{
AccumDistSeries[0] = 0;
IWPositiveSeries[0] = 0;
IWMomentumSeries[0] = 0;
return;
}
AccumDistSeries[0] = AccumDistSeries[1] + AccumDist();
IWPositiveSeries[0] = IWPositive();
IWMomentumSeries[0] = (((( sma_3[0] - sma_10[0] ) / sma_3[0] ) * 100 +
(( sma_iwpos_3[0] - sma_iwpos_10[0] ) / sma_iwpos_3[0] ) * 100 ) / 2 );
//Print(String.Format("Date: {0}, Accum: {1}", Time[0], IWPositive()));
Value[0] = IWMomentumSeries[0] - sma_iwmomentum_15[0];
if (Value[0] > 0)
{
PlotBrushes[0][0] = Brushes.ForestGreen;
}
if (Value[0] < 0)
{
PlotBrushes[0][0] = Brushes.Red;
}
//Print(String.Format("Date: {0}, Accum: {1}", Time[0], Value[0]));
//Print(String.Format("Date: {0}, SMA: {1}", Time[0], sma_iwmomentum_15[0]));
}
#region Properties
[NinjaScriptProperty]
[Display(Name="Stroke Cot", Order=1, GroupName="2 - Cot Plot")]
public Stroke StrokePlot
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Plot Style", Order=2, GroupName="2 - Cot Plot")]
public PlotStyle StylePlot
{ get; set; }
#endregion
}

Comment