Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

[Custom Indicator] Ninja Trader gets inresponsive and uses much more memory

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [Custom Indicator] Ninja Trader gets inresponsive and uses much more memory

    Hi there,

    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:

    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
    
        }​​

    #2
    Hello lakman184,

    From the given code the only item that I can see is using plotbrushes which will take memory to use because a unique brush is being used for each bar. There are more efficient ways to color bars if you need to color a whole chart as an example. That would be done using OnRender.

    Have you tried to comment out your code to see which specific line makes the script work the way you described? That would help to understand which part of the script is causing large memory use.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    605 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    351 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    105 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    560 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    561 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X