please help diagnose this custom indicator not plotting right

 

slope_angle_not_right

#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot bbwidth
#property indicator_label1  "bbwidth"
#property indicator_type1   DRAW_NONE
#property indicator_color1  clrGreenYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot bbwidthslope
#property indicator_label2  "bbwidthslope"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrTomato
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int      BBandPeriod =20;
input int      EntrySlope;
input int      ExitSlope;
//--- indicator buffers
double         BBWidthBuffer[];
double         BBWidthSlopeBuffer[];
//--- custom define
#define PI  3.14159265359
#define RadToDeg(x)  (x)*180/PI

//--- custom functions
double GetAngle(datetime time1, double price1, datetime time2, double price2)
  {
   int x1,y1,x2,y2;
   
   //convert the (time,price) coordinates to (x,y) coordinates
   ChartTimePriceToXY(0,0,time1,price1,x1,y1);
   ChartTimePriceToXY(0,0,time1,price1,x2,y2);
   // flip Y coordinates because screen point (0,0) s upper-leftmost
   // and not lowre-leftmost as we usually draw the axes on paper
   y1=ChartHeightInPixelsGet()-y1;
   y2=ChartHeightInPixelsGet()-y2;
   //Calculate the slope, notiece the forced casting that need to be done
   double m=(double)(y2-y1)/(double)(x2-x1)*((double)ChartWidthInPixels()/(double)ChartHeightInPixelsGet());
   //Calculate the angle in degrees
   double angle=RadToDeg(MathArctan(m));
   
   return(angle);
  }

//+------------------------------------------------------------------+
//| The function receives the chart width in pixels                  |
//+------------------------------------------------------------------+
int ChartWidthInPixels(const long chart_ID=0)
  {
    //--- prepare the variable to get the property value
    long result=-1;
    //--- reset the error value
    ResetLastError();
    //--- receive the property value
    if(!ChartGetInteger(chart_ID,CHART_WIDTH_IN_PIXELS,0,result))
      {
        //--- display the error message in Experts journal
        Print(__FUNCTION__+", Error Code = ",GetLastError());
      }
    //--- return the value of the chart property
    return((int)result);
  }
  
//+------------------------------------------------------------------+
//| The function receives the chart heigth value in pixels           |
//+------------------------------------------------------------------+
int ChartHeightInPixelsGet(const long chart_ID=0,const int sub_window=0)
  {
    //--- prepare the variable to get the property value
    long result=-1;
    //--- reset the error value
    ResetLastError();
    //--- receive the property value
    if(!ChartGetInteger(chart_ID,CHART_HEIGHT_IN_PIXELS,sub_window,result))
    {
      //--- display the error mesage in Expert journal
      Print(__FUNCTION__+", Error Code = ",GetLastError());
    }
    //--- return the value of the chart property
    return((int)result);
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BBWidthBuffer);
   SetIndexBuffer(1,BBWidthSlopeBuffer);
   IndicatorDigits(Digits+2);
//---
   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 begin=!prev_calculated?rates_total-BBandPeriod-1:rates_total-prev_calculated;
   double bandsUpper, bandsLower, bandsMain; // bbwidth[];
   
   
   for (int i=begin;i>=0;i--)
   {  
      bandsUpper=iBands(NULL,PERIOD_CURRENT,BBandPeriod,2,0,PRICE_CLOSE,MODE_UPPER,i);
      bandsLower=iBands(NULL,PERIOD_CURRENT,BBandPeriod,2,0,PRICE_CLOSE,MODE_LOWER,i);
      bandsMain=iBands(NULL,PERIOD_CURRENT,BBandPeriod,2,0,PRICE_CLOSE,MODE_MAIN,i);
      BBWidthBuffer[i]=(bandsUpper-bandsLower)/bandsMain*100;

   }
   
    for (int i=begin;i>=0;i--)
   {  

      BBWidthSlopeBuffer[i]=GetAngle(Time[i],BBWidthBuffer[i],Time[i-1],BBWidthBuffer[i-1]);
   }   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

any clue what happened to my codes not plotting the BBWdithSlopeBuffer right. At places where the slopes are nearly 45-65degress, it shot up to 89degree level. Where the BBWidthBuffer plateaued, the BBWidthSlopeBuffer does not go to near zero too. Any help is appreciated.

 
Wilson Wong:

any clue what happened to my codes not plotting the BBWdithSlopeBuffer right. At places where the slopes are nearly 45-65degress, it shot up to 89degree level. Where the BBWidthBuffer plateaued, the BBWidthSlopeBuffer does not go to near zero too. Any help is appreciated.

Maybe the physical representation could not be done since the physical width range and height range are to tight to draw...

Why using lines?

Why not an histogram?

And why calculatin angles? Could it be %diff between your sources?


;)