CopyBuffer Error.

 

Hi.

my problem is here. when I add my expert to chart this Alert come continuously:  "Error copying Moving Average indicator buffer - error:",GetLastError()" 

all of codes are in below. I would be appreciate if you could test this expert and guide me.  

thanks.

 if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0
      || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(SlowMovingAverageDefinition,3,0,3,SlowMovingAveragearray)<0 || CopyBuffer(FastMovingAverageDefinition,4,0,3,FastMovingAveragearray) )
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }    

all of expert is here.

//+------------------------------------------------------------------+
//|                                            plot MA indicator.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

input int      ADX_Period=9;     // ADX Period
input int      Slow_MA_Period=9;      //Slow Moving Average Period
input int      Fast_MA_Period=50;      //Slow Moving Average Period

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//--- Other parameters
int    adxHandle; // handle for our ADX indicator
int    SlowMovingAverageDefinition; // handle for our slow MA indicator
int    FastMovingAverageDefinition; // handle for our fast MA indicator
double plsDI[],minDI[],adxVal[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each bars
double SlowMovingAveragearray[]; // Dynamic array to hold the values of slow Moving Average for each bars
double FastMovingAveragearray[]; // Dynamic array to hold the values of fast Moving Average for each bars

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Get handle for ADX indicator
adxHandle                   =iADX(NULL,0,ADX_Period);
SlowMovingAverageDefinition = iMA(_Symbol,_Period,Slow_MA_Period,0,MODE_EMA,PRICE_CLOSE);
FastMovingAverageDefinition = iMA(_Symbol,_Period,Fast_MA_Period,0,MODE_EMA,PRICE_CLOSE);
   if(adxHandle<0 || SlowMovingAverageDefinition<0 || FastMovingAverageDefinition<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
      return(-1);
     }
   return(INIT_SUCCEEDED);
     }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void OnDeinit(const int reason)
  {
//--- Release our indicator handles
   IndicatorRelease(adxHandle);
   IndicatorRelease(SlowMovingAverageDefinition);
   IndicatorRelease(FastMovingAverageDefinition);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void OnTick()
  {
     
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     //--- Define some MQL5 Structures we will use for our trade
   MqlTick latest_price;      // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);      // Initialization of mrequest structure
   
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   

// the ADX DI+values array
   ArraySetAsSeries(plsDI,true);
// the ADX DI-values array
   ArraySetAsSeries(minDI,true);
// the ADX values arrays
   ArraySetAsSeries(adxVal,true);
// the MA-8 values arrays
   ArraySetAsSeries(SlowMovingAveragearray,true);
   // the MA-8 values arrays
   ArraySetAsSeries(FastMovingAveragearray,true);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
  
   //--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }


     //--- Copy the new values of our indicators to buffers (arrays) using the handle
   if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0
      || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(SlowMovingAverageDefinition,3,0,3,SlowMovingAveragearray)<0 || CopyBuffer(FastMovingAverageDefinition,4,0,3,FastMovingAveragearray) )
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }    
   return;
    }
//+------------------------------------------------------------------+


Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Indicators Lines
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Indicators Lines
  • www.mql5.com
Some technical indicators have several buffers drawn in the chart. Numbering of indicator buffers starts with 0. When copying indicator values using the CopyBuffer() function into an array of the double type, for some indicators one may indicate the identifier of a copied buffer instead of its number.
 
ramin Beygi:

Hi.

my problem is here. when I add my expert to chart this Alert come continuously:  "Error copying Moving Average indicator buffer - error:",GetLastError()" 

all of codes are in below. I would be appreciate if you could test this expert and guide me.  

thanks.

all of expert is here.

 if(CopyBuffer(SlowMovingAverageDefinition,3,0,3,SlowMovingAveragearray)<0 || CopyBuffer(FastMovingAverageDefinition,4,0,3,FastMovingAveragearray) )

you are missing a comparison in the second part, like  <0   so it will be looking to see if it returns true instead


 
Paul Anscombe:

Hi.

thank you for your attention.

I add the second part but I still have that Error.if(CopyBuffer(SlowMovingAverageDefinition,3,0,3,SlowMovingAveragearray)<0 || CopyBuffer(FastMovingAverageDefinition,4,0,3,FastMovingAveragearray)<0 ) 

 my completely expert code is here. I would be appreciate if you could test this expert in meta trader 5 and advice me.

Im beginner and Im confused.

thank you again.

//+------------------------------------------------------------------+
//|                                            plot MA indicator.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

input int      StopLoss=30;      // Stop Loss
input int      TakeProfit=100;   // Take Profit
input int      ADX_Period=9;     // ADX Period
input int      Slow_MA_Period=9;      //Slow Moving Average Period
input int      Fast_MA_Period=50;      //Slow Moving Average Period
input int      EA_Magic=12345;   // EA Magic Number
input double   Adx_Min=22.0;     // Minimum ADX Value
input double   Lot=0.1;          // Lots to Trade

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//--- Other parameters
int adxHandle; // handle for our ADX indicator
int SlowMovingAverageDefinition; // handle for our slow MA indicator
int FastMovingAverageDefinition; // handle for our fast MA indicator
double plsDI[],minDI[],adxVal[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each bars
double SlowMovingAveragearray[]; // Dynamic array to hold the values of slow Moving Average for each bars
double FastMovingAveragearray[]; // Dynamic array to hold the values of fast Moving Average for each bars
double p_close; // Variable to store the close value of a bar
int STP, TKP;   // To be used for Stop Loss & Take Profit values

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Get handle for ADX indicator
adxHandle=iADX(NULL,0,ADX_Period);
//--- Get the handle for Moving Average indicator
SlowMovingAverageDefinition = iMA(_Symbol,_Period,Slow_MA_Period,0,MODE_EMA,PRICE_CLOSE);
FastMovingAverageDefinition = iMA(_Symbol,_Period,Fast_MA_Period,0,MODE_EMA,PRICE_CLOSE);
   if(adxHandle<0 || SlowMovingAverageDefinition<0 || FastMovingAverageDefinition<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
      return(-1);
     }
     //--- Let us handle currency pairs with 5 or 3 digit prices instead of 4
   STP = StopLoss;
   TKP = TakeProfit;
   if(_Digits==5 || _Digits==3) // For a 5-digit or 3-digit price chart, we multiply both the Stop Loss and the Take Profit by 10.
     {
      STP = STP*10;
      TKP = TKP*10;
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void OnDeinit(const int reason)
  {
//--- Release our indicator handles
   IndicatorRelease(adxHandle);
   IndicatorRelease(SlowMovingAverageDefinition);
   IndicatorRelease(FastMovingAverageDefinition);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void OnTick()
  {
  
  //--- Do we have enough bars to work with
   if(Bars(_Symbol,_Period)<60) // if total bars is less than 60 bars. Bars(_Symbol,_Period) give us count of bars in current chart.
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }  
     
     // We will use the static Old_Time variable to serve the bar time.
// At each OnTick execution we will check the current bar time with the saved one.
// If the bar time isn't equal to the saved time, it indicates that we have a new tick.

   static datetime Old_Time;
   datetime New_Time[1];
   bool IsNewBar=false;

// copying the last bar time to the element New_Time[0]
   int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
   if(copied>0) // ok, the data has been copied successfully
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
        {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
         if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
         Old_Time=New_Time[0];            // saving bar time
        }
     }
   else
     {
      Alert("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
     }
     
     //--- EA should only check for new trade if we have a new bar
   if(IsNewBar==false)
     {
      return;
     }
 
//--- Do we have enough bars to work with
   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }
     
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     //--- Define some MQL5 Structures we will use for our trade
   MqlTick latest_price;      // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);      // Initialization of mrequest structure
   
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
   
/*
     Let's make sure our arrays values for the Rates, ADX Values and MA values 
     is store serially similar to the timeseries array
*/

// the rates arrays
   ArraySetAsSeries(mrate,true);
// the ADX DI+values array
   ArraySetAsSeries(plsDI,true);
// the ADX DI-values array
   ArraySetAsSeries(minDI,true);
// the ADX values arrays
   ArraySetAsSeries(adxVal,true);
// the MA-8 values arrays
   ArraySetAsSeries(SlowMovingAveragearray,true);
   // the MA-8 values arrays
   ArraySetAsSeries(FastMovingAveragearray,true);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
  
   //--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }

//--- Get the details of the latest 3 bars
   if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
     //--- Copy the new values of our indicators to buffers (arrays) using the handle
   if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0
      || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(SlowMovingAverageDefinition,3,0,3,SlowMovingAveragearray)<0 || CopyBuffer(FastMovingAverageDefinition,4,0,3,FastMovingAveragearray)<0 )
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
     //--- we have no errors, so continue
//--- Do we have positions opened already?
   bool Buy_opened=false;  // variable to hold the result of Buy opened position
   bool Sell_opened=false; // variables to hold the result of Sell opened position

   if(PositionSelect(_Symbol)==true) // we have an opened position
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         Buy_opened=true;  //It is a Buy
        }
      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         Sell_opened=true; // It is a Sell
        }
     }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
     // Copy the bar close price for the previous bar prior to the current bar, that is Bar 1
   p_close=mrate[1].close;  // bar 1 close price

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
   
   /*
    1. Check for a long/Buy Setup :tow MA increasing upwnwards, 
    previous price close above it, ADX > 22, +DI > -DI
*/
//--- Declare bool type variables to hold our Sell Conditions
   bool Start_Buy_Situation = (SlowMovingAveragearray[0] > FastMovingAveragearray[0] &&  SlowMovingAveragearray[1] < FastMovingAveragearray[1]); //Slow MA go uper than Fast MA
   bool Buy_Situation       = (SlowMovingAveragearray[0] > FastMovingAveragearray[0]); //Slow MA is still uper than Fast MA
   bool Buy_Condition_1     = (SlowMovingAveragearray[1] < SlowMovingAveragearray[2]) && (SlowMovingAveragearray[1] < SlowMovingAveragearray[0]);//check if Slope of line is positive or negative
   bool Buy_Condition_2     = (p_close > SlowMovingAveragearray[1]);         // previuos price closed above MA
   bool Buy_Condition_3     = (adxVal[0]>Adx_Min);          // Current ADX value greater than minimum value (22)
   bool Buy_Condition_4     = (plsDI[0]>minDI[0]);          // +DI greater than -DI

//--- Putting all together  
  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if (Start_Buy_Situation)
{
 while(Buy_Situation)
  {  
   if(Buy_Condition_1 && Buy_Condition_2)
     {
      if(Buy_Condition_3 && Buy_Condition_4)
        {
         // any opened Buy position?
         if(Buy_opened)
           {
            Alert("We already have a Buy Position!!!");
            return;    // Don't open a new Buy Position
           }
         ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // latest ask price
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
        }
     }
    
     
    

  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   /*
    1. Check for a short/sell Setup :tow MA dicreasing downwards, 
    previous price close below it, ADX > 22, +DI < -DI
*/
//--- Declare bool type variables to hold our Sell Conditions

   bool Start_Sell_Situation = (SlowMovingAveragearray[0] < FastMovingAveragearray[0] &&  SlowMovingAveragearray[1] > FastMovingAveragearray[1]); //Slow MA go lower than Fast MA
   bool Sell_Situation       = (SlowMovingAveragearray[0] < FastMovingAveragearray[0]); //Slow MA is still lower than Fast MA
   bool Sell_Condition_1     = (SlowMovingAveragearray[1] > SlowMovingAveragearray[2]) && (SlowMovingAveragearray[1] > SlowMovingAveragearray[0]);//check if Slope of line is positive or negative
   bool Sell_Condition_2     = (p_close <SlowMovingAveragearray[1]);  // Previous price closed below MA
   bool Sell_Condition_3     = (adxVal[0]>Adx_Min);                         // Current ADX value greater than minimum (22)
   bool Sell_Condition_4     = (plsDI[0]<minDI[0]);                         // -DI greater than +DI

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
   if (Start_Buy_Situation)
    {
     while(Buy_Situation)
      {
       //--- Putting all together
   if(Sell_Condition_1 && Sell_Condition_2)
     {
      if(Sell_Condition_3 && Sell_Condition_4)
        {
         // any opened Sell position?
         if(Sell_opened)
           {
            Alert("We already have a Sell position!!!");
            return;    // Don't open a new Sell Position
           }
         ZeroMemory(mrequest);
         mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // latest Bid price
         mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                          // currency pair
         mrequest.volume = Lot;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Sell order request could not be completed -error:",GetLastError());
            ResetLastError();
            return;
           }
        }
     }
   return;
      }
    } 


  }
//+------------------------------------------------------------------+


  

 
ramin Beygi:

Hi.

thank you for your attention.

I add the second part but I still have that Error.if(CopyBuffer(SlowMovingAverageDefinition,3,0,3,SlowMovingAveragearray)<0 || CopyBuffer(FastMovingAverageDefinition,4,0,3,FastMovingAveragearray)<0 ) 

 my completely expert code is here. I would be appreciate if you could test this expert in meta trader 5 and advice me.

Im beginner and Im confused.

thank you again.


  

What is the error number that you are getting?
 
  if(CopyBuffer(SlowMovingAverageDefinition,3,0,3,SlowMovingAveragearray)<0 || CopyBuffer(FastMovingAverageDefinition,4,0,3,FastMovingAveragearray)<0 )
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }


what are these numbers supposed to be? 
they should be the buffer number you want from iMA  which only has one so should be 0
this will resolve your issue with error 4806
regards

 
Paul Anscombe:

Hi.

thanks.

my problem resolved. but I have 2 MA indicator and one ADX indicator. why I just need to set one Buffer number for 2 MA indicator but I Need 3 different buffer number for one ADX indicator. 

adxHandle=iADX(NULL,0,ADX_Period);
SlowMovingAverageDefinition = iMA(_Symbol,_Period,Slow_MA_Period,0,MODE_EMA,PRICE_CLOSE);
FastMovingAverageDefinition = iMA(_Symbol,_Period,Fast_MA_Period,0,MODE_EMA,PRICE_CLOSE);

   if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0
      || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(SlowMovingAverageDefinition,0,0,3,SlowMovingAveragearray)<0 || CopyBuffer(FastMovingAverageDefinition,0,0,3,FastMovingAveragearray)<0 )
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }