How to place opposite orders once SL is hit? - page 2

 
jota9913:

Hi everyone!


So basically I'm coding an EA that places one trade depending on a given signal, and that trade runs just perfect. It places the trade when it should, and it changes the stop loss to breakeven once the first take profit target is hit.

I want to code it so if the stop loss is hit instead, it places a trade in the opposite direction, and if that stop loss is hit, it places another trade in the opposite direction again. For example, the first trade is a Long, the stop loss is hit so it places a Short, that stop loss is also hit so it places a Long.

So far I've been successful at coding the first order, but I can't quite figure out how to check if the stop loss is hit, and place the second trade, the third, fourth, and so on.

I don't have problems with the coding itself, but I'm struggling with the logic of the programm.

Lets say the first trade is a long trade. Once that first trade opens, it sets a boolean longTrade to true, and if the stop loss is hit, it sets the SLHit boolean to true, the longTrade boolean to false, and the shortTrade boolean to true. 

Any ideas on a better way to do this?

The way to handle this might depend on whether you have a netting account or a hedging account.

In case of a netting account will your open position be closed. You can monitor your open position until it gets closed. Then you'll have to determine whether it got closed by the profit taker, or the stoploss. If it was the stoploss then you can place your opposite trade.

In case of a hedging position it could be that your open position does not get closed, but that a new position with opposing sign got opened (resulting in two open positions, with a combined volume of zero). Or that your open position got closed, similar as for a netting account. This depends on how the code was implemented. Nevertheless, the resulting volume of the combined positions is zero. Once you determine that this volume is zero, can you continue as described for a netting account.

 
WindmillMQL:

The way to handle this might depend on whether you have a netting account or a hedging account.

In case of a netting account will your open position be closed. You can monitor your open position until it gets closed. Then you'll have to determine whether it got closed by the profit taker, or the stoploss. If it was the stoploss then you can place your opposite trade.

In case of a hedging position it could be that your open position does not get closed, but that a new position with opposing sign got opened (resulting in two open positions, with a combined volume of zero). Or that your open position got closed, similar as for a netting account. This depends on how the code was implemented. Nevertheless, the resulting volume of the combined positions is zero. Once you determine that this volume is zero, can you continue as described for a netting account.

OP, this is what you want to do for any pending orders that modify a position size other than flattening it completely (stop-loss). For example if you are (1) lot long and want to reverse at a stop level then you'd place a (2) lot stop order. When it executes your net position will be (1) lot short. If you have a netted account you are finished, however, if you have a hedging account then you must do one more step in order to reconcile the offsetting orders as closed so you will not incur extra fees and slippage by closing them independently. This process is known as "closeby". You can do it manually or programatically, but as long as you do it then you will not have to pay extra commissions and the 2 lot short and 1 lot long orders will be merged into a single 1 lot short position. 

Reason: