Wrong prices when trades open MQL5

 

I have a problem with entry price of trades in my EA. I do it with this code:

int ticket = trade.Buy(CalculateLotSize(diff*10000),NULL,0,longSL,longTP,"BUY");
int ticket = trade.Sell(CalculateLotSize(diff*10000),NULL,0,shortSL,shortTP,"SELL");

But the trades are open with wrong price on the chart, as shown in the attached picture.

I tried to change the spread but nothing changes. Does someone know what's wrong?

Thank you

 
RiccardoBorello01: I have a problem with entry price of trades in my EA. I do it with this code: But the trades are open with wrong price on the chart, as shown in this picture (buy). I tried to change the spread but nothing changes. Does someone know what's wrong? Thank you
You can't change the opening price of a Market order, nor can you change the spread.

Forum on trading, automated trading systems and testing trading strategies

MT4 didn't close position when TP reached

Fernando Carreiro, 2022.05.24 02:47

There is the Bid price and there is the Ask price, and the difference between the two is the Spread.

  • Buy (or Long) positions, open at the Ask price and close at the Bid price.
  • Sell (or Short) positions, open at the Bid price and close at the Ask price.

Charts, display the OHLC bars as Bid prices (or Last prices). So, the Take-Profit or the Stop-Loss price for a Sell (short) position will close at the Ask price, which is not shown on the chart. So, even if the low price (bid) has touched or crossed the price level of the Take-Profit, the Ask price may still not have reached it. That is why your position's Take-Profit was not triggered.


 

Also, please don't link to external sites for your images.

If you can't insert (Alt+I) an image due to low rating, then just attach the image file instead (green icon at the end "+ Attach file").

 
Fernando Carreiro #:
You can't change the opening price of a Market order, nor can you change the spread.

In the tester i set the spread to 2 to backtest the EA, and after noticing this problem on the chart i set it to 1000 (just to try) but nothing has changed
 
RiccardoBorello01 #: In the tester i set the spread to 2 to backtest the EA, and after noticing this problem on the chart i set it to 1000 (just to try) but nothing has changed

In the tester, setting the spread is just for simulation. On live trading you can't change the spread.

However, you seem to have ignored the rest of my post. So please pay attention about the ask and bid prices for orders.

There is the Bid price and there is the Ask price, and the difference between the two is the Spread.

  • Buy (or Long) positions, open at the Ask price and close at the Bid price.
  • Sell (or Short) positions, open at the Bid price and close at the Ask price.

Charts, display the OHLC bars as Bid prices (or Last prices).


 
Fernando Carreiro #:

In the tester, setting the spread is just for simulation. On live trading you can't change the spread.

However, you seem to have ignored the rest of my post. So please pay attention about the ask and bid prices for orders.

yes, I understand why the prices are not touched, but why changing the spread in the backtest does the results remain the same?

thank you

 
RiccardoBorello01 #: yes, I understand why the prices are not touched, but why changing the spread in the backtest does the results remain the same? thank you

Show a screenshot of where and how you are changing the spread.

Also, which symbol? Show screenshot of the contract specifications.

Show the log entries for a few of the trades, both buy and sell trades.

 
Fernando Carreiro #:

Show a screenshot of where and how you are changing the spread.

Also, which symbol? Show screenshot of the contract specifications.

Show the log entries for a few of the trades, both buy and sell trades.

I am using the EA on USDCHF on m1, here are the screenshots with examples of trades.
Files:
sell1.png  6 kb
sell2.png  9 kb
buy1.png  5 kb
buy2.png  9 kb
 
Instead in this case it touches the stop loss in advance
Files:
sl1.png  8 kb
sl2.png  13 kb
 
RiccardoBorello01 #: I am using the EA on USDCHF on m1, here are the screenshots with examples of trades.

You did not show the log entries I requested. The screenshots of trades does not provide enough information about the current ask and bid prices when the orders were placed.

Please provide log entries for sell and buy orders for two different tests with the spread changed.

 
Fernando Carreiro #:

You did not show the log entries I requested. The screenshots of trades does not provide enough information about the current ask and bid prices when the orders were placed.

Please provide log entries for sell and buy orders for two different tests with the spread changed.

Ok, here are the right pictures
Files:
buy1.png  5 kb
buy2.png  9 kb
buy3.png  34 kb
sell1.png  5 kb
sell2.png  8 kb
sell3.png  33 kb