Coding for beginners - this is how Steve started

The forum for experienced coders to upload their helpful hints, tips and lessons.
Post Reply
garyfritz

Re: Coding for beginners - this is how Steve started

Post by garyfritz »

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.
jjasper7
Trader
Posts: 87
Joined: Mon Mar 19, 2012 11:44 pm
Location: Mesa,AZ (near Phoenix)

Re: Coding for beginners - this is how Steve started

Post by jjasper7 »

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!
garyfritz

Re: Coding for beginners - this is how Steve started

Post by garyfritz »

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.
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 »

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
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.
jjasper7
Trader
Posts: 87
Joined: Mon Mar 19, 2012 11:44 pm
Location: Mesa,AZ (near Phoenix)

Re: Coding for beginners - this is how Steve started

Post by jjasper7 »

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.
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 »

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
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
NeoTrader
Trader
Posts: 436
Joined: Wed Apr 04, 2012 2:52 pm
Location: small Village at Lake Chiemsee, Bavaria, Germany

Re: Coding for beginners - this is how Steve started

Post by NeoTrader »

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

-
jjasper7
Trader
Posts: 87
Joined: Mon Mar 19, 2012 11:44 pm
Location: Mesa,AZ (near Phoenix)

Re: Coding for beginners - this is how Steve started

Post by jjasper7 »

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?
Jimdandy
Trader
Posts: 22
Joined: Fri Jul 20, 2012 3:14 am

Re: Coding for beginners - this is how Steve started

Post by Jimdandy »

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...
garyfritz

Re: Coding for beginners - this is how Steve started

Post by garyfritz »

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.
Post Reply

Return to “Coding Lessons - info for all”