MT5, mql5 bugs to be fixed. - page 3

 

Win 10 latest build, MT5 V5  build 1966, UHD 4K screen

Tester (EA) in Visual Mode breaks synchronisation between Candles and horizontal time scale on chart, when changing resolution (magnifier +/-). Screen shot attached. 0900h candle shows on scale at 0745h.

Reported in parallel to Service Desk, now.

Files:
sc2.png  294 kb
 

Hello, I got a problem when written and tested the following code that all of sudden all of my expert advisors execute buy and sell order at the same time in every expert advisor despite the signal, I didnt have this problem until I coded following advisor. Then I sent this code to my friend and he got same problem, could you give us some advice how to fix it? Thank you a lot.

// import trade.mqh
#include<Trade\Trade.mqh>

// Create an instance of CTrade
CTrade trade;

input int SmallMovingAverage=20;
input int MiddleMovingAverage=50;
input int BigMovingAverage=100;
input int a1=100;//SL BUY
input int b1=100;//TP BUY
input int a2=100;//SL SELL
input int b2=100;//TP SELL

void OnTick()
  {
   // Get the Ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
     
   // Get the Bid price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   //create a string for the signal
   string signal=" ";
   
   //create an Array for several prices
   double SmallMovingAverageArray[],MiddleMovingAverageArray[],BigMovingAverageArray[];
   
   //define the properties of the Moving Average
   int SmallMovingAverageDefinition = iMA (_Symbol,_Period,SmallMovingAverage,0,MODE_EMA,PRICE_CLOSE);
   
   //define the properties of the Moving Average
   int MiddleMovingAverageDefinition = iMA (_Symbol,_Period,MiddleMovingAverage,0,MODE_EMA,PRICE_CLOSE);
   
   //define the properties of the Moving Average
   int BigMovingAverageDefinition = iMA (_Symbol,_Period,BigMovingAverage,0,MODE_EMA,PRICE_CLOSE);

   // Defined EA, one line, from candle 0,for 3 candles, store result
   CopyBuffer(SmallMovingAverageDefinition,0,0,3,SmallMovingAverageArray); 

   // Defined EA, one line, from candle 0,for 3 candles, store result
   CopyBuffer(MiddleMovingAverageDefinition,0,0,3,MiddleMovingAverageArray); 

   // Defined EA, one line, from candle 0,for 3 candles, store result
   CopyBuffer(BigMovingAverageDefinition,0,0,3,BigMovingAverageArray);  
   
 
   //and was below the SMA before
   if (SmallMovingAverageArray[1] < MiddleMovingAverageArray[1]&& MiddleMovingAverageArray[1]<BigMovingAverageArray[1])
   {
   signal="buy";
   }
   
   //and was below the SMA before
   if (SmallMovingAverageArray[1] > MiddleMovingAverageArray[1]&& MiddleMovingAverageArray[1]>BigMovingAverageArray[1])
   
   {    
   signal="sell"; 
   } 

   //buy
   if (signal =="buy" && PositionsTotal()<1)
   trade.Buy(1.00,NULL,Ask,Ask-a1*_Point,Ask+b1*_Point,NULL);
   
   //sell
   if (signal =="sell" && PositionsTotal()<1)
   trade.Sell(1.00,NULL,Bid,Bid+a2*_Point,Bid-b2*_Point,NULL);

   
   //chart output
   Comment ("The signal is now: ",signal); 
 
   
    
}

 
Miloš Hirko :

Hello, I got a problem when written and tested the following code that all of sudden all of my expert advisors execute buy and sell order at the same time in every expert advisor despite the signal, I didnt have this problem until I coded following advisor. Then I sent this code to my friend and he got same problem, could you give us some advice how to fix it? Thank you a lot.

Categorically it is NOT possible to receive an indicator handle AT EVERY TICK! An indicator handle must be created ONCE in OnInit ().

Example: 

Creating an iMA indicator handle, getting indicator values

How to start with MQL5
How to start with MQL5
  • 2020.03.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Miloš Hirko :


Now, now I have corrected your code. The EA checks the signal ONLY AT THE TIME OF THE NEW BAR BIRTH (it makes no sense to work inside the bar on every tick).

//+------------------------------------------------------------------+
//|                                                           11.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
//---
#include <Trade\Trade.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
//--- input parameters
input int                  Inp_Small_MA_ma_period        = 20;          // Small MA: averaging period
input int                  Inp_Small_MA_ma_shift         = 0;           // Small MA: horizontal shift
input ENUM_MA_METHOD       Inp_Small_MA_ma_method        = MODE_EMA;    // Small MA: smoothing type
input ENUM_APPLIED_PRICE   Inp_Small_MA_applied_price    = PRICE_CLOSE; // Small MA: type of price
input int                  Inp_Middle_MA_ma_period       = 50;          // Middle MA: averaging period
input int                  Inp_Middle_MA_ma_shift        = 0;           // Middle MA: horizontal shift
input ENUM_MA_METHOD       Inp_Middle_MA_ma_method       = MODE_EMA;    // Middle MA: smoothing type
input ENUM_APPLIED_PRICE   Inp_Middle_MA_applied_price   = PRICE_CLOSE; // Middle MA: type of price
input int                  Inp_Big_MA_ma_period          = 1000;        // Big MA: averaging period
input int                  Inp_Big_MA_ma_shift           = 0;           // Big MA: horizontal shift
input ENUM_MA_METHOD       Inp_Big_MA_ma_method          = MODE_EMA;    // Big MA: smoothing type
input ENUM_APPLIED_PRICE   Inp_Big_MA_applied_price      = PRICE_CLOSE; // Big MA: type of price
//---
input ushort   InpStopLossBuy    = 100;      // Stop Loss Buy, in points (1.00045-1.00055=10 points)
input ushort   InpTakeProfitBuy  = 100;      // Take Profit Buy, in points (1.00045-1.00055=10 points)
input ushort   InpStopLossSell   = 100;      // Stop Loss Sell, in points (1.00045-1.00055=10 points)
input ushort   InpTakeProfitSell = 100;      // Take Profit Sell, in points (1.00045-1.00055=10 points)
//---
double   m_stop_loss_buy            = 0.0;      // Stop Loss Buy           -> double
double   m_take_profit_buy          = 0.0;      // Take Profit Buy         -> double
double   m_stop_loss_sell           = 0.0;      // Stop Loss Sell          -> double
double   m_take_profit_sell         = 0.0;      // Take Profit Sell        -> double

int      handle_iMA_Small;                      // variable for storing the handle of the iMA indicator
int      handle_iMA_Middle;                     // variable for storing the handle of the iMA indicator
int      handle_iMA_Big;                        // variable for storing the handle of the iMA indicator

datetime m_prev_bars                = 0;        // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   m_trade.SetExpertMagicNumber(159);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(Symbol());
   m_trade.SetDeviationInPoints(30);
//---
   m_stop_loss_buy         = InpStopLossBuy     * Point();
   m_take_profit_buy       = InpTakeProfitBuy   * Point();
   m_stop_loss_sell        = InpStopLossSell    * Point();
   m_take_profit_sell      = InpTakeProfitSell  * Point();
//--- create handle of the indicator iMA
   handle_iMA_Small=iMA(Symbol(),Period(),Inp_Small_MA_ma_period,Inp_Small_MA_ma_shift,
                        Inp_Small_MA_ma_method,Inp_Small_MA_applied_price);
//--- if the handle is not created
   if(handle_iMA_Small==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator (\"Small\") for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//--- create handle of the indicator iMA
   handle_iMA_Middle=iMA(Symbol(),Period(),Inp_Middle_MA_ma_period,Inp_Middle_MA_ma_shift,
                         Inp_Middle_MA_ma_method,Inp_Middle_MA_applied_price);
//--- if the handle is not created
   if(handle_iMA_Middle==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator (\"Middle\") for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//--- create handle of the indicator iMA
   handle_iMA_Big=iMA(Symbol(),Period(),Inp_Big_MA_ma_period,Inp_Big_MA_ma_shift,
                      Inp_Big_MA_ma_method,Inp_Big_MA_applied_price);
//--- if the handle is not created
   if(handle_iMA_Big==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator (\"Big\") for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(Symbol(),Period(),0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;

   if(PositionsTotal()>0)
      return;
//---
   double SmallMovingAverageArray[],MiddleMovingAverageArray[],BigMovingAverageArray[];
   ArraySetAsSeries(SmallMovingAverageArray,true);
   ArraySetAsSeries(MiddleMovingAverageArray,true);
   ArraySetAsSeries(BigMovingAverageArray,true);
   int start_pos=0,count=6;
   if(!iGetArray(handle_iMA_Small,0,start_pos,count,SmallMovingAverageArray) ||
      !iGetArray(handle_iMA_Middle,0,start_pos,count,MiddleMovingAverageArray) ||
      !iGetArray(handle_iMA_Big,0,start_pos,count,BigMovingAverageArray))
     {
      return;
     }

   MqlTick tick;
   if(!SymbolInfoTick(Symbol(),tick))
      return;

   if(SmallMovingAverageArray[1]<MiddleMovingAverageArray[1]&&MiddleMovingAverageArray[1]<BigMovingAverageArray[1])
     {
      m_trade.Buy(1.00,Symbol(),tick.ask,tick.ask-m_stop_loss_buy,tick.ask+m_take_profit_buy);
      return;
     }

   if(SmallMovingAverageArray[1]>MiddleMovingAverageArray[1]&&MiddleMovingAverageArray[1]>BigMovingAverageArray[1])
     {
      m_trade.Sell(1.00,Symbol(),tick.bid,tick.bid+m_stop_loss_sell,tick.bid-m_take_profit_sell);
      return;
     }
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+
Files:
11.mq5  17 kb
Reason: