How do I close only some lots of an open position, when reaching some pip targets?

 
Hello,

I have a minimum knowledge of mql4, and I am searching for a solution for a coding problem I have.

I want the EA to start closing partially an open position, if the profit (in pips) reaches certains amounts.

Example:
Imagine there is an open position (buy or sell, doesn't matter) of 1 lot.
I would like the EA to:
- Close 0.1 lots if the profit in pip is 20 (just and exmple). We would be left with 0.9 lots (which would be closed when the closing conditions are met).
- Close another 0.1 lots if the profit (in pips) reaches 40. We would be left with 0.8 lots (which would be closed when the closing conditions are met).
- Close another 0.1 lots if the profit (in pips) reaches 60. We would be left with 0.7 lots (which would be closed when the closing conditions are met).

The "sub-lot" quantity (0.1 in the above example) should be adjustable, as well as the several pip targets.

I understand that we should have a variable (lotsremaining, or something like that) that would be updated every time there was a "sub-lot" closed. Its value would then be used by the OrderClose, if the closing conditions were met (for the whole position).

Related to this, I would like to know:
- In what unit does OrderProfit work? Does it give the results in pips or money?
(from what I've seen is in money, but I would like to confirm)
- The OrderLots: is it updated if we sell only part of the lots used to open the position?

Thank you for your attention and help.

MD
 

you can close a partial lot that is:

open 1.0 lot
close 0.1 lot.

You will need to keep track of your order. If you have only 1 trade open you are fine, but generally the only thing that keeps constant when closing parital lot is magic number. What mt does it closes your whole position of 1.0 lot and open a new order for 0.9 lot at the same price so ticket number changes but magic number is copied from old ticket. You don't need to keep track of remaining position since it is in the order itself.
Actually this could be quite simple - just close 0.1 lot whenever profit reaches 20 pips and keep track of your order by magic number.

to get profit in pips for buy order (OrderClosePrice()-OrderOpenPrice())/Point
for sell order multiplu by -1.

 
irusoh1 wrote:

you can close a partial lot that is:

open 1.0 lot
close 0.1 lot.

You will need to keep track of your order. If you have only 1 trade open you are fine, but generally the only thing that keeps constant when closing parital lot is magic number. What mt does it closes your whole position of 1.0 lot and open a new order for 0.9 lot at the same price so ticket number changes but magic number is copied from old ticket. You don't need to keep track of remaining position since it is in the order itself.
Actually this could be quite simple - just close 0.1 lot whenever profit reaches 20 pips and keep track of your order by magic number.

to get profit in pips for buy order (Price()-OrderOpenPrice())/Point
for sell order multiplu by -1.


Hi,

Thanks irusoh1.
With what you said in mind, I did try, but it doesn't seem to work...
When I had it closing (by mistake) the same number of lots as the number of lots in the opening order, it did close.
When I had it closing 0.1 (the open was 1 lot), it did not close, when it reached the fisrt pip target.
Maybe it's just my coding...I am far from an expert...

Have you (or anyone) seen this working? how is the code.

Thanks anyway.
 
It may depend on your broker. do a script that will print marketinfo on minlot,lotstep etc. that show what minimum you can trade.
Also in order to test it, do it manually. open 1.0 lot using pf9 then try and close 0.1 lot. you can change it in the dialog box.
take notice of the order number and comment and how they change.
Reason: