How to convert string to bool

 
Hi i want to ask how to convert string to bool something like this 

bool trade_m15=true;
String trade=StringConcatenate(“trade_m”,Period())
bool test = trade 
What im basically trying-to attempt is i have array of time frames and i want to check if the user want to trade these timeframes or not, i know the pseudo code does not make any sense because I don’t know how to explain it in code i hope you guys got my idea thanks


 

I think what you are looking for is variable variables, where 'trade' would become 'trade_m15'? If so, I am sorry to report that this isn't possible in MQL4.

 
Frederick Langemark:

I think what you are looking for is variable variables, where 'trade' would become 'trade_m15'? If so, I am sorry to report that this isn't possible in MQL4.

So if i have to check a block of code for every timeframe no way around it?
 

You could check and store the periods in an array when during the initialization of the script, and then check that they are in the array...

input bool  trade_m1    =  true;
input bool  trade_m5    =  true;
input bool  trade_m15   =  false;
input bool  trade_m30   =  true;
      int   count, tradePeriods[];

int OnInit() {
   if (trade_m1) {
      count =  ArrayResize(tradePeriods, ArraySize(tradePeriods) + 1);
      tradePeriods[ArraySize(tradePeriods) - 1] =  PERIOD_M1;
   }
   
   if (trade_m5) {
      count =  ArrayResize(tradePeriods, ArraySize(tradePeriods) + 1);
      tradePeriods[ArraySize(tradePeriods) - 1] =  PERIOD_M5;
   }
   
   if (trade_m15) {
      count =  ArrayResize(tradePeriods, ArraySize(tradePeriods) + 1);
      tradePeriods[ArraySize(tradePeriods) - 1] =  PERIOD_M15;
   }
   
   if (trade_m30) {
      count =  ArrayResize(tradePeriods, ArraySize(tradePeriods) + 1);
      tradePeriods[ArraySize(tradePeriods) - 1] =  PERIOD_M30;
   }
   
   return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {
   if (InArray(tradePeriods, Period())) {
      Print("Period Enabled");
   } else {
      Print("Period Disabled");
   }
   
   return(rates_total);
}

bool InArray(int& array[], int value) {
   bool  inArray  =  false;
   
   if (ArraySize(array) > 0) {
      for (int i = 0; i < ArraySize(array); i++) {
         if (value == array[i]) {
            inArray  =  true;
         }
      }
   }
   
   return inArray;
}

Although, I'm not sure that really saves you that much trouble.

 

You could have an external variable like this:

extern string timframes="M5,M15,H1";

 and then in you OnCalculate function, you declare a string array:

string TF[];
int t=StringSplit(Timeframe,44,TF);

at that point the stringsplit function would have seperated the text by delimiter (44 is comma), so you would have your TF array with all the different timeframes.

You can then check if your conditions are true or not, or have a mini function that returns the integer value of said timeframe:

int convertTF(string tf)
   {
   if(tf=="M1")return 1;
   else if(tf=="M5") return 5;
   else if(tf=="M15") return 15;
   else if(tf=="M30") return 30;
   else if(tf=="H1") return 60;
   else if(tf=="H4") return 240;
   else if(tf=="D1") return 1440;
   else if(tf=="W1") return 10080;
   else return 5;
   
   }
Reason: