" ChartIndicatorAdd " and Error 4114 for any chart symbol and current time frame. Is it important ? - page 2

 

hi

i add an indicator by "ChartIndicatorAdd " in void OnInit

and with " ChartIndicatorDelete " in void OnDeinit i delete it.

so when i change time frame or delete my indicator from chart the added indicator also will be remove.

-----

is it possible ? : when change time frame added indicator dont remove but when i delete my indicator from chart the added indicator also removed , .. ?

 
TIMisthebest:

hi

i add an indicator by "ChartIndicatorAdd " in void OnInit 

and with " ChartIndicatorDelete " in void OnDeinit i delete it. 

so when i change time frame or delete my indicator from chart the added indicator also will be remove.

-----

is it possible ? : when change time frame added indicator dont remove but when i delete my indicator from chart the added indicator also removed , .. ?

void OnDeinit(const int reason)
  {
   if(reason==REASON_CHARTCHANGE)
      return;
//--- deleting indicator
...
  }

In OnInit () it is necessary to check: this indicator already exists?

void OnInit()
  {
   ChartIndicatorsTotal
   ...
   ChartIndicatorName
   ...
 
barabashkakvn:

In OnInit () it is necessary to check: this indicator already exists?

thank to reply, can you please check this:

#property copyright "Copyright © 2014, BY MEHRDAD SHIRI"
#property version   "1.00"
#property link      "https://login.mql5.com/en/users/timisthebest"
#property description "m100shiri@yahoo.com"
//+------------------------------------------------------------------+
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   0
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+-----------------------------------+
//|  INDICATOR INPUT PARAMETERS       |
//+-----------------------------------+
//----------------------------------------------------------------------------------------------------------//
//------------------------------------------------------------------------------------------ Ichimoku
input int tenkan_sen=9;              // period of Tenkan-sen
input int kijun_sen=26;              // period of Kijun-sen
input int senkou_span_b=52;          // period of Senkou Span B
double         Tenkan_sen[];
double         Kijun_sen[];
double         Senkou_Span_A[];
double         Senkou_Span_B[];
double         Chinkou_Span[];
int    Ichimoku_handle;              //--- variable for storing the handle of the iIchimoku indicator
//------------------------------------------------------------------------------------------ Moving_Average
double ExtMoving_AverageBuffer[];
int   Moving_Average_handle;
input ENUM_TIMEFRAMES    InpPeriod_MA_TIMEFRAMES=PERIOD_CURRENT;        // timeframe
input int            InpMAPeriod=50;         // Period
input int            InpMAShift=0;           // Shift
input ENUM_MA_METHOD InpMAMethod=MODE_SMA;  // Method
input ENUM_APPLIED_PRICE InpAppliedPrice_MA=PRICE_CLOSE;   // Applied price
//----------------------------------------------------------------------------------------------------------//

//----------------------------------------------------------------------------------------------------------//
bool foreground;
//string IndicatorName;
string short_name;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {     
//----------------------------------------------------------------------------------------------------------//
//------------------------------------------------------------------------------------------ Ichimoku
   SetIndexBuffer(0,Tenkan_sen,INDICATOR_CALCULATIONS);         //---  INDICATOR_data_store
   ArraySetAsSeries(Tenkan_sen,true);
   
   SetIndexBuffer(1,Kijun_sen,INDICATOR_CALCULATIONS);          //---  INDICATOR_data_store
   ArraySetAsSeries(Kijun_sen,true);

   SetIndexBuffer(2,Senkou_Span_A,INDICATOR_CALCULATIONS);               //---  INDICATOR_data_store
   ArraySetAsSeries(Senkou_Span_A,true);

   SetIndexBuffer(3,Senkou_Span_B,INDICATOR_CALCULATIONS);              //---  INDICATOR_data_store
   ArraySetAsSeries(Senkou_Span_B,true);

   SetIndexBuffer(4,Chinkou_Span,INDICATOR_CALCULATIONS);      //---  INDICATOR_data_store
   ArraySetAsSeries(Chinkou_Span,true);
            
   Ichimoku_handle=iIchimoku(NULL,0,tenkan_sen,kijun_sen,senkou_span_b);
   if(Ichimoku_handle==INVALID_HANDLE) Print("*****---> Failed to get handle of the Ichimoku indicator error =","  error = ",Ichimoku_handle,GetLastError());  //--- if the handle is not created
   if(!ChartIndicatorAdd(0,0,Ichimoku_handle)) 
     {PrintFormat("*****---> Failed to add Ichimoku indicator on %d chart window. Error code  %d",0,GetLastError());}
//------------------------------------------------------------------------------------------  PLOT Moving_Average  
   SetIndexBuffer(5,ExtMoving_AverageBuffer,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(ExtMoving_AverageBuffer,true);
   Moving_Average_handle=iMA(NULL,InpPeriod_MA_TIMEFRAMES,InpMAPeriod,InpMAShift,InpMAMethod,InpAppliedPrice_MA);
   //Print("*****---> Moving_Average_handle = ",Moving_Average_handle,"  error = ",GetLastError());
   if(Moving_Average_handle==INVALID_HANDLE) Print("*****---> Failed to get handle of the Moving_Average indicator error =",GetLastError());
   if(!ChartIndicatorAdd(0,0,Moving_Average_handle)) 
      {PrintFormat("*****---> Failed to add Moving_Average indicator on %d chart window. Error code  %d",0,GetLastError());}   
//---  __FUNCTION__,reason);
     //{PrintFormat("*****---> Failed to add Moving_Average indicator on %d chart window. Error code  %d",0,GetLastError());}
//----------------------------------------------------------------------------------------------------------//

//----------------------------------------------------------------------------------------------------------//
//*************************************************************************   
   foreground=ChartGetInteger(0,CHART_FOREGROUND);
   ChartSetInteger(0,CHART_FOREGROUND,false);  
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits + 1);

   short_name="a";
   //short_name = "MS_WAVE["+TF+"]("+(string)Bands_Mode+","+priceToString(Power_Price)+","+priceToString(Price_Type)+","+(string)Bands_Period+","+DoubleToString(Bands_Deviation,1)+","+(string)Power_Period+")";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//*************************************************************************   
//--- initialization done
  }
//----------------------------------------------------------------------------------------------------------//
//+------------------------------------------------------------------+
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     &TickVolume[],
                const long     &Volume[],
                const int      &Spread[])
{
   if (!ArrayGetAsSeries(Close)) ArraySetAsSeries(Close,true);
   if (!ArrayGetAsSeries(Time))  ArraySetAsSeries(Time,true);
   if (!ArrayGetAsSeries(High))  ArraySetAsSeries(High,true);
   if (!ArrayGetAsSeries(Low))   ArraySetAsSeries(Low,true);
   int to_copy;
//----------------------------------------------------------------------------------------------------------//
//------------------------------------------------------------------------------------------ Ichimoku
   //---
   /*
   //if(CopyBuffer(Ichimoku_handle,0,0,rates_total,Tenkan_sen)!=rates_total) return(0);
   if(CopyBuffer(Ichimoku_handle,0,0,rates_total,Tenkan_sen)<rates_total) return(0);
   if(CopyBuffer(Ichimoku_handle,1,0,rates_total,Kijun_sen)<rates_total) return(0);
   if(CopyBuffer(Ichimoku_handle,2,-kijun_sen,rates_total,Senkou_Span_A)<rates_total) return(0);
   if(CopyBuffer(Ichimoku_handle,3,-kijun_sen,rates_total,Senkou_Span_B)<rates_total) return(0);
   if(CopyBuffer(Ichimoku_handle,4,0,rates_total,Chinkou_Span)<rates_total) return(0);   
   */
   if (!checkCalculated(Ichimoku_handle,rates_total,"Ichimoku_handle")) return(0);
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
      else {to_copy=rates_total-prev_calculated; if(prev_calculated>0) to_copy++;}
   if (!doCopy(Ichimoku_handle,0,0,to_copy,Tenkan_sen,"Tenkan_sen buffer")) return(0);
   if (!doCopy(Ichimoku_handle,1,0,to_copy,Kijun_sen,"Kijun_sen buffer"   )) return(0);
   if (!doCopy(Ichimoku_handle,2,-kijun_sen,to_copy,Senkou_Span_A,"Senkou_Span_A buffer"   )) return(0);
   if (!doCopy(Ichimoku_handle,3,-kijun_sen,to_copy,Senkou_Span_B,"Senkou_Span_B buffer"   )) return(0);
   if (!doCopy(Ichimoku_handle,4,0,to_copy+kijun_sen,Chinkou_Span,"Chinkou_Span buffer"   )) return(0);
   //---
//------------------------------------------------------------------------------------------ Moving_Average
   //---
   /*
   int copy_Moving_Average=CopyBuffer(Moving_Average_handle,0,0,rates_total,ExtMoving_AverageBuffer);
   if(copy_Moving_Average<=0) Print("*****---> An attempt to get the values if Custom Moving Average has failed",GetLastError());  
   */
   if (!checkCalculated(Moving_Average_handle,rates_total,"Moving_Average")) return(0); 
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
      else {to_copy=rates_total-prev_calculated; if(prev_calculated>0) to_copy++;}
   if (!doCopy(Moving_Average_handle,0,0,to_copy,ExtMoving_AverageBuffer,"Moving_Average buffer")) return(0);
   //---
//----------------------------------------------------------------------------------------------------------//

   Comment(""
           ,"a"
           ,"\n "

          );
          
     

//----------------------------------------------------------------------------------------------------------//         
//--- done
   return(rates_total);
}
//+------------------------------------------------------------------+
//----------------------------------------------------------------------------------------------------------//
void OnDeinit(const int reason)
  {
   //PrintFormat("*****---> %s: Deinitialization reason code=%d",__FUNCTION__,reason);
  
      Comment("");
      
      if(!IndicatorRelease(Ichimoku_handle))
                  Print("IndicatorRelease() failed. Error ",GetLastError());
             
      for(int j=ChartIndicatorsTotal(0,0)-1;j>=0;j--)
         {
          if(reason==REASON_CHARTCHANGE) break;  // return;
          string ch_indicator_name=ChartIndicatorName(0,0,j);
          if(!ChartIndicatorDelete(0,0,ch_indicator_name)) 
             printf("Error(%i) while deleting indicator %s from chart",GetLastError(),ch_indicator_name);
         }
      
      ChartRedraw();
      ChartSetInteger(0,CHART_FOREGROUND,foreground);
      //ZeroMemory();
      
  }
//----------------------------------------------------------------------------------------------------------//
bool checkCalculated(int bufferHandle, int total, string checkDescription)
    {
     int calculated=BarsCalculated(bufferHandle);
     if (calculated<total)
        {
         Print("Not all data of "+checkDescription+" is calculated (",calculated,"bars ). Error",GetLastError());
         return(false);
        }
     return(true);
    }
//---
bool doCopy(int bufferHandle, int buffer_number, int start_position, int copyCount, double& buffer[], string copyDescription)
    {
     if(CopyBuffer(bufferHandle,buffer_number,start_position,copyCount,buffer)<=0)
       {
        Print("Getting "+copyDescription+" failed! Error",GetLastError());
        return(false);
       }
     return(true);
    }
//----------------------------------------------------------------------------------------------------------//

there is error 4114

 
Do I understand correctly: when you change the timeframe on the chart, you should leave two indicators?
 
barabashkakvn:
Do I understand correctly: when you change the timeframe on the chart, you should leave two indicators?

" when you change the timeframe on the chart, you should leave two indicators? "  YES. it redraw for new timeframe,

but

if we remove the indicator from chart ( or delete it from chart ) i want to remove two another indicator ( ichimoko & MA ) also.

 
TIMisthebest:

" when you change the timeframe on the chart, you should leave two indicators? "  YES. it redraw for new timeframe,

...

I did not understand. Redraw - create a new handles "Ichimoku" and "MA"?
 
barabashkakvn:
I did not understand. Redraw - create a new handles "Ichimoku" and "MA"?
yes . in each time frame we have this indicator.
 
   SetIndexBuffer(5,ExtMoving_AverageBuffer,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(ExtMoving_AverageBuffer,true);
   Print("InpPeriod_MA_TIMEFRAMES=",InpPeriod_MA_TIMEFRAMES); !!!!!!
   Moving_Average_handle=iMA(NULL,InpPeriod_MA_TIMEFRAMES,InpMAPeriod,InpMAShift,InpMAMethod,InpAppliedPrice_MA);
Change the time frame and you'll get an error.
 
barabashkakvn:
Change the time frame and you'll get an error.

yes, you are right.

and how to fix it?

 
void OnDeinit(const int reason)
  {
//PrintFormat("*****---> %s: Deinitialization reason code=%d",__FUNCTION__,reason);
   Comment("");
/*
   if(!IndicatorRelease(Ichimoku_handle))
      Print("IndicatorRelease() failed. Error ",GetLastError());
   if(!IndicatorRelease(Moving_Average_handle))
      Print("IndicatorRelease() failed. Error ",GetLastError());
      */

   for(int j=ChartIndicatorsTotal(0,0)-1;j>=0;j--)
     {
      //if(reason==REASON_CHARTCHANGE) break;  // return;
      string ch_indicator_name=ChartIndicatorName(0,0,j);
      if(ch_indicator_name==short_name)
         break;
      if(!ChartIndicatorDelete(0,0,ch_indicator_name))
         printf("Error(%i) while deleting indicator %s from chart",GetLastError(),ch_indicator_name);

     }

   ChartRedraw();
   ChartSetInteger(0,CHART_FOREGROUND,foreground);
//ZeroMemory();
   Print("OnDeinit ;",ChartPeriod(),"; ",ChartSymbol(),"; ",ChartIndicatorsTotal(0,0));
  }
Reason: