Error 4806

 

I am currently a beginner in mql5 and I am experimenting in creating new projects, I have an error that I have not seen before, I have read some articles but I can't really find the solution, it happens when I try to use CopyBuffer() from rsi_handle to an already defined dynamic array , this happens only in bakctesting, in real-time execution it works correctly, check the iBars returns a valid value and it is correct, I am open to any advice or solution that can help, thank you for your attention.

#property version   "1.00"
#property indicator_buffers 2
#include <CDynamicArray.mqh>

input int RSI_Period = 14;
input int EMA_Period = 5;
int Wilders_Period = RSI_Period * 2 - 1;
input double QQE = 4.238;
input double lotSize = 1;
input double stopLoss = 1500;

datetime lastCloseTime = 0;
datetime minimunWaitTime = 30;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int rsi_handle;
int rsi_ma_handle;

double RsiMaArray[];
double rsiarray[];

int OnInit()
  {
//---
   SetIndexBuffer(0, RsiMaArray, INDICATOR_CALCULATIONS);
   SetIndexBuffer(1, rsiarray, INDICATOR_CALCULATIONS);
   rsi_handle = iRSI(_Symbol, PERIOD_CURRENT, RSI_Period, PRICE_CLOSE);
   Alert("RSI HANDLE: ", rsi_handle);
   if (rsi_handle < 0){
      Alert("Fail to obtain RSI_HANDLE");
      return (INIT_FAILED);
   }
   int rsicopied = CopyBuffer(rsi_handle, 1, 0, iBars(_Symbol, PERIOD_CURRENT), rsiarray);
   if (rsicopied < 0) {
      Alert("FAILED RSI COPY");
      Alert("FAILED ERROR: ", GetLastError());
      return (INIT_FAILED);
   }
   
   
   rsi_ma_handle = iMA(_Symbol, PERIOD_CURRENT, EMA_Period, 0, MODE_EMA, rsi_handle);
   if (rsi_ma_handle < 0) {
      Alert("Fail to obtain RSI_MA_HANDLE");
      return (INIT_FAILED);
   }
   
   int total_bars = iBars(_Symbol, PERIOD_CURRENT);
   Alert("Total bars: ", total_bars);
    
   if (total_bars < 2) {
        Alert("Not enough bars available for initialization.");
        return (INIT_FAILED);
    }
   
   int copied = CopyBuffer(rsi_ma_handle, 0, 0, total_bars, RsiMaArray);
   Alert("Copied: ", copied);
   if ( copied < 0)
   {
      Alert("Failed to copy RsiMaArray: ", GetLastError());
      return(INIT_FAILED);
   } 
   
   for (int i = 0; i < (int) RsiMaArray.Size() - 1; i++)
   {
      AtrRsi.AddValue(MathAbs(RsiMaArray[i] - RsiMaArray[i + 1]));
      if (i > 398)
      {
         MaAtrRsi.AddValue(AtrRsi.iMAOnArrayMQL4(0, Wilders_Period, 0, MODE_EMA, 0));
         dar.AddValue(MaAtrRsi.iMAOnArrayMQL4(0, Wilders_Period, 0, MODE_EMA, 0) * QQE);  
      }
   }
   
   ArraySetAsSeries(RsiMaArray, true);   
//---
   return(INIT_SUCCEEDED);
  }
Projects assist in creating profitable trading robots! Or at least, so it seems
Projects assist in creating profitable trading robots! Or at least, so it seems
  • www.mql5.com
A big program starts with a small file, which then grows in size as you keep adding more functions and objects. Most robot developers utilize include files to handle this problem. However, there is a better solution: start developing any trading application in a project. There are so many reasons to do so.
Files:
net.png  14 kb
 
zumaya01: I have an error that I have not seen before, I have read some articles but I can't really find the solution, it happens when I try to use CopyBuffer() from rsi_handle
int OnInit() {

   rsi_handle = iRSI(_Symbol, PERIOD_CURRENT, RSI_Period, PRICE_CLOSE);

   int rsicopied = CopyBuffer(rsi_handle, 1, 0, iBars(_Symbol, PERIOD_CURRENT), rsiarray);
  1. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

  2. Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.