StrToDouble

 

There are news event values(actual,forecast,previous) on dailyfx.com/calendar website that need to be converted from STRING into a DOUBLE in my EA to make a trading decision. The problem arises when other characters(not numbers) are present in the STRING, e.g. $569k or -103B. So when the function takes -103B and converts it to DOUBLE, you get -103, but when the function takes $569k and converts it to DOUBLE, you get 0.

StrToDouble("-103B")=-103

StrToDouble("$569k")=0

Other than taking the 1st character or the 2nd character in the STRING, and writing a loop to to see if it is a number, is there anything else that could be done, so that when converting STRING value $569k to DOUBLE, that the function would output 569 instead of 0?

 

afaik, u gotit in one i think... ;)

by combo of for loop + StringLen() + StringGetChar() + StringConcatenate() u can extract digits (0..9) into a string [if scan L>R] OR directly into a double [if scan R>L]

ie, if StringGetChar() gives digit charCode at i'th position then do something with it...

StrToDouble() stops on FIRST non +|-|0..9 chars

ur first works cuz will parse upto 'B' giving -103

ur secnd works cuz will parse upto '$' giving 0

.

u write own function then is own private number getter can use like StrToDouble()

maybe u publish here 4 others?

Best

 

It might of been the long way to do things.. but I couldn't see writing a function in the inside the platform to search your string, remove the unnecessary crap, and then return a double.

Much easier to use Regular Expression to pull out the data for you.. so here is a simple DLL I wrote... just for you.



Place this file "str2double.dll" in the MetaTrader's root directory ie: C:\Program Files\Interbank FX Trader 4\


example usage.


#import "str2double.dll"

double StringToDouble(string str);

#import


int init()
{
double myvalue = StringToDouble("$569k");
Comment("My value: " + DoubleToStr(myvalue, 2));
return(0);
}


Hope this works well for you!

Also if the String you pass to the function can not be parsed it will return 0.00



 

Crap, I'm an idiot.. I'm not converting the K into 569,000.

I'll fix this soon.


Let me know what else needs done if anything


[Edited]

Fixed so it will use a multiplier.
http://www.inf-group.info/820079


k or K = multiplier of 1,000
m or M = multiplier of 1,000,000
b or B = multiplier of 1,000,000,000

ie:

double myvalue = StringToDouble("-5.13m");
Comment("My value: " + DoubleToStr(myvalue, 2));

sets Comment as.. "My Value: -5130000.00"


*note: I know this can still be improved, but it really all depends on the string you are receiving from the dailyfx.com/calendar website. Let me know if you need the dll to be expanded more*

 

yewnix - u clever hacker... please, can u maybe show code?

and maybe wat i need 4 tools 2 do this or maybe even dummy step-by-step guide u did to do this little exercise?

i only got assembler and can get ms or borland C compilers [language not issue] but not clue bout .dll but this looks so useful to crank out goodies to ease much mql4 coding pain - ;)

best

 
yewnix wrote >>

It might of been the long way to do things.. but I couldn't see writing a function in the inside the platform to search your string, remove the unnecessary crap, and then return a double.

Much easier to use Regular Expression to pull out the data for you.. so here is a simple DLL I wrote... just for you.

http://www.inf-group.info/282015

Place this file "str2double.dll" in the MetaTrader's root directory ie: C:\Program Files\Interbank FX Trader 4\

example usage.

#import "str2double.dll"

double StringToDouble(string str);

#import

int init()
{
double myvalue = StringToDouble("$569k");
Comment("My value: " + DoubleToStr(myvalue, 2));
return(0);
}

Hope this works well for you!

Also if the String you pass to the function can not be parsed it will return 0.00


waaawaaawuuuuwaaaa(borat reference), thx BUDDY!

That dll works like a charm, there is no need to worry about the K, 569k is still less then 700k.

 
c0d3:

waaawaaawuuuuwaaaa(borat reference), thx BUDDY!

That dll works like a charm, there is no need to worry about the K, 569k is still less then 700k.

Okay. If you'd like me to remove the multiplier from the code let me know.

I might just write in 2 functions actually. One to use the multiplier and one to not.

 
yewnix wrote >>

Okay. If you'd like me to remove the multiplier from the code let me know.

I might just write in 2 functions actually. One to use the multiplier and one to not.

having two functions could work, but i do not use the multiplier, not sure if others might be interested in having the multiplier, i'm very happy with the 1st dll, where the "K" is just taken out, as it really does not affect the trading decision.

 
c0d3:

having two functions could work, but i do not use the multiplier, not sure if others might be interested in having the multiplier, i'm very happy with the 1st dll, where the "K" is just taken out, as it really does not affect the trading decision.

The first one actually does have an issue. Don't think it allows decimal points.

Here is the latest and greatest one.

Its one function, 2 arguments.


http://www.inf-group.info/747242


#import "str2double.dll"

double StringToDouble(string str, int useMultiplier);

// 0 is false, 1 is true.. in your case you'd use 0 since you don't care about it

#import

int init()
{
double myvalue = StringToDouble("$569k", 0);
Comment("My value: " + DoubleToStr(myvalue, 2));
return(0);
}


Enjoy!

 
yewnix wrote >>

The first one actually does have an issue. Don't think it allows decimal points.

Here is the latest and greatest one.

Its one function, 2 arguments.

http://www.inf-group.info/747242

#import "str2double.dll"

double StringToDouble(string str, int useMultiplier);

// 0 is false, 1 is true.. in your case you'd use 0 since you don't care about it

#import

int init()
{
double myvalue = StringToDouble("$569k", 0);
Comment("My value: " + DoubleToStr(myvalue, 2));
return(0);
}

Enjoy!

Thx!!!! This DLL is what i was looking for, for a long time, without writing loops in the platform, with this DLL the news events double values are stable at all times, appreciate your help.

 
yewnix:

The first one actually does have an issue. Don't think it allows decimal points.

Here is the latest and greatest one.

Its one function, 2 arguments.


http://www.inf-group.info/747242


#import "str2double.dll"

double StringToDouble(string str, int useMultiplier);

// 0 is false, 1 is true.. in your case you'd use 0 since you don't care about it

#import

int init()
{
double myvalue = StringToDouble("$569k", 0);
Comment("My value: " + DoubleToStr(myvalue, 2));
return(0);
}


Enjoy!

I found a problem, the following command: StringToDouble("-B106.0") outputs 106, the minus sign is missing, would you have any ideas as to why that happens?

Reason: