View Full Version : How do you convert Price to Y axis values?
David Lean
12-21-2009, 11:43 PM
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 :-
A function that converts PricePanel Y Values (ie: Close[0]) to GDI Coordinate values. Similar to ChartControl.GetXByBarIdx() but for Y values.
OR
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
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
NinjaTrader_Josh
12-22-2009, 07:07 AM
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.
David Lean
12-22-2009, 03:42 PM
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)
NinjaTrader_Josh
12-23-2009, 07:17 AM
Unfortunately there is no information I will be able to provide.
el capitan
12-27-2009, 04:58 PM
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
}
David Lean
12-27-2009, 05:43 PM
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.
David Lean
12-30-2009, 08:53 PM
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();
}
TNixon
01-19-2011, 12:27 PM
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. :)
David Lean
01-19-2011, 08:20 PM
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.
http://prcwqq.bay.livefilestore.com/y1phHaeAAgd2MPeffBI7w3Tvd1Bw4xH_vBfU0o33K5fqD7Kk4X W-pbDSMmgHU6ToEC9pbA-zVl73qdzFtWwnJ2_qyQu3jEwpHCF/Ninja%206.5%20ChartControl%20Params.jpg?psid=1
As I'm unsure if this will turn into a graphic you can see in this post a code sample is:-
Remember to convert the Chart Property values into Pixels.
int barWidth = ChartControl.ChartStyle.GetBarPaintWidth(ChartCont rol.BarWidth);
To adjust the painting width of your bars.
http://prcwqq.bay.livefilestore.com/y1pLIFRYVLEk_FEhHHbB_owN5ejxSjt3RZJiC1qtuzKxlnfQhJ 33rbXiDZB5IIsa858jaj4Kpqpdj3f6TBTp-7Qx7Nkv4Rk11c_/Ninja%206.5%20ChartControl%20Bars%20Params.jpg?psi d=1
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.
http://prcwqq.bay.livefilestore.com/y1pmRsbv9zQLMgH3aEyD_q4QidMPGQp7S8tJONj1MUFA7x2ocf zLQLf4i62txhJ453Bj0rKMVNwomPUwV7YTPwc3C2VjxcHGZUN/Ninja6.5%20Bounds%20Params.jpg?psid=1
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?
TNixon
01-20-2011, 12:47 AM
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! :)
Trader.Jon
01-22-2011, 05:28 PM
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?
http://www.volumedigger.com/NinjaTrader/Strategies/dsChartTrader.aspx
TJ
TNixon
01-22-2011, 06:00 PM
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.
TNixon,
Are you familiar with this resource?
http://www.volumedigger.com/NinjaTrader/Strategies/dsChartTrader.aspx
TJ
koganam
01-27-2011, 12:36 AM
How about simply using ChartControl.GetYByValue(this, price);or coding a function thus:
private int GetYPos(double price, Rectangle bounds, double min, double max)
{
return ChartControl.GetYByValue(this, price);
}
Mindset
01-27-2011, 01:21 AM
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.
TNixon
01-29-2011, 09:45 PM
How about simply using ChartControl.GetYByValue(this, price);or coding a function thus:
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. :)
Radical
12-05-2011, 02:49 PM
Edit: Never mind, figured it out.
David Lean
12-06-2011, 05:31 PM
Edit: Never mind, figured it out.
Glad you did. I had an issue logging on yesterday, so I couldn't post my reply.
I assume you realised that the Mouse coords are only valid for the plot area that is displaying at the time.
"***************
I'm having trouble putting all the information in this thread into context...I have Y-coordinates (in pixels) from MouseEventHandler, and I'm hoping to covert those into price values, but I don't even see how this would be possible given the fact that there can be many many different price values for each Y-coordinate over the course of a day. Am I correct in assuming that this is impossible?
***************
"
koganam
12-06-2011, 06:47 PM
Glad you did. I had an issue logging on yesterday, so I couldn't post my reply.
I assume you realised that the Mouse coords are only valid for the plot area that is displaying at the time.
"***************
I'm having trouble putting all the information in this thread into context...I have Y-coordinates (in pixels) from MouseEventHandler, and I'm hoping to covert those into price values, but I don't even see how this would be possible given the fact that there can be many many different price values for each Y-coordinate over the course of a day. Am I correct in assuming that this is impossible?
***************
"
The price chart is a 2-dimensional graph with independent axes. The meaning of Y-values are indeterminate unless they are referenced to x-values.
Radical
12-06-2011, 08:55 PM
Thanks for the responses guys, that's what I eventually figured out after a bunch of trial and error.