• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Absolute and fixed y locations for lines and text

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

    Absolute and fixed y locations for lines and text

    I would like to draw lines as illustrated in the attached diagram.
    I want the y axis value to be an absolute screen location.
    So for example the y location for the purple line is 95% of screen height and the yellow at 85%. Same for the texts "1" and "2" and their enclosing boxes. I want the lines to scroll left of right with the chart bars but always stay fixed at their absolute y screen values.
    at say 50 and 100 pixels for top of screen and not move with vertical up or down scrolling Is this possible and if yes how?
    Thanks for your help

    #2
    Hello shinuvu, thanks for your post.

    You can use ChartControl.PresentationSource to get absolute device coordinates. The sample we provide on custom rendering, "SampleCustomRender" has a large circle that is drawn in terms of device coordinates. That code starts at line 187 of the script.

    Best regards.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for that . I have made some progress. What I need to know now is for this variable

      SharpDX.Vector2 startPoint

      how can I set startPoint.X corresponding to a specific bar - ideally by bar/index number

      Can I also set startPoint.Y from a specific bar high/low value?

      Comment


        #4
        Hello shinuvu, thanks for your reply.

        To get an x index by bar number, use GetXByBarIndex(). For the Y start point, use GetYByValue() and give it something like Bars.GetClose(Bars.Count - 1) for the most recent close price.

        Please let me know if I can assist any further.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris
          Need a little more help.

          I have exactly want I want (see below) EXCEPT its drawing in an indicator window and I want it drawn on the main price chart. How do I do that ? Will you need to see my code?

          Here is a snippet.

          protected override void OnRender(ChartControl chartControl, ChartScale chartScale_)
          {

          SharpDX.Vector2 startPoint;
          SharpDX.Vector2 endPoint;

          startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
          endPoint = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);

          // These Vector2 objects are equivalent with WPF System.Windows.Point and can be used interchangeably depending on your requirements
          // For convenience, NinjaTrader provides a "ToVector2()" extension method to convert from WPF Points to SharpDX.Vector2
          SharpDX.Vector2 startPoint1 = new System.Windows.Point(ChartPanel.X, ChartPanel.Y + ChartPanel.H).ToVector2();
          SharpDX.Vector2 endPoint1 = new System.Windows.Point(ChartPanel.X + ChartPanel.W, ChartPanel.Y).ToVector2();

          // SharpDX.Vector2 objects contain X/Y properties which are helpful to recalculate new properties based on the initial vector
          float width = endPoint.X - startPoint.X;
          float height = endPoint.Y - startPoint.Y;


          drawMyLine(1, chartControl, mc1Brush.ToDxBrush(RenderTarget), .35, height, 100, 110, 1);
          drawMyLine(1, chartControl, mc1Brush.ToDxBrush(RenderTarget), .25, height, 100, 110, 1);
          drawMyLine(1, chartControl, mc1Brush.ToDxBrush(RenderTarget), .15, height, 100, 110, 1);
          drawMyLine(1, chartControl, mc1Brush.ToDxBrush(RenderTarget), .05, height, 100, 110, 1);

          .......................

          void drawMyLine(int isTop, ChartControl chartControl, SharpDX.Direct2D1.Brush brush, double percent, double height, int x1, int x2, int text)
          {

          SharpDX.Vector2 startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
          SharpDX.Vector2 endPoint = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);

          SharpDX.Vector2 startPointLeftV = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y); //Left vertical bar
          SharpDX.Vector2 endPointLeftV = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H); //Left vertical bar
          SharpDX.Vector2 startPointRightV = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y); //Right vertical bar
          SharpDX.Vector2 endPointRightV = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H); //Right vertical bar


          SharpDX.Vector2 rectPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y); //Right vertical bar

          startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
          endPoint = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);


          float startingX = startPoint.X;
          float startingY = startPoint.Y;

          double x1Coordinate = chartControl.GetXByBarIndex(ChartBars, x1);

          startPoint.X = (float)x1Coordinate;

          double x2Coordinate = chartControl.GetXByBarIndex(ChartBars, x2);

          double halfway = (x2Coordinate - x1Coordinate)/ 2 + x1Coordinate;

          endPoint.X = (float)x2Coordinate;

          float endingX = endPoint.X;
          float endingY = startingY;


          double percentMTLine = percent * (double)height;


          SharpDX.Vector2 startPoint2 = startPoint;
          SharpDX.Vector2 endPoint2 = endPoint;

          startPoint2.Y = startingY;
          startPoint2.X = startingX;

          startPoint.Y = startPoint.Y + (float)percentMTLine;
          endPoint.Y = startPoint.Y;

          RenderTarget.DrawLine(startPoint, endPoint, brush, 4);

          //now left point
          endPoint.X = startPoint.X;
          endPoint.Y = startPoint.Y + (isTop) * 10;

          if (isTop == -1)
          startingY = endPoint.Y-2;//resetting endPoint.Y for text and box
          else
          startingY = startPoint.Y+2;

          RenderTarget.DrawLine(startPoint, endPoint, brush, 4);



          Thanks
          Hope you can help
          Shinuvu

          Click image for larger version

Name:	fixedlines.png
Views:	8
Size:	13.0 KB
ID:	1091506Click image for larger version

Name:	fixedlines.png
Views:	9
Size:	13.0 KB
ID:	1091507
          Attached Files

          Comment


            #6
            Hi shinuvu, thanks for the follow up.

            This should be an easy fix. In State.SetDefaults set IsOverlay = true; and the main panel will be the render target of your script.

            Kind regards,

            -ChrisL
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Perfect. Thank you so so much. Wish I could take you out for a few beers. Can't though - total lockdown here :-(

              Comment


                #8
                Just an afterthought. Is it possible to click on the screen and discover which box was clicked on. I.E magenta 11 or black 6 or blue 2?
                I have attached a new diagram with the lines on the price chart. Click image for larger version

Name:	fixedlines2.png
Views:	6
Size:	18.3 KB
ID:	1091574

                Comment


                  #9
                  Hi shinuvu, happy I can help.

                  You can take this example that demonstrates a mouse click event:

                  https://ninjatrader.com/support/foru...-owner-problem

                  Please let me know if there are any questions on this material.
                  Chris L.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Fiocco, Today, 05:59 AM
                  0 responses
                  16 views
                  0 likes
                  Last Post Fiocco
                  by Fiocco
                   
                  Started by catinabag, Today, 06:09 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post catinabag  
                  Started by junposlee, Today, 02:59 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post junposlee  
                  Started by patpat, Yesterday, 11:02 AM
                  2 responses
                  8 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by jameslee001, Yesterday, 08:09 AM
                  1 response
                  11 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X