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 5 of 7
Author:  garyfritz [ Fri Jul 20, 2012 5:34 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

traderduke wrote:with the "run once per candle" removed from the Start(), I'm getting multiple entries in one candle, ...
What is the code for limiting one trade per candle??
If you yank one piece of code without considering how it affects other parts of the code, you are likely to get unexpected results. You did.

The rest of the EA was apparently coded to assume it got invoked once per candle. There is no bit of code you can paste in to say "trade only once per candle." You'll have to dig through the code and find where those assumptions are.
Author:  jjasper7 [ Fri Jul 20, 2012 7:51 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

in coding a program, somewhere i have gotten out of balance with my parenthesis, and the compiler errors off with the dreaded '-unexpected end of program' message. is there some way to get a clue where the bracket/parenthesis is located? i have tried to change all the comment /*'s out and hunt by eliminating zones of the program, and gone over everything with a fine-toothed comb, but keep missing the error, and am wondering if there is some trick to finding the problem? HELP!
Author:  garyfritz [ Fri Jul 20, 2012 8:22 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

The best solution is to use a good editor that shows you the matching parens. w633's IDE does this nicely.

Narrow the search down to one function if possible -- comment out the last half of the program and recompile, then expand or contract the commented area until you find the area with the problem. Then run the cursor through that function, looking where the editor says the matching paren is. (Search for "(" to simplify this step.) When the editor doesn't agree with what you thought it was, you've probably found your problem.

In the future, the editor can help you avoid hitting this problem at all. Just make sure the parens match properly when you enter or edit the code.
Author:  SteveHopwood [ Fri Jul 20, 2012 9:00 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

jjasper7 wrote:in coding a program, somewhere i have gotten out of balance with my parenthesis, and the compiler errors off with the dreaded '-unexpected end of program' message. is there some way to get a clue where the bracket/parenthesis is located? i have tried to change all the comment /*'s out and hunt by eliminating zones of the program, and gone over everything with a fine-toothed comb, but keep missing the error, and am wondering if there is some trick to finding the problem? HELP!
This is why I never go more than a very few lines of code without doing a compile. Unless what I am c coding is really simple, I do a compile after typing each line of code or setting up a loop/conditional construct. No help to you now, but perhaps so in the future.

To avoid the dreaded unbalanced parenthesis thingy, I set up the construct and test it before entering in the details. For example, I want to code a routine that is used if an order was placed before the start of the current candle. I start out by coding and compiling this:

Code: Select all

if (OrderOpenTime() < Time[0])
{

}//if (OrderOpenTime() < Time[0])
If the above compiles successfully, I know I have not left myself with the dreaded error. From there, I type each line of code and do a compile at the end of each line. Yes, this takes time but you have already discovered this; nowhere near as long as it takes to hunt down the offending missing bracket further down the line.

:D
Author:  jjasper7 [ Fri Jul 20, 2012 11:18 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

thank you gary and steve, for your suggestions. they sure are ellusive to hunt down if you have no idea where to look. gary, where do i find this editor (w633's IDE)? or do you have more info to hunt for it?

i have hunted for a c-program editor and have downloaded notepad++ to try. great suggestion.
Author:  SteveHopwood [ Fri Jul 20, 2012 11:41 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

jjasper7 wrote:thank you gary and steve, for your suggestions. they sure are ellusive to hunt down if you have no idea where to look. gary, where do i find this editor (w633's IDE)? or do you have more info to hunt for it?
http://www.stevehopwoodforex.com/phpBB3 ... f=15&t=627 You will love it.

:D
Author:  NeoTrader [ Sat Jul 21, 2012 8:05 am ]
Post subject:  Re: Coding for beginners - this is how Steve started

hi traderduke,
traderduke wrote:What is the code for limiting one trade per candle??
Maybe you can make use of the following function...

Code: Select all

bool isNewBar() {
	static int prevTime;
	bool newBar=false;
	
	if(Time[0]!=prevTime) {
		newBar=true;
		prevTime=Time[0];
	}
	
	return(newBar);
}
usage:

Code: Select all

if ( isNewBar ) {
    your trading logic;
}
happy weekend,

NeoTrader

-
Author:  jjasper7 [ Tue Jul 24, 2012 7:20 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

thanks for pointing me to the editor - that found it!

i am trying to adjust the stoploss in pivoty slope, but keep getting error 4200 when i send the ordermodify.
4200 = ERR_OBJECT_ALREADY_EXISTS 4200 Object exists already.
and i do not understand what it is trying to say is wrong. the s/l and t/p are both 0 and i am sending valid new settings. i am using fxcm and have ecn true. the line generating the error is...
rslt=OrderModify(OrderTicket(), OrderOpenPrice(), NewStop, OrderTakeProfit(), OrderExpiration(), Red);
any suggestions?
Author:  Jimdandy [ Thu Jul 26, 2012 4:33 am ]
Post subject:  Re: Coding for beginners - this is how Steve started

Many thanks Gary for the explanation of why if(A==B) does not always work.... this has long puzzled me and resulted in many a if(NormalizeDouble(A,Digits)==NormalizeDouble(B,Digits)) line....

I usually run into this problem when trying to make sure that I am not trying to move a Stoploss or TakeProfit value that has already been moved. For instance..
if(StopLoss == OrderOpenPrice()) DoMoveToBreakeven == false;

It would still try to modify the order's Stoploss because they were not REALLY equal..... Now I understand it... many thanks.... Now, If only I had all that hair back that I pulled out.... PipPip........JimDandy...
Author:  garyfritz [ Thu Jul 26, 2012 4:15 pm ]
Post subject:  Re: Coding for beginners - this is how Steve started

Glad it helped, JD. Note that even NormalizeDouble(A,Digits)==NormalizeDouble(B,Digits) is not guaranteed to work right, since they will be equal in the first Digits digits, but NOT necessarily after that!

If you want a safe comparison, you need to do something like this:

Code: Select all

bool CloseEnough(double num1, double num2)
{
   if (num1 == 0 && num2 == 0) return(true);  0 == 0
   if (MathAbs(num1 - num2)/(MathAbs(num1)+MathAbs(num2)) < 0.000000001) return(true);
   return(false);
}
That scales the two values so they have to match in about the first 8 significant digits, no matter how far left or right of the decimal point. So e.g. 0.000001 and 0.000001000000001 are "equal" but 0.000001 and 0.0000011 are NOT -- because they're 10% different.

Note that with this code, 100000000000 and 100000000100 are considered "equal" even though they differ by 100. They're only different by 0.0000001%, so that's "close enough." If that bothers you, you could modify the test to also reject numbers that differ by more than a threshold value *without* scaling.
All times are UTC Page 5 of 7