#property copyright "me, 2016"
#property link      "http://www.mql5.com"
#property version   "3.1"

#property description "Scans all available symbols for setup based on method at http://www.forexfactory.com/showthread.php?t=588548"

#property indicator_chart_window

input int RSI_Period = 13; // RSI_Period: 8-25
input ENUM_APPLIED_PRICE RSI_Price = PRICE_CLOSE;
input int Volatility_Band = 34; // Volatility_Band: 20-40
input double StdDev = 1.6185; // Standard Deviations: 1-3
input int RSI_Price_Line = 2;      
input ENUM_MA_METHOD RSI_Price_Type = MODE_SMA;
input int Trade_Signal_Line = 7;   
input ENUM_MA_METHOD Trade_Signal_Type = MODE_SMA;
input int secBeforeNewBar = 600; // Will find all pairs for setup "x" seconds before new bar
input bool UseAlerts = false;
input bool UseNotifications = false;
input bool UseEmail = false;

double RSIBuf[], UpZone[], MdZone[], //yellow
       DnZone[], MaBuf[], //green
       MbBuf[]; //red

string Symbols[];
int SymbolCount;
string lastSyms;
datetime now;
bool alerted = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{  
   IndicatorShortName("Mehdi(" + IntegerToString(RSI_Period) + "," + IntegerToString(Volatility_Band) + "," + IntegerToString(RSI_Price_Line) + "," + IntegerToString(Trade_Signal_Line) +  ")");
   return(0);
}

void OnDeinit(const int reason)
{
   Comment("");
}

//+------------------------------------------------------------------+
//| Traders Dynamic Index                                            |
//+------------------------------------------------------------------+
int start()
{
 if (IsNewBar()) alerted = false;
 
 if ((RemainingTime() < secBeforeNewBar) && (!alerted)) {
   string allsyms;
   string Currencies[] = {"AUD", "CAD", "CHF", "EUR", "GBP", "HKD", "JPY", "NZD", "RUB", "SGD", "USD", "XAG", "XAU"};
   int CurrencyCount = ArrayRange(Currencies, 0);
   int Loop, SubLoop;
   string TempSymbol;
   double curTdiGreen, curTdiRed, curTdiYellow;
   double prevTdiGreen, prevTdiRed, prevTdiYellow;
   for(Loop = 0; Loop < CurrencyCount; Loop++)
     {
       for(SubLoop = 0; SubLoop < CurrencyCount; SubLoop++)
         {
           TempSymbol = Currencies[Loop] + Currencies[SubLoop];
           if(MarketInfo(TempSymbol, MODE_BID) > 0)
             {
               RefreshRates();
               ArrayResize(Symbols, SymbolCount + 1);
               Symbols[SymbolCount] = TempSymbol;
               curTdiGreen   = iCustom(TempSymbol,0,"TDI Red Green.ex4",13,0,34,2,0,7,0,4,0);
               prevTdiGreen  = iCustom(TempSymbol,0,"TDI Red Green.ex4",13,0,34,2,0,7,0,4,1);
               curTdiRed     = iCustom(TempSymbol,0,"TDI Red Green.ex4",13,0,34,2,0,7,0,5,0);
               prevTdiRed    = iCustom(TempSymbol,0,"TDI Red Green.ex4",13,0,34,2,0,7,0,5,1);
               curTdiYellow  = iCustom(TempSymbol,0,"TDI Red Green.ex4",13,0,34,2,0,7,0,2,0);
               prevTdiYellow = iCustom(TempSymbol,0,"TDI Red Green.ex4",13,0,34,2,0,7,0,2,1);

               if (IsStochUp(TempSymbol))
               { //list all pairs with buy signal
                 if ((curTdiGreen>curTdiRed)&&(curTdiRed>curTdiYellow)&&(prevTdiRed>prevTdiGreen))
                   allsyms = allsyms + TempSymbol +" ^\n";
                 } else {  //list all pairs with sell signal
                 if ((curTdiYellow>curTdiRed)&&(curTdiRed>curTdiGreen)&&(prevTdiGreen>prevTdiRed))
                   allsyms = allsyms + TempSymbol +" v\n";
               }
               SymbolCount++;
             }
         }
       }
       
   if (lastSyms!=allsyms) {
      if (UseAlerts)
         Alert(allsyms);
      if (UseNotifications)
         SendNotification(allsyms);
      if (UseEmail)
         SendMail("Mehdi Scanner", allsyms);
   
      lastSyms = allsyms;
   }
   
   Comment(allsyms);
   alerted = true;

 }   
   return (0);
}



bool IsStochUp(string pair)
{
   if (iStochastic(pair,0,14,3,3,MODE_SMA,0,MODE_MAIN,0) > 50)
   {
      return true;
   }
   return false;
}


double RemainingTime()
{
double g;
g=Time[0]+Period()*60-TimeCurrent();
return(g);
}


bool IsNewBar() {
   if (now != Time[0]) {
      now = Time[0];
      return (true);
   }
   return (false);
}