Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Gaby V.NinjaTrader Customer Service

    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.
        Gaby V.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Jonker, Today, 01:19 PM
        0 responses
        1 view
        0 likes
        Last Post Jonker
        by Jonker
         
        Started by futtrader, Today, 01:16 PM
        0 responses
        4 views
        0 likes
        Last Post futtrader  
        Started by Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,789 views
        0 likes
        Last Post aligator  
        Started by Jimmyk, 01-26-2018, 05:19 AM
        6 responses
        838 views
        0 likes
        Last Post emuns
        by emuns
         
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        6 responses
        3,294 views
        1 like
        Last Post jgualdronc  
        Working...
        X