Search List of Pairs for Corresponding Value

 

In an EA I need to search a predefined list of currency pairs for a specific pair and return the corresponding value. The pair to be searched for is declared as an input at the start of the script.

In the example below, how would I search for "EURCAD" and have it return the value 3.0?

extern string Pair="EURCAD";

EURAUD=6.0
EURCAD=3.0
EURCHF=6.0
EURCZK=13.0
EURDKK=13.0
EURGBP=6.0
EURHKD=13.0

Could someone kindly provide example code as it relates to this scenario? I've been searching hours now for the best way to achieve this and I'm stuck.

Thank you for the assistance!

 
Canadian Trader:

In an EA I need to search a predefined list of currency pairs for a specific pair and return the corresponding value. The pair to be searched for is declared as an input at the start of the script.

In the example below, how would I search for "EURCAD" and have it return the value 3.0?

extern string Pair="EURCAD";

EURAUD=6.0
EURCAD=3.0
EURCHF=6.0
EURCZK=13.0
EURDKK=13.0
EURGBP=6.0
EURHKD=13.0

Could someone kindly provide example code as it relates to this scenario? I've been searching hours now for the best way to achieve this and I'm stuck.

Thank you for the assistance!

Here's an easy way... 


enum PAIRS = {EURUSD, EURAUD, EURJPY};
double get_value_for[] = {6.0, 3.0, 6.0};

input PAIRS inp_pair = EURUSD; 

my_value = get_value_for[inp_pair];
 
nicholi shen:

Here's an easy way... 


Thanks for the reply and feedback. I was more or less able to adapt your code to my needs. (I had to delete the "=" in the first line to get it to work). This is a great start.

Question. The code you provided requires splitting the predefined list in to two parts; one for the PAIR, and one for ist's corresponding value. Is there a method that keeps this list together? I will need to update this list in the EA periodically by copying and pasting. Splitting it in to two parts makes updating messy and opens the chance for formatting errors etc. Thanks.

enum PAIRS = {EURUSD, EURAUD, EURJPY};
double get_value_for[] = {6.0, 3.0, 6.0};
 
Canadian Trader:

Thanks for the reply and feedback. I was more or less able to adapt your code to my needs. (I had to delete the "=" in the first line to get it to work). This is a great start.

Question. The code you provided requires splitting the predefined list in to two parts; one for the PAIR, and one for ist's corresponding value. Is there a method that keeps this list together? I will need to update this list in the EA periodically by copying and pasting. Splitting it in to two parts makes updating messy and opens the chance for formatting errors etc. Thanks.

bool get_symbol_val(string symbol, double &value)
{
   struct __{string symbol; double value;} array[] = {
      {"EURUSD", 10.5},
      {"USDJPY", 12.4},
      {"GBPUSD", 14.5}
   };
   for(int i=ArraySize(array) - 1; i >= 0; i--)
      if(StringFind(symbol, array[i].symbol) >= 0)
      {
         value = array[i].value
         return true;
      }
   return false;
}
 

or even better... 


enum SYMBOLS{
   EURUSD = 0,
   USDJPY = 1,
   GBPUSD = 2
};

double get_symbol_val(SYMBOLS symbol)
{
   double array[3];
   array[EURUSD] = 10.5;
   array[USDJPY] = 11.5;
   array[GBPUSD] = 12.5;
   return array[symbol];
}
 
nicholi shen:

or even better... 


Thank you. I will play arround with this one and see what I come up with. This still requires splitting the list in to two parts.
Reason: