#property copyright ""
#property link      ""

#property indicator_separate_window
#property  indicator_buffers 4
#property indicator_color1 Lime
#property indicator_color2 Pink
#property indicator_color3 Green
#property indicator_color4 Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
#property indicator_width4 3
#property indicator_level1 20
#property indicator_levelcolor Blue
#property indicator_levelwidth 1
#property indicator_levelstyle 0
#property indicator_level2 -20

extern int LookBack = 1000;
extern bool MaxMin = true;
extern string inp = "== Indicators Percentage ==";
extern int MACD_per = 25;
extern int SAR_per = 25;
extern int RSI_per = 10;
extern int STOCH_per = 10;
extern int SMA1_per = 10;
extern int EMA1_per = 10;
extern int EMA2_per = 10;
extern string imdp = "== Indicators MACD Parameters ==";
extern int MACD_fastEMA = 12;
extern int MACD_slowEMA = 26;
extern int MACD_SMA = 9;
int MACDApplPrice = PRICE_CLOSE;
extern string isrp = "== Indicators SAR Parameters ==";
extern double SAR_step = 0.02;
extern double SAR_maximum = 0.2;
extern string irsip = "== Indicators RSI Parameters ==";
extern int RSI_period = 14;
int RSIApplPrice = PRICE_CLOSE;
extern string ishp = "== Indicators STOCH Parameters ==";
extern int STOCH_kPeriod = 5;
extern int STOCH_dPeriod = 3;
extern int STOCH_slowing = 3;
int StochApplPrice = 0;
int StochMAMode = MODE_SMA;
extern string imap = "== Indicators MA Parameters ==";
extern int SMA1_period = 21;
extern int SMA1_shift = 0;
extern int SMA1_method = 0;
extern int SMA1_applyTo = 0;
extern int EMA1_period = 52;
extern int EMA1_shift = 0;
extern int EMA1_method = 1;
extern int EMA1_applyTo = 0;
extern int EMA2_period = 104;
extern int EMA2_shift = 0;
extern int EMA2_method = 1;
extern int EMA2_applyTo = 0;
double HistBufferUp[];
double HistBufferDown[];
double HistMaxBufferUp[];
double HistMinBufferDown[];
int M1TFWeights[8] = {55, 25, 13, 7, 0, 0, 0, 0, 0};     //M1
int M5TFWeights[8] = {15, 50, 25, 15, 0, 0, 0, 0, 0};    //M5
int M15TFWeights[8] = {7, 15, 41, 20, 10, 7, 0, 0, 0};    //M15
int M30TFWeights[8] = {3, 7, 20, 40, 20, 7, 3, 0, 0};     //M30
int H1TFWeights[8] = {3, 7, 10, 20, 40, 10, 7, 3, 0};    //H1
int H4TFWeights[8] = {0, 3, 7, 10, 20, 40, 10, 7, 3};    //H4
int D1TFWeights[8] = {0, 0, 0, 0, 5, 22, 46, 22, 5};     //D1
int W1TFWeights[8] = {0, 0, 0, 0, 0, 10, 18, 50, 22};    //W1
int MN1TFWeights[8] = {0, 0, 0, 0, 0, 0, 10, 20, 70};     //MN1
int TimeFrames[8] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
double ProbHist[1000];
double ProbHistMin[1000];
double ProbHistMax[1000];
int HistCount;
datetime PrevTime;

int init() {
   ArrayResize(ProbHist,LookBack);
   ArrayResize(ProbHistMin,LookBack);
   ArrayResize(ProbHistMax,LookBack);

   SetIndexBuffer(0, HistMaxBufferUp);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexEmptyValue(0, 0);
   SetIndexBuffer(1, HistMinBufferDown);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexEmptyValue(1, 0);
   SetIndexBuffer(2, HistBufferUp);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   SetIndexEmptyValue(2, 0);
   SetIndexBuffer(3, HistBufferDown);
   SetIndexStyle(3, DRAW_HISTOGRAM);
   SetIndexEmptyValue(3, 0);

   IndicatorShortName("II Prob Hist");
   return (0);
}

int deinit() {
   return (0);
}

int start() {
int i;
double MasterProb = 0;
int TFWeight = 0;
double PercMultiplier = 0.01;
int shift;

      for (int j = 0; j < 9; j++) {
         if (Period() == PERIOD_M1) TFWeight = M1TFWeights[j];
         if (Period() == PERIOD_M5) TFWeight = M5TFWeights[j];
         if (Period() == PERIOD_M15) TFWeight = M15TFWeights[j];
         if (Period() == PERIOD_M30) TFWeight = M30TFWeights[j];
         if (Period() == PERIOD_H1) TFWeight = H1TFWeights[j];
         if (Period() == PERIOD_H4) TFWeight = H4TFWeights[j];
         if (Period() == PERIOD_D1) TFWeight = D1TFWeights[j];
         if (Period() == PERIOD_W1) TFWeight = W1TFWeights[j];
         if (Period() == PERIOD_MN1) TFWeight = MN1TFWeights[j];
         
         if (TFWeight > 0) {
/*         
            if (j==0) shift = iBarShift(NULL,PERIOD_M1,Time[i],False)+1;
            if (j==1) shift = iBarShift(NULL,PERIOD_M5,Time[i],False)+1;
            if (j==2) shift = iBarShift(NULL,PERIOD_M15,Time[i],False)+1;
            if (j==3) shift = iBarShift(NULL,PERIOD_M30,Time[i],False)+1;
            if (j==4) shift = iBarShift(NULL,PERIOD_H1,Time[i],False)+1;
            if (j==5) shift = iBarShift(NULL,PERIOD_H4,Time[i],False)+1;
            if (j==6) shift = iBarShift(NULL,PERIOD_D1,Time[i],False)+1;
            if (j==7) shift = iBarShift(NULL,PERIOD_W1,Time[i],False)+1;                                                            
            if (j==8) shift = iBarShift(NULL,PERIOD_MN1,Time[i],False)+1;    

            if (TimeFrames[j] == Period()) shift = i;
*/
            shift = 0;
//            if (i==30) Print(" j = ", j, " Shift = ", shift, " TF = ", TimeFrames[j]);

            if (MACDLong(TimeFrames[j], shift)) {
               MasterProb += TFWeight * MACD_per * PercMultiplier;
            }
            if (MACDShort(TimeFrames[j], shift)) {
               MasterProb -= TFWeight * MACD_per * PercMultiplier;
            }
            if (SARLong(TimeFrames[j], shift)) {
               MasterProb += TFWeight * SAR_per * PercMultiplier;
            }
            if (SARShort(TimeFrames[j], shift)) {
               MasterProb -= TFWeight * SAR_per * PercMultiplier;
            }
            if (RSILong(TimeFrames[j], shift)) {
               MasterProb += TFWeight * RSI_per * PercMultiplier;
            }
            if (RSIShort(TimeFrames[j], shift)) {
               MasterProb -= TFWeight * RSI_per * PercMultiplier;
            }
            if (STOCHLong(TimeFrames[j], shift)) {
               MasterProb += TFWeight * STOCH_per * PercMultiplier;
            }
            if (STOCHShort(TimeFrames[j], shift)) {
               MasterProb -= TFWeight * STOCH_per * PercMultiplier;
            }
            if (SMA1Long(TimeFrames[j], shift)) {
               MasterProb += TFWeight * SMA1_per * PercMultiplier;
            }
            if (SMA1Short(TimeFrames[j], shift)) {
               MasterProb -= TFWeight * SMA1_per * PercMultiplier;
            }
            if (EMA1Long(TimeFrames[j], shift)) {
               MasterProb += TFWeight * EMA1_per * PercMultiplier;
            }
            if (EMA1Short(TimeFrames[j], shift)) {
               MasterProb -= TFWeight * EMA1_per * PercMultiplier;
            }
            if (EMA2Long(TimeFrames[j], shift)) {
               MasterProb += TFWeight * EMA2_per * PercMultiplier;
            }
            if (EMA2Short(TimeFrames[j], shift)) {
               MasterProb -= TFWeight * EMA2_per * PercMultiplier;
            } 
         }
        
      }
      
      if (MasterProb > ProbHistMax[0]) ProbHistMax[0] = MasterProb;
      if (MasterProb < ProbHistMin[0]) ProbHistMin[0] = MasterProb;         
         
      ProbHist[0] = MasterProb;

    for (i = LookBack; i>-1; i--) {
         
      if (ProbHist[i] < 0.0) {
         HistBufferDown[i] = ProbHist[i];
         HistMinBufferDown[i] = ProbHistMin[i];
      } else {
         HistBufferUp[i] = ProbHist[i];
         HistMaxBufferUp[i] = ProbHistMax[i];
      }
   }
   
      if (iTime(NULL,0,1) != PrevTime) {
         for (i = LookBack; i>-1; i--) {
             ProbHist[i+1] = ProbHist[i];
             ProbHistMin[i+1] = ProbHistMin[i];
             ProbHistMax[i+1] = ProbHistMax[i]; 
         }
         ProbHistMin[0] = 0;
         ProbHistMax[0] = 0;
      }   
      
      PrevTime = iTime(NULL,0,1); 

   
//   Print("P0 = ", ProbHist[0], "P1 = ", ProbHist[1],"P2 = ", ProbHist[2]);
   return (0);
}

bool MACDLong(int TF, int BarNo = 0) {
   double MACDMain = iMACD(Symbol(), TF, MACD_fastEMA, MACD_slowEMA, MACD_SMA, MACDApplPrice, MODE_MAIN, BarNo);
   double MACDSignal = iMACD(Symbol(), TF, MACD_fastEMA, MACD_slowEMA, MACD_SMA, MACDApplPrice, MODE_SIGNAL, BarNo);
   if (MACDSignal < MACDMain) return (1);
   return (0);
}

bool MACDShort(int TF, int BarNo = 0) {
   double MACDMain = iMACD(Symbol(), TF, MACD_fastEMA, MACD_slowEMA, MACD_SMA, MACDApplPrice, MODE_MAIN, BarNo);
   double MACDSignal = iMACD(Symbol(), TF, MACD_fastEMA, MACD_slowEMA, MACD_SMA, MACDApplPrice, MODE_SIGNAL, BarNo);
   if (MACDSignal > MACDMain) return (1);
   return (0);
}

bool SARLong(int TF, int BarNo = 0) {
   double SAR = iSAR(Symbol(), TF, SAR_step, SAR_maximum, BarNo);
   if (SAR < Open[0]) return (1);
   return (0);
}

bool SARShort(int TF, int BarNo = 0) {
   double SAR = iSAR(Symbol(), TF, SAR_step, SAR_maximum, BarNo);
   if (SAR > Open[0]) return (1);
   return (0);
}

bool RSILong(int TF, int BarNo = 0) {
   double RSI = iRSI(Symbol(), TF, RSI_period, RSIApplPrice, BarNo);
   if (RSI < 35.0) return (1);
   return (0);
}

bool RSIShort(int TF, int BarNo = 0) {
   double RSI = iRSI(Symbol(), TF, RSI_period, RSIApplPrice, BarNo);
   if (RSI > 65.0) return (1);
   return (0);
}

bool STOCHShort(int TF, int BarNo = 0) {
   double Stoch = iStochastic(Symbol(), TF, STOCH_kPeriod, STOCH_dPeriod, STOCH_slowing, StochMAMode, StochApplPrice, MODE_MAIN, BarNo);
   if (Stoch > 65.0) return (1);
   return (0);
}

bool STOCHLong(int TF, int BarNo = 0) {
   double Stoch = iStochastic(Symbol(), TF, STOCH_kPeriod, STOCH_dPeriod, STOCH_slowing, StochMAMode, StochApplPrice, MODE_MAIN, BarNo);
   if (Stoch < 35.0) return (1);
   return (0);
}

bool SMA1Short(int TF, int BarNo = 0) {
   double SMA1 = iMA(Symbol(), TF, SMA1_period, 0, SMA1_method, SMA1_applyTo, BarNo);
   if (SMA1 > High[SMA1_shift]) return (1);
   return (0);
}

bool SMA1Long(int TF, int BarNo = 0) {
   double SMA1 = iMA(Symbol(), TF, SMA1_period, 0, SMA1_method, SMA1_applyTo, BarNo);
   if (SMA1 < Low[SMA1_shift]) return (1);
   return (0);
}

bool EMA1Long(int TF, int BarNo = 0) {
   double SMA1 = iMA(Symbol(), TF, SMA1_period, 0, SMA1_method, SMA1_applyTo, BarNo);
   double EMA1 = iMA(Symbol(), TF, EMA1_period, 0, EMA1_method, EMA1_applyTo, BarNo);
   if (EMA1 < SMA1) return (1);
   return (0);
}

bool EMA1Short(int TF, int BarNo = 0) {
   double SMA1 = iMA(Symbol(), TF, SMA1_period, 0, SMA1_method, SMA1_applyTo, BarNo);
   double EMA1 = iMA(Symbol(), TF, EMA1_period, 0, EMA1_method, EMA1_applyTo, BarNo);
   if (EMA1 > SMA1) return (1);
   return (0);
}

bool EMA2Long(int TF, int BarNo = 0) {
   double EMA1 = iMA(Symbol(), TF, EMA1_period, 0, EMA1_method, EMA1_applyTo, BarNo);
   double EMA2 = iMA(Symbol(), TF, EMA2_period, 0, EMA2_method, EMA2_applyTo, BarNo);
   if (EMA1 < EMA2) return (1);
   return (0);
}

bool EMA2Short(int TF, int BarNo = 0) {
   double EMA1 = iMA(Symbol(), TF, EMA1_period, 0, EMA1_method, EMA1_applyTo, BarNo);
   double EMA2 = iMA(Symbol(), TF, EMA2_period, 0, EMA2_method, EMA2_applyTo, BarNo);
   if (EMA1 > EMA2) return (1);
   return (0);
}