//+------------------------------------------------------------------+
//|                                         4Min_Binary_Strategy.mq4 |
//|                                                              JDP |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "JDP"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window
#define  NL    "\n"

extern bool DoAlert = true;
int CurrentState = 0;
double Price;
int LastTm;
int Total,TotalSuccess;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 (Time[0]+180>=TimeCurrent()) {
      if (iCustom(Symbol(),0,"SMA CrossOver_Justin",1,1)!=EMPTY_VALUE) {
         if (iCustom(Symbol(),0,"ADXcrossesNon-repainting",3,0,0)!=EMPTY_VALUE) {
            if (iCustom(Symbol(),0,"@@ADXcrossesNon-repainting",5,0,0)!=EMPTY_VALUE) {
               if (CurrentState==0) {
                  CurrentState=1;
                  Price = Close[0];
                  if (DoAlert) {
                     if (LastTm < Time[0]) {
                        Alert("CALL: ",Symbol()," : ",DoubleToStr(Close[0],Digits)," : EXPIRY: ",TimeToStr(Time[0]+300));
                     }   
                  }
                  LastTm = Time[0];
               }
            }
         }
      } 
      if (iCustom(Symbol(),0,"SMA CrossOver_Justin",0,1)!=EMPTY_VALUE) {
         if (iCustom(Symbol(),0,"ADXcrossesNon-repainting",3,1,0)!=EMPTY_VALUE) {
            if (iCustom(Symbol(),0,"@@ADXcrossesNon-repainting",5,1,0)!=EMPTY_VALUE) {
               if (CurrentState==0) {
                  CurrentState = -1;
                  Price = Close[0];
                  if (DoAlert) {
                     if (LastTm < Time[0]) {
                        Alert("PUT: ",Symbol()," : ",DoubleToStr(Close[0],Digits)," : EXPIRY: ",TimeToStr(Time[0]+300));
                     }   
                  }
                  LastTm = Time[0];
               }   
            }
         }
      }
   //}   
   if (CurrentState==1) {
      if (LastTm==Time[1]) {
         
         if (Close[1]>Price) {
            Print("CALL SUCCESS: ",DoubleToStr(Price,Digits)," TO ",DoubleToStr(Close[1],Digits));
            TotalSuccess = TotalSuccess + 1;
         } else {
            Print("CALL FAILURE: ",DoubleToStr(Price,Digits)," TO ",DoubleToStr(Close[1],Digits));
         }
         Total = Total + 1;
         CurrentState = 0;
      }
   }
   if (CurrentState==-1) {
      if (LastTm==Time[1]) {
         if (Close[1]<Price) {
            Print("PUT SUCCESS: ",DoubleToStr(Price,Digits)," TO ",DoubleToStr(Close[1],Digits));
            TotalSuccess = TotalSuccess + 1;
         } else {
            Print("PUT FAILURE: ",DoubleToStr(Price,Digits)," TO ",DoubleToStr(Close[1],Digits));
         }
         Total = Total + 1;
         CurrentState = 0;
      }
   }
   DoDisplay();
   return(rates_total);
  }
//+------------------------------------------------------------------+
void DoDisplay() {
   string s;
   double tmp;
   s = NL + "5 Minute Binary Strategy" + NL;
   s = s + "===============" + NL + NL;
   if (DoAlert) {
      s = s + "ALERT: ON" + NL;
   } else {
      s = s + "ALERT: OFF" + NL;
   }   
   if (CurrentState==0) {
      s = s + "STATE: WAITING"+NL+NL;
   }
   if (CurrentState==1) {
      s = s + "STATE: CALL"+NL+NL;
   }   
   if (CurrentState==-1) {
      s = s + "STATE: PUT"+NL+NL;
   }   
   s = s + "TOTAL TRADES: " + IntegerToString(Total) + NL;
   s = s + "TOTAL WINS: " + IntegerToString(TotalSuccess) + NL;
   if (Total>0) {
      tmp = TotalSuccess;
      tmp = tmp/Total;
      s = s + "SUCCESS PERCENTAGE: " + DoubleToStr(tmp*100,2)+NL;
   }   
   Comment(s);
}