Closed with a loss even I coded profit > 0.0 - page 2

 
Ubzen:

Yeah I believe so. However, not all brokers (example ECN) allow slippage/deviation. In case of ECN, you get the Best Price after you submit the order. Which Execution_Type allows slippage and which doesn't is something you'll need to check for before submitting the order. I cannot remember which you need .. nor have I tested them extensively.

If I have set slippage with 3 points and this still happens, what would be the cause?
 
doshur:
It happened yesterday at US timing 9.30 when it release Non Farm. I can be 99% sure there is rapid price movements.
If so, the question is: what code could protect you to really close the trade with profit > 0 in any market situation?
 
doshur: If I have set slippage with 3 points and this still happens, what would be the cause?

Perhaps its an ECN broker ... Or the kind which does-not care what you specify within the Slippage parameter. In that case, you'll have to use Pending Orders. Create a small script and check the Execution Mode.

 
figurelli:
If so, the question is: what code could protect you to really close the trade with profit > 0 in any market situation?
yup
 

I think I have a solution

if profit in pips > spread then close position. (I think I set the slippage == spread) 

 
doshur: I think I have a solution if profit in pips > spread then close position. (I think I set the slippage == spread) 

Wouldn't work. When you check the closing condition everything would be alright. When you try to close the order on Market_Execution you'll suffer whatever slippage the Market impose at that time.

Your best option here again is using Limit (Pending) Orders (of the Opposite_Type). Obviously, how close you can set the order depends upon your Symbols Stop_Levels. 

 
Ubzen:

Wouldn't work. When you check the closing condition everything would be alright. When you try to close the order on Market_Execution you'll suffer whatever slippage the Market impose at that time.

Your best option here again is using Limit (Pending) Orders (of the Opposite_Type). Obviously, how close you can set the order depends upon your Symbols Stop_Levels. 

even when I set my slippage == spread?
 
doshur: even when I set my slippage == spread?
Yes. The Slippage is Ignored for Market Executions. So it does not matter what you set it to.
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Symbol Properties - Documentation on MQL5
 
This is a good discussion. Thanks.
 
doshur:
It happened yesterday at US timing 9.30 when it release Non Farm. I can be 99% sure there is rapid price movements.


Hi Doshur.


I had the same problem yesterday during the "Unemployment Rate" event, we both trade at broker X.

My model should open a new trade at the purple line but the terminal freezes for 7 seconds. It was too busy in the market to process the order.

When the trade was finally executed, I got the HIGH of the BAR!!!

I was stopped out after a few seconds and then, my model opened a new trade, The second trade was correct.



If you open or close a trade during macro events, you don't have the guarantee that the trade is executed for that price.

In my case, there was a difference of 30 Pips! between the trigger price and the execution price.

the simulation server of Metaquotes did execute the trade instantly for the correct price.