//+------------------------------------------------------------------+
//|                                               BI_T101_OTR_V1.mq4 |
//+------------------------------------------------------------------+

#property copyright "OnTheRoad_Based On T101(Julius Figueroa) BI"

// part of codes are from SMJ alert codes , tradersyoung cpair name postfix , jpy pairs extract, Regards, OTR
// Robert added code to use IBFX or Other pairs list per T101

#define  BASKET_CPAIRS  14

#define  TIME_SEC    1

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 DeepSkyBlue
//#property indicator_color4 RoyalBlue

//---- input parameters

extern bool UseIBFX = true;
extern int     Per        = 10;
extern int     Per1       = 5;

extern int     History    = 288;

extern double  S_R        = 0.000;

extern double  S_R_BRK_dist_for_Alert_on   = 0.000;
//extern double  S_R_BRK_dist_for_Alert_off  = 0.006;

extern string  Note_Check_Candle = "Check_Candle : 0 for current , 1 for last closed candle";
extern int     Check_Candle    = 0;

//extern bool    Alerts_enable      = false;
extern bool    Voice_Alert_enable = true;
//extern bool    Email_Alert_enable = false;
//extern bool    Msngr_Alert_enable = false;

extern string  alert_source_selection_menu = " 0 for S_R , 1 for Fast/Slow cross";
extern int     alert_mode_selection = 1;

int     Max_Alerting_Sec   = 5;

extern   string   Alert_Sound_File_Buy = "Alert.wav";
extern   string   Alert_Sound_File_Sell = "Alert2.wav";

extern bool    Comment_enable = true;


//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];

// IBFX pairs
static string T101_basket_symbolsIBFX[BASKET_CPAIRS]={
   "GBPUSD",
   "EURGBP",
   "EURJPY",
   "GBPJPY",
   "USDCHF",
   "AUDJPY",
   "NZDUSD",
   
   "EURUSD",
   "USDJPY",
   "EURCHF",
   "GBPCHF",
   "CHFJPY",
   "AUDUSD",
   "CHFJPY",
   "NZDJPY" };

// Other brokers pairs
static string T101_basket_symbols[BASKET_CPAIRS]={
   "CADJPY", 
   "AUDUSD", 
   "USDJPY", 
   "EURUSD", 
   "EURCHF",
   "GBPJPY", 
   "USDCAD",
    
   "GBPUSD", 
   "EURGBP", 
   "GBPCHF",
   "CHFJPY", 
   "AUDJPY", 
   "EURJPY", 
   "USDCHF" };

string indic_name = "BI_T101_OTR_V1_F" ;

static datetime dtsv;

static int voice_alarm_timer = 0 ;

datetime       timenowb = 0;
datetime       timenows = 0;
   
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   // dtsv = TimeCurrent();
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,indicator_color1);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,indicator_color2);
   SetIndexBuffer(1,ExtMapBuffer2);
   
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,indicator_color3);
   SetIndexBuffer(2,ExtMapBuffer3);

// SetLevelValue( 0, 0 ) ;
// SetLevelStyle( STYLE_DOT , 1 , Silver ) ;

   SetIndexEmptyValue( 0, 0.0 );
   SetIndexEmptyValue( 1, 0.0 );

   SetIndexEmptyValue( 2, 0.0 );

   indic_name = indic_name + "_(" + Per + "_" + Per1 + "_S/R_" + DoubleToStr( S_R , 4 ) + ") ";

    //return;
   IndicatorShortName(indic_name);
   
   Sleep(1000);
   
   int i ;
   string addchar;
   if (StringLen(Symbol())>6)
      {
      addchar=StringSubstr(Symbol(),6);
      for (i=0;i<BASKET_CPAIRS; i++)
         {
         if (UseIBFX)
         {
           if (StringLen(T101_basket_symbolsIBFX[i])==6) T101_basket_symbolsIBFX[i]=T101_basket_symbols[i]+addchar;
         }
         else
         {
           if (StringLen(T101_basket_symbols[i])==6) T101_basket_symbols[i]=T101_basket_symbols[i]+addchar;
         }
         }
      }
       
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    i , j , l , limit , counted_bars ;
   double coef ;


   if ( Bars < 100 ) { Comment ( "Bars less than 100! exiting..." ) ; return(0); }
 
   if ( ( TimeCurrent() - dtsv ) < ( TIME_SEC  ) ) return; // + MathSqrt(1.1*Period())
   
   if ( voice_alarm_timer > 0 ) voice_alarm_timer -- ;

   RefreshRates ();
   
   dtsv = TimeCurrent() ;
   
   counted_bars = IndicatorCounted() ;

   if(counted_bars<0) return(-1);
   
   if(counted_bars>0) counted_bars--;


   limit = Bars-counted_bars +1 ; // for last candle unsyncronized redrawin!
   
   if ( History < 30 ) History = 30 ;   
   if ( History > 10000 ) History = 10000 ;   
      
   if ( limit < 0 ) return(0);
   
   RefreshRates( );

   if ( Per < 2 ) Per = 2 ;
   if ( Per > 100 ) Per = 100 ;
   
   if ( Per1 < 1 ) Per1 = 1 ;
   if ( Per1 > 100 ) Per1 = 100 ;

   if ( Check_Candle < 0 ) Check_Candle = 0 ;
   if ( Check_Candle > 3 ) Check_Candle = 3 ;

   if ( Max_Alerting_Sec < 2 ) Max_Alerting_Sec = 2 ;
   if ( Max_Alerting_Sec > 10 ) Max_Alerting_Sec = 10 ;
   
   if ( alert_mode_selection < 0 ) alert_mode_selection = 0 ;
   if ( alert_mode_selection > 1 ) alert_mode_selection = 1 ;
   
   int Max_per = Per;
   if ( Per1 > Per ) Max_per = Per1;

   if ( limit > History ) limit = History + Max_per;

   
   for ( i = limit ; i >= 0 ; i -- ) 
      {
      ExtMapBuffer1[i]=0;
      ExtMapBuffer2[i]=0;
      ExtMapBuffer3[i]=0;
      }
 
 
   for ( i = limit ; i >= 0 ; i -- ) 
      {
      for ( j = 0 ; j < BASKET_CPAIRS ; j ++ )
         {
           if (UseIBFX)
           {
             l = StringFind(T101_basket_symbolsIBFX[j],"JPY");
         
             if ( l == -1 ) coef = 1;
             else coef = 100 ;
          
             ExtMapBuffer1[i] += iMA(T101_basket_symbolsIBFX[j],Period(),Per1,0,MODE_SMA,PRICE_CLOSE,i) / coef;
             ExtMapBuffer2[i] += iMA(T101_basket_symbolsIBFX[j],Period(),Per,0,MODE_SMA,PRICE_CLOSE,i) / coef;
           }
           else
           {
             l = StringFind(T101_basket_symbols[j],"JPY");
         
             if ( l == -1 ) coef = 1;
             else coef = 100 ;
          
             ExtMapBuffer1[i] += iMA(T101_basket_symbols[j],Period(),Per1,0,MODE_SMA,PRICE_CLOSE,i) / coef;
             ExtMapBuffer2[i] += iMA(T101_basket_symbols[j],Period(),Per,0,MODE_SMA,PRICE_CLOSE,i) / coef;

           }
         }
         
      if ( S_R > 0 ) ExtMapBuffer3[i] = S_R;

      }


      
      if ( Voice_Alert_enable ) //Alerts_enable )
         {

         int brk_up_alert_sr1 = 0;
         int brk_up_alert_sr2 = 0;
         int brk_down_alert_sr1 = 0;
         int brk_down_alert_sr2 = 0;

/*      
         if (( ExtMapBuffer1[i+Check_Candle] < S_R - S_R_BRK_dist_for_Alert_on ) &&
             ( ExtMapBuffer2[i+Check_Candle] < S_R - S_R_BRK_dist_for_Alert_on ) &&
             ( ExtMapBuffer2[i+Check_Candle] < ExtMapBuffer1[i+Check_Candle] ) &&
             (( ExtMapBuffer1[i+1+Check_Candle] >= S_R ) ||
             ( ExtMapBuffer2[i+1+Check_Candle] >= S_R ) ) )
               { brk_down_alert_sr1 = 1; if ( voice_alarm_timer <= 0 ) voice_alarm_timer = Max_Alerting_Sec ; }
             
         if (( ExtMapBuffer1[i+Check_Candle] > S_R + S_R_BRK_dist_for_Alert_on ) &&
             ( ExtMapBuffer2[i+Check_Candle] > S_R + S_R_BRK_dist_for_Alert_on ) &&
             ( ExtMapBuffer2[i+Check_Candle] > ExtMapBuffer1[i+Check_Candle] ) &&
             (( ExtMapBuffer1[i+1+Check_Candle] <= S_R ) ||
             ( ExtMapBuffer2[i+1+Check_Candle] <= S_R ) ) )
               { brk_up_alert_sr1 = 1; if ( voice_alarm_timer <= 0 ) voice_alarm_timer = Max_Alerting_Sec ; }
*/

         if (( ExtMapBuffer1[Check_Candle] < ( S_R - S_R_BRK_dist_for_Alert_on ) ) && 
             ( ExtMapBuffer2[Check_Candle] < ( S_R - S_R_BRK_dist_for_Alert_on ) ) && 
             ( ExtMapBuffer1[Check_Candle] < ExtMapBuffer2[Check_Candle] ) &&
             (( ExtMapBuffer2[Check_Candle+1] >= S_R ) || ( ExtMapBuffer1[Check_Candle+1] >= S_R ) ) )
               { brk_down_alert_sr1 = 1; voice_alarm_timer = Max_Alerting_Sec ; }
               
         if (( ExtMapBuffer1[Check_Candle] > ( S_R + S_R_BRK_dist_for_Alert_on ) ) && 
             ( ExtMapBuffer2[Check_Candle] > ( S_R + S_R_BRK_dist_for_Alert_on ) ) && 
             ( ExtMapBuffer1[Check_Candle] > ExtMapBuffer2[Check_Candle] ) &&
             (( ExtMapBuffer2[Check_Candle+1] <= S_R ) || ( ExtMapBuffer1[Check_Candle+1] <= S_R ) ) )
               { brk_up_alert_sr1 = 1; voice_alarm_timer = Max_Alerting_Sec ; }
               

         if (( ExtMapBuffer1[Check_Candle] > ( S_R + S_R_BRK_dist_for_Alert_on ) )&&(ExtMapBuffer1[Check_Candle+1] <= S_R)) { brk_up_alert_sr1 = 1; voice_alarm_timer = Max_Alerting_Sec ; }

         if ( Comment_enable ) Comment ( "\n BI_T101_KVN_OTR   (Fast)Red= " , ExtMapBuffer1[0] , "  ,(Slow)Blue= " , ExtMapBuffer2[0] , "   ,BRK_UP=  " , brk_up_alert_sr1 , "   ,BRK_DWN=  " , brk_down_alert_sr1 );

         if ( alert_mode_selection == 0 ) // S_R level cross
            {
            if ((  brk_down_alert_sr1 == 1 )&&( voice_alarm_timer > 0 )&& ( Time[0] != timenows ) )
               {
               Alert(" BI_T101_KVN_OTR Probable SHORT (S/R BreakDown)");
               PlaySound(Alert_Sound_File_Sell);
               timenows = Time[0];
               }

            if ((  brk_up_alert_sr1 == 1 )&&( voice_alarm_timer > 0 )&& ( Time[0] != timenowb ) )
               {
               Alert(" BI_T101_KVN_OTR Probable LONG (S/R BreakUp)");
               PlaySound(Alert_Sound_File_Buy);
               timenowb = Time[0];
               }
            }

         if ( alert_mode_selection == 1 ) // S_R level cross
            {
            if ((ExtMapBuffer1[ Check_Candle ] < ( ExtMapBuffer2[ Check_Candle ] - S_R_BRK_dist_for_Alert_on ) ) && 
               ( ExtMapBuffer1[Check_Candle+1] > ( ExtMapBuffer2[Check_Candle+1] + S_R_BRK_dist_for_Alert_on ) ) && 
               ( Time[0] != timenows ) )
               {
               Alert(" BI_T101_KVN_OTR Probable SHORT (Fast/Slow BRKDN)");
               PlaySound(Alert_Sound_File_Sell);
               timenows = Time[0];
               }

            if ((ExtMapBuffer1[ Check_Candle ] > ( ExtMapBuffer2[ Check_Candle ] + S_R_BRK_dist_for_Alert_on ) ) && 
               ( ExtMapBuffer1[Check_Candle+1] < ( ExtMapBuffer2[Check_Candle+1] - S_R_BRK_dist_for_Alert_on ) ) && 
               ( Time[0] != timenowb ) )
               {
               Alert(" BI_T101_KVN_OTR Probable LONG (Fast/Slow BRKUP)");
               PlaySound(Alert_Sound_File_Buy);
               timenowb = Time[0];
               }

            }


/*            
         if ( Email_Alert_enable )
            {
            }
            
         if ( Msngr_Alert_enable )
            {
            }         
*/
      }
      
      return(0);
   }   