Ask, bid and close: How do they work?

 

Hello. I'm developing an EA to trade forex and I came upon a problem I can't seem to fix.

I'm trying to buy and sell based on the CLOSE price, which is the price that is readable upon looking at the candles, but everytime a moment in which a trade is about to take place, the bot simply ignores my request to trade based on the CLOSE price and uses ASK and BID instead. The problem though is that the ASK/BID price is usually miles away (3.5 pips) from the closing price, which makes a huge difference on my profit and losses. How can I fix this? I'm aware spread exists, but I'm using a Raw Spread account, is such a huge margin normal? The broker says it averages on 0.1 pips on this particular account, but this is definitely not what's happening during backtest.

See pic related for a better understanding of the issue.

Files:
printForex.PNG  23 kb
 
Mateus Soraggi:

Hello. I'm developing an EA to trade forex and I came upon a problem I can't seem to fix.

I'm trying to buy and sell based on the CLOSE price, which is the price that is readable upon looking at the candles, but everytime a moment in which a trade is about to take place, the bot simply ignores my request to trade based on the CLOSE price and uses ASK and BID instead. The problem though is that the ASK/BID price is usually miles away (3.5 pips) from the closing price, which makes a huge difference on my profit and losses. How can I fix this? I'm aware spread exists, but I'm using a Raw Spread account, is such a huge margin normal? The broker says it averages on 0.1 pips on this particular account, but this is definitely not what's happening during backtest.

See pic related for a better understanding of the issue.

The prices you see on the chart, including the current close prices are all "Bid" prices.

However, there is something called "spread" which is the difference between prices when buying or selling and it depends on the Symbol and the Broker you are using, as well as the current Market conditions which keep changing. This is the difference between Ask and Bid, where Ask = Bid + Spread.

"Buy" orders, open at "Ask" price and close at "Bid" price.

"Sell" orders, open at "Bid" price and close at "Ask" price.

So when you place an Order on the Market, you have to take into consideration, and the fact that the chart is ONLY showing you Bid prices.

On MQL4, during back-tests, you should set the Spread manually to a value of your choice, because if you accept the default it will use the current market spread, and during the weekend, it will be very large. That is why you are getting 3.5, because its the weekend. Use a fixed spread. This however, does not apply to MQL5 backtests because it uses the real spread in the data.

 
Fernando Carreiro:

The prices you see on the chart, including the current close prices are all "Bid" prices.

However, there is something called "spread" which is the difference between prices when buying or selling and it depends on the Symbol and the Broker you are using, as well as the current Market conditions which keep changing. This is the difference between Ask and Bid, where Ask = Bid + Spread.

"Buy" orders, open at "Ask" price and close at "Bid" price.

"Sell" orders, open at "Bid" price and close at "Ask" price.

So when you place an Order on the Market, you have to take into consideration, and the fact that the chart is ONLY showing you Bid prices.

On MQL4, during back-tests, you should set the Spread manually to a value of your choice, because if you accept the default it will use the current market spread, and during the weekend, it will be very large. That is why you are getting 3.5, because its the weekend. Use a fixed spread. This however, does not apply to MQL5 backtests because it uses the real spread in the data.

Hello Fernando, thanks for replying.

I am actually using MQL5, but such absurd spreads make no sense to me. As the candle hit my trading price and I start a trade proper, it already starts at a considerable loss of 3.5 pips.

The real spread in the data is clearly not working as intended. Is it possible to set a fixed one for MQL5? Or at least one that actually corresponds to reality?

 
Mateus Soraggi:

Hello Fernando, thanks for replying.

I am actually using MQL5, but such absurd spreads make no sense to me. As the candle hit my trading price and I start a trade proper, it already starts at a considerable loss of 3.5 pips.

The real spread in the data is clearly not working as intended. Is it possible to set a fixed one for MQL5? Or at least one that actually corresponds to reality?

During news events spreads can get very large. So if you are using MQL5, check if it was during a news event!

If you are in fact using real tick data for the test, then check the tick data details for that time period.

If using synthesized tick data, then have the EA print out the spread during a period before and after to see if that is what is happening.

 
Fernando Carreiro:

During news events spreads can get very large. So if you are using MQL5, check if it was during a news event!

If you are in fact using real tick data for the test, then check the tick data details for that time period.

If using synthesized tick data, then have the EA print out the spread during a period before and after to see if that is what is happening.

I selected the option to use ticks based on real ticks, which should right, isn't it? Also, I'm not using a custom symbol, it's the EURUSD one.

Still, it happens almost in every trade, not only on particular moments. That screenshot I posted was not an outlier, but what actually happens pretty much 100% of the time.

Also, how do I check the tick data details as you described?

 
Mateus Soraggi:

I selected the option to use ticks based on real ticks, which should right, isn't it? Also, I'm not using a custom symbol, it's the EURUSD one.

Still, it happens almost in every trade, not only on particular moments. That screenshot I posted was not an outlier, but what actually happens pretty much 100% of the time.

Also, how do I check the tick data details as you described?

1. Print out the bid/ask prices before, during and after.

2. Check if you have not maybe defined a large delay in the Backtesting as this can have a large impact or lower timeframes. My default, it should be 1000ms (1 second). Test with zero delay and random delay too.

 
Fernando Carreiro:

1. Print out the bid/ask prices before, during and after.

2. Check if you have not maybe defined a large delay in the Backtesting as this can have a large impact or lower timeframes. My default, it should be 1000ms (1 second). Test with zero delay and random delay too.

Thanks a lot for your replies, but I really can't seem to make this work.
I did print the bid/ask prices and they continue to have an absurd spread, I also tested the delays (I was testing with zero delay) you suggested and the spread stays the same.
I'll record my screen with the chart open and post it here to make it clearer. I really want to make this work, I've been trying for hours and hours.

 
Mateus Soraggi:

Thanks a lot for your replies, but I really can't seem to make this work.
I did print the bid/ask prices and they continue to have an absurd spread, I also tested the delays (I was testing with zero delay) you suggested and the spread stays the same.
I'll record my screen with the chart open and post it here to make it clearer. I really want to make this work, I've been trying for hours and hours.

Try to clear all the price quote data and tick data, both in the operations section and in the tester section, and have it all downloaded again. There could be some corrupt data!
 
Also, remember that the Chart price data and the Tester price data are separate and not shared, so they can theoretically be different.
Reason: