Stuck iCustom and CopyBuffer functions slow down the price movement on the chart.

 

Hi guys, 

i apreciate any help.


I am stuck, i think this is little problem, that i can't see because my inexperience, even to search about my problem.


I start a indicator, that plots on chart an arrow from technical indexes from 2 indicators.

It actually analizes the crossing of 2 EMA's and and index from a custom Brazilian Indicator, named RAFI.


Basically, if the 2 EMA is crossed and RAFI gives me the buffer 3 it plots the arrow, for buy or sell depending on the case.

But i think, that my inexperience, is causing me trouble.

It Works! But it let the princing movemento of opened candle very slow, and after i remove it. It just comes back in high speed, till the actual current price of the candle.


#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   5
//--- plot shortmedia
#property indicator_label1  "shortMedia"
#property indicator_type1   DRAW_NONE
#property indicator_color1  clrTurquoise
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot longmedia
#property indicator_label2  "longMedia"
#property indicator_type2   DRAW_NONE
#property indicator_color2  clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "RAFI"
#property indicator_type3   DRAW_NONE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

#property indicator_label4  "Compra"
#property indicator_type4   DRAW_ARROW
#property indicator_color4  clrDodgerBlue
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

#property indicator_label5  "Venda"
#property indicator_type5   DRAW_ARROW
#property indicator_color5  clrTomato
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//#property script_show_inputs

int      EMAShort=5;
int      EMALong=15;
double   RAFIindex = 3;
//--- indicator buffers


double         shortMediaBuffer[];
double         longMediaBuffer[];
double         RAFIBuffer[];
double         CompraBuffer[];
double         VendaBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
      SetIndexBuffer(0,shortMediaBuffer,INDICATOR_DATA);
      SetIndexBuffer(1,longMediaBuffer,INDICATOR_DATA);
      SetIndexBuffer(2,RAFIBuffer,INDICATOR_DATA);
      SetIndexBuffer(3,CompraBuffer,INDICATOR_DATA); 
      SetIndexBuffer(4,VendaBuffer,INDICATOR_DATA);  
   //---
      PlotIndexSetInteger(3,PLOT_ARROW,233);
      PlotIndexSetInteger(4,PLOT_ARROW,234);
      PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,10);
      PlotIndexSetInteger(4,PLOT_ARROW_SHIFT,-10);
      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[])
  {
//---
   CopyBuffer(iMA(_Symbol,_Period,EMAShort,0,MODE_EMA,PRICE_CLOSE),0,0,rates_total,shortMediaBuffer);
   CopyBuffer(iMA(_Symbol,_Period,EMALong,0,MODE_EMA,PRICE_CLOSE),0,0,rates_total,longMediaBuffer);
   CopyBuffer(iCustom(_Symbol,_Period,"Rafi",PRICE_CLOSE),0,0,rates_total,RAFIBuffer);
   
   for(int i=1; i<rates_total; i++)
   {
      CompraBuffer[i] =  
                      RAFIBuffer[i] > RAFIindex && 
                      shortMediaBuffer[i]>longMediaBuffer[i] && 
                      shortMediaBuffer[i-1]<longMediaBuffer[i-1] ? low[i] : 0; 
                      
      VendaBuffer[i] = 
                      RAFIBuffer[i] > RAFIindex && 
                      shortMediaBuffer[i]<longMediaBuffer[i] && 
                      shortMediaBuffer[i-1]>longMediaBuffer[i-1] ? high[i] : 0;
                                  
   }   
//--- return value of prev_calculated for next call
   return(rates_total);
  }

Can someone please, help me!

I just can't see wich part of code is my mistake.

i will let this indicator after some tests free.

Just help on this please?

 

Get your handles to iMA, iMA, and iCustom in OnInit().

Also, if you look at the example indicators supplied with MT5, you will see how they use prev_calculated and rates_total in a way so that all values are not copied and recalculated each time. An example is Examples\ATR.mq5.
 
Anthony Garot:

Get your handles to iMA, iMA, and iCustom in OnInit().

Also, if you look at the example indicators supplied with MT5, you will see how they use prev_calculated and rates_total in a way so that all values are not copied and recalculated each time. An example is Examples\ATR.mq5.

Hi Anthony,

thanks a lot! I also see another mate here, that do almost the same as i do, and it make my mind clear.

As a developer, i really know in theory, what i am doing wrong. But, at that time, i was really stuck.

So, againg, thanks a lot!