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

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:	456
Size:	13.0 KB
ID:	1091506Click image for larger version

Name:	fixedlines.png
Views:	428
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:	438
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:



                  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 usazencort, Today, 01:16 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post usazencort  
                  Started by kaywai, 09-01-2023, 08:44 PM
                  5 responses
                  603 views
                  0 likes
                  Last Post NinjaTrader_Jason  
                  Started by xiinteractive, 04-09-2024, 08:08 AM
                  6 responses
                  22 views
                  0 likes
                  Last Post xiinteractive  
                  Started by Pattontje, Yesterday, 02:10 PM
                  2 responses
                  21 views
                  0 likes
                  Last Post Pattontje  
                  Started by flybuzz, 04-21-2024, 04:07 PM
                  17 responses
                  230 views
                  0 likes
                  Last Post TradingLoss  
                  Working...
                  X