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

How do you convert Price to Y axis values?

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

    How do you convert Price to Y axis values?

    Hi,
    I'd like to use Draw Functions to draw Divergence lines on both the Indicator Panel AND the Price Panel. I believe that this is not supported, one indicator can only put the Draw functions on one panel or another. (please tell me how if I'm wrong)

    So I'm looking for either :-
    1. A function that converts PricePanel Y Values (ie: Close[0]) to GDI Coordinate values. Similar to ChartControl.GetXByBarIdx() but for Y values.
      OR
    2. A Function that tells me the Upper & Lower values that are visible in the Y Axis scale so I can write my own convertion function.
      OR
    3. Any suggestions on how to map between the Price Scale & the GDI Graphics scale.

    While I suspect this is not supported, I know others have done it before so any thoughts you can provide would be most welcome.
    Thanks

    #2
    Your understanding of one panel or another is accurate. What you can try is to use overriding the Plot method, but that is still one panel or the other and unfortunately it is also unsupported. Good luck.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh.
      Any thoughts on the Y Axis to GDI Y Coord conversion, part of the question?

      (Doesn't have to be supported, any pointers welcome)

      Comment


        #4
        Unfortunately there is no information I will be able to provide.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          publicoverridevoid Plot(Graphics graphics, Rectangle bounds, double min, double max)
          {
          double price1 = 100;
          int y = (int)(bounds.Height * (max - price1) / (max - min));

          // Draw something with y
          }

          Comment


            #6
            For a 1st post you certainly started strong.
            I can't believe I'd become so myopic on the Graphics & bounds parameters & scanning thru the unsupported methods that I'd totally overlooked the min & max parameters.

            I'd failed to realise that they are the Min & Max of the Y-Axis, which I'd been looking for for ages. (& was told repeately was unsupported)

            I salute thee, El-Captain.

            Note to others using this formula:
            Remember you will need to add bounds.Top to your result in order to put it into the correct Panel.
            Even in Panel 1 (the price Panel) bounds.Top is 5. You may need to adjust your calculation to compensate for it.

            Comment


              #7
              Full Code Example

              As a few people emailed me about this. Attached is a more complete sample. With key part here.

              public override void Plot(Graphics graphics, Rectangle bounds, double min, double max) {
              base.Plot(graphics, bounds, min, max);


              SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
              graphics.SmoothingMode = SmoothingMode.AntiAlias;

              GraphicsPath path = new GraphicsPath();
              int lastX = -1;
              int lastY = -1;
              double seriesvalue = 0;
              Gui.Chart.Plot plot = Plots[
              0];
              for(int cntBar = ChartControl.FirstBarPainted; cntBar <= ChartControl.LastBarPainted; cntBar++)
              {

              int x = ChartControl.GetXByBarIdx(cntBar);

              // --- Use the LOW
              seriesvalue = Low[CurrentBar - cntBar];


              int y = (int)(bounds.Top + bounds.Height * (max - seriesvalue) / (max - min));
               

              if (lastX >= 0)
              {
              path.AddLine(lastX - plot.Pen.Width /
              2, lastY, x - plot.Pen.Width / 2, y);
              }

              lastX = x;
              lastY = y;
              }
              // --
              graphics.DrawPath(Plots[0].Pen, path);
              graphics.SmoothingMode = oldSmoothingMode;
              path.Dispose();
              }
              Attached Files

              Comment


                #8
                Is there any way to get at the panel bounds outside of an indicator plot? I'm converting mouse coordinates to time/price in a chart panel, and what I'm having to do right now is *really* ugly.

                Comment


                  #9
                  Originally posted by TNixon View Post
                  Is there any way to get at the panel bounds outside of an indicator plot? I'm converting mouse coordinates to time/price in a chart panel, and what I'm having to do right now is *really* ugly.
                  I'm not sure what you mean. So I hope the following guess helps. If not post me a picture.

                  1. In the Plot event the ChartControl lets you get at the entire window. Including each of the Indicator Panels you user may have open.



                  As I'm unsure if this will turn into a graphic you can see in this post a code sample is:-
                  Code:
                   
                  [COLOR=blue][FONT=Calibri]Remember to convert the Chart Property values into Pixels.[/FONT][/COLOR]
                  [FONT=Calibri]int        barWidth = ChartControl.ChartStyle.[B]GetBarPaintWidth[/B](ChartControl.BarWidth);[/FONT]
                  To adjust the painting width of your bars.

                  Note: In V7.0 you can have variable bar spacing so you need other properties to know exactly how far to space each bar.


                  And to play within the bound of a single plot use these parameters.



                  NOTE: As I'm unsure if embeding images from SkyDrive will work, I've added them as Attachments too

                  If you are really saying "No I want to draw completely outside of the plot area (chartcontrol)". Then I've not gone looking for that. From a .NET perspective you can find the parent(s) of the graphics control & work your way up the the window. But I'd suggest you be very careful. Most of the PLOT capability is "not supported" by Ninja. But they did provide the capability. If you start overwriting the X/Y Axis etc you are really in "not supported" land. AND given your code is not isolated but compiled into 1 big run-time. It would be easy to trash your UI context enought that Ninja just dies.
                  What are you really trying to do? Capture Right Mouse clicks & paint your own Context Menus, Add a custom button to the Ninja Toolbar?
                  Attached Files

                  Comment


                    #10
                    Wow, thanks for the detailed answer!

                    As for what I'm actually doing: I like using the ChartTrader for order placing better than any of the other methods, and the combination of ChartTrader + ATM strategies is pretty good, but there are some things that are super-clunky about it. And if you don't use ATM strategies to manage stops and targets (I've just been using an extremely simple one), then it can really get ridiculously hairy, especially if you end up with multiple open positions.

                    So I'm basically creating a chart-based order method that does everything I want it to do, exactly how I want it to do it, and that involves a lot of custom drawing, and knowing where the mouse is in the price and time axes. (mostly price obviously)

                    What I'm currently doing is just storing the bounds rectangle into a separate variable in my indicator when the Plot routine is called, and then exposing GetPriceFromPoint and GetTimeFromPoint functions, but that's pretty ugly, and I don't actually know for sure that plot will get called at the right time if the chart area changes in order to update that variable, so I was looking for something that didn't rely on the plot function's bounds rect.

                    Thanks again!

                    Comment


                      #11
                      Originally posted by TNixon View Post
                      Wow, thanks for the detailed answer!..

                      So I'm basically creating a chart-based order method that does everything I want it to do, exactly how I want it to do it, and that involves a lot of custom drawing, and knowing where the mouse is in the price and time axes. (mostly price obviously)

                      Thanks again!
                      TNixon,

                      Are you familiar with this resource?


                      TJ

                      Comment


                        #12
                        I did look at that.

                        The video shows somebody manually typing price values in...

                        If that's all it does, I don't have any clue why it even used ChartTrader in its name.

                        Still though, even if it does allow trading on the chart, the odds that it does the things I want in addition to what the built-in chart trader allows are *extremely* slim.

                        Originally posted by Trader.Jon View Post
                        TNixon,

                        Are you familiar with this resource?


                        TJ

                        Comment


                          #13
                          How about simply using
                          Code:
                          ChartControl.GetYByValue(this, price);
                          or coding a function thus:

                          Code:
                          private int GetYPos(double price, Rectangle bounds, double min, double max)
                                  {
                                      return ChartControl.GetYByValue(this, price);
                                  }

                          Comment


                            #14
                            ChartHelper

                            I found this somewhere on a forum and it is an excellent piece of work - when I google it now I can't seem to locate it.
                            So here is my version but this is NOT my work.

                            It has all sorts of conversion methods from price to canvas co ordinate and beyond.
                            Attached Files

                            Comment


                              #15
                              Originally posted by koganam View Post
                              How about simply using
                              Code:
                              ChartControl.GetYByValue(this, price);
                              or coding a function thus:

                              Code:
                              private int GetYPos(double price, Rectangle bounds, double min, double max)
                                      {
                                          return ChartControl.GetYByValue(this, price);
                                      }
                              What I wanted was the inverse of this. Figuring out the price at a coordinate, rather than getting the Y of a known price. David's uber-detailed answer with charts gave me everything I needed.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kingsteven, Today, 08:34 PM
                              2 responses
                              5 views
                              0 likes
                              Last Post kingsteven  
                              Started by musclay, Today, 10:25 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post musclay
                              by musclay
                               
                              Started by timmbbo, Today, 10:06 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post timmbbo
                              by timmbbo
                               
                              Started by timmbbo, Today, 10:00 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post timmbbo
                              by timmbbo
                               
                              Started by biegea, Today, 08:54 PM
                              2 responses
                              11 views
                              1 like
                              Last Post TraderTate  
                              Working...
                              X