Timing issue with virtual and market SL

 

Hello!

I have an EA which uses virtual SL (stop loss checked via code) to partially close positon when price drops and market SL (stop loss send with buy order). I set the market SL equal to the last virtual SL. I trade physical stocks (not CFDs) but my broker still allows sell orders (borrowing the stocks from the broker). So if the last SL is hit, both the virutal and the market SL are triggered resulting in an open sell position. As an example, 50 lots are still open and the last SL is hit, the virtual stop loss triggers a sell order with lot 50 and the market stop loss also triggeres a sell order with lot 50 resulting in total with an open sell position of 50 lot. Before the virtual stop loss is handled, I check if PositionsTotal()>0 . The Problem is that the virtual SL and the market SL happens more or less exactly at the same time so that PositionTotal() is still returning 1 instead of 0. I added "sleep(10000)" and checked PositionsTotal() again and this seems to work but I honestly do not want to wait 10 secs every time I check my virtual stop loss. In 10 secs the price can drop dramatically. After I checked PositionTotal() I added 

PositionSelect(_Symbol);
if(PositionGetDouble(POSITION_VOLUME)==0) return;

and this seemed to work but maybe it is just coincidence.

One possible solution would be to place the market SL slightly below the last virtual stop loss but as you know there are cases that on the next day the price starts far below the closing price of the last day and thus as soon as the exchange opens both the virutal and the market SL are triggered.

Any ideas how to better solve this? Thank you.

 
ammer.jens:

Hello!

I have an EA which uses virtual SL (stop loss checked via code) to partially close positon when price drops and market SL (stop loss send with buy order). I set the market SL equal to the last virtual SL. I trade physical stocks (not CFDs) but my broker still allows sell orders (borrowing the stocks from the broker). So if the last SL is hit, both the virutal and the market SL are triggered resulting in an open sell position. As an example, 50 lots are still open and the last SL is hit, the virtual stop loss triggers a sell order with lot 50 and the market stop loss also triggeres a sell order with lot 50 resulting in total with an open sell position of 50 lot. Before the virtual stop loss is handled, I check if PositionsTotal()>0 . The Problem is that the virtual SL and the market SL happens more or less exactly at the same time so that PositionTotal() is still returning 1 instead of 0. I added "sleep(10000)" and checked PositionsTotal() again and this seems to work but I honestly do not want to wait 10 secs every time I check my virtual stop loss. In 10 secs the price can drop dramatically. After I checked PositionTotal() I added 

and this seemed to work but maybe it is just coincidence.

One possible solution would be to place the market SL slightly below the last virtual stop loss but as you know there are cases that on the next day the price starts far below the closing price of the last day and thus as soon as the exchange opens both the virutal and the market SL are triggered.

Any ideas how to better solve this? Thank you.

You are issuing two separate orders 1= SL with the broker to Sell  2=a sell order via your virtual SL   

Solution = do not do both, what is the point of issuing a sell order to close at the SL price when you already have that order placed with the broker. You only need to issue a sell order to close if you want to close at a price other than your SL or TP

 

Hello Paul, thank's for your response!

First, I trade with a netting account which means there is only one position at a time with an overall SL. If I want to fix some profits, I have to sent sell orders to do so. Thus, I actually prefer to handle everything (TPs and SLs) virtually. The reason why I use a market SL as well is in cases of a server or MT5 crash. If you have open orders and price drops one can gain big losses. So it is basically just for security reasons.

As outlined above, one solution would be to place the market SL below the last virutal one but sometimes the new candle starts below both of them and thus triggers them both pretty much at the same time.

 

The solution is do not use both. If you want to use a hard stop, place it reasonably below so it will ONLY trigger at a TRUE emergency if that is the purpose.

I have been trading for the past 2,5 years full time only with EA, not manual. 22k+ trades, i do not use hard stops and never had any problem. Granted i use a VPS, not home desktop.

 
Hi Enrique, thank you for you comment. I will then probably do it only virtually. I use a Windows 2019 server on which MT5 is running. Is VPS far different from that? I mean especially in terms of crashing?
 
If you only execute virtual SL/TP within the range of hard SL/TP you will not have that issue.
 
well, yes, as Enrique stated, if the hard SL is far enough away from the virtual one or do I understand it incorrectly?
 
What I mean is, you only execute soft SLs inside the range of SL to TP. If the price is equal or outside of this range, it's not the EAs job anymore. Because that is handled by the Broker.

Thw boundary needs to be considered separate and should probably use something like a deviation or slippage threshold.
 
ah okay, now I understood. That is actually not a bad idea to be honest. I will test it. thank you very much for your input!
Reason: