How to place opposite orders once SL is hit?

 

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?

 
I would place a pending order at that SL price.
Good luck.
 

You didn't specify a language so it's hard to give a answer in code.

But you can monitor the number of open positions and once a SL get's hit this number will change and you can use that moment in time to see what position closed and open new opposing positions if need be.

 
Soewono Effendi:
I would place a pending order at that SL price.
Good luck.
I actually never though of that, it seems so simple but it will maybe work. Thank you a lot, will give it a try and then give feedback.
 
Marco vd Heijden:

You didn't specify a language so it's hard to give a answer in code.

But you can monitor the number of open positions and once a SL get's hit this number will change and you can use that moment in time to see what position closed and open new opposing positions if need be.

I'm sorry, completely forgot about it. The language is MQL5. Alright, I'm going to try that too. Thank you for answering.

 
Soewono Effendi: I would place a pending order at that SL price.

Won't work in the USA as hedging isn't allowed. Even one tick away might not.

 
William Roeder:

Won't work in the USA as hedging isn't allowed. Even one tick away might not.

I'm not in the USA, so I have an hedge account and I am allowed to place trades in different directions.
 
William Roeder:

Won't work in the USA as hedging isn't allowed. Even one tick away might not.

How is a pending order that get filled as soon as an opposite position closed, considered as hedging ?

Enlighten me please ...

 
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?

You can create a OnTrade event. When any position is closed, if profit < 0 open a new opposite position.

 
Soewono Effendi:

How is a pending order that get filled as soon as an opposite position closed, considered as hedging ?

Enlighten me please ...


chances are the pending order will be executed before the stop loss is hit and both positions are in place..the stop loss will never use ask price when bid position is still open unless the programmer tracks the delta of ask price in his code and open opposite position when the opposite position price has reached the stop loss price...this is what in my mind..but could be wrong...
 
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?

Specify - "if Stop Loss worked" - what do you mean? "Stop Loss worked and LOSS was received" or "Stop Loss worked and profit was received"?

Reason: