Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Find Horizontal Midpoint of Chart

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

    Find Horizontal Midpoint of Chart

    Hello,
    I'm modifying an NT drawing tool. For debugging I need to draw a horizontal line on a chart from the drawing tool modification. I tried to access High[0] and Low[0] but that doesn't compile. So, how can I find the chart horizontal midpoint to use with Draw.HorizontalLine()?
    Thanks in Advance.

    #2
    Hi ramckay,

    Please put the code below in the OnRender() function:

    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
        if (ChartControl == null) return;
    
        double middle = (chartScale.MaxValue + chartScale.MinValue) / 2;
    
        Draw.HorizontalLine(this, "test", middle, Brushes.Cyan);
    
        base.OnRender(chartControl, chartScale);
    
        if (IsInHitTest) return;
    }

    Regards,
    William
    ninZa
    NinjaTrader Ecosystem Vendor - ninZa.co

    Comment


      #3


      Hi William,
      I put the code in OnRender() and the compiler is complaining about 'this' in Draw.Horizontal(). Error is:

      Argument1: cannot convert from 'NinjaTrader.NinjaScript.DrawingTools.TestLoseFocu s' to 'NinjaTrader.NinjaScript.NinjaScriptBase'.

      I tried:
      'NinjaTrader.NinjaScript.NinjaScriptBase.this' - compiler complains.
      'Draw.HorizontalLine((NinjaTrader.NinjaScript.Ninj aScriptBase)this, "test", middle, Brushes.Cyan);' - compiler complains.

      Is there a using namespace that needs to be included?

      Thanks for the help. I'm in new territory with DrawingTools. You're saving me hours of research.
      Rod


      Click image for larger version

Name:	Screenshot (37).png
Views:	152
Size:	303.5 KB
ID:	1315338

      Comment


        #4
        Hi ramckay,

        You can use the RenderTarget.DrawLine() function from the SharpDX library to draw a horizontal line on a chart.

        Please place the following code inside the OnRender() function:

        Code:
        protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
            if (ChartControl == null) return;
        
            double middle = (chartScale.MaxValue + chartScale.MinValue) / 2;
        
            float y = chartScale.GetYByValue(middle);
            int largeNumber = (int)Math.Pow(10, 8);
            float x1 = ChartControl.GetXByBarIndex(ChartBars, ChartBars.FromIndex) + largeNumber;
            float x2 = ChartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex) - largeNumber;
        
            SharpDX.Vector2 point0 = new SharpDX.Vector2(x1, y);
            SharpDX.Vector2 point1 = new SharpDX.Vector2(x2, y);
        
            RenderTarget.DrawLine(point0, point1, Brushes.Yellow.ToDxBrush(RenderTarget));
            base.OnRender(chartControl, chartScale);
        
            if (IsInHitTest) return;
        }
        Regards,
        William
        Last edited by ninZa; 08-23-2024, 04:57 AM.
        ninZa
        NinjaTrader Ecosystem Vendor - ninZa.co

        Comment


          #5
          Hi William,
          I added the code to OnRender and then selected the name of the drawing tool from the Drawing Tool menu. A line is not drawn. I set breakpoint in OnRender and the process ends up in Dispatcher.cs at this while block:
          Code:
                          while (frame.Continue && GetMessage(ref msg, IntPtr.Zero, 0, 0))
                          {
                              TranslateAndDispatchMessage(ref msg);
                          }
                          if (_frameDepth == 1 && _hasShutdownStarted)
                          {
                              ShutdownImpl();
                          }
          ​
          I don't know what it is waiting for. I do not have any drawing methods in the drawing tool I'm working with so I'm guessing Dispatcher is waiting for a mouse point. If that sounds plausible I'll copy the methods for drawing a horizontal line into the Drawing Tool to give the disatcher something to work with.
          Thanks,
          Rod

          Comment


            #6
            Hi William,
            I added the code to and existing and working Drawing Tool and it works as advertised - horizontal line in the middle of the chart.
            I appreciate the help.
            Thanks,
            Rod

            Comment


              #7
              Hi ramckay,
              You are welcome!
              I'm glad to hear that I helped solve your problem.

              Regards,
              William

              ninZa
              NinjaTrader Ecosystem Vendor - ninZa.co

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              612 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              355 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              105 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              561 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              564 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X