unable to draw trend line, and there is no error message

 

Dear Forum Members

Please help me to identify, where I have done wrong in the code and unable to draw trendline.

BandWidth line is drawn without any problem.

Thanks in advance.

  #property description "Bollinger BandWidth Indicator"
//+----------------------------------------------------------------------------------------------------------+
//| Define Indicator Properties
//+----------------------------------------------------------------------------------------------------------+
  #property indicator_separate_window
  #property indicator_buffers 4
  #property indicator_plots   1
  #property indicator_height  125
//+----------------------------------------------------------------------------------------------------------+
//| Define Input Parameters
//+----------------------------------------------------------------------------------------------------------+
  input group "iBand Indicator Parameters"
  input int               BB_Period    = 21;    // Bollinger Band Averaging Period
  input int               BB_Shift     = 0;     // Horizontal Shift
  input double            BB_Deviation = 2.0;   // 
//+----------------------------------------------------------------------------------------------------------+
//| Define Indicator Plots
//+----------------------------------------------------------------------------------------------------------+
  #property indicator_label1  "BandWidth"
  #property indicator_type1   DRAW_LINE
  #property indicator_color1  clrDodgerBlue
  #property indicator_style1  STYLE_SOLID
  #property indicator_width1  2
  //+--------------------------------------------------------------------------------------------------------+
  //| Define Indicator Handle(s) and Buffer(s)
  //+--------------------------------------------------------------------------------------------------------+
    int       hBollinger;
    double    bBBBandWidth[];
  //+--------------------------------------------------------------------------------------------------------+
  //| Define Local Indicator Array(s) and Variable(s)
  //+--------------------------------------------------------------------------------------------------------+
    double    aBBUpper[];
    double    aBBMiddle[];
    double    aBBLower[];
//+----------------------------------------------------------------------------------------------------------+
//| Custom indicator initialization function
//+----------------------------------------------------------------------------------------------------------+
int OnInit()
{
  //+--------------------------------------------------------------------------------------------------------+
  //| Set and Plot Indicator Buffers binding with declared array, in which we will store the values of data
  //+--------------------------------------------------------------------------------------------------------+
    SetIndexBuffer(0,bBBBandWidth,INDICATOR_DATA);
    SetIndexBuffer(1,aBBUpper,INDICATOR_CALCULATIONS);
    SetIndexBuffer(2,aBBMiddle,INDICATOR_CALCULATIONS);
    SetIndexBuffer(3,aBBLower,INDICATOR_CALCULATIONS);
  //+--------------------------------------------------------------------------------------------------------+
  //| Indicator Plot Mapping ...
  //+--------------------------------------------------------------------------------------------------------+
    PlotIndexSetDouble(0,PLOT_EMPTY_VALUE, EMPTY_VALUE);
  //+--------------------------------------------------------------------------------------------------------+
  //| Indicator Parameters ...
  //+--------------------------------------------------------------------------------------------------------+
    IndicatorSetString(INDICATOR_SHORTNAME,"iFx Bollinger BandWidth ");
    IndicatorSetInteger(INDICATOR_DIGITS,5);
    IndicatorSetDouble(INDICATOR_MINIMUM,0.00);
  //+--------------------------------------------------------------------------------------------------------+
  //| Define and Initialize Indicator Handles ...
  //+--------------------------------------------------------------------------------------------------------+
    hBollinger = iBands(_Symbol,PERIOD_CURRENT,BB_Period,BB_Shift,BB_Deviation,PRICE_CLOSE);
    if(hBollinger < 0)
      {
        Print(__LINE__,"Error creating hBollinger, code ",GetLastError()); return(INIT_FAILED);
      }
  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[])
{
    int       start_Position = 0;
    int       limit = rates_total - prev_calculated;
  //+--------------------------------------------------------------------------------------------------------+
  //| Set Indicator Buffers / Arrays as Timeseries ...
  //+--------------------------------------------------------------------------------------------------------+
    ArraySetAsSeries(bBBBandWidth,true);
    ArraySetAsSeries(aBBUpper,true);
    ArraySetAsSeries(aBBMiddle,true);
    ArraySetAsSeries(aBBLower,true);
  //+--------------------------------------------------------------------------------------------------------+
  //| Indicator Buffers Initialize  ...
  //+--------------------------------------------------------------------------------------------------------+
    ArrayInitialize(bBBBandWidth,EMPTY_VALUE);
    ArrayInitialize(aBBUpper,0);
    ArrayInitialize(aBBMiddle,0);
    ArrayInitialize(aBBLower,0);
  //+--------------------------------------------------------------------------------------------------------+
  //| Indicator Data preparation ... Check all data calculated and than copy them into calculation arrays
  //+--------------------------------------------------------------------------------------------------------+
    if(BarsCalculated(hBollinger) < rates_total) return(0);
      if(CopyBuffer(hBollinger,UPPER_BAND,start_Position,rates_total,aBBUpper) <= 0) return(rates_total);
      if(CopyBuffer(hBollinger,BASE_LINE,start_Position,rates_total,aBBMiddle) <= 0) return(rates_total);
      if(CopyBuffer(hBollinger,LOWER_BAND,start_Position,rates_total,aBBLower) <= 0) return(rates_total);
  //+--------------------------------------------------------------------------------------------------------+
  //| Indicator Main Loop ... additionally BandWidth converted into % by multiplying by 100
  //+--------------------------------------------------------------------------------------------------------+
    for(int i = start_Position; i < (rates_total-1) && !IsStopped(); i++)
    {
      if(aBBMiddle[i] != 0)    // Avoid ZERO divide error
      {
        bBBBandWidth[i] = NormalizeDouble(((aBBUpper[i] - aBBLower[i]) / aBBMiddle[i])*100,5);
      } // END Of if...condition
    } // END Of for...loop
  //+--------------------------------------------------------------------------------------------------------+
  //| Calulate 'PEAK & LOW' values in previous 200 candles
  //+--------------------------------------------------------------------------------------------------------+
    int      lookBack    = 200; // look back in 200 candles for Peak & Low Bandwidth
    int      index_MaxBW = ArrayMaximum(bBBBandWidth,1,lookBack);
    int      index_MinBW = ArrayMinimum(bBBBandWidth,1,lookBack);
    datetime startPeak   = index_MaxBW;
    datetime startLow    = index_MinBW;
    datetime currentTime = TimeCurrent();
    double   BW_Peak     = bBBBandWidth[index_MaxBW];
    double   BW_Low      = bBBBandWidth[index_MinBW];
    // print for testing purpose
    Print("BandWidth PEAK ",BW_Peak," LOW ",BW_Low);
    if((BW_Peak != 0) && (BW_Low != 0))
      {
        TrendLine("Last PEAK",startPeak,BW_Peak,currentTime,BW_Peak,clrDarkGreen,2,STYLE_SOLID,false);
        TrendLine("Last LOW",startLow,BW_Low,currentTime,BW_Low,clrRed,2,STYLE_SOLID,false);
      }
//--- return value of prev_calculated for next call
    return(rates_total);
}
//+----------------------------------------------------------------------------------------------------------+
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       TrendLine()
//| APPLICATION:  to draw Trend Line
//+----------------------------------------------------------------------------------------------------------+
bool TrendLine(const string InpName,const datetime inpTimeStart,const double InpPriceStart,
               const datetime inpTimeCurrent,const double InpPriceCurrent,const color InpColor,const int InpWidth, 
               const ENUM_LINE_STYLE InpStyle,const bool InpHidden)
  {
//--- Indicator subwindow number
    // subwindow_number = ChartWindowFind(0,subwindow_shortname);
    int subWindowID = ChartWindowFind(0,"iFx Bollinger BandWidth ");
    // print for testing purpose
    Print("SubWindow ID ",subWindowID);
    // ObjectCreate(chart_ID,name,OBJ_TREND,sub_window,time1,price1,time2,price2)
    if(!ObjectCreate(0,InpName,OBJ_TREND,subWindowID,inpTimeStart,InpPriceStart,inpTimeCurrent,InpPriceCurrent))
      {
        Print(__FUNCTION__,": Failed to create OBJ_TREND, Error ",GetLastError());
        return(false);
      }
    ObjectSetString(subWindowID,InpName,OBJPROP_TEXT,InpName);
    ObjectSetString(subWindowID,InpName,OBJPROP_FONT,"Arial");
    ObjectSetInteger(subWindowID,InpName,OBJPROP_FONTSIZE,11);
    ObjectSetInteger(subWindowID,InpName,OBJPROP_COLOR,InpColor);
    ObjectSetInteger(subWindowID,InpName,OBJPROP_WIDTH,InpWidth);
    ObjectSetInteger(subWindowID,InpName,OBJPROP_STYLE,InpStyle);
    ObjectSetInteger(subWindowID,InpName,OBJPROP_BACK,false);
    ObjectSetInteger(subWindowID,InpName,OBJPROP_HIDDEN,InpHidden);
  return(true);
  } // END Of method TrendLine()