//+------------------------------------------------------------------+
//|                                                kiosotto SELL.mq4 |
//|                                   Copyright 2014, Masakazu Corp. |
//|                                          http://www.masakazu.com |
//+------------------------------------------------------------------+
// NoRepaint, mod by Genry 18-dec-2018
// Kiosotto SELL BUY 2015 v4 Alert [mobidik]ms-nrp.mq4

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 clrAqua
#property indicator_color2 clrMagenta
#property indicator_width1 3
#property indicator_width2 3

#property indicator_levelcolor clrSilver

extern string Symbol_Calc = "";
extern int  dev_period    = 150;
extern int  AlertsLevel   = 15;
extern int  bars          = 900;
extern int  SignalBar     = 1;
extern bool AlertsMessage = false; 
extern bool AlertsSound   = false;
extern bool AlertsEmail   = false;
extern bool AlertsMobile  = false;


datetime TimeBar;
double ExtMapBuffer1[];
double ExtMapBuffer2[];

int init()
  {
   IndicatorBuffers(2);
   
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtMapBuffer1);

   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,ExtMapBuffer2);

   SetLevelValue(0,AlertsLevel);
   if (Symbol_Calc == "") Symbol_Calc = Symbol();
    else  StringToUpper(Symbol_Calc);
    
   string name = WindowExpertName()+" "+Symbol_Calc+" Per="+(string)dev_period+" Lev="+(string)AlertsLevel;
   IndicatorShortName(name);
   return(0);
  }

int start()
  {
  { 
   int counted_bars=IndicatorCounted();
   int i,j,limit;
   double hpres, lpres, TSBUL, TSBER, sbl, sbr;
   limit=Bars-counted_bars;
   j=0;
   if (bars== 0) bars = limit;
   if (limit> bars)  limit=bars;
   for(i = limit; i >= 0; i--) {
      j = 0;
      TSBUL = 0;
      TSBER = 0;
      hpres =0;
      lpres = 9999999;
      while (j<dev_period)   
      {
        sbl = 0;
        sbr = 0;
         {
            int shift=i+j; 
            datetime date = iTime(Symbol_Calc, 0, shift); 
            int bsht = iBarShift(Symbol_Calc, Period(), date, false);
            int kolichestvo = bsht-MathRound(Period()/Period());
            if (kolichestvo < 0 ) {kolichestvo=0;}
            for (int n=bsht;n>kolichestvo;n--) //for (int n=bsht;n>=kolichestvo;n--)
            {
               double ii = iRSI(Symbol_Calc,0,dev_period,PRICE_CLOSE,n);
               double hnw = iHigh(Symbol_Calc, Period(), n);
               double lnw = iLow(Symbol_Calc, Period(), n);
               double cle = iClose(Symbol_Calc, Period(), n);
                             
                             
               if (hnw> hpres)
               {
                  hpres = hnw;
                  sbl = sbl + ii*cle;
               }
               if (lpres> lnw)
               {
                  lpres = lnw;
                  sbr = sbr + ii*cle;
               }               
              }            
             }
                 TSBUL = TSBUL + sbl ;
                 TSBER = TSBER + sbr ;
         
               j++;
           }

          if(TSBUL!=0) ExtMapBuffer1[i] = TSBER/TSBUL; else ExtMapBuffer1[i] = 0;
          if(TSBER!=0) ExtMapBuffer2[i] = TSBUL/TSBER; else ExtMapBuffer2[i] = 0;
       }
  }
   
 if(AlertsMessage || AlertsSound || AlertsEmail || AlertsMobile)
  { 
   string message1 = (WindowExpertName()+" - "+Symbol_Calc+"  "+PeriodString()+" - Signal Up");
   string message2 = (WindowExpertName()+" - "+Symbol_Calc+"  "+PeriodString()+" - Signal Dn");
       
    if(TimeBar!=Time[0] && ExtMapBuffer2[SignalBar]>AlertsLevel)
     { 
        if (AlertsMessage) Alert(message1);
        if (AlertsSound)   PlaySound("alert2.wav");
        if (AlertsEmail)   SendMail(Symbol_Calc+" - "+WindowExpertName()+" - ",message1);
        if (AlertsMobile)  SendNotification(message1);
        TimeBar=Time[0];
     }
    if(TimeBar!=Time[0] && ExtMapBuffer1[SignalBar]>AlertsLevel)
     { 
        if (AlertsMessage) Alert(message2);
        if (AlertsSound)   PlaySound("alert2.wav");
        if (AlertsEmail)   SendMail(Symbol_Calc+" - "+WindowExpertName()+" - ",message2);
        if (AlertsMobile)  SendNotification(message2);
        TimeBar=Time[0];
    }
  }
  return(0);
}
//+------------------------------------------------------------------+
//| Period String                                                    |
//+------------------------------------------------------------------+
string PeriodString()
{
    switch (_Period) 
     {
        case PERIOD_M1:  return("M1");
        case PERIOD_M5:  return("M5");
        case PERIOD_M15: return("M15");
        case PERIOD_M30: return("M30");
        case PERIOD_H1:  return("H1");
        case PERIOD_H4:  return("H4");
        case PERIOD_D1:  return("D1");
        case PERIOD_W1:  return("W1");
        case PERIOD_MN1: return("MN1");
     }    
   return("M" + string(_Period));
}
//-------------------------------------------------------------------+