really really simple: why my indicator doesn't work?

 

Hi! Why my indicator below endend up with really strange values in buffers? Please...

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Buy
#property indicator_label1  "Buy"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Sell
#property indicator_label2  "Sell"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- indicator buffers
double         BuyBuffer[];
double         SellBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BuyBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,SellBuffer,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   
//---
   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[])
  {
//---

for(int i=1; i<rates_total; i++)
   {
      double var_max = open[i-1]/low[i-4];
      if(var_max > 1.003)
         {
            BuyBuffer[i] = open[i];
         }
      else if(var_max < 0.997)
         {
            SellBuffer[i] = open[i];
         }
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

At the first glance, I can see three problems.

  • i-4 is not going to work at i=1;
  • you are recalculating all the bars on every tick;
  • and empty values are better populated explicitly.
 

This should work:

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[])
  {
//---
   // we're starting at bar 3, which is actually 4th
   // as the numbering starts at zero
   // int startpos=fmax(prev_calculated-1,3);
   // important correction: if we're working with open
   // prices only, we don't need to recalculate the current
   // bar on a new tick, hence it becomes:
   int startpos=fmax(prev_calculated,3);

   for(int i=startpos; i<rates_total; i++)
     {
      // while it could work without this, it's better to
      // populate every value to avoid e.g. issues when
      // switching timeframes
      BuyBuffer[i]=EMPTY_VALUE;
      SellBuffer[i]=EMPTY_VALUE;
      
      // I also took the liberty to move the calculation 
      // one bar forward as open prices normally aren't
      // changing so we can use open[i] without the fear
      // of repainting
      double var_max = open[i]/low[i-3];
      if(var_max > 1.003)
        {
         BuyBuffer[i] = open[i];
        }
      else
         if(var_max < 0.997)
           {
            SellBuffer[i] = open[i];
           }
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
  1. i-4 may not work because you didn't set your arrays to non-series.
  2. Your buffers will not work because you didn't set them to non-series.
  3. You didn't set your look back (4)
              How to do your lookbacks correctly #9#14 & #19
 
You should look up Examples/Fractals and look for every line that are not included in yours.
Reason: