Coding for beginners - this is how Steve started

The forum for experienced coders to upload their helpful hints, tips and lessons.
Post Reply
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.

Re: Coding for beginners - this is how Steve started

Post by SteveHopwood »

garyfritz wrote:Warning, educational but geeky detail ahead:

etc
Better out than in. :lol:

As it happens, I first met this problem over 30 years ago programming in BBC Basic for my first pc. I have not met it lately (i.e. in the last 25 years) so it looks as though those infantile cretins at Empty4 have managed to reinvent the bent wheel.

So, how about my original solution of turning doubles into strings and comparing the strings?

:D
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.
rob2360
Trader
Posts: 10
Joined: Thu Feb 09, 2012 10:59 am

Re: Coding for beginners - this is how Steve started

Post by rob2360 »

SteveHopwood wrote: So, how about my original solution of turning doubles into strings and comparing the strings?
:D
Problem with double to string conversion is it is pretty cpu intensive; although most Empty4 programming is not optimised for speed!

The old fashioned way of solving the problem was to do something like
if (MathAbs( x - y) < 0.001) { }....

where 0.001 was your accuracy. Note the MathAbs function is just there so to make sure you don't care whether x or y is the larger value...
garyfritz

Re: Coding for beginners - this is how Steve started

Post by garyfritz »

Well, it should work. But it seems like a pretty convoluted and high-overhead way to do something pretty simple.

The (A-B) < ReallySmall approach works fine (especially with the scaling addition) and is very simple. You could encapsulate it into your own CloseEnough() function and forget about the details. (BTW I realized I forgot something -- it should be MathAbs(A-B) < ReallySmall.) (Whoops, rob2360 beat me to it. :))

And the Empty4 boys are not alone in this. It's the standard way that most languages work. I've only come across 1 or 2 languages (out of many dozens) in 35 years that tried to handle it intelligently. (I don't *think* any of the common languages today do anything but simple equality tests, though it's easy to extend C++/C#/Java/etc to do so.)
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.

Re: Coding for beginners - this is how Steve started

Post by SteveHopwood »

Hehe. I am just a pianist who stumbled across programming by accident. When geeks get going properly, I retire and leave them to it.

For now, I shall use CompareDoubles(). As it happens, I am doing battle with this in a private (but soon to be public due to the generosity of the sponser) grid trading manager. I am waiting to see if the solution solves the problem.

Described briefly, the ea modifies the stop loss and take profit of pending trades to reflect those of an open trade. The ea kept on insisting that the various sl/tp's were not equal even though they are.

These posts cropped up at just the right time. I will let you know if CompareDoubles() does the job. The member has not whinged for a while, so it is looking promising. :lol:

:D
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.
tiberium
Posts: 2
Joined: Mon Jan 30, 2012 1:34 am

Re: Coding for beginners - this is how Steve started

Post by tiberium »

Thanks Steve this forum especially almost threads in this forum very helpful for newbie trader like me that want to learn how to coding in Empty4 (maybe in the future in MT5 and another programmable trading platform :D )
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.

Re: Coding for beginners - this is how Steve started

Post by SteveHopwood »

garyfritz wrote:Well, it should work. But it seems like a pretty convoluted and high-overhead way to do something pretty simple.

The (A-B) < ReallySmall approach works fine (especially with the scaling addition) and is very simple. You could encapsulate it into your own CloseEnough() function and forget about the details. (BTW I realized I forgot something -- it should be MathAbs(A-B) < ReallySmall.) (Whoops, rob2360 beat me to it. :))

And the Empty4 boys are not alone in this. It's the standard way that most languages work. I've only come across 1 or 2 languages (out of many dozens) in 35 years that tried to handle it intelligently. (I don't *think* any of the common languages today do anything but simple equality tests, though it's easy to extend C++/C#/Java/etc to do so.)
Being called here to read tiberium's post (welcome, tiberium) caused me to re-read this and Rob's post.

This is clearly a better solution than the CompareDoubles() thingy and I shall adopt it. No idea why I did not get this first time around - must have been tired. Cheers.

:D
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
traderduke
Trader
Posts: 306
Joined: Mon Nov 28, 2011 7:30 pm
Location: East Coast USA

Re: Coding for beginners - this is how Steve started

Post by traderduke »

Can anybody tell me the reason for a good run using
int start()
{
// Only run once per completed bar L-118
if(timeprev==Time[0]) return(0);
timeprev = Time[0];
as opposed to not in
int start()
{
// Only run once per completed bar L-118
// if(timeprev==Time[0]) return(0);
// timeprev = Time[0];

I see the different entries but WHY??
garyfritz

Re: Coding for beginners - this is how Steve started

Post by garyfritz »

I have no idea what the EA is doing or how it's good vs. not. But the difference between those two code snippets is that the first one runs the rest of the start() code only once per completed bar, whereas the second one runs all the start() code on every tick. You'd have to figure out how that changes the EA behavior.
User avatar
traderduke
Trader
Posts: 306
Joined: Mon Nov 28, 2011 7:30 pm
Location: East Coast USA

Re: Coding for beginners - this is how Steve started

Post by traderduke »

Gary
Thank for that explanation. The "second one runs all the start() code on every tick" seems to give the closest backtest results to forward testing.
garyfritz wrote:I have no idea what the EA is doing or how it's good vs. not. But the difference between those two code snippets is that the first one runs the rest of the start() code only once per completed bar, whereas the second one runs all the start() code on every tick. You'd have to figure out how that changes the EA behavior.
f-trader
Trader
Posts: 27
Joined: Sat Feb 25, 2012 4:08 pm

Re: Coding for beginners - this is how Steve started

Post by f-trader »

SteveHopwood wrote: Described briefly, the ea modifies the stop loss and take profit of pending trades to reflect those of an open trade. The ea kept on insisting that the various sl/tp's were not equal even though they are.
I had the same problem.
Compared 2 doubles which were Normalized before ( NormalizeDouble ) and went almost crazy
because they should have been identical but the if( a == b) resulted in false.

hanover helped me.
Now I use his MathFix function. And all is fine.

Example: Double A;
Double B;

I compare them like this:

Code: Select all

if (mf(A) == mf(B)) {
   code .....
}
Just copy the following functions into your code:

Code: Select all

//+------------------------------------------------------------------+
//|  Function: Better than NormalizeDouble                             |
//+------------------------------------------------------------------+
double mf(double value) {
	return(MathFix(value,Digits));
}  //------------END FUNCTION-------------//

//+------------------------------------------------------------------+
double MathFix(double n, int d=0)
//+------------------------------------------------------------------+
// Corrects a rounding/accuracy bug in MQL4's MathRound() function
//   (use MathFix(n) instead of MathRound(n)
// Rounds n to d decimal places, e.g.
// MathFix(2.54,1) returns 2.5
// MathFix(2.57,1) returns 2.6
// MathFix(2.99)   returns 3
{
	return(MathRound(n*MathPow(10,d)+0.000000000001*MathSign(n))/MathPow(10,d));
}  //------------END FUNCTION-------------//

//+------------------------------------------------------------------+
int MathSign(double n)
//+------------------------------------------------------------------+
// Returns the sign of a number (i.e. -1, 0, +1)
// Usage:   int x=MathSign(-25)   returns x=-1
{
	if (n > 0) return(1);
	else if (n < 0) return (-1);
	else return(0);
}  //------------END FUNCTION-------------//
Post Reply

Return to “Coding Lessons - info for all”