Partial closing of position..

 

Hi everybody,

I am new on this forum and on MT4. I am programing an EA with MT4 for the forex market and I would like to know if somebody has a smart way to close partially a position otherway than closing all and open a smaller one.


Also,is there afunction in MT4 to get the current strategy profit of the EA runing ?


Thanks for your help

 
Volcanbleu: I would like to know if somebody has a smart way to close partially a position
  1. Perhaps you should read the manual. OrderClose
  2. You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot. See my code
  3. You also must check if you have already done it, to avoid repeated closing. Alternatives:
    • Move SL to Break Even+1 before the partial close. That way you know that you already did it.
    • Set a flag in persistent storage (files, global variables w/ flush)
    • Open two orders initially, and close one (manually or by TP.)
 
Volcanbleu:

I am new on this forum and on MT4. I am programing an EA with MT4 for the forex market and I would like to know if somebody has a smart way to close partially a position otherway than closing all and open a smaller one.

Also,is there afunction in MT4 to get the current strategy profit of the EA runing ?

To perform a partial close - open new position in reverse direction with a smaller lot, then call OrderCloseBy. For example, if you have 1 lot in buy, you can open 0.4 lot in sell and then call OrderCloseBy passing tickets of thess two orders to it. As a result you'll get 0.6 lot in buy.

If your strategy is marked by specific magic number, you should traverse account history for this magic, and so you can accumulate the strategy profit from profits of all matching orders. I'm sure you can find ready-made functions in the code base.

 
Stanislav Korotky:

To perform a partial close - open new position in reverse direction with a smaller lot, then call OrderCloseBy. For example, if you have 1 lot in buy, you can open 0.4 lot in sell and then call OrderCloseBy passing tickets of thess two orders to it. As a result you'll get 0.6 lot in buy.

If your strategy is marked by specific magic number, you should traverse account history for this magic, and so you can accumulate the strategy profit from profits of all matching orders. I'm sure you can find ready-made functions in the code base.

Why not simply OrderClose() with requested volume ?
 
Alain Verleyen:
Why not simply OrderClose() with requested volume ?
Thank yo guys for your help. Apparently, MT5 has solved the pb by introducing compensation type orders managing. Serious brokers seem to work with MT5 so I think I am going to switch to MT5 which is much more powerfull.
 
Thank you very much Stanislas for your advice for closing the position. I will try that.
 
Volcanbleu:
Thank you very much Stanislas for your advice for closing the position. I will try that.

Hi Stanislas,


I tried your partial closing system and it seems to work fine... thanks again..

 
Volcanbleu:

Hi Stanislas,


I tried your partial closing system and it seems to work fine... thanks again..


Just be aware that some brokers do not support OrderCloseBy()

You can check it by calling:  MarketInfo(Symbol(), MODE_CLOSEBY_ALLOWED);

 
Volcanbleu:

I tried your partial closing system and it seems to work fine... thanks again..

As Alain noted, OrderClose with partial volume should work fine as well and preferred for simple cases.
Reason: