Hi all,
I am trying to partially close my position once a certain risk reward is reached. Both proportion of the position to be close, and the risk reward needed to be reached are inputs.
I am struggling regarding the volume, as I want the partial cloture to happen only once, so when the volume of the position is the initial opening volume. I am using a function to calculate the volume for each position, depending on the risk and the distance of the stoploss (which changes for each trades).
Here is my code, which is currently closing all the position once the rr is reached for partial. Any one would have an idea how to get the partial close to only happen once?
Thank you!
Hi all,
I am trying to partially close my position once a certain risk reward is reached. Both proportion of the position to be close, and the risk reward needed to be reached are inputs.
I am struggling regarding the volume, as I want the partial cloture to happen only once, so when the volume of the position is the initial opening volume. I am using a function to calculate the volume for each position, depending on the risk and the distance of the stoploss (which changes for each trades).
Here is my code, which is currently closing all the position once the rr is reached for partial. Any one would have an idea how to get the partial close to only happen once?
Thank you!
Logic:
if the position volume is equal to the initial Lots setting or Lot risk allocation then splitting is allowed.
if the position volume is less then the Lots setting or Lot risk allocation it has already been split.
That is the process for a single split.
You can check per cent of initial Lots if you wanted two or more splits. eg. if (position_volume > 0.5 * Lots) split()
Hi all,
I am trying to partially close my position once a certain risk reward is reached. Both proportion of the position to be close, and the risk reward needed to be reached are inputs.
I am struggling regarding the volume, as I want the partial cloture to happen only once, so when the volume of the position is the initial opening volume. I am using a function to calculate the volume for each position, depending on the risk and the distance of the stoploss (which changes for each trades).
Here is my code, which is currently closing all the position once the rr is reached for partial. Any one would have an idea how to get the partial close to only happen once?
Thank you!
Just look in ...\MQL5\Include\Trade\Trade.mqh
for PositionClosePartial(..) to see how it is done correctly.
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.
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.) Can't be done in US brokers due to FIFO.
Thank you all
Forum on trading, automated trading systems and testing trading strategies
Partial close with variable size position
Raphael A, 2023.08.29 10:17
ulong ticket = PositionGetTicket(i); if(PositionSelectByTicket(posticket)){
In these two lines you select different positions. Delete the second line.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all,
I am trying to partially close my position once a certain risk reward is reached. Both proportion of the position to be close, and the risk reward needed to be reached are inputs.
I am struggling regarding the volume, as I want the partial cloture to happen only once, so when the volume of the position is the initial opening volume. I am using a function to calculate the volume for each position, depending on the risk and the distance of the stoploss (which changes for each trades).
Here is my code, which is currently closing all the position once the rr is reached for partial. Any one would have an idea how to get the partial close to only happen once?
Thank you!