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

Is this correct for drawing a bar timer at current price line?

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

    Is this correct for drawing a bar timer at current price line?

    This is in the OnTimerTick handler:

    Code:
    Draw.Text(this, "NinjaScriptInfo", timeLeft, 0, Bars.GetClose(Bars.Count - 1));
    I *assume* not as it's not showing at the price line
    Last edited by funk10101; 03-02-2024, 02:21 PM.

    #2
    Hello funk10101,

    Thanks for your post and welcome to the NinjaTrader Forums!

    To clarify, are you trying to modify a copy of the BarTimer indicator that comes with NinjaTrader?

    Draw.Text() would be the correct method to use to draw text on the chart.

    To mimic the type of syntax the BarTimer Draw.TextFixed() method uses, you could use the Draw.Text() syntax below.

    Draw.Text(NinjaScriptBase owner, string tag, bool isAutoScale, string text, int barsAgo, double y, int yPixelOffset, Brush textBrush, SimpleFont font, TextAlignment alignment, Brush outlineBrush, Brush areaBrush, int areaOpacity)

    See this help guide page for more information about Draw.Text(): https://ninjatrader.com/support/help.../draw_text.htm

    I would also recommend adding prints to your script to see exactly how the code is behaving, such as printing out Bars.GetClose(Bars.Count-1) to see what price you are passing into the Draw.Text() method. Prints will appear in a New > NinjaScript Output window.

    And, keep an eye on the Log tab of the Control Center for any error messages that may appear.​

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your response. I've made the changes and printed out the results. They are correct, however for some reason, Draw.Text() is just not displaying. Draw.TextField() with the "similar" variables works as intended.

      Code:
      Draw.Text(this, "NinjaScriptInfo", false, timeLeft, 0, Bars.GetClose(Bars.Count-1), 0, Brushes.White, MyFont, System.Windows.TextAlignment.Right, Brushes.Transparent, thisBrush, 100);
                                  Print(timeLeft);​
      Does it have something to do with being inside an onTimerTick() handler or something?

      Comment


        #4
        Hello funk10101,

        Thanks for your notes.

        You will need to use the Draw.Text() syntax that allows you to pass in a DateTime value when calling this method instead of a barsAgo value.

        Draw.Text(NinjaScriptBase owner, string tag, bool isAutoScale, string text, DateTime time, double y, int yPixelOffset, Brush textBrush, SimpleFont font, TextAlignment alignment, Brush outlineBrush, Brush areaBrush, int areaOpacity)

        You could pass in Bars.GetTime(CurrentBar) in for the Datetime time argument when calling this method in the script.

        For example:

        Draw.Text(this, "NinjaScriptInfo1", true, timeLeft, Bars.GetTime(CurrentBar), Bars.GetClose(Bars.Count-1), 0, Brushes.Blue, myFont, System.Windows.TextAlignment.Left, Brushes.Transparent, Brushes.Transparent, 0);

        When testing this on my end I see the timer being drawn in the current bar at the close price of the bar.

        Please let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          BAM! Thank you.

          Comment


            #6
            Curious, it lags behind the actual current price. Looking at PriceLine I see it's calling OnRender and OnMarketData, I assume I need to refactor the timer, right?
            Last edited by funk10101; 03-04-2024, 09:13 AM.

            Comment


              #7
              Hello funk10101,

              Thanks for your notes.

              I see that the bar timer in the modified script is updating at the same time as the NinjaTrader Bar Timer indicator which is expected.

              The Draw.Text() method is being called based on the timer and will draw at the Close price calculated when the timer is triggered.

              You could consider implementing custom logic in the script to modify how often the timer is called if you want it to update differently.
              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                I've decided to modify "Priceline". I've added this where RenderTarget.DrawLine() is:

                Code:
                RenderTarget.DrawText(timeLeft, new SharpDX.DirectWrite.TextFormat(NinjaTrader.Core.Globals.DirectWriteFactory, "Arial", 12f), new RectangleF(startX, y, 150.00f, 100.00f), fgBrush.ToDxBrush(RenderTarget));
                I'm getting this error:

                Code:
                Indicator 'MyPriceLine': Error on calling 'OnRender' method on bar 17231: Object reference not set to an instance of an object.
                Any ideas?

                Comment


                  #9
                  Hello funk10101,

                  Thanks for your notes.

                  This error message "Object reference not set to an instance of an object." is advising that something you are accessing has not been created at the time you are accessing it.

                  Before calling RenderTarget.DrawText() I suggest creating the values you are trying to pass into the method prior to using them in the RenderTarget.DrawText() method.

                  For example, create a TextFormat prior to passing the value into the RenderTarget.DrawText() method.

                  The code below from the Using SharpDX for Custom Chart Rendering help guide demonstrates this.

                  Code:
                  // define the point for the text to render
                  SharpDX.Vector2 startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
                   
                  // construct the text format with desired font family and size
                  SharpDX.DirectWrite.TextFormat textFormat = new SharpDX.DirectWrite.TextFormat(Core.Globals.DirectWriteFactory, "Arial", 36);
                   
                  // construct the rectangleF struct to describe the position and size the text
                  SharpDX.RectangleF rectangleF = new SharpDX.RectangleF(startPoint.X, startPoint.Y, ChartPanel.W, ChartPanel.H);
                  
                  // define the brush used for the text
                  SharpDX.Direct2D1.SolidColorBrush customDXBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.DodgerBlue);
                  
                  // execute the render target text command with desired values
                  RenderTarget.DrawText("I am some text", textFormat, rectangleF, customDXBrush);
                   
                  
                  // always dispose of textFormat when finished
                  textFormat.Dispose();
                  
                  // always dipose of brush when finished
                  customDXBrush.Dispose();
                  ​

                  RenderTarget.DrawText(): https://ninjatrader.com/support/help...t_drawtext.htm

                  I suggest studying the Using SharpDX for Custom Chart Rendering help guide documentation below for detailed information and sample code.

                  Using SharpDX for Custom Chart Rendering: https://ninjatrader.com/support/help..._rendering.htm
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    Ok, got it! I have one last question(I believe), I'm not sure where to put the "Dispose()" calls. In my "OnRender()" I have this code which they get called immediately after displaying the text. Is this correct?:

                    Code:
                    RenderTarget.DrawLine(new SharpDX.Vector2(startX, y), new SharpDX.Vector2(endX, y), LastStroke.BrushDX, LastStroke.Width, LastStroke.StrokeStyle);
                                    RenderTarget.DrawText(timeLeft, textFormat, rectangleF, customDXBrush);
                    
                                    // always dispose of textFormat when finished
                                    textFormat.Dispose();
                    
                                    // always dipose of brush when finished
                                    customDXBrush.Dispose();​

                    Comment


                      #11
                      Hello funk10101,

                      Thanks for your notes.

                      You would dispose of the device dependent resources at the end of the OnRender() section of code so they are disposed on each render pass.

                      An example of this could be seen in the SampleCustomRender reference sample that comes with NinjaTrader.

                      To view the SampleCustomRender reference sample code, open a New > NinjaScript Editor window, open the Indicators folder, and double-click on the SampleCustomRender file.

                      Please let us know if we may assist further.
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks so much for your help. I really appreciate it.
                        Cheers!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by cre8able, Yesterday, 01:16 PM
                        3 responses
                        11 views
                        0 likes
                        Last Post cre8able  
                        Started by ChartTourist, Today, 08:22 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post ChartTourist  
                        Started by LiamTwine, Today, 08:10 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post LiamTwine  
                        Started by Balage0922, Today, 07:38 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post Balage0922  
                        Started by JoMoon2024, Today, 06:56 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post JoMoon2024  
                        Working...
                        X