Switching symbol so my script can work on multiple pairs.

 

Hello! I am wondering how I can allow my script to work on multiple pairs. I know that scripts are limited to the pair you start them on, but there must be a way to automatically get the Symbol of a pair when you know which pair you want.

E.G I know I want "EURUSD" but my graph is on "GBPUSD". I have the symbol index position of "EURUSD" via the following code:

int findPairIndex(string pair)
{
   for (int i = 0; i < SymbolsTotal(false); i++)
   {
      if (SymbolName(i, false) == pair)
      {
         return i;
      }
   }
   return -1;
}

However, I don't know how to get the symbol of that pair to make a MqlTradeRequest to open a position for it.

Alternatively, if there is a way to open a position on a pair without the use of said pair's symbol, that would be ideal.

Thanks in advance for the support.

 
NoneTaken:

Hello! I am wondering how I can allow my script to work on multiple pairs. I know that scripts are limited to the pair you start them on, but there must be a way to automatically get the Symbol of a pair when you know which pair you want.

E.G I know I want "EURUSD" but my graph is on "GBPUSD". I have the symbol index position of "EURUSD" via the following code:

However, I don't know how to get the symbol of that pair to make a MqlTradeRequest to open a position for it.

Alternatively, if there is a way to open a position on a pair without the use of said pair's symbol, that would be ideal.

Thanks in advance for the support.

Er... in your code, your 'pair' variable contains the symbol you want, so you can use that to make a MqlTradeRequest.

If you need the findPairIndex() function to verify that your 'pair' refers to a valid/existing symbol, then any return value other than -1 should be a good indication.

 
Oh right I didn't know that a symbol can be represented as a string instead of using the symbol itself, thank you!