Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plotting an indicator on the previous day's tick candles

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

    Plotting an indicator on the previous day's tick candles

    Hi,

    I have an indicator that calculates for each day a value that I want to plot from the first candle of the day until the end of the day.

    I use a tick chart (so the number of bars will change for each day).

    I tried to use a counter that resets for each session with Bars.IsFIrstBarOfSession, as well as a counter of all the bars we have loaded on the chart to have my lines plotted dynamically, but it doesn't work. If I understand correctly, CurrentBar is going from left to right, but is it updated at each new candle close Or are the values fixed 'forever'?

    Any idea of where the code is wrong?

    Code:
        public class pdh : Indicator
        {
            private int numberOfBars = 0;
            private int totalNumberOfBars = 0;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "pdh";
                    Calculate                                    = Calculate.OnBarClose;
                    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(new Stroke(Brushes.Green, DashStyleHelper.Solid, 1, 100), PlotStyle.Bar, "MyPlot");
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(null, new BarsPeriod() { BarsPeriodType = BarsPeriodType.Day, Value = 1}, 10, null, null);
    
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < 1 || CurrentBars[1] < 5) return;
                if(BarsInProgress == 1) return;
                double avgHighs = 0;
                for (int i = 0; i < 5; i++)
                {
                    avgHighs += Highs[1][i];
                }
                avgHighs = avgHighs/5;
                 ​
                if (Bars.IsFirstBarOfSession)
                 {
    
                      numberOfBars = 0;
                 }
    
                numberOfBars++;
                totalNumberOfBars++;
    
                Print("Number of bars: " + numberOfBars);
                Print("CurrentBar: " + CurrentBar);
                Draw.Line(this, "highs" + CurrentBar, false, CurrentBar - numberOfBars, avgHighs, CurrentBar, avgHighs, new SolidColorBrush(Color.FromRgb(209, 212, 220)), DashStyleHelper.Dash, 2);
    
    
            }
    
        }​

    Many thanks.

    #2
    Hello datlayzard,

    Thank you for your post.

    It looks like your Draw.Line method is drawing from CurrentBar - numberOfBars to CurrentBar.

    Draw.Line(NinjaScriptBase owner, string tag, bool isAutoScale, int startBarsAgo, double startY, int endBarsAgo, double endY, Brush brush, DashStyleHelper dashStyle, int width)

    Draw.Line() - https://ninjatrader.com/support/help.../draw_line.htm


    startBarsAgo and endBarsAgo are bars ago values, so this would be drawing from before the first bar on the chart to the first bar on the chart.

    Instead, you may want to draw from numberOfBars (startBarsAgo) to 0 (endBarsAgo).

    Please let me know if you have any other questions.

    Comment


      #3
      Thank you Gaby, it's working.

      Do you know if it can cause slow processing because it's calculating for each candle everything from 0?
      I have a pretty good computer but it feels a bit slow. Is it worth using OnRender for this type of stuff?

      Best

      Comment


        #4
        Hello,

        If you're not satisfied with the performance when creating a lot of drawing objects, directly rendering in OnRender vs using Draw objects will be faster and use less resources. However it is more complex.

        For an example script on how to render lines in OnRender, have a look at the SampleCustomRender script built into NinjaTrader 8.

        Using SharpDX for custom rendering - https://ninjatrader.com/support/help..._rendering.htm

        I also recommend reading through the section on this page called 'Using DrawObjects vs custom graphics in OnRender()' which also has a code example:



        Please let me know if you have any other questions.
        Last edited by NinjaTrader_Gaby; 11-15-2023, 01:49 PM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        574 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        333 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