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

Coding for beginners - this is how Steve started
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=301
Page 3 of 7
Author:  SteveHopwood [ Tue Apr 24, 2012 8:17 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

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
Author:  rob2360 [ Tue Apr 24, 2012 8:37 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

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...
Author:  garyfritz [ Tue Apr 24, 2012 8:42 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

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.)
Author:  SteveHopwood [ Tue Apr 24, 2012 9:48 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

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
Author:  tiberium [ Sat Apr 28, 2012 1:43 am ]
Post subject:  Re: Coding for beginners - this is how Steve started

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 )
Author:  SteveHopwood [ Sat Apr 28, 2012 8:14 am ]
Post subject:  Re: Coding for beginners - this is how Steve started

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
Author:  traderduke [ Thu Jul 12, 2012 2:11 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

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??
Author:  garyfritz [ Thu Jul 12, 2012 2:17 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

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.
Author:  traderduke [ Thu Jul 12, 2012 10:18 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

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.
Author:  f-trader [ Thu Jul 12, 2012 10:42 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

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-------------//
All times are UTC Page 3 of 7