Get the number of candles that have passed since the Indicator has printed a value.

 

Hey , I have not yet worked on indicators that much and therefore I'm now facing a problem that might be easy to solve for soeme or does not make any sense. The Indicator Automatic Trendlines paints a candle which fufills certain criteria in a colour. Now I want to create an indicator thats relys on that candle, I have the value imported through the iCustom function, but I have not yet been able to understand how I can save the value in regards to getting the date from that day where the criteria is fullfilled. I need the distance from the candle which is the newest at that point to the last painted candle which is either a resistance or support candle. I researched and found out about the Bars() function but I do not know how to get the specific dates for the candles. The indicator does only Print out a value when the criteria is fullfilled otherwise it is an EMPTY_VALUE.

Is there any way to getthat date from the candle which fullfills the criteria to use the Bars() function or is there any other way to get the number of candles that have passed since that certain candle ? Help is appreciated. 

#property version   "1.00"
#property indicator_buffers 3//4
#property indicator_plots   3
#property indicator_separate_window

#property indicator_label1 "change"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed



input int   iLookBack   = 5;

   // indicator
   const string IndicatorName = "CustomToTest\\AutoTrendCandle"; 

   int handle_iCustomTrendline; 
 
   double BufferTREND[]; 
   
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
{
   //--- indicator buffers mapping
  // SetIndexBuffer(0, size, INDICATOR_CALCULATIONS);

   SetIndexBuffer(1, BufferTREND, INDICATOR_DATA);


   //PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, iLookBack);



      //--- create handle of the indicator iCustom
         handle_iCustomTrendline=iCustom(Symbol(),Period(),IndicatorName);
      //--- if the handle is not created 
         if(handle_iCustomTrendline==INVALID_HANDLE)
           {
            //--- tell about the failure and output the error code 
            PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",
                        Symbol(),
                        EnumToString(Period()),
                        GetLastError());
            //--- the indicator is stopped early 
            return(INIT_FAILED);
           }
           
      //---      

   return(INIT_SUCCEEDED);
} 

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[])
{
//---
   int    start = iLookBack;
   double Y = 0;

   if(rates_total < iLookBack)
      return(0);
   if(prev_calculated == 0) start = iLookBack - 1;
   else start = prev_calculated - 1;
   
  for(int i = start; i < rates_total; i++)
   {
    int copy=CopyBuffer(handle_iCustomTrendline,0,1,rates_total,BufferTREND);

    return(rates_total);

   }
}
 
Justin101:

Hey , I have not yet worked on indicators that much and therefore I'm now facing a problem that might be easy to solve for soeme or does not make any sense. The Indicator Automatic Trendlines paints a candle which fufills certain criteria in a colour. Now I want to create an indicator thats relys on that candle, I have the value imported through the iCustom function, but I have not yet been able to understand how I can save the value in regards to getting the date from that day where the criteria is fullfilled. I need the distance from the candle which is the newest at that point to the last painted candle which is either a resistance or support candle. I researched and found out about the Bars() function but I do not know how to get the specific dates for the candles. The indicator does only Print out a value when the criteria is fullfilled otherwise it is an EMPTY_VALUE.

Is there any way to getthat date from the candle which fullfills the criteria to use the Bars() function or is there any other way to get the number of candles that have passed since that certain candle ? Help is appreciated. 

I'm looking for the same thing myself. I'd like to know if you found a way
 
khulanimabila #:
I'm looking for the same thing myself. I'd like to know if you found a way
Just introduce a datetime variable, call it time_last_print and update it each time you use the print function. When you need the bar number use iBarShift and fill in the time variable. As always with iFunctions it used ArraySetAsSeries true, so 0 will be the most recent bar, 1 the last etc.
 

Hi

I not sure if I understood what you really need here, but if the indicator returns those displayed candles as indicator buffers. You can simply check the value of this indicator for each candle (Shift) and then get the time of this candle(shift). For example:

ArraySetAsSeries(BufferTREND, true); //set array with indicator avlues as series (indexed as bars on the chart)


//find last bar with no empty value
for(int i=0; i<rates_total;i++){
        if(BufferTREND!=EMPTY_VALUE){
                //here you can msave the index/time of the signal candle or make necessary calculations(count distance etc)
                int indexCandle = i;  
                datetime timeCandle  = Time(
                break;
        }
}
 … 
datetime Time(int i){
   datetime times[1];
   int copied = CopyTime(NULL, 0, i, 1,times);
   if(copied < 1){
      //Print("Error: Download date time array problem: "+IntegerToString(GetLastError()));
      return 0;
   }
   return times[0];
}

Best Regards

 
CopyBuffer should be outside of the loop in the OnCalculate function. Comments above seem to be a good approach for what you want to do