Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Questions related to drawings on the chart

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

    Questions related to drawings on the chart

    Hi,

    I have 2 questions:

    Question 1:
    I had a TextBox drawn in an indicator, usually that will be removed after I remove that indicator, but sometime it is not, and the TextBox will stay there forever, if I add my indicator again, the TextBox won't behave as I intended and has no effect. Wonder if there is a way to remove that object, instead of creating a new chart?

    Question 2:
    I have a function to handle mouse click events, the function itself works fine until I added a line to draw a horizontal line, I got an error message after I click the mouse: "Unhandled exception: "barsAgo" needed to be between 0 and 76, but was 74", I am now confused, because 74 is actually between 0 and 76? Also, the Close[0] in mouse click function is not the same as the Close[0] on the chart (the value of Close[0] in OnBarUpdate() function is always correct though)

    The origin of the mouse click function was from a previous post:
    https://forum.ninjatrader.com/forum/ninjatrader-8/indicator-development/96421-draw-dot-from-indicator-owner-problem?_gl=1*172zak4*_gcl_au*MTk2MDI0Mjc3NS4xNzA5 NzU3MDM1#post792624

    protected void MouseClicked(object sender, MouseButtonEventArgs e)
    {
    // convert e.GetPosition for different dpi settings
    clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(e.Get Position(ChartPanel as IInputElement).X, ChartControl.PresentationSource);
    clickPoint.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPo sition(ChartPanel as IInputElement).Y, ChartControl.PresentationSource);

    convertedPrice = Instrument.MasterInstrument.RoundToTickSize(chartS cale.GetValueByY((float)clickPoint.Y));
    convertedTime = ChartControl.GetTimeBySlotIndex((int)ChartControl. GetSlotIndexByX((int)clickPoint.X));

    Draw.Line(this, "myTag", false, CurrentBar+2, Close[0]+5 ,CurrentBar+7, Close[0]+5, Brushes.Black, DashStyleHelper.Solid, 3, true);

    if (convertedPrice > Close[0])
    my_value_1 = 5010;

    if (convertedPrice < Close[0])
    my_value_1 = 5000;

    // trigger the chart invalidate so that the render loop starts even if there is no data being received
    ChartControl.InvalidateVisual();
    e.Handled = true;
    }
    Last edited by warpinator; 04-06-2024, 08:09 PM.

    #2
    follow up,

    found a goofy way to fix the Close[0] issue, set a global variable "double the_real_close" and assign its value as Close[0] in OnBarUpdate() function.

    And then in mouse click function, instead of comparing Close[0], I am now comparing the_real_close. Now the indicator behaves as I expected, really curious what caused the inaccuracy of Close[0] though.

    I still couldn't figure out the DrawLine() issue.

    Thanks so much
    Last edited by warpinator; 04-06-2024, 07:02 PM.

    Comment


      #3
      Hello warpinator,

      Thanks for your post.

      When you mention you "had a TextBox drawn in an indicator" are you referring to using the Draw.Text() method to draw text on a chart?

      Or, are you referring to using custom rendering to create a text box at specific X, Y coordinates?

      The following example script from our publicly available User App Share gives a good example of creating a text box with changing information:
      This indicator provides a customizable text box which displays a position&#8217;s UnRealized PnL, Realized PnL of a selected account, and the overall cash value of the account.


      Custom rendering a text box requires the use of SharpDX rendering. Here's a link to our help guide that goes over using SharpDX to custom render objects and text on your chart:


      When you mentioned you "couldn't figure out the DrawLine() issue", what exactly is the issue you are referring to?

      Is the line not being drawn on the chart as expected?

      Are you encountering an error when the Draw.Line() method is called?

      The reference sample on the forum thread link you shared demonstrates custom rendering a object on the chart using SharpDX when the mouse is clicked. The script converts the x and y to price and time and uses SharpDX to custom render an object at that price and time.

      SharpDX would need to be used in OnRender() to custom render an object on the chart. RenderTarget.DrawLine() should be used in OnRender() to custom render the line object on the chart.

      See the SharpDX for Custom Rendering help guide documentation linked above for more information.

      RenderTarget.DrawLine(): https://ninjatrader.com/support/help...t_drawline.htm

      The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

      Comment


        #4
        Sorry for the confusion Brandon, I replied my own message, should've put all information in the original post.

        The Draw.Line() issue was mentioned in the original post, in the mouse click function, after I added indicator and click the mouse button, I got an error message:
        "Unhandled exception: "barsAgo" needed to be between 0 and 76, but was 74"

        I didn't understand the error message, because number 74 is actually between 0 and 76 (there are total 77 bars in the chart where the indicator was added)

        Comment


          #5
          Hello warpinator,

          Thanks for your notes.

          Please create a reduced test script that contains only the code needed to reproduce the error and share that test script with us along with the steps to reproduce the behavior so we may investigate this matter further.

          To create a copy of your script to modify, open a New > NinjaScript Editor, select your script, right-click in the Editor, select 'Save as', name the script, and click OK.

          To export the test script go to Tools > Export > NinjaScript AddOn.

          Exporting: https://ninjatrader.com/support/help...tAsSourceFiles

          We look forward to assisting further.
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment


            #6
            Thanks Brandon,

            I attached 2 indicators that contain only the codes to reproduce errors.

            Close0 shows the Close[0] values I get from OnBarUpdate() and MouseClicked() are different.

            barsAgo shows when I tried to draw a horizontal line inside MouseClicked() function, I got an error message, same DrawLine() inside OnBarUpdate() function works fine (the short horizontal line in the screenshot)

            This information is probably irrelevant: I was doing a market reply for March 28th session, ES mini June contract
            Last edited by warpinator; 04-08-2024, 12:36 AM.

            Comment


              #7
              Hello warpinator,

              Thanks for your notes.

              When trying to download the Shared.zip file you shared I am taken to a page that states "Invalid file specified".

              Please export each test script as separate files and reshare those files along with the exact steps you are taking to reproduce the behavior so we may use those same steps to try and reproduce the behavior on our end and investigate the matter further.

              Make sure that you have debugging prints added to the test scripts as well so we see the values you are reporting in the Output window when testing the scripts.

              We look forward to further assisting.
              <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

              Comment


                #8
                Sorry Brandon,

                I must have done something wrong while uploading the files, I will upload 2 indicators in 2 separate replies, this is the first one,

                I set the chart margin to 250, so I have the room to display drawn texts.

                After I added this indicator, I saw the last close price right away, which was the Close[0] read inside OnBarUpdate() function.

                And then, I click mouse button anywhere inside the chart, I would see a second number that was the Close[0] read inside MouseClicked() function,

                Those numbers are different, the first number is keeping updated as market moves as expected, but the 2nd number never changes.

                I will attach the next indicator in another reply

                Thanks!

                Update:
                My chart was HiLo bars, after I changed to OHLC, I noticed the 2nd number might be the close value of the bar on the far left (that is why it never changes), but I double checked, I did try to get the value of Close[0] though
                Attached Files
                Last edited by warpinator; 04-08-2024, 10:41 AM.

                Comment


                  #9
                  Hi Brandon,

                  This is the 2nd indicator, I found a goofy get around by using a global variable:
                  private double the_real_last
                  that helped to get the correct close price of last bar inside MouseClicked() function, but when I tried to draw a line inside MouseClicked() function, I got an error message. Here is what I did.

                  I added the indicator, then a short horizontal line was drawn right away, that was done by Draw.Line() inside OnBarUpdate() function.

                  And then I clicked the mouse button anywhere inside the chart, and expecting a 2nd horizontal line to be drawn, the location of the 2nd line is determined whether the mouse click was above or below the last bar close price, that is done by Draw.Line() inside MouseClicked() function

                  At this point, I got an error message:
                  Unhandled exception: 'barsAgo' needed to be between 0 and 76, but was 74

                  Thanks!
                  Attached Files
                  Last edited by warpinator; 04-08-2024, 10:27 AM.

                  Comment


                    #10
                    Hello warpinator,

                    Thanks for that information.

                    You would need to surround the drawing object code in a TriggerCustomEvent() because you are using CurrentBar which is not going to be in context in the MouseClicked event driven method.

                    This also applies to using Close[0] within the MouseClicked event driven method. You would need to use TriggerCustomEvent() if you want to access Close in the MouseClicked method.

                    See this help guide page for more information about TriggerCustomEvent(): https://ninjatrader.com/support/help...ustomevent.htm

                    Further, if you want to draw a horizontal line on the chart you could consider using Draw.HorizontalLine() in the script instead of Draw.Line().

                    Draw.HorizontalLine(): https://ninjatrader.com/support/help...zontalline.htm
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #11
                      Thanks Brandon,

                      I will look into TriggerCustomEvent() documentation.

                      The reason I didn't use Draw.HorizontalLine() is because I would like to draw a line with specific starting and ending points.

                      Wonder if TriggerCustomEvent() is also related to this error message:
                      Unhandled exception: 'barsAgo' needed to be between 0 and 76, but was 74

                      If so, wonder why...

                      Thanks so much!

                      Comment


                        #12
                        Hello warpinator,

                        Thanks for your notes.

                        That is correct, Draw.Line() has a startBarsAgo and endBarsAgo passed into the method. Since you are using CurrentBar, which is not going to be in context in the MouseClicked event-driven method, you would have to surround the Draw method with TriggerCustomEvent().

                        The Draw.HorizontalLine() method does not require a barsAgo be passed into the method so you could consider using Draw.HorizontalLine() instead of Draw.Line() if that suits your use case.
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        601 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        347 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        103 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        559 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        558 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X