Separate string by comma

 

Hello,

Planning on using multiple orders:

extern string TP="18,46,72";

How do I separate this into int variables?

Hoping to have room for unlimited int variables so how

can I find the number of orders and the individual int values?

THX

 

Hello,

I found an include at Forex Factory that has what I was looking for however I don't know how to call it.

#include <HandyUtilityFunctions.mqh> http://www.forexfactory.com/attachment.php?attachmentid=651955&d=1299014907

extern string TP="18,46,72";

//+----------------------------------------------------------------------------+
//| int StrToIntegerArray(string str, int &a[], string delim=",", int init=0)
//+----------------------------------------------------------------------------+
// Breaks down a single string into integer array 'a' (elements delimited by 'delim')
//  e.g. string is "1,2,3,4,5";  if delim is "," then the result will be
//  a[0]=1   a[1]=2   a[2]=3   a[3]=4   a[4]=5
//  Unused array elements are initialized by value in 'init' (default is 0)
//+----------------------------------------------------------------------------+
int StrToIntegerArray(string str, int &a[], string delim=",", int init=0)
{
        for (int i=0; i<ArraySize(a); i++) 
                a[i] = init;
        int z1=-1, z2=0;
        if (StringRight(str,1) != delim)  str = str + delim;
        for (i=0; i<ArraySize(a); i++)  {
                z2 = StringFind(str,delim,z1+1);
                a[i] = StrToNumber(StringSubstr(str,z1+1,z2-z1-1));
                if (z2 >= StringLen(str)-1)   break;
                z1 = z2;
        }
        return(StringFindCount(str,delim));
}
 
Subgenius:

Hello,

I found an include at Forex Factory that has what I was looking for however I don't know how to call it.

#include <HandyUtilityFunctions.mqh> http://www.forexfactory.com/attachment.php?attachmentid=651955&d=1299014907

extern string TP="18,46,72";


#include <HandyUtilityFunctions.mqh>

extern string myString = "1,39,89,77,500,32,12,74,";
extern int MaxNumOrders = 300;

int start()
{
   int myArray[];   
   int foo = ArrayResize( myArray, MaxNumOrders); 
   int i = StrToIntegerArray(myString,myArray);
   Print("My array has ",i," elements in it" );
   for(int j=0; j<i; j++)
   {
      Print(myArray[j]);
   }   
   return(0);
}

Output from Stratagy Tester:


2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: 74
2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: 12
2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: 32
2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: 500
2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: 77
2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: 89
2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: 39
2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: 1
2011.11.25 02:16:55     2011.08.09 00:30  demo EURUSD,M5: My array has 8 elements in it

 

Hello,

I'm trying to get prices for trailingstop-start, takeprofit, and stoploss from modifying the integer array by multiplying it by a double point and

I don't get a double price which I would need to add or subtract from the market price.

Could I just put the array & point equation in the order?

Like: OrderSend(Symbol(),OP_BUY,1,Ask,3,SLarray[SLslv]*UsePoint-Ask,TParray[TPtpv]*UsePoint+Ask,"My order #2",16384,0,Green);

Here's my code:

//+-Input Parameters------------------------------------------------+
extern string TSS="9,27"; extern string TP="18,46,72"; extern string SL="32,55,66"; extern double Lotsize=1.0;
//+-expert start function--------------------------------------------+
int start()
   { 
   double UsePoint; if(Digits==2||Digits==3) UsePoint=0.01; else if(Digits==4||Digits==5) UsePoint=0.0001;
   //----UPDATE INPUT PARAMETERS
   //---Split strings(identify numbers from strings)
   int TSSorders = StringFindCount(TSS,",") + 1; int TPorders = StringFindCount(TP,",") + 1; int SLorders = StringFindCount(SL,",") + 1;
   //---Create Array(convert strings into arrays)
   int TSSarray[]; int TSSA = ArrayResize(TSSarray,TSSorders); int tsto = StrToIntegerArray(TSS,TSSarray); for(int tsta=0; tsta<tsto; tsta++) //Print(TSSarray[0]+" "+TSSarray[1]);
   int TParray[]; int TPA = ArrayResize(TParray,TPorders); int t = StrToIntegerArray(TP,TParray); for(int p=0; p<t; p++) //Print(TParray[0]+" "+TParray[1]+" "+TParray[2]);
   int SLarray[]; int SLA = ArrayResize(SLarray,SLorders); int s = StrToIntegerArray(SL,SLarray); for(int l=0; l<s; l++) //Print(SLarray[0]+" "+SLarray[1]+" "+SLarray[2]);
   //---Translate Array to point values(change array values into point values)
   for(int TSStpv=0; TSStpv<TSSorders;) //Print("TSSorders="+TSSorders); Print("TSSarray="+TSSarray[TSStpv]);
      {
      TSSarray[TSStpv]=(TSSarray[TSStpv]*UsePoint); Print(TSSarray[TSStpv]*UsePoint);
      TSStpv++;
      } 
   return(0);
   }
//+-expert initialization function-----------------------------------+
int init()
   {return(0);}
//+-StrToIntegerArray----------------------------------------------------------+
int StrToIntegerArray(string str, int &a[], string delim=",", int init=0)
{
        for (int i=0; i<ArraySize(a); i++) 
                a[i] = init;
        int z1=-1, z2=0;
        if (StringRight(str,1) != delim)  str = str + delim;
        for (i=0; i<ArraySize(a); i++)  {
                z2 = StringFind(str,delim,z1+1);
                a[i] = StrToNumber(StringSubstr(str,z1+1,z2-z1-1));
                if (z2 >= StringLen(str)-1)   break;
                z1 = z2;
        }
        return(StringFindCount(str,delim));
}
//+-StringRight----------------------------------------------------------------+
string StringRight(string str, int n=1)
{
        if (n > 0)  return(StringSubstr(str,StringLen(str)-n,n));
        if (n < 0)  return(StringSubstr(str,-n,StringLen(str)-n));
        return("");
}
//+-StrToNumber----------------------------------------------------------------+
double StrToNumber(string str)
{
        int    dp   = -1;
        int    sgn  = 1;
        double num  = 0.0;
        for (int i=0; i<StringLen(str); i++)  {
                string s = StringSubstr(str,i,1);
                if (s == "-")  sgn = -sgn;   else
                        if (s == ".")  dp = 0;       else
                        if (s >= "0" && s <= "9")  {
                                if (dp >= 0)  dp++;
                                if (dp > 0)
                                        num = num + StrToInteger(s) / MathPow(10,dp);
                                else
                                        num = num * 10 + StrToInteger(s);
                        }
        }
        return(num*sgn);
}
//+-StringFindCount------------------------------------------------------------+
int StringFindCount(string str, string str2)
{
        int c = 0;
        for (int i=0; i<StringLen(str); i++)
        if (StringSubstr(str,i,StringLen(str2)) == str2)  c++;
        return(c)
}

Reason: