indicator not autorefreshing and not sending buffer values properly

 

Hi, the indicator below stopped working a few days ago and I'm struggling with the problem.

I attach the indicator which is not auto-refreshing the arrows and also the EA in which you can see the indicator is always returning the value "0".


Indicator


#define VERSION "2.0"
#property copyright "loncis"
#property link      ""
#property version   VERSION
#property description ""

#define SIGNAL_EMPTY -1

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xb61b1b
#property indicator_label1 "Sell"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0xb61b1b
#property indicator_label2 "Buy"

input int OffsetMinutes=40;

//--- indicator buffers
double Buffer1[];
double Buffer2[];

datetime time_alert; //used when sending alert
bool Audible_Alerts = true;
double myPoint; //initialized in OnInit

static bool signalFlag = false;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//| NUMERO 2 COMO INDICADOR                                          |
//+------------------------------------------------------------------+
int OnInit() {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 226);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 225);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 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[])  {
                
 if (OffsetMinutes >= _Period && OffsetMinutes <= 1) {
       // return (0);
    }
    //int nextPeriod = getNextPeriod();   
    int limit = rates_total - prev_calculated;
    //--- counting from 0 to rates_total
    ArraySetAsSeries(Buffer1, true);
    ArraySetAsSeries(Buffer2, true);
    //--- initial zero
    if (prev_calculated < 1) {
        ArrayInitialize(Buffer1, 0);
        ArrayInitialize(Buffer2, 0);
    } else
        limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)     {
   
   if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
               //Indicator Buffer 1 VENTA      
          if((Close[i] < iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,i)      //vela actual cierra por debajo de SMA 14  
         && (iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,i) < iMA(NULL,0,25,0,MODE_EMA,PRICE_CLOSE,i))
         &&((Open[i+1] < Close[i+1])|| (Open[i+1] == Close[i+1])) // vela anterior alcista or doji 
         
       
          )  ){ 
          Buffer1[i] = High[i] + iATR(NULL, 0, 14, i)/2; //Set indicator value at Candlestick Low - Average True Range
                               if(i == 0 && Time[0] != time_alert) { //Instant alert, only once per bar
                                    //myAlert("indicator", "CALL"); 
                                    time_alert = Time[0]; 
                                    signalFlag = false;
                                 } 
                               
          } else{
           Buffer1[i] = SIGNAL_EMPTY;
        }                
        
        ///Indicator Buffer 2 COMPRA
         if ((Close[i] > iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,i) //vela actual cierra por encima de SMA 14
         &&(iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,i) > iMA(NULL,0,25,0,MODE_EMA,PRICE_CLOSE,i)) //SMA 14 por encima SMA 25
         && ((Open[i+1] > Close[i+1]) || (Open[i+1] == Close[i+1])) // vela anterior bajista  or doji      
       
        
         
     
       ))  {
             Buffer2[i] = Low[i] - iATR(NULL, PERIOD_CURRENT, 14, i)/2; //Set indicator value at Candlestick High + Average True Range
                              if (i == 0 && Time[0] != time_alert) { //Instant alert, only once per bar
                                   //myAlert("indicator", "PUT"); 
                                   time_alert = Time[0];
                                   signalFlag = false;
                               }                                                               
                                                                                 
           } else{
             Buffer2[i] = SIGNAL_EMPTY;
            }    
 
  }
  
  return(rates_total);
   }
    
//+------------------------------------------------------------------+


and the EA

//+------------------------------------------------------------------+
//|                                                    EA_Suarez.mq4 |
//+------------------------------------------------------------------+
#define VERSION "1.01"
#define SIGNAL_EMPTY -1

#property copyright "loncis"
#property version   VERSION
#property strict

input string _setIdicators = "--- indicators settings";
input double _Lot = 0.03;
input int _HoldDuringBars = 3;
input int _MagicNumber = 1;
input string info4 = "--- Time Filter ---";
input bool _UseTimeFilter = true;
input int        _HourBegin = 6;             
input int        _MinuteBegin = 0;
input int        _HourEnd = 9;             
input int        _MinuteEnd = 0;

datetime nextBarTime;

// add setting for indicators
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){

  
   double indSuarezCall = iCustom(_Symbol, _Period, "i_Suarez5M", 1, 1);
   double indSuarezPut = iCustom(_Symbol, _Period, "i_Suarez5M", 0, 1); 

   closeOrders();
   if(!isNewBar()){
      return;
   }
   
    bool indSuarezCall_signal = indSuarezCall != SIGNAL_EMPTY;
     bool indSuarezPut_signal = indSuarezPut != SIGNAL_EMPTY;
      Print("::::::::::::::::signal empty = " + SIGNAL_EMPTY);
    Print("indSuarezCall = " + indSuarezCall);  
    
     Print("::::::::::::::::signal empty = " + SIGNAL_EMPTY);
   Print("indSuarePut = " + indSuarezPut);  
   
   // has CALL signal
   
   
  
   if(indSuarezCall_signal){
      if(checkOpenPosition(OP_BUY, _MagicNumber)){
         OrderSend(_Symbol, OP_BUY, _Lot, Ask, 0, 0, 0, "CALL", _MagicNumber,0,clrGreen);
      }
   }
   // has PUT signal
    
   
   if(indSuarezPut_signal){
      if(checkOpenPosition(OP_SELL, _MagicNumber)){
         OrderSend(_Symbol, OP_SELL, _Lot, Bid, 0, 0, 0, "PUT", _MagicNumber,0,clrGreen);
      }
   }
}
//+------------------------------------------------------------------+
bool checkOpenPosition(int type, int mn) {
    bool Result = True;
    for (int i = 0; i < OrdersTotal(); i++)
        if (OrderSelect(i, SELECT_BY_POS))
            if (OrderType() == type && OrderMagicNumber() == mn && OrderSymbol() == _Symbol)
                if (OrderOpenTime() >= iTime(_Symbol, PERIOD_CURRENT, 0))
                    Result = False;
    for (int i = 0; i < OrdersHistoryTotal(); i++) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
            if (OrderType() == type && OrderOpenTime() >= iTime(_Symbol, PERIOD_CURRENT, 0) && OrderMagicNumber() == mn && OrderSymbol() == _Symbol)
                Result = False;
    }

    return (Result);
}

void closeOrders(){
   int barOpen;   
   for (int i = 0; i < OrdersTotal(); i++){
      if (OrderSelect(i, SELECT_BY_POS)){
         barOpen = iBarShift(OrderSymbol(),Period(),OrderOpenTime());
         if(barOpen >= _HoldDuringBars){
            if(OrderType() == OP_BUY)
               OrderClose(OrderTicket(), OrderLots(), Bid, 0);
            if(OrderType() == OP_SELL)
               OrderClose(OrderTicket(), OrderLots(), Ask, 0);      
         }
      }
   }      
}

bool isTradeTimeInt(int hb, int mb, int he, int me, bool filter) {
    datetime db, de; // begin and end time of work
    int hc; // Watch of the current server's time

    if (!filter) {
        return (true);
    }
    db = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE) + " " + hb + ":" + mb);
    de = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE) + " " + he + ":" + me);
    hc = TimeHour(TimeCurrent());
    if (db >= de) {
        if (hc >= he) de += 24 * 60 * 60;
        else db -= 24 * 60 * 60;
    }
    if (TimeCurrent() >= db && TimeCurrent() <= de) return (True);
    else return (False);
}


bool isNewBar(){
   if(nextBarTime == Time[0])
      return(false);
   nextBarTime = Time[0];
   return(true);
}


Thanks a lot!

 
    int limit = rates_total - prev_calculated;
    if (prev_calculated < 1) {
        ArrayInitialize(Buffer1, 0);
        ArrayInitialize(Buffer2, 0);
    } else
        limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)     {
   
   if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
   :
  }
  return(rates_total);

Broken indicator.

First time prev_calculated is zero, so limit becomes Bars+1. The test on i prevents the Array out of range. The second time limit is zero, i is minus one, and bar zero in never calculated.

See How to do your lookbacks correctly.

 
whroeder1:

Broken indicator.

First time prev_calculated is zero, so limit becomes Bars+1. The test on i prevents the Array out of range. The second time limit is zero, i is minus one, and bar zero in never calculated.

See How to do your lookbacks correctly.

thanks a lot!
Reason: