Copied buffer returns 0.0. Array inside indicator does return correct values

 

Hello, I made an indicator that shows the round number s/r levels on a chart.

I wanted to use it inside an EA, but when i tried to use copybuffer the array thats copied to, returned 0.0.

I checked the indicator arrays, they return correct value.
I tried setting the size of the array, inside EA and indi, no luck.
I checked if copybuffer returned a bool which would equal that it copied correctly, it returns the amount copied.
I checked the EA arrays, which return 0.0 after it copied correctly.
I tried looping over the objects and writing their price values into a separate array, it returned 0.0 again.
I checked if EA and Indi return any error, by printing, there are none.

this is the EA code:

int SRhandle;
double support[];

int OnInit(){
   SRhandle = iCustom(_Symbol,PERIOD_CURRENT,"SupportAndResistanceV2",100,10);
   Print("handle = ", SRhandle);
   return(INIT_SUCCEEDED);
  }
  
void OnTick(){
  Print(CopyBuffer(SRhandle,0,0,2,support), " ", GetLastError());
  
  CopyBuffer(SRhandle,0,0,2,support);
  for(int i=0; i<2; i++) Print(support[i]," ", i);
  //Print(GetLastError()); 
  }

this is the indicator code:

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_label1  "Support/Resistance"

double Resistance[];
double Support[];

input double Precision = 100;   //Increments/Precision
input int MaxLines = 10;
input color ResistanceCLR = clrRed;
input color SupportCLR = clrGreen;

int OnInit(){
   SetIndexBuffer(0,Support,INDICATOR_DATA);
   SetIndexBuffer(1,Resistance,INDICATOR_DATA);     
   return(INIT_SUCCEEDED);
  }

int  OnCalculate( const int        rates_total,       // price[] array size 
                  const int        prev_calculated,   // number of handled bars at the previous call 
                  const int        begin,             // index number in the price[] array meaningful data starts from 
                  const double&    price[]){          // array of values for calculation 
                  
   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);             
   double NumberToRound = bid;
   
   double RoundedNumberResistance = round((NumberToRound / Precision) + 0.5) * Precision;
   double RoundedNumberSupport = round((NumberToRound / Precision) + 0.5) * Precision - Precision;

   Resistance[0] = RoundedNumberResistance;
   Support[0] = RoundedNumberSupport;
   
   for(int i=1; i<MaxLines; i++){
      RoundedNumberResistance += Precision;
      RoundedNumberSupport -= Precision;
      Resistance[i] = RoundedNumberResistance;
      Support[i] = RoundedNumberSupport;
      }     
               
   for(int i=0; i<MaxLines; i++){
      ObjectCreate(0,"Resistance"+i,OBJ_HLINE,0,0,Resistance[i]);
      ObjectSetInteger(0,"Resistance"+i,OBJPROP_COLOR,ResistanceCLR);
      ObjectCreate(0,"Support"+i,OBJ_HLINE,0,0,Support[i]);
      ObjectSetInteger(0,"Support"+i,OBJPROP_COLOR,SupportCLR);
      
      //Print(i, " ",Resistance[i]," ", Support[i]);
      }
   //Print(GetLastError());   
   return(rates_total);
}

void OnDeinit(const int reason){
   for(int i=0; i<MaxLines; i++){
      ObjectsDeleteAll(0,"Resistance"+i);
      ObjectsDeleteAll(0,"Support"+i);
      }
}

If someone could help me, its much appreciated. Thank you for your time.

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Try adding top indicator

#property indicator_buffers 2
#property indicator_plots   2
 
Amir Yacoby #:

Try adding top indicator

Thanks for the response, ive added it, and chart_window, label1, like in the code above. But the results did not change..

 
Elmer013 #:

Thanks for the response, ive added it, and chart_window, label1, like in the code above. But the results did not change..

The code fills 10 cells in the buffers - 0..10 but as the buffer is not defined AsSeries, those are the left bars, not the most recent ones.

Add in OnInit

 ArraySetAsSeries(Support,true);
ArraySetAsSeries(Resistance,true);

And OnCalculate

ArraySetAsSeries(price,true);
 
Amir Yacoby #:

The code fills 10 cells in the buffers - 0..10 but as the buffer is not defined AsSeries, those are the left bars, not the most recent ones.

Add in OnInit

And OnCalculate

@Amir Yacoby Thanks for the idea's..

I already added setindexbuffer inside OnInit.
I am not using the price array to get the bids. I'm using symbolinfodouble to get the bid. I now tried setting bid = price[1], whilst ArraySetAsSeries(price,true). 

The above did not fix the issue...

I dont think that it has anything to do with time series, as when printing the buffers inside the indicator i do get the correct values. But when using copybuffer inside EA it returns 0.0. Please correct me if im wrong.

 
Elmer013 #:

@Amir Yacoby Thanks for the idea's..

I already added setindexbuffer inside OnInit.
I am not using the price array to get the bids. I'm using symbolinfodouble to get the bid. I now tried setting bid = price[1], whilst ArraySetAsSeries(price,true). 

The above did not fix the issue...

I dont think that it has anything to do with time series, as when printing the buffers inside the indicator i do get the correct values. But when using copybuffer inside EA it returns 0.0. Please correct me if im wrong.

Sorry was a typo for the late hour.
Meant of course in OnInit to add
ArraySetAsSeries(Support,true);
ArraySetAsSeries(Resistance,true);
 
Elmer013 #:

I dont think that it has anything to do with time series, as when printing the buffers inside the indicator i do get the correct values. But when using copybuffer inside EA it returns 0.0. Please correct me if im wrong.

Of course it has anything to do with the time series. 
You see it in the indicator, but how does the EA copying the data array should know which side is the current prices and which side is the former. 

Your original code was filling the 0..10 indexes(when the timeseries was false it means that for the EA those were indexes rates_total-1 until the rates_total-11 data - those are the far left prices on the chart)- which can be many years back.
Your indicator printed it correctly, but it didn't care the prices are so old.

Your EA tries to read the current prices(from the right side of the chart)- indexed 0..2 but those were not filled.

Another thing, you create 10 OBJ_HLINE objects on each tick, I don't know what you are trying to do here but you will do the work only on the first tick, and all the next ones will be trying to create duplicate objects.

* Also you can unite the two loops to one, having the same from-to-end condition:

   for(int i=1; i<MaxLines; i++){
      RoundedNumberResistance += Precision;
      RoundedNumberSupport -= Precision;
      Resistance[i] = RoundedNumberResistance;
      Support[i] = RoundedNumberSupport;
      ObjectCreate(0,"Resistance"+i,OBJ_HLINE,0,0,Resistance[i]);
      ObjectSetInteger(0,"Resistance"+i,OBJPROP_COLOR,ResistanceCLR);
      ObjectCreate(0,"Support"+i,OBJ_HLINE,0,0,Support[i]);
      ObjectSetInteger(0,"Support"+i,OBJPROP_COLOR,SupportCLR);
      
      //Print(i, " ",Resistance[i]," ", Support[i]);
      }
 
I managed to solve it myself. It turned out I forgot to set the Support/Resistance array as series. Thus it returning 0.0 inside the EA. I changed the OnInit function as follows:
int OnInit(){
   SetIndexBuffer(0,Support,INDICATOR_DATA);
   SetIndexBuffer(1,Resistance,INDICATOR_DATA);   
   PlotIndexSetString(0,PLOT_LABEL,"Support");
   PlotIndexSetString(1,PLOT_LABEL,"Resistance");
   ArraySetAsSeries(Support,true);
   ArraySetAsSeries(Resistance,true);
   return(INIT_SUCCEEDED);
  }

Stupid mistake.

int  OnCalculate( const int        rates_total,       // price[] array size 
                  const int        prev_calculated,   // number of handled bars at the previous call 
                  const int        begin,             // index number in the price[] array meaningful data starts from 
                  const double&    price[]){          // array of values for calculation 
   ArraySetAsSeries(price,true);                  
   double bid = price[0]; //SymbolInfoDouble(_Symbol,SYMBOL_BID);             
   double NumberToRound = bid;

@Amir Yacoby thank you for your time.

 
Elmer013 #:
I managed to solve it myself. It turned out I forgot to set the Support/Resistance array as series. Thus it returning 0.0 inside the EA. I changed the OnInit function as follows:
Thats exactly what I was saying..
 
Amir Yacoby #:
Thats exactly what I was saying..

somehow your message starting with: "Of course it has anything to do with the time series......" did not show up on my end.. Thanks anyhow! Your noticing of the loop and lines have been helpful too.

Reason: