//#property strict

#property indicator_separate_window
#property indicator_width1    1
#property indicator_width2    1
#property indicator_width3    1
#property indicator_buffers   3
#property indicator_color1    DodgerBlue
#property indicator_color2    clrLimeGreen
#property indicator_color3    clrLimeGreen
//--- indicator parameters
int            InpMAPeriod=5;             // Period
int            InpMAShift=0;              // Shift

//--- indicator buffer
double ExtLineBuffer[], ExtLine2Buffer[], ExtLine3Buffer[];

int OnInit(void) {
   int    draw_begin = InpMAPeriod - 1;

   IndicatorShortName("Signal 5 * - ");
   IndicatorDigits(Digits);

   if(InpMAPeriod<2) return(INIT_FAILED);

//   IndicatorBuffers(2);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexShift(0, InpMAShift);
   SetIndexDrawBegin(0, draw_begin);
   SetIndexBuffer(0, ExtLineBuffer);
   SetIndexStyle(1, DRAW_LINE, STYLE_DASH);
   SetIndexShift(1, InpMAShift);
   SetIndexDrawBegin(1, draw_begin);
   SetIndexBuffer(1, ExtLine2Buffer);
   SetIndexStyle(2, DRAW_LINE, STYLE_DOT);
   SetIndexShift(2, InpMAShift);
   SetIndexDrawBegin(2, draw_begin);
   SetIndexBuffer(2, ExtLine3Buffer);

   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[]) {

   if (rates_total < InpMAPeriod - 1 || InpMAPeriod < 2) return(0);

//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtLineBuffer, false);
   ArraySetAsSeries(ExtLine2Buffer, false);
   ArraySetAsSeries(ExtLine3Buffer, false);
//   ArraySetAsSeries(close, false);     ArraySetAsSeries(open, false);
   ArraySetAsSeries(high, false);      ArraySetAsSeries(low, false);

//--- first calculation or number of bars was changed
   if (prev_calculated == 0) { ArrayInitialize(ExtLineBuffer, 0);
      ArrayInitialize(ExtLine2Buffer, 0);
      ArrayInitialize(ExtLine3Buffer, 0); }

   Calcul_5stars(rates_total,prev_calculated,high,low);  // open,close,

//--- return value of prev_calculated for next call
   return(rates_total); }

//+------------------------------------------------------------------+
void Calcul_5stars(int rates_total,int prev_calculated,const double &pr_hi[],const double &pr_lo[]) { // const double &pr_cl[],const double &pr_op[],
   int i, j, limit;

/*      tmpHiLo = int (((iHigh(NULL,0,Bars-i+5)-iLow(NULL,0,Bars-i+5)) + (iHigh(NULL,0,Bars-i+1)-iLow(NULL,0,Bars-i+1)) + (iHigh(NULL,0,Bars-i+2)-
         iLow(NULL,0,Bars-i+2)) + (iHigh(NULL,0,Bars-i+3)-iLow(NULL,0,Bars-i+3)) + (iHigh(NULL,0,Bars-i+4)-iLow(NULL,0,Bars-i+4))) / Point / 5);
         
         if ((iHigh(NULL,0,Bars-i) - iLow(NULL,0,Bars-i)) / Point > tmpHiLo * 2.2) */

//--- first calculation or number of bars was changed
   if (prev_calculated == 0) {
      limit = InpMAPeriod;
      //--- calculate first visible value
      double firstValue=0, firstValue2;
      for(i = 1; i <= limit; i ++) firstValue += pr_hi[i-1] > pr_lo[i-1] && StrToInteger(DoubleToStr(pr_lo[i-1], 0)) ? pr_hi[i-1] - pr_lo[i-1] : 0;
      firstValue /= InpMAPeriod;
      firstValue2 = pr_hi[limit] > pr_lo[limit] && StrToInteger(DoubleToStr(pr_lo[limit], 0)) ? pr_hi[limit] - pr_lo[limit] : 0;
      ExtLineBuffer[limit] = firstValue;
      ExtLine2Buffer[limit] = firstValue2;
      ExtLine3Buffer[limit] = firstValue * 2.2; }
   else  limit = prev_calculated - 1;

/*   haOpen = (ExtOpenBuffer[i-1]+ExtCloseBuffer[i-1])/2;
   haClose=(open[i]+high[i]+low[i]+close[i])/4;
   haHigh=MathMax(high[i],MathMax(haOpen,haClose));
   haLow=MathMin(low[i],MathMin(haOpen,haClose));
   ExtOpenBuffer[0] = iOpen(NULL,0,Bars-1);
   ExtCloseBuffer[0] = iClose(NULL,0,Bars-1);

//--- main loop */
   for(i = limit+1; i < rates_total && ! IsStopped(); i ++) { j = i - InpMAPeriod;
      ExtLineBuffer[i] = ExtLineBuffer[i-1] + ((pr_hi[i-1] > pr_lo[i-1] && StrToInteger(DoubleToStr(pr_lo[i-1], 0)) ? pr_hi[i-1] - pr_lo[i-1] : 0) -
         ((pr_hi[j-1] > pr_lo[j-1] && StrToInteger(DoubleToStr(pr_lo[j-1], 0)) ? pr_hi[j-1] - pr_lo[j-1] : 0))) / InpMAPeriod;
      ExtLine2Buffer[i-1] = pr_hi[i-1] > pr_lo[i-1] && StrToInteger(DoubleToStr(pr_lo[i-1], 0)) ? pr_hi[i-1] - pr_lo[i-1] : 0;
      ExtLine3Buffer[i] = ExtLineBuffer[i] * 2.2; }                     ExtLine2Buffer[i-1] = ExtLine2Buffer[i-2];

//      double StDev = iStdDev(NULL, 0, 5, Bars-i+1, MODE_SMA, PRICE_TYPICAL, 0);    // écartype
//      iStdDevOnArray(ExtCma2Buffer, 100, 14, 0, MODE_SMA, rates_total-limit-1);
//      ExtLineBuffer[i] = ExtCmaBuffer[i] * 1000; }
 }
//+------------------------------------------------------------------+
