On-chart independent close button for every open position

 

Hi,

I've been studying and coding EAs and scripts for a few weeks so I'm fairly new to MQL language. I felt a necessity to code an EA that creates a close button for every open position directly on the chart and at the right most corner of the screen, near the price level of each particular position. This button should close that particular position (One-Click close without confirmation).

I'm putting a screenshot of what I want. I've created it using Photoshop. Now I don't want to do it via .dll or other external libraries. I want to use the native mq4 button object.


I'd be obliged if experience guys could instruct me or give me some guide as to how am I to put those small buttons so that they stick to their position level.

Thank you.

 
Eleni Anna Branou:
Seems very interesting and useful too.
Yes, it's useful because you don't need to go through the terminal window to find a particular trade, especially when you've got many open positions. I know there is "right-click > close trade" possibility but I think having an independent close button which is always present right there, near that particular position is the most convenient solution to quickly spot and close a position.
 
It is possible, but you would have to use ChartXYToTimePrice to get the y co-ordinate for the price and you will have to modify the y distance in OnChartEvent(), CHARTEVENT_CHART_CHANGE
 
Keith Watford:
It is possible, but you would have to use ChartXYToTimePrice to get the y co-ordinate for the price and you will have to modify the y distance in OnChartEvent(), CHARTEVENT_CHART_CHANGE
Much appreciated Keith. Although I'm not familiar with these functions but I'll give it a try. I'll let you know about my progress.
 
Ehsansia:
Much appreciated Keith. Although I'm not familiar with these functions but I'll give it a try. I'll let you know about my progress.
I remember when I was unfamiliar with them. I really enjoyed playing around with them.
 
Keith Watford:
It is possible, but you would have to use ChartXYToTimePrice to get the y co-ordinate for the price and you will have to modify the y distance in OnChartEvent(), CHARTEVENT_CHART_CHANGE
I realised that I made a mistake, you will need to use ChartTimePriceToXY not ChartXYToTimePrice
 
Keith Watford:
I realised that I made a mistake, you will need to use ChartTimePriceToXY not ChartXYToTimePrice
Actually, I'm trying both of them but I still can't figure out how to put the button on the coordinates that I want. Playing back and forth... I've found my whole afternoon's job :-))
 
Ehsansia:
Actually, I'm trying both of them but I still can't figure out how to put the button on the coordinates that I want. Playing back and forth... I've found my whole afternoon's job :-))

Well, make sure that you anchor the button to

CORNER_RIGHT_UPPER

and the x co=ordinate is 0

Then you just have to set and move the y co-ordinate

 
Keith Watford:

Well, make sure that you anchor the button to

CORNER_RIGHT_UPPER

and the x co=ordinate is 0

Then you just have to set and move the y co-ordinate

:-0 I was puzzled by it if you didn't tell me this. I was thinking of finding a way to calculate screen's width in pixels to put the button at that x coordinate and I was thinking what if the EA is run on a wider screen. Your valuable suggestion solves this problem. I've to improve the way I think when coding :-)

 
Yes, just remember that when you use ChartTimePriceToXY the co-ordinates will relate to the Upper left corner. It doesn't matter in this case because you are only interested in the y co-ordinate and you are anchored to an upper corner.
 
Keith Watford:
Yes, just remember that when you use ChartTimePriceToXY the co-ordinates will relate to the Upper left corner. It doesn't matter in this case because you are only interested in the y co-ordinate and you are anchored to an upper corner.
Your suggestions helped me a lot. Sure, I'll follow your instructions.