stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Normalizedouble not working
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5420
Page 1 of 2
Author:  fx805 [ Thu Mar 15, 2018 7:39 pm ]
Post subject:  Normalizedouble not working

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
Author:  tomele [ Thu Mar 15, 2018 8:58 pm ]
Post subject:  Normalizedouble not working

DoubleToString is what you might be searching for.

Cheers
Author:  fx805 [ Thu Mar 15, 2018 9:08 pm ]
Post subject:  Normalizedouble not working

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
Author:  tomele [ Thu Mar 15, 2018 9:36 pm ]
Post subject:  Normalizedouble not working

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
Author:  fx805 [ Thu Mar 15, 2018 9:43 pm ]
Post subject:  Normalizedouble not working

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.. :)
Author:  fx805 [ Fri Mar 16, 2018 12:57 am ]
Post subject:  Normalizedouble not working

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(); 
Author:  renexxxx [ Fri Mar 16, 2018 1:11 am ]
Post subject:  Normalizedouble not working

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.
Author:  SteveHopwood [ Fri Mar 16, 2018 1:22 am ]
Post subject:  Normalizedouble not working

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:
Author:  SteveHopwood [ Fri Mar 16, 2018 11:56 pm ]
Post subject:  Normalizedouble not working

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:
Author:  tomele [ Sat Mar 17, 2018 12:39 am ]
Post subject:  Normalizedouble not working

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
All times are UTC Page 1 of 2