Download MetaTrader 5

Partial closing of position..

To add comments, please log in or register
Volcanbleu
94
Volcanbleu  

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

whroeder1
16286
whroeder1  
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.)
Stanislav Korotky
22447
Stanislav Korotky  
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.

Alain Verleyen
Moderator
33595
Alain Verleyen  
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 ?
Volcanbleu
94
Volcanbleu  
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.
Volcanbleu
94
Volcanbleu  
Thank you very much Stanislas for your advice for closing the position. I will try that.
Volcanbleu
94
Volcanbleu  
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..

Drazen Penic
2730
Drazen Penic  
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);

Stanislav Korotky
22447
Stanislav Korotky  
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.
To add comments, please log in or register