Normalizedouble not working

fx805
Posts: 5
Joined: Sun Dec 29, 2013 4:37 am

Normalizedouble not working

Post by fx805 »

I swear-- its always the small things i get hung up lol..

So was coding up a function last night and was just trying to Comment my Tp and Sl levels on the screen.. They showed as the full numbers going many Digits past the decimal point... Ok.. So i Normalize it down to 2 digits... and it just wont work.. I either get the same number of many digits showing or sometimes ill get 4 .. But either way it was never consistent..I even made up a dummy test EA just to test this one specific thing and it did the same problems there.. You can see that the Tp normalized down but the Sl didnt.. Same code used for both..

Any ideas?

the code used;

Code: Select all

void OnTick()
  {
            GetSlAmount();
            GetTpAmount();
            
            Comment("Sl amount = ",slamount,
            "\n Tp Amount = ",TpAmount);
   
  }
//+------------------------------------------------------------------+
void GetSlAmount()            {
             slamount=NormalizeDouble(.01*(SlPercent*Balance),2);
                                 }//end of GetSlAmount(); 
                              
void GetTpAmount()
                                 {
             TpAmount=NormalizeDouble(.01*(TpPercent*Balance),2);
                                 }//end of GetTpAmount();
image of result..
Sl and TP.JPG
You do not have the required permissions to view the files attached to this post.
User avatar
tomele
Administrator
Posts: 1208
Joined: Tue May 17, 2016 3:40 pm
Location: Germany, Forest of Odes, Defending the Limes

Normalizedouble not working

Post by tomele »

DoubleToString is what you might be searching for.

Cheers
Happy pippin, Thomas :-BD

It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so.
(Mark Twain)

Keep the coder going: Donate
fx805
Posts: 5
Joined: Sun Dec 29, 2013 4:37 am

Normalizedouble not working

Post by fx805 »

Thanks tomele.. I tried it before and that didnt work.. I gave it another go with DoubletoSting and my resluts are as you see below.. with 4 digits past the decimal point... Though i put 2 in the doubletostring brackets.

Code: Select all

DoubleToString(slamount=(.01*(SlPercent*Balance)),2);
sl and tp2.JPG
You do not have the required permissions to view the files attached to this post.
User avatar
tomele
Administrator
Posts: 1208
Joined: Tue May 17, 2016 3:40 pm
Location: Germany, Forest of Odes, Defending the Limes

Normalizedouble not working

Post by tomele »

I wouldnt code an assignment into a formatting function. No idea what that will cause.

I would code it as:

Code: Select all

slamount=.01*SlPercent*Balance;
Comment("Sl amount = ",DoubleToString(slamount,2));
Cheers
Happy pippin, Thomas :-BD

It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so.
(Mark Twain)

Keep the coder going: Donate
fx805
Posts: 5
Joined: Sun Dec 29, 2013 4:37 am

Normalizedouble not working

Post by fx805 »

i did try it that way also.. I tried it both ways..

Code: Select all

DoubleToString(slamount=(.01*(SlPercent*Balance)),2);
and

Code: Select all

slamount=.01*SlPercent*Balance;
DoubleToString(slamount,2);
I got the same results both ways... Crazy...

Thanks for taking your time to take a look.. :)
fx805
Posts: 5
Joined: Sun Dec 29, 2013 4:37 am

Normalizedouble not working

Post by fx805 »

This is getting weirder and weirder.. Do you think it could be a bug on Empty4?

Can some of you guys try this code and see what you get on the screen... As you can see in the code im just using a static value for Balance and on the screen its showing the whole number and not reducing it down to the 2 digits the doubletostring(,2) is asking for.. This makes me wonder what other functions are working wonky..

ps-- i tried this on two different Empty4 terminals-- both using the current build and got the same resluts.

Code: Select all

double slamount,TpAmount,SLAMOUNT, Balance;
int SlPercent=1;
int TpPercent=2;

int OnInit()
  {
EventSetTimer(1);
return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {}
void OnTimer()
  {
  Balance=1234.56789;
  GetSlAmount();
  Comment("Sl amount = ",slamount);
 }
//+--------------------------------------------+
void GetSlAmount(){
 slamount=Balance;
DoubleToString(slamount,2);
}//end of GetSlAmount(); 
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Normalizedouble not working

Post by renexxxx »

Just use StringFormat() as in:

Code: Select all

Comment(StringFormat("Sl amount = %5.2f",slamount));
As an aside, you should stop using global variables if you don't need to. I will continue this war on global variables, as I truly believe this will cause the end of man kind. A global variable lives it's life as a ball in a pin ball machine: it gets pushed and knocked around by various obstructions (functions) and you end up not having a clue what it's value is. Stop them -- before it is too late!

Rene.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Normalizedouble not working

Post by SteveHopwood »

renexxxx » Fri Mar 16, 2018 1:11 am wrote:Just use StringFormat() as in:

Code: Select all

Comment(StringFormat("Sl amount = %5.2f",slamount));
As an aside, you should stop using global variables if you don't need to. I will continue this war on global variables, as I truly believe this will cause the end of man kind. A global variable lives it's life as a ball in a pin ball machine: it gets pushed and knocked around by various obstructions (functions) and you end up not having a clue what it's value is. Stop them -- before it is too late!

Rene.
This is an example of: One of the best coders available anywhere as shown you the way forward

Best to take notice of what he says. I do.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Normalizedouble not working

Post by SteveHopwood »

I just noticed that this thread got locked somehow. No idea how. It might have been me - don't remember locking it. It might have been someone else. It might have been the software. No idea.

Whatever the cause, it is open again. It looks like a useful discussion.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
tomele
Administrator
Posts: 1208
Joined: Tue May 17, 2016 3:40 pm
Location: Germany, Forest of Odes, Defending the Limes

Normalizedouble not working

Post by tomele »

fx805 » 16 Mar 2018, 01:57 wrote:This is getting weirder and weirder.. Do you think it could be a bug on Empty4?

Can some of you guys try this code and see what you get on the screen... As you can see in the code im just using a static value for Balance and on the screen its showing the whole number and not reducing it down to the 2 digits the doubletostring(,2) is asking for.. This makes me wonder what other functions are working wonky..

ps-- i tried this on two different Empty4 terminals-- both using the current build and got the same resluts.

Code: Select all

double slamount,TpAmount,SLAMOUNT, Balance;
int SlPercent=1;
int TpPercent=2;

int OnInit()
  {
EventSetTimer(1);
return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {}
void OnTimer()
  {
  Balance=1234.56789;
  GetSlAmount();
  Comment("Sl amount = ",slamount);
 }
//+--------------------------------------------+
void GetSlAmount(){
 slamount=Balance;
DoubleToString(slamount,2);
}//end of GetSlAmount(); 
The thread is open again?

Great, I am in the mood to rant a little bit.

Every time I or someone else suspected a bug in Empty4 it turned out to be inability to read the manual. Well, some few exceptions admitted.

First RTFM !!!!!
You will never learn to code if you ignore the language reference. Examples below.

DoubleToString does nothing until you assign the result like in:

Code: Select all

string OhThatIsTheResultIDidntKnowThat=DoubleToString(slamount,2);
Then you should REALLY make yourself familiar with implicit and explicit type conversion. Implicit conversion may not give what you want like in:

Code: Select all

Sl amount = ",slamount
From here you have to find the way yourself.

I closed that thread again and have the utter craving to defy every post wanting to help you. If any of the admins/mods wants to reopen it again, please do so.

If you understand how to put those pieces together, you will have on your screen what you want. And maybe a brilliant MQ4 coder career ahead.

If not, you better start a bricklayer career. Usually there is more money earned than in trading Forex.

:smile: :smile: :smile: :smile: :smile:

Let me know the outcome by PM.

Cheers
Happy pippin, Thomas :-BD

It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so.
(Mark Twain)

Keep the coder going: Donate
Locked

Return to “Coders Hangout”