Enum to string

Back to topics list To post a new topic, please log in or register
avatar
11
fx_matt 2015.10.25 09:58
 

I'm trying to create a drop down menu for currency pairs for an indicator. Using the enum function I can get the pairs in the menu but I need to reference the pairs in the code as a string.

For example I need to reference "EURUSD"  and "GBPUSD" as strings in different calculations but the input I'm assuming on the enum function references numbers.

Below is how I started, I would like BaseCurrency and QuoteCurrency to reference the pairs as strings if possible. any help would be appreciated, still a beginner 

Thanks 

 

#property indicator_separate_window
#property indicator_color1     Aqua
#property indicator_color2     Aqua

#property indicator_level2     2.0
#property indicator_level3    -2.0
#property indicator_levelcolor clrDarkGray
#property indicator_levelstyle STYLE_DOT
#property indicator_levelwidth 1
#property indicator_buffers    2


enum ENUM_FX_PAIRS
  {
   EURUSD,
   GBPUSD,
   AUDUSD,
   NZDUSD,
   USDJPY,
   USDCHF,
   USDCAD
  };


input   ENUM_FX_PAIRS   BaseCurrency  = "EURUSD";
input   ENUM_FX_PAIRS   QuoteCurrency = "GBPUSD";

extern int        ATR_Period = 5;
extern bool       ATR_Chart_Timeframe = True;
extern int        ATR_Timeframe = 5;


double ExtMapBuffer1[];
double ExtMapBuffer2[];

string Chart_Sym = BaseCurrency + " " + QuoteCurrency + " Period (" + string(ATR_Period) + ") " ;

 

 

 

avatar
736
honest_knave 2015.10.25 11:13 #
 

enum ENUM_FX_PAIRS
  {
   EURUSD,
   GBPUSD,
   AUDUSD,
   NZDUSD,
   USDJPY,
   USDCHF,
   USDCAD
  };


//input   ENUM_FX_PAIRS   BaseCurrency  = "EURUSD";
//input   ENUM_FX_PAIRS   QuoteCurrency = "GBPUSD";
input   ENUM_FX_PAIRS   BaseCurrency  = EURUSD;
input   ENUM_FX_PAIRS   QuoteCurrency = GBPUSD;

Don't put the quotation marks around EURUSD and GBPUSD

 

Use the following to return the string "EURUSD":

EnumToString(BaseCurrency)

 

 

avatar
11
fx_matt 2015.10.25 11:40 #
 
honest_knave:

Don't put the quotation marks around EURUSD and GBPUSD

 

Use the following to return the string "EURUSD":

 

 

Got it thanks 
avatar
736
honest_knave 2015.10.25 11:43 #
 

No problem. I'd suggest adding this to your code too:

#property strict

 It would have flagged up the issue with your quotation marks.

avatar
1324
jjc 2015.10.25 11:44 #
 
fx_matt:

Below is how I started, I would like BaseCurrency and QuoteCurrency to reference the pairs as strings if possible. any help would be appreciated, still a beginner  

The following example allows entry of two separate currencies, e.g. NZD and CAD, and converts those into a currency pair string such as "NZDCAD". Doesn't care about the order; will return "USDJPY" regardless of whether the inputs are USD and JPY, or JPY and USD.

// Declares the enum
enum ENUM_FX_PAIRS {USD, EUR, GBP, AUD, NZD, JPY, CHF, CAD};

// Takes two currencies and finds the matching symbol in the market watch. Doesn't care about
// the order. For example, if c1 is JPY and c2 is USD, then this will return USDJPY.
// By default, looks only at symbols selected for inclusion in the market watch, but 
// can be configured to look at all available broker symbols. By default, ignores
// suffixes (EURUSD+, EURUSD, EURUSDcx) etc. If there are multiple matching symbols
// then it will simply return the first. Can be configured to look for a specific
// suffix, including the absence of any suffix
// [Not fully tested!]
string ConvertInputsToPair(ENUM_FX_PAIRS c1, ENUM_FX_PAIRS c2, bool InMarketWatchOnly = true, string SymbolSuffix = "don't care")
{
   if (c1 == c2) return ""; // Can't have a pair if they're the same
   
   string strC1 = EnumToString(c1);
   string strC2 = EnumToString(c2);
   
   for (int i = 0; i < SymbolsTotal(InMarketWatchOnly); i++) {
      string strTest = SymbolName(i, InMarketWatchOnly);
      int find1 = StringFind(strTest, strC1);
      int find2 = StringFind(strTest, strC2);
      
      if ((find1 == 0 || find1 == 3) && (find2 == 0 || find2 == 3)) {
         if (SymbolSuffix == "don't care") {
            // Not requiring a particular suffix
            return strTest;
         
         } else if (SymbolSuffix == "") {
            // If the caller has requested a blank suffix, then we want the symbol
            // name to be 6 characters long
            if (StringLen(strTest) == 6) {
               return strTest;
            } else {
               // Not a match; keep looping
            }
         
         } else {
            if (StringSubstr(strTest, 6) == SymbolSuffix) {
               return strTest;
            } else {
               // Not a match; keep looping
            }
         }
      }
   }
   
   // If we get here, then there's no matching symbol
   return "";
}


// EXAMPLE USAGE...

input    ENUM_FX_PAIRS   Currency1 = JPY;
input    ENUM_FX_PAIRS   Currency2 = USD;
input    bool            SearchWholeSymbolListNotJustMarketWatch = false;

void OnStart()
{
   string strPair = ConvertInputsToPair(Currency1, Currency2, !SearchWholeSymbolListNotJustMarketWatch);
   MessageBox("[First] matching pair in market watch is : " + strPair);
}

 

avatar
11
fx_matt 2015.10.26 02:18 #
 
Great tips
Back to topics list  

To add comments, please log in or register