Array out of range - página 2

 

obrigado a todos q ajudaram, depois de tentar muitas coisas percebi q o Copybuffer sempre é uma serie, ficou meio confuso mas é por que eu devia ter ciado uma função pra copiar as medias moveis pro aray do Sort ao inves de usar o "i" do loop pra isso tbm.


Mas pelo menos funciona vou aperfeiçoando ele com o tempo e conforme aprendo mais, agora começa a parte dificl de criar um EA baseado nisso, obrigado pela ajuda


//+------------------------------------------------------------------+
//|                                                      FiboMAs.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_minimum -2
#property indicator_maximum 10
#property indicator_buffers 9
#property indicator_plots   9
//----inputs MAs
input int p1=3;
input int p2=13;
input int p3=34;
input int p4=55;
input int p5=89;
input int p6=144;
input int p7=233;
input int p8=610;
input int p9=987;
//--- plot MA1
#property indicator_label1  "MA1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot MA2
#property indicator_label2  "MA2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDarkOrange
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
//--- plot MA3
#property indicator_label3  "MA3"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrGreenYellow
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2
//--- plot MA4
#property indicator_label4  "MA4"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrMediumSpringGreen
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2
//--- plot MA5
#property indicator_label5  "MA5"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrOrchid
#property indicator_style5  STYLE_SOLID
#property indicator_width5  2
//--- plot MA6
#property indicator_label6  "MA6"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrFuchsia
#property indicator_style6  STYLE_SOLID
#property indicator_width6  2
//--- plot MA7
#property indicator_label7  "MA7"
#property indicator_type7   DRAW_LINE
#property indicator_color7  clrBlue
#property indicator_style7  STYLE_SOLID
#property indicator_width7  2
//--- plot MA8
#property indicator_label8  "MA8"
#property indicator_type8   DRAW_LINE
#property indicator_color8  clrIndigo
#property indicator_style8  STYLE_SOLID
#property indicator_width8  2
//--- plot MA9
#property indicator_label9  "MA9"
#property indicator_type9   DRAW_LINE
#property indicator_color9  clrWhiteSmoke
#property indicator_style9  STYLE_SOLID
#property indicator_width9  2
//--- indicator buffers
double         ma1Buffer[];
double         ma2Buffer[];
double         ma3Buffer[];
double         ma4Buffer[];
double         ma5Buffer[];
double         ma6Buffer[];
double         ma7Buffer[];
double         ma8Buffer[];
double         ma9Buffer[];
//------ MA handles

int MA1Handle,MA2Handle,MA3Handle,MA4Handle,MA5Handle,MA6Handle,MA7Handle,MA8Handle,MA9Handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ma1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,ma2Buffer,INDICATOR_DATA);
   SetIndexBuffer(2,ma3Buffer,INDICATOR_DATA);
   SetIndexBuffer(3,ma4Buffer,INDICATOR_DATA);
   SetIndexBuffer(4,ma5Buffer,INDICATOR_DATA);
   SetIndexBuffer(5,ma6Buffer,INDICATOR_DATA);
   SetIndexBuffer(6,ma7Buffer,INDICATOR_DATA);
   SetIndexBuffer(7,ma8Buffer,INDICATOR_DATA);
   SetIndexBuffer(8,ma9Buffer,INDICATOR_DATA);
   
   
   PlotIndexGetInteger(0,PLOT_DRAW_BEGIN,p1);
   PlotIndexGetInteger(1,PLOT_DRAW_BEGIN,p2);
   PlotIndexGetInteger(2,PLOT_DRAW_BEGIN,p3);
   PlotIndexGetInteger(3,PLOT_DRAW_BEGIN,p4);
   PlotIndexGetInteger(4,PLOT_DRAW_BEGIN,p5);
   PlotIndexGetInteger(5,PLOT_DRAW_BEGIN,p6);
   PlotIndexGetInteger(6,PLOT_DRAW_BEGIN,p7);
   PlotIndexGetInteger(7,PLOT_DRAW_BEGIN,p8);
   PlotIndexGetInteger(9,PLOT_DRAW_BEGIN,p9);
   
   
   
//----setting MAs handle (instanciando as medias moveis)    

   MA1Handle=iMA(NULL,0,p1,0,MODE_EMA,PRICE_CLOSE);
   MA2Handle=iMA(NULL,0,p2,0,MODE_EMA,PRICE_CLOSE);
   MA3Handle=iMA(NULL,0,p3,0,MODE_EMA,PRICE_CLOSE);
   MA4Handle=iMA(NULL,0,p4,0,MODE_EMA,PRICE_CLOSE);
   MA5Handle=iMA(NULL,0,p5,0,MODE_EMA,PRICE_CLOSE);
   MA6Handle=iMA(NULL,0,p6,0,MODE_EMA,PRICE_CLOSE);
   MA7Handle=iMA(NULL,0,p7,0,MODE_EMA,PRICE_CLOSE);
   MA8Handle=iMA(NULL,0,p8,0,MODE_EMA,PRICE_CLOSE);
   MA9Handle=iMA(NULL,0,p9,0,MODE_EMA,PRICE_CLOSE);

   if(p1>p2 || p2>p3 || p3>p4 || p4>p5 || p5>p6 || p6>p7 || p7>p8 || p8>p9)
     {

      Print("Invalid inputs");
      return(0);
     }

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+


int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {



  

   if(rates_total<p9)
      return(0);

//---- MAs
   double MA1[],MA2[],MA3[],MA4[],MA5[],MA6[],MA7[],MA8[],MA9[];
   
  


// creating mas array
   double mas[9];
   double maSorted[9];
   int n=ArraySize(mas);
   int to_copy;
  
   
   if(prev_calculated==0)
     {
      to_copy=rates_total;
        }else{
      to_copy=1;
     
      }

//----copy the handles buffers to an arrary
   if(CopyBuffer(MA1Handle,0,0,to_copy, MA1)==0 ||
      CopyBuffer(MA2Handle, 0, 0, to_copy, MA2)==0 ||
      CopyBuffer(MA3Handle, 0, 0, to_copy, MA3)==0 ||
      CopyBuffer(MA4Handle, 0, 0, to_copy, MA4)==0 ||
      CopyBuffer(MA5Handle, 0, 0, to_copy, MA5)==0 ||
      CopyBuffer(MA6Handle, 0, 0, to_copy, MA6)==0 ||
      CopyBuffer(MA7Handle, 0, 0, to_copy, MA7)==0 ||
      CopyBuffer(MA8Handle, 0, 0, to_copy, MA8)==0 ||
      CopyBuffer(MA9Handle,0,0,to_copy,MA9)==0)
     {

      Print("Error in CopyBuffer");
      return(0);
     }

   for(int i=0; i<to_copy; i++)
     {
     

  
      int bi=i;
      if(prev_calculated>0)bi=rates_total-1;
 
  
      mas[0]=MA1[i];
      mas[1]=MA2[i];
      mas[2]=MA3[i];
      mas[3]=MA4[i];
      mas[4]=MA5[i];
      mas[5]=MA6[i];
      mas[6]=MA7[i];
      mas[7]=MA8[i];
      mas[8]=MA9[i];



      for(int j=0; j<n; j++)
        {
         maSorted[j]=mas[j];
         
        }
    
      Sort(n,maSorted);

      int buffer1 = ArrayBsearch(maSorted,mas[0]);
      int buffer2 = ArrayBsearch(maSorted,mas[1]);
      int buffer3 = ArrayBsearch(maSorted,mas[2]);
      int buffer4 = ArrayBsearch(maSorted,mas[3]);
      int buffer5 = ArrayBsearch(maSorted,mas[4]);
      int buffer6 = ArrayBsearch(maSorted,mas[5]);
      int buffer7 = ArrayBsearch(maSorted,mas[6]);
      int buffer8 = ArrayBsearch(maSorted,mas[7]);
      int buffer9 = ArrayBsearch(maSorted,mas[8]);


      ma1Buffer[bi] = buffer1;
      ma2Buffer[bi] = buffer2;
      ma3Buffer[bi] = buffer3;
      ma4Buffer[bi] = buffer4;
      ma5Buffer[bi] = buffer5;
      ma6Buffer[bi] = buffer6;
      ma7Buffer[bi] = buffer7;
      ma8Buffer[bi] = buffer8;
      ma9Buffer[bi] = buffer9;

     Print("buffer1:    ",buffer1, "     buffer2:     ",buffer2);
    
   
    

     }
     
     Print("oncalculate print   ","rates_total:  ", rates_total, "  prev calculeted:  ", prev_calculated, "  MA1:  ", ArraySize(MA1), "  to copy  ", to_copy);
//--- return value of prev_calculated for next call
   return(rates_total);
  };
//+------------------------------------------------------------------+

void Swap(double &arr[],int j)
  {

   double temp;
   temp=arr[j];
   arr[j]=arr[j+1];
   arr[j+1]=temp;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Sort(int n,
          double &mas_1[])
  {

//////ORGANIZANDO O ARRAR MASORTED

   for(int i=0; i<n; i++)
     {
      for(int j=0; j<n-1; j++)
        {
         if(mas_1[j]>mas_1[j+1])
           {

            Swap(mas_1,j);

           }
        }
     }
  }
//+------------------------------------------------------------------+
 
pedro de:

obrigado a todos q ajudaram, depois de tentar muitas coisas percebi q o Copybuffer sempre é uma serie, ficou meio confuso mas é por que eu devia ter ciado uma função pra copiar as medias moveis pro aray do Sort ao inves de usar o "i" do loop pra isso tbm.


Mas pelo menos funciona vou aperfeiçoando ele com o tempo e conforme aprendo mais, agora começa a parte dificl de criar um EA baseado nisso, obrigado pela ajuda


Ah, Obrigado MinionsLab pela dica do print, chegou uma hora que o codigo tinha um print pra tudo e me ajudou mto

 
pedro de:

Ah, Obrigado MinionsLab pela dica do print, chegou uma hora que o codigo tinha um print pra tudo e me ajudou mto

Valeu!

Assista videos de como usar o Debugger do MetaEditor, vai te ajudar pra caramba!

Abração!


;)

 
Esse problema é simples de resolver, basta colocar o tamanho dos arrays quando declarar... (entre os colchetes, na declaração, o processador do seu pc e do pc da corretora precisa saber quantos espaços na memória serão armazenados). Vc coloca ou o próprio valor (inteiro positivo), ou faz um input com uma variável int... 
Razão: