//#property strict

#property indicator_separate_window
#property indicator_minimum   20
#property indicator_maximum   100
/*#property indicator_level1    30.0
#property indicator_level2    70.0 */
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
#property indicator_width1    2
#property indicator_width2    2
#property indicator_width3    2
#property indicator_width4    2
#property indicator_width5    1
#property indicator_buffers   5
#property indicator_color1    clrLimeGreen
#property indicator_color2    clrBlue
#property indicator_color3    clrOrange
#property indicator_color4    clrPurple
#property indicator_color5    clrSilver
//--- indicator parameters
int            InpMAPeriod=100;           // Period
int            InpMAShift=0;              // Shift

//--- indicator buffer
double ExtLineBuffer[],ExtCmaBuffer[],ExtLine2Buffer[],ExtCma2Buffer[],ExtStdvBuffer[],
   TS=MarketInfo(Symbol(), MODE_TICKSIZE), TV=MarketInfo(Symbol(), MODE_TICKVALUE), TS_=MarketInfo("USDCHF", MODE_TICKSIZE), TV_=MarketInfo("USDCHF", MODE_TICKVALUE),
   dtmp=0.0, dtmp2=0.0, dtmp3=0.0, Pt, Tv, TsTv;//=(TS*TV) / (TS_*TV_);

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

   IndicatorShortName("ProMkt_"+string(InpMAPeriod)+")");
   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);
   SetIndexShift(1, InpMAShift);
   SetIndexDrawBegin(1, draw_begin);
   SetIndexBuffer(1, ExtCmaBuffer);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexShift(2, InpMAShift);
   SetIndexDrawBegin(2, draw_begin);
   SetIndexBuffer(2, ExtLine2Buffer);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexShift(3, InpMAShift);
   SetIndexDrawBegin(3, draw_begin);
   SetIndexBuffer(3, ExtCma2Buffer);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexShift(4, InpMAShift);
   SetIndexDrawBegin(4, draw_begin);
   SetIndexBuffer(4, ExtStdvBuffer);

   if (! TS_) TS_ = 0.00001;     if (! TV_) TV_ = 1.115;       TsTv = (TS_*TV_) / (TS*TV);      Tv = TV_ / TV;       Pt = TS_ / TS;
   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(ExtCmaBuffer, false);
   ArraySetAsSeries(ExtLine2Buffer, false);
   ArraySetAsSeries(ExtCma2Buffer, false);
   ArraySetAsSeries(ExtStdvBuffer, 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(ExtCmaBuffer, 0);
      ArrayInitialize(ExtLine2Buffer, 0);
      ArrayInitialize(ExtCma2Buffer, 0);
      ArrayInitialize(ExtStdvBuffer, 0); }

   Calcul_ProMkt(rates_total,prev_calculated,open,close,high,low);

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

//+------------------------------------------------------------------+
void Calcul_ProMkt(int rates_total,int prev_calculated,const double &pr_cl[],const double &pr_op[],const double &pr_hi[],const double &pr_lo[]) {
   int i, j, limit;     double w_high=iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, InpMAPeriod, 0)) * TsTv, 
      w_low=iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, InpMAPeriod, 0)) * TsTv, height=w_high-w_low;

//--- first calculation or number of bars was changed
   if (prev_calculated == 0) {
      limit = InpMAPeriod;
      //--- calculate first visible value
      double firstValue=0, firstValue2=0;
      for(i = 0; i < limit; i ++) { firstValue += fabs(pr_cl[i] - pr_op[i]) * TsTv;
         firstValue2 += (fabs(pr_cl[i]-pr_op[i]) + fabs(pr_hi[i]-pr_lo[i])) / 2 * TsTv; }
      ExtCmaBuffer[limit-1] = firstValue / InpMAPeriod / (Point * Pt) - 7;
      ExtCma2Buffer[limit-1] = firstValue2 / InpMAPeriod / (Point * Pt) - 40;
      firstValue /= InpMAPeriod * height * 100;
      firstValue2 /= InpMAPeriod * height * 100;
//Alert("***** "+height+" *****");
      ExtLineBuffer[limit-1] = firstValue / (Point * Pt) + 15;
      ExtLine2Buffer[limit-1] = firstValue2 / (Point * Pt) + 15;                       // * TsTv / (Point * Pt) / Tv
      ExtStdvBuffer[limit-1] = iStdDev(NULL, 0, 25, 0, MODE_SMA, PRICE_TYPICAL, rates_total-limit-1) / Point / 20 + 20; }
   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; i < rates_total && ! IsStopped(); i ++) { j = i - InpMAPeriod;
      w_high = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, InpMAPeriod, rates_total-i-2)) * TsTv;
      w_low = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, InpMAPeriod, rates_total-i-2)) * TsTv;      height = w_high-w_low;
      ExtCmaBuffer[i] = ExtCmaBuffer[i-1] + (fabs(pr_cl[i]-pr_op[i]) - fabs(pr_cl[j]-pr_op[j])) * TsTv / InpMAPeriod / (Point * Pt);
      ExtLineBuffer[i] = ExtCmaBuffer[i] / height / 100 + 15;
      ExtCma2Buffer[i] = ExtCma2Buffer[i-1] + ((fabs(pr_cl[i]-pr_op[i]) + fabs(pr_hi[i]-pr_lo[i])) / 2 -
         (fabs(pr_cl[j]-pr_op[j]) + fabs(pr_hi[j]-pr_lo[j])) / 2) * TsTv / InpMAPeriod / (Point * Pt);
      ExtLine2Buffer[i] = ExtCma2Buffer[i] / height / 100 + 15;                  // * TsTv / (Point * Pt) / Tv
      ExtStdvBuffer[i] = iStdDev(NULL, 0, 25, 0, MODE_SMA, PRICE_TYPICAL, rates_total-i-2) / Point / 20 + 20; }

//      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; }

   for(i = limit; i < rates_total-1 && ! IsStopped(); i ++) dtmp = (dtmp * (i-1) + (ExtLine2Buffer[i] - ExtLineBuffer[i])) / i;
   for(i = limit; i < rates_total && ! IsStopped(); i ++) ExtLine2Buffer[i] -= dtmp - 4 + 7.0 * (Tv - 1.0);

   for(i = limit; i < rates_total-2 && ! IsStopped(); i ++) dtmp2 = (dtmp2 * (i-1) + (ExtCmaBuffer[i] - ExtLineBuffer[i])) / i;
   for(i = limit; i < rates_total-2 && ! IsStopped(); i ++) ExtCmaBuffer[i] -= dtmp2 - 4 + 7.0 * (Tv - 1.0);

   for(i = limit; i < rates_total-2 && ! IsStopped(); i ++) dtmp3 = (dtmp3 * (i-1) + (ExtCma2Buffer[i] - ExtLineBuffer[i])) / i;
   for(i = limit; i < rates_total-2 && ! IsStopped(); i ++) ExtCma2Buffer[i] -= dtmp3 - 4 + 7.0 * (Tv - 1.0);

   ExtStdvBuffer[rates_total-1] = ExtStdvBuffer[rates_total-2]; }
//+------------------------------------------------------------------+
