#ifndef TF_INDICATORS_SYMBOL_RESOLVER_MQH
#define TF_INDICATORS_SYMBOL_RESOLVER_MQH

bool TF_IsKnownCurrency(const string currency)
  {
   string codes[8]={"USD","EUR","GBP","JPY","CHF","CAD","AUD","NZD"};
   for(int i=0;i<8;i++) if(currency==codes[i]) return(true);
   return(false);
  }

int TF_CurrencyIndex(const string currency)
  {
   string codes[8]={"USD","EUR","GBP","JPY","CHF","CAD","AUD","NZD"};
   for(int i=0;i<8;i++) if(currency==codes[i]) return(i);
   return(-1);
  }

string TF_CurrencyCode(const int index)
  {
   string codes[8]={"USD","EUR","GBP","JPY","CHF","CAD","AUD","NZD"};
   if(index<0 || index>=8) return("???");
   return(codes[index]);
  }

bool TF_SymbolCurrencies(const string symbol,string &base,string &quote)
  {
   base=SymbolInfoString(symbol,SYMBOL_CURRENCY_BASE);
   quote=SymbolInfoString(symbol,SYMBOL_CURRENCY_PROFIT);
   StringToUpper(base);
   StringToUpper(quote);
   return(StringLen(base)==3 && StringLen(quote)==3);
  }

bool TF_ResolveForexPair(const string wanted_base,const string wanted_quote,
                         string &resolved_symbol,bool &inverted)
  {
   resolved_symbol="";
   inverted=false;
   string direct=wanted_base+wanted_quote;
   string inverse=wanted_quote+wanted_base;
   if(SymbolSelect(direct,true))
     {
      string base,quote;
      if(TF_SymbolCurrencies(direct,base,quote) && base==wanted_base && quote==wanted_quote)
        {
         resolved_symbol=direct;
         return(true);
        }
     }
   if(SymbolSelect(inverse,true))
     {
      string base,quote;
      if(TF_SymbolCurrencies(inverse,base,quote) && base==wanted_quote && quote==wanted_base)
        {
         resolved_symbol=inverse;
         inverted=true;
         return(true);
        }
     }

   int passes=2;
   for(int pass=0;pass<passes;pass++)
     {
      bool selected_only=(pass==0);
      int total=SymbolsTotal(selected_only);
      for(int i=0;i<total;i++)
        {
         string candidate=SymbolName(i,selected_only);
         string base,quote;
         if(!TF_SymbolCurrencies(candidate,base,quote)) continue;
         if(base==wanted_base && quote==wanted_quote)
           {
            if(SymbolSelect(candidate,true))
              {
               resolved_symbol=candidate;
               inverted=false;
               return(true);
              }
           }
         if(base==wanted_quote && quote==wanted_base && resolved_symbol=="")
           {
            if(SymbolSelect(candidate,true))
              {
               resolved_symbol=candidate;
               inverted=true;
              }
           }
        }
      if(resolved_symbol!="") return(true);
     }
   return(false);
  }

bool TF_FindConversionSymbol(const string from_currency,const string to_currency,
                             string &symbol,bool &inverted)
  {
   if(from_currency==to_currency)
     {
      symbol="";
      inverted=false;
      return(true);
     }
   return(TF_ResolveForexPair(from_currency,to_currency,symbol,inverted));
  }

#endif
