Error on ask price

 

I have a simple EA that enters long when an ema is going up, and short when an EMA is going down. When I run this EA in the Strategy Tester Visualization, the price at which buy operations are entered is much larger than the real price. In turn, the Sell operations appear to be on the right price. See position of Long and Short arrows in the image below. Apologies is this is basic, I am new to MQL5 coding, but I have followed several tutorials by the line, and I cannot find what could be the cause of this. Any thoughts?


Here is the code:

#include <Trade\Trade.mqh>  
CTrade Trade;

//General variables
   input string    PairName="CHFJPY";
   double Lots=0.01;               // stores  size of lots to buy or sell
   bool IsNewbar; //Bollean to do calcualtion only on new bar.
   double ClosePrice[];

// Moving average variables
   int              MovAve_Handle;
   double           MovAve_Buffer[]; 


int OnInit()
{
   MovAve_Handle= iMA(PairName,PERIOD_M1, 100,0, MODE_SMA,PRICE_CLOSE);
   ArraySetAsSeries(MovAve_Buffer,true);
          
   ArraySetAsSeries(ClosePrice,true);
   CopyClose(PairName,PERIOD_M1,1,2,ClosePrice);
            
   return(INIT_SUCCEEDED);
}



void OnTick()
{
    double Ask = NormalizeDouble(SymbolInfoDouble(PairName,SYMBOL_ASK),_Digits); // Get the Ask Price
    double Bid = NormalizeDouble(SymbolInfoDouble(PairName,SYMBOL_BID),_Digits); // Get the Bid Price
    
///---check for new bar
      static datetime StartTime;
      datetime ThisBarTime = iTime(PairName,PERIOD_M1,0);
      if(StartTime != ThisBarTime) {IsNewbar=true; StartTime = ThisBarTime; } else {IsNewbar=false;}
      if(IsNewbar==true){
      
       // close price
            CopyClose(PairName,PERIOD_M1,1,2,ClosePrice);
            
      //Data from Moving Average
            CopyBuffer(MovAve_Handle,0,1,2,MovAve_Buffer);
            
            
         //Enter long at ask price
            if (ClosePrice[0] > MovAve_Buffer[0] && ClosePrice[1]<=MovAve_Buffer[1]) {
             Trade.Buy(Lots,PairName, Ask);
            }

         //Enter Short at bid price
            if (ClosePrice[0] < MovAve_Buffer[0] && ClosePrice[1]>=MovAve_Buffer[1]) {
             Trade.Sell(Lots,PairName, Bid);
            }
      }   
}



The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
Files:
Buy.png  88 kb
 
On the chart you see only bid prices. Use Comment() to see actual ask and bid and Print() to log ask and bid the moment you trade.
 

I found out that the problem is that the spread is preset at 60 points in the historical runs. I have learned that apparently that is set by the broker, and one could adjust the spread manually in MQL4, but no so easily in MQL5. In MQL5, there is an option under symbol, where one can set the spread manually. but it does not work. By this I mean you can set it to say 10, as in the example attached, but once in the simulator... it goes back to the original value (60). Any thoughts on how to adjust the spread in MQL5 for the historical simulations?

Files:
Spread.png  141 kb
 
Camilo Mora #: I found out that the problem is that the spread is preset at 60 points in the historical runs. I have learned that apparently that is set by the broker, and one could adjust the spread manually in MQL4, but no so easily in MQL5. In MQL5, there is an option under symbol, where one can set the spread manually. but it does not work. By this I mean you can set it to say 10, as in the example attached, but once in the simulator... it goes back to the original value (60). Any thoughts on how to adjust the spread in MQL5 for the historical simulations?

Are you sure, you are saving the value by clicking OK?

It should work, as long as you don't test with real tick data.

 

Hello Fernando,


Unfortunately, on my end changing the spread manually does not work. This is the approach I am using:

1. On the Strategy Tester in MQL5, Click on the Symbol icon.

2. Change spread (lets say to 1).

3. Click Ok.

4. Back to the Strategy tester, Click Start.

That will open, the Strategy Tester Visualization, but the spread appears as 60, and it does not change for the lapse of the simulation. I show the steps on the images attached. Any thoughts on how to modify the spread?. I am looking into some videos claiming that one has to create a new symbol and modify the data manually on the file created....sounds laborious, but will check and report.

Files:
Spread1.png  141 kb
Spread2.png  55 kb
Spread3.png  90 kb
 
Camilo Mora #: Unfortunately, on my end changing the spread manually does not work. This is the approach I am using:
  1. On the Strategy Tester in MQL5, Click on the Symbol icon.
  2. Change spread (lets say to 1).
  3. Click Ok.
  4. Back to the Strategy tester, Click Start.

That will open, the Strategy Tester Visualization, but the spread appears as 60, and it does not change for the lapse of the simulation. I show the steps on the images attached. Any thoughts on how to modify the spread?. I am looking into some videos claiming that one has to create a new symbol and modify the data manually on the file created....sounds laborious, but will check and report.

That is very strange.

Have you tried with a different broker or a different account type with the same broker?

 

I have tried in two brokers and the problem is the same.

I found that another person got to the same trouble. https://www.mql5.com/en/forum/58825

The end suggestion on that post was to use TesterDeposit() and TesterWithdrawal(), to adjust the gains. I am still trying to figure out how would that work...

How to change spread during backtesting ?
How to change spread during backtesting ?
  • 2015.05.16
  • www.mql5.com
Hi. All I spent nearly 8 hours to find out how to change spread during backtesting and optimization...
 
Camilo Mora #:

I have tried in two brokers and the problem is the same.

I found that another person got to the same trouble. https://www.mql5.com/en/forum/58825

The end suggestion on that post was to use TesterDeposit() and TesterWithdrawal(), to adjust the gains. I am still trying to figure out how would that work...

No, I would suggest a clean new installation, as it seems the problem is with your MetaTrader setup.

By the way, what build are you using?

The latest official build is 3980.

 

My build is 3981. I will re-install the 3980 as you suggest to check.

Files:
Build.png  62 kb
 

I reinstalled MQL5, and that fixed something but not completely.

If I change the spread in Symbol, the spread used in the Testing changes somewhat ....like if it was some transformation in between.

In the example attached, I set the spread to 10 in the Tested Symbol form ...but in the Visualization the spread used is 17. Images attached. Any thoughts?

Files:
SpreadSet.png  41 kb
SpreadUsed.png  44 kb
 
Camilo Mora #:

I reinstalled MQL5, and that fixed something but not completely.

If I change the spread in Symbol, the spread used in the Testing changes somewhat ....like if it was some transformation in between.

In the example attached, I set the spread to 10 in the Tested Symbol form ...but in the Visualization the spread used is 17. Images attached. Any thoughts?

What modelling are you using?