Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator is not visible on chart

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

    Indicator is not visible on chart

    I am here with enclosing the the code that is not showing the plot line on chart. Please suggest the remedy.

    THE CODE

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class DsClose : Indicator
    {
    private int[] fb;
    private int[] lb;
    private List<Tuple<double,long,int>> gblData;
    int latestfb=0;
    private Series<double> mydataseries;
    protected override void OnStateChange()
    {

    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "DsClose";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    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;
    AddPlot(Brushes.Lime, "Clo1");
    }
    else if (State == State.Configure)
    {
    }
    else if(State == State.DataLoaded)
    {
    #region First Last bar arrays
    Array.Resize<int>(ref fb,Count);
    Array.Resize<int>(ref lb,Count);
    for(int i=0;i<=Count-1;i++)
    {
    if(Bars.IsFirstBarOfSessionByIndex(i))
    {
    if(i==0)
    {
    fb[i]=i;lb[i]=i;
    }
    if(i>0)
    {
    fb[i]=i;
    lb[i]=lb[i-1];
    }
    }
    if(!Bars.IsFirstBarOfSessionByIndex(i))
    {
    fb[i]=fb[i-1];
    lb[i]=i;
    }

    }
    #endregion

    #region Creating a Data holder
    var Mydata = new List<Tuple<double,long,int>>();

    for(int i=0;i<=Count-1;i++)
    {
    Mydata.Add(Tuple.Create(Bars.GetClose(i), Bars.GetVolume(i), i));
    }
    gblData=Mydata;
    #endregion
    mydataseries=new Series<double>(this,MaximumBarsLookBack.TwoHundred FiftySix);

    /* #region Plot Area

    var Sorteddata=new List<Tuple<double,long,int>>();;
    for(int i=0;i<=Count-1;i++)
    {
    var templis=gblData.GetRange(fb[i],i-fb[i]+1);
    var tlis=new List<Tuple<double,long,int>>();
    tlis.AddRange(templis.OrderBy(t => t.Item1));
    var sortlist=new List<Tuple<double,long,int>>();
    long localvol=0; double localclo=0.00; int localidx=0;
    //int compare=tlis.Count();
    for(int k=0;k<tlis.Count();k++)
    {
    if(k==0)
    {
    if(tlis.Count()>1)
    {
    localclo=tlis.ElementAt(k).Item1;
    localvol=tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;
    }
    else
    {
    sortlist.Add(Tuple.Create(tlis.ElementAt(k).Item1, tlis.ElementAt(k).Item2,tlis.ElementAt(k).Item3));
    }

    }
    if(k>0 && k!=tlis.Count()-1)
    {
    if(tlis.ElementAt(k).Item1==tlis.ElementAt(k-1).Item1)
    {
    localclo=tlis.ElementAt(k).Item1;
    localvol=localvol+ tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;
    }
    else
    {
    sortlist.Add(Tuple.Create(localclo,localvol,locali dx));
    localclo=tlis.ElementAt(k).Item1;
    localvol=tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;

    }

    }
    if(k>0 && k==tlis.Count()-1)
    {
    if(tlis.ElementAt(k).Item1==tlis.ElementAt(k-1).Item1)
    {
    localclo=tlis.ElementAt(k).Item1;
    localvol=localvol+ tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;
    }
    else
    {
    sortlist.Add(Tuple.Create(localclo,localvol,locali dx));
    localclo=tlis.ElementAt(k).Item1;
    localvol=tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;
    sortlist.Add(Tuple.Create(localclo,localvol,locali dx));

    }

    }
    Sorteddata.AddRange(sortlist.OrderByDescending(t => t.Item2));
    Clo1[0]=Sorteddata.ElementAt(0).Item1;
    // var x = gblData.Count;
    }


    }

    #endregion */

    }

    }


    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.


    for(int i=0;i<=Count-1;i++)
    {
    if(Bars.IsFirstBarOfSessionByIndex(i))
    {
    if(i==0)
    {
    //fb[i]=i;lb[i]=i;
    }
    if(i>0)
    {
    //fb[i]=i;
    //lb[i]=lb[i-1];
    latestfb=i;
    }
    }

    }




    var Sorteddata=new List<Tuple<double,long,int>>();
    double val=0;
    for(int i=0;i<=Count-1;i++)
    {
    if(i>=latestfb)
    {
    var templis=new List<Tuple<double,long,int>>();
    templis=gblData.GetRange(latestfb,i-latestfb+1);
    var tlis=new List<Tuple<double,long,int>>();
    tlis.AddRange(templis.OrderBy(t => t.Item1));
    var sortlist=new List<Tuple<double,long,int>>();
    long localvol=0; double localclo=0.00; int localidx=0;
    //int compare=tlis.Count();
    for(int k=0;k<tlis.Count();k++)
    {
    if(k==0)
    {
    if(tlis.Count()>1)
    {
    localclo=tlis.ElementAt(k).Item1;
    localvol=tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;
    }
    else
    {
    sortlist.Add(Tuple.Create(tlis.ElementAt(k).Item1, tlis.ElementAt(k).Item2,tlis.ElementAt(k).Item3));
    }

    }
    if(k>0 && k!=tlis.Count()-1)
    {
    if(tlis.ElementAt(k).Item1==tlis.ElementAt(k-1).Item1)
    {
    localclo=tlis.ElementAt(k).Item1;
    localvol=localvol+ tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;
    }
    else
    {
    sortlist.Add(Tuple.Create(localclo,localvol,locali dx));
    localclo=tlis.ElementAt(k).Item1;
    localvol=tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;

    }

    }
    if(k>0 && k==tlis.Count()-1)
    {
    if(tlis.ElementAt(k).Item1==tlis.ElementAt(k-1).Item1)
    {
    localclo=tlis.ElementAt(k).Item1;
    localvol=localvol+ tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;
    sortlist.Add(Tuple.Create(localclo,localvol,locali dx));
    }
    else
    {
    sortlist.Add(Tuple.Create(localclo,localvol,locali dx));
    localclo=tlis.ElementAt(k).Item1;
    localvol=tlis.ElementAt(k).Item2;
    localidx=tlis.ElementAt(k).Item3;
    sortlist.Add(Tuple.Create(localclo,localvol,locali dx));

    }

    }
    //Sorteddata.AddRange(sortlist.OrderByDescending(t => t.Item2));
    //Clo1[0]=Sorteddata.ElementAt(0).Item1;
    // var x = gblData.Count;
    }
    Sorteddata.Clear();
    Sorteddata.AddRange(sortlist.OrderByDescending(t => t.Item2));
    val=Sorteddata.ElementAt(0).Item1;
    mydataseries[0]=val;

    Clo1[0]=val;//Sorteddata.ElementAt(0).Item1;
    //Print("Close At High Vol :" + Clo1[0]);

    }
    }
    // Clo1[0]=val; //var x = gblData.Count;
    }

    #region Properties

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Clo1
    {
    get { return Values[0]; }
    }
    #endregion

    }
    }

    #region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private DsClose[] cacheDsClose;
    public DsClose DsClose()
    {
    return DsClose(Input);
    }

    public DsClose DsClose(ISeries<double> input)
    {
    if (cacheDsClose != null)
    for (int idx = 0; idx < cacheDsClose.Length; idx++)
    if (cacheDsClose[idx] != null && cacheDsClose[idx].EqualsInput(input))
    return cacheDsClose[idx];
    return CacheIndicator<DsClose>(new DsClose(), input, ref cacheDsClose);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.DsClose DsClose()
    {
    return indicator.DsClose(Input);
    }

    public Indicators.DsClose DsClose(ISeries<double> input )
    {
    return indicator.DsClose(input);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.DsClose DsClose()
    {
    return indicator.DsClose(Input);
    }

    public Indicators.DsClose DsClose(ISeries<double> input )
    {
    return indicator.DsClose(input);
    }
    }
    }

    #endregion


    #2
    Hello appasri,

    While we cannot debug the code for you we can try to help with questions that you have about it. Are you seeing an error or is there a specific part of the logic which you found is the problem?

    If you are unsure what the problem is then you will need to reduce the code to find the problem. You can also use Print statements within your code to observe in the output window what the logic is doing.

    Comment


      #3
      Jesse thank you for the reply.
      When I lay the indicator on the chart it simply shows the text "calculating". The indicator never shows up on the chart. Please advise.

      Comment


        #4
        Here is script output. But the same values are not manifested into a line on chart.
        Attached Files

        Comment


          #5
          Hello appasri,

          That could be expected with the given logic. You are running multiple loops of all bars for every bar on the chart. You are using a 1 tick chart so that could be a huge amount of data and may take an extremely long time to load.

          For example you use the following loop 5 times total meaning the bars are being processed many more times than they usually would be:

          for(int i=0;i<=Count-1;i++)

          As an example on bar 1 this loop will execute the logic 2 times in OnBarUpdate because you used it there 2 times. On bar 2 that will execute the logic 4 times. On bar 3 it will be 6 times. If the chart had 10000 bars on the last bar the logic is executed 20,000 times. The way this is coded is extremely inefficient and will very likely have a hard to loading or working. Assuming this did eventually load it may cause the chart to lag each time a tick is received as it will loop over the bars all over again for each tick.

          You would need to re work the logic of this script to avoid using so many loops over all of the bars on the chart.

          If you need to use the loops you could try loading an extremely small amount of data, for example 10 bars and check if that works to produce a result.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          576 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          334 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          553 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X