Swing line (histogram)

Author: Copyright 2018, mladen
Price Data Components
0 Views
0 Downloads
0 Favorites
Swing line (histogram)
//------------------------------------------------------------------
#property copyright   "Copyright 2018, mladen"
#property link        "mladenfx@gmail.com"
#property description "Swing line - histogram"
#property version     "1.00"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_label1  "Swing line histogram"
#property indicator_type1   DRAW_COLOR_HISTOGRAM
#property indicator_color1  clrLimeGreen,clrPaleVioletRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
#property indicator_maximum 1
#property indicator_minimum 0
//--- input parameters
//--- buffers
double histo[],histoc[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,histo,INDICATOR_DATA);
   SetIndexBuffer(1,histoc,INDICATOR_COLOR_INDEX);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator de-initialization function                      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
double  work[][5];
#define hHi   0
#define hLo   1
#define lHi   2
#define lLo   3
#define trend 4
//
//---
//
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(Bars(_Symbol,_Period)<rates_total) return(prev_calculated);
   if(ArrayRange(work,0)!=rates_total) ArrayResize(work,rates_total);

//
//---
//
   int i=(int)MathMax(prev_calculated-1,0); for(; i<rates_total && !_StopFlag; i++)
     {
      if(i==0)
        {
         work[i][hHi]   = high[i]; work[i][hLo] = low[i];
         work[i][lHi]   = high[i]; work[i][lLo] = low[i];
         work[i][trend] = -1;
         continue;
        }
      //
      //---
      //
      work[i][trend] = work[i-1][trend];
      work[i][hHi]   = work[i-1][hHi]; work[i][hLo] = work[i-1][hLo];
      work[i][lHi]   = work[i-1][lHi]; work[i][lLo] = work[i-1][lLo];

      if(work[i-1][trend]==1)
        {
         work[i][hHi] = MathMax(work[i-1][hHi],high[i]);
         work[i][hLo] = MathMax(work[i-1][hLo],low[i]);
         if(high[i]<work[i][hLo]) { work[i][trend]=-1; work[i][lHi]=high[i]; work[i][lLo]=low[i]; }
        }
      if(work[i-1][trend]==-1)
        {
         work[i][lHi] = MathMin(work[i-1][lHi],high[i]);
         work[i][lLo] = MathMin(work[i-1][lLo],low[i]);
         if(low[i]>work[i][lHi]) { work[i][trend]=1; work[i][hHi]=high[i]; work[i][hLo]=low[i]; }
        }

      //
      //---
      //

      histo[i] = 1;
      histoc[i]=(work[i][trend]==1) ? 0 :(work[i][trend]==-1) ? 1 :(i>0) ? histoc[i-1]: 0;
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments