Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

How to created ohlcwidget at mouse click location on chart?
 Post subject: How to created ohlcwidget at mouse click location on chart? Post rating: 0   New post Posted: Wed 10 Jul, 2013, 10:33 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Dear Support,

I am trying to create an ohlcwidget object at the mouse location upon mouse click on the chart, but I cannot find a good way to locate the widget object at the mouse position.

The chart's mouseevetnhandler can tell the mouse position (getX(); getY()), but it is in pixels (from 0 to somewhere around 700-1000, based on the current screen resolution and chart size).
The ohlcwidget can be moved to a location on chart with setPosX(), but this function's parameter is in range 0.1-1.0.

How could I convert the pixels to [0.1-1.0]?


 
The Best Answer  Post subject: Re: How to created ohlcwidget at mouse click location on chart? Post rating: 3   New post Posted: Thu 11 Jul, 2013, 14:15 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
edit 2: SOLVED/HACKED
presuming nothing drastic changes with the Jpanel/Container/Component hierarchy on the platform this works for mouselisteners on the main chart. your homework is to modify it to work even if the indicator panels below are the one's with the mouselisteners (hint: main chart is located at 0,0 top left, indicators are at 0,Y at various levels of Y)
console.getOut().println("-------------------------------------");
x = (double)e.getSourceEvent().getX(); //mouse x
y = (double)e.getSourceEvent().getY(); //mouse y
w = (double)e.getSourceEvent().getComponent().getParent().getParent().getWidth();  //whitearea w
h = (double)e.getSourceEvent().getComponent().getParent().getParent().getHeight(); //whitearea h
setx = (float)(x / w); // relative to the whitearea
sety = (float)(y / h); // relative to the whitearea
IOhlcChartObject o = chart.getChartObjectFactory().createOhlcInformer();
o.setPosX(setx);   
o.setPosY(sety);
chart.addToMainChart(o);
See compilable file attached as solved.java for further insight
**************************************************************************************************************************************************


edit 1: quite a bit of work needed as things look shady with indicators plotted below the main chart
edit 1: (proportions address the entire white area, main chart and indicator panels, our mouselistener is within the main chart)
**************************************************************************************************************************************************


Rough attempt - perhaps someone can find the obvious error as I've not given it thought

0.0f to 1.0f tells me that instead of supporting absolute pixel positioning e.g. a fixed position of 200, 200, there's an attempt to use proportions (modelled in percentages).

If we have Chart 1 open up at 800w 800h and Chart 2 open up at 1000w, 1000h, in order to place some graphic widget in the middle requires two different coordinates 400x, 400y and 500x, 500y.

If we then resize the charts to 700w,700h and the second to 900w, 900h then the numbers we used for the centre are now off.

We can deal with this resizing/redrawing problem (if we consider it to be one) by using proportions. So instead of saying 400, 400 and 500, 500 above, we can say 50%, 50% (0.5x, 0.5y) to mean 400x,400y and 500x,500y. This translates into 350x,350y and 450x,450y after resizing as the proportion carries across after resizing.

As the chart size refers to the entire area wherein you may place your widget, then what you must do in order to determine the proportions for placement is a case of division.

Begin with a screen size you can work with such as 1000w,1000h for reference.

Then ask yourself 'where would l like to place my widget?'.

Top left corner - 0x, 0y
Left side with vertical middle - 0x, 500y
Top side 1/3rd across - 333x, 0y
Centre - 500x, 500y
Bottom right corner - 999x, 999y

Then to get the proportions divide the desired x/y positions by their reference width/height to give

Top left corner: 0x, 0y -> 0x/1000w, 0y/1000h = 0.0setx,0.0sety
Left side with vertical middle: 0x, 500y -> 0x/1000w, 500y/1000h = 0.0setx, 0.5sety
Top side 1/3rd across: 333x, 0y -> 333x/1000w, 0y/1000h -> 0.333setx, 0.0sety
Centre: 500x, 500y -> 500x/1000w, 500y/1000h -> 0.5setx, 0.5sety
Bottom right corner: 999x, 999y -> 999x/1000w, 999y/1000h -> 0.999setx, 0.999sety

As the resulting positions set are percentages, then the system only has to double check the current chart size after a resize to determine where to place the widget (if it wants to work in this way).

In the case of the mouse position, you also require the current width and height of the chart in order to determine the proportions.

ChartWidth = w
ChartHeight = h
MouseX = x
MouseY = y
setx = x/w
sety = y/h
double x = (double)e.getSourceEvent().getX();
double y = (double)e.getSourceEvent().getY();
double w = (double)e.getSourceEvent().getComponent().getWidth();
double h = (double)e.getSourceEvent().getComponent().getHeight();
IOhlcChartObject o = chart.getChartObjectFactory().createOhlcInformer();
o.setPosX((float)(x / w));
o.setPosY((float)(y / h));
chart.addToMainChart(o);
So as I said on the first line it's a ROUGH attempt as there is some drifting error I've not considered - lazy right now but also wanted to get the help started
Towards the left it's accurate but it loses accuracy towards the right - obviously a rounding / pixel division / pixel compensation (+1 here, -1 there) issue.
Compilable file Strategy1.java included for those looking for extra homework ;)

SORRY FOR MULTIPLE FILES - RENAMED CLASS TO 'Solved' DURING EDIT - CAN'T DELETE ATTACHMENTS ALREADY MADE :?


Attachments:
Solved.java [3.38 KiB]
Downloaded 394 times
Solved.java [3.38 KiB]
Downloaded 390 times
Solved.java [3.39 KiB]
Downloaded 369 times
Strategy1.java [2.59 KiB]
Downloaded 392 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: How to created ohlcwidget at mouse click location on chart? Post rating: 0   New post Posted: Thu 11 Jul, 2013, 22:47 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Million (+1) thanks. I really appreciate the detailed answer, which we always got from you...

Quote:
e.getSourceEvent().getComponent().getWidth();
e.getSourceEvent().getComponent().getHeight();

I was also trying to get this information (the chart dimension), but I thought it is related to the IChart object. But I found nothing there.

About the precision:
As a start, this is good enough for me. Later on I will probably 'do the homework' :). I also see that when you are too close to the right edge of the chart, the widget is created with half (or more) part outside the chart. This can be also fine tuned, to compare: if (mouseX+widgetW) > chartW {...}.


I have a strange issue however when you click on a future date on the chart. This is possible if you scroll your chart to the utmost right.Then on the right half of the chart there are no candles (yet).
I have some screenshots here.
Do you have any idea why is that happening? It seems to me that somehow there is some kind of connection between price data and the ohlc object, and when there is no price data, some drawing function is failing. But this is just a wild, and educated guess.


 
 Post subject: Re: How to created ohlcwidget at mouse click location on chart? Post rating: 1   New post Posted: Fri 12 Jul, 2013, 01:54 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
Please check the green part (2nd edit) and the file Solved.java - The OHLC widget now draws accurately where ever you place the mouse pointer (including future areas) as long as the mouselistener in use is attached to the mainchart (and not the indicators below).
The problem in the first post was that setPosX() and setPosY() are proportions relating to the entire WHITE AREA (maincharts + indicators below the maincharts) and not just the main charts area.

You're right about the far right hand side and half missing (positioning based on that top left corner).
if (mouseX+widgetW) > chartW {...} style checks are the way to work it out.

Both e.getSourceEvent().getComponent().getWidth(); and e.getSourceEvent().getComponent().getHeight(); are indeed the dimensions of the mainchart area (minus the small price area to the right) generating the 'e' event. IChart interface restricts us from seeing the AWT/Swing GUI information. The mouse events are the key as they give us AWT/Swing GUI information about which components/containers are generating the mouse events.

And yes there is the notion of a relationship between Time (X), Price (Y) and how some things are drawn on the charts - it's just another coordinate system.

The key thing you mention there is CLICK in to the future. Seems then that it is information being taken from the click event that is used to draw the widget and the algorithm there is breaking when the click is beyond a certain area.
It's interesting that the widget grows down and not across - perhaps the setting of vertical alignment plays role.


 
 Post subject: Re: How to created ohlcwidget at mouse click location on chart? Post rating: 0   New post Posted: Fri 12 Jul, 2013, 12:19 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Oh, okay.
I haven't realized (God knows why not) that the 2nd edit contains that `fix` :).


 

Jump to:  

  © 1998-2026 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com