String Split

 

Good day,

May i just enquire if i have a string sent via Pipeline where said string goes as ""EURUSD",OP_Buy,0.1,Ask,2,0,0"

How can i split it into using String Split

Symbol="EURUSD"

cmd=OP_Buy

volume=0.1

so on so forth using the following codes

void OnStart()
  {
string to_split="_life_is_good_"; // A string to split into substrings
   string sep="_";                // A separator as a character
   ushort u_sep;                  // The code of the separator character
   string result[];               // An array to get strings
   //--- Get the separator code
   u_sep=StringGetCharacter(sep,0);
   //--- Split the string to substrings
   int k=StringSplit(to_split,u_sep,result);
   //--- Show a comment 
   PrintFormat("Strings obtained: %d. Used separator '%s' with the code %d",k,sep,u_sep);
   //--- Now output all obtained strings
   if(k>0)
     {
      for(int i=0;i<k;i++)
        {
         PrintFormat("result[%d]=\"%s\"",i,result[i]);
     }
  }
  }
 
JD05:

Good day,

May i just enquire if i have a string sent via Pipeline where said string goes as ""EURUSD",OP_Buy,0.1,Ask,2,0,0"

How can i split it into using String Split

Symbol="EURUSD"

cmd=OP_Buy

volume=0.1

so on so forth using the following codes

Please see https://www.mql5.com/en/docs/strings/stringsubstr
Documentation on MQL5: String Functions / StringSubstr
Documentation on MQL5: String Functions / StringSubstr
  • www.mql5.com
String Functions / StringSubstr - Reference on algorithmic/automated trading language for MetaTrader 5
 

Hi Heijden,


Many thanks for the prompt reply.


Have a great day.


Regards,

JD

 

StringSubstr will not help you 

for example

if your broker price 4 digit and your StringSubstr function calculate depended on 5 digit price

maybe your symbols have prefix like eurusd.m 

it will not help you in this case so you must use your code   

 
so in your case the code will be like this 
void OnStart()
  {
   string to_split="EURUSD,OP_BUY,0.1,1.12309,3,1.12109,1.12409,mycomment"; 
   string sep=",";               
   ushort u_sep;                 
   string result[];              
   u_sep=StringGetCharacter(sep,0);
   int k=StringSplit(to_split,u_sep,result);

   string symbol=result[0];
   int cmd=ordertype(result[1]);
   double Volume=StrToDouble(result[2]);
   double Price=StrToDouble(result[3]);
   int Slippage=StrToInteger(result[4]);
   double StopLoss=StrToDouble(result[5]);
   double TakeProfit=StrToDouble(result[6]);
   string Comment=result[7];

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int ordertype(string value)
  {
   if(value=="OP_BUY")return(0);
   if(value=="OP_SELL")return(1);
   return(-1);
  }
 
Omar AlKassar:
so in your case the code will be like this 

Good day Omar,


Many thanks for the enlightenment.

Its really of great help.

Sincerely,

JD

Reason: