Problem converting integer to string and concatenation

 

Hi

I am trying to allow an EA to accept some external integer values as string times "HH;MM".

I have the following code which compiles and runs but an error occurs and it says the time is formatted incorrectly.

I suspect it may be my concatenation method but can anyone advise?

extern int StartHr         = 1;
extern int StartMn         =10;
extern int EndHr           = 2;
extern int EndMn           =20;      

string strStartHour  = StringFormat("%[0][2]",IntegerToString(StartHr));
string strStartMin   = StringFormat("%[0][2]",IntegerToString(StartMn));
string strEndHour    = StringFormat("%[0][2]",IntegerToString(EndHr));
string strEndMin     = StringFormat("%[0][2]",IntegerToString(EndMn));
//Print(strEndHour);
string strChartServerStartTime   = StringConcatenate(strStartHour, ":",strStartMin);
string strChartServerEndTime     = StringConcatenate(strEndHour, ":",strEndMin);
string Chart_Server_Start_Time = strChartServerStartTime;
string Chart_Server_End_Time = strChartServerEndTime;


Thanks


Paxman

 

Hello we have

TimeToString()

Function and also

StringToTime()

Function so i am not sure what you are trying to do but time is usually datetime datatype.

You can also see here https://www.mql5.com/en/docs/constants/structures/mqldatetime

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Date Type Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Date Type Structure
  • www.mql5.com
Constants, Enumerations and Structures / Data Structures / Date Type Structure - Reference on algorithmic/automated trading language for MetaTrader 5
 
Paxman:

Hi

I am trying to allow an EA to accept some external integer values as string times "HH;MM".

I have the following code which compiles and runs but an error occurs and it says the time is formatted incorrectly.

I suspect it may be my concatenation method but can anyone advise?

extern int StartHr         = 1;
extern int StartMn         =10;
extern int EndHr           = 2;
extern int EndMn           =20;      

string strStartHour  = StringFormat("%[0][2]",IntegerToString(StartHr));
string strStartMin   = StringFormat("%[0][2]",IntegerToString(StartMn));
string strEndHour    = StringFormat("%[0][2]",IntegerToString(EndHr));
string strEndMin     = StringFormat("%[0][2]",IntegerToString(EndMn));
//Print(strEndHour);
string strChartServerStartTime   = StringConcatenate(strStartHour, ":",strStartMin);
string strChartServerEndTime     = StringConcatenate(strEndHour, ":",strEndMin);
string Chart_Server_Start_Time = strChartServerStartTime;
string Chart_Server_End_Time = strChartServerEndTime;


Thanks


Paxman

string p="23;52"; //"HH;MM"
string s[];
StringSplit( p, ';', s);
int h= StringToInt(s[0]);
int m=StringToInt(s[1]);

print(StringFormat("%02d;%02d", h, m));



 

did u try like this

string Chart_Server_Start_Time = StringFormat("%02d;%02d",StartHr ,StartMn);
 
Marco vd Heijden:

Hello we have

Function and also

Function so i am not sure what you are trying to do but time is usually datetime datatype.

You can also see here https://www.mql5.com/en/docs/constants/structures/mqldatetime

The EA set file accepts a string input as set out HH:MM.

Strategy tester cannot increment such strings to allow testing of timeslots.

My idea was to feed the code with the string but allow the user to input integers.

I think the ideas mentioned here will lead to a solution but i can not see any working without a modification currently. I will try myself and see what i can do.

I tried to do the conversion in stages and i had hoped to check the results using Print (as debug does not work in MT4 so i am told) but could not get print to work either.

BTW i misstyped the separator it should have been a ":".

Thanks

Paxman

 
paul selvan:

did u try like this

This would solve part of the problem but i wonder how to insert the ":" character in between the StartHr StartMin?

Thanks

Paxman
 
Soewono Effendi:
string p="23;52"; //"HH;MM"
string s[];
StringSplit( p, ';', s);
int h= StringToInt(s[0]);
int m=StringToInt(s[1]);

print(StringFormat("%02d;%02d", h, m));



This is interesting but i an starting from a point of two integer values and trying to convert to HH:MM and not the other way about.

Thanks

Paxman

 
After looking on the web i will try string additions not concatenate and see if that works.
 
input int StartHour  =09;
input int StartMinute=15;

datetime g_StartTime;

int OnInit()
  {
   string timestr=StringFormat("%02d:%02d",StartHour,StartMinute);
   g_StartTime=StringToTime(timestr);
   ...
  }

void OnTick()
  {
   if(TimeCurrent()>=g_StartTime) DoSomeWork();
This will work for the current day. If the EA runs overnight however you need to recompute g_StartTime as it contains the current date.
 
Paxman:

This would solve part of the problem but i wonder how to insert the ":" character in between the StartHr StartMin?

Thanks

Paxman

 replace ";" by ":" 

string Chart_Server_Start_Time = StringFormat("%02d:%02d",StartHr ,StartMn);
 
paul selvan:

 replace ";" by ":" 

Ah sorry for some bizzare reason i thought the ; split the format but did not form part of it LOL thanks.

Reason: