NewBar() problems for NewProgrammer!

 

Hello All,

I have been manual trading for a few years, looking to automation of a few simple systems and learning programming. I have read through CodersGuru's guide and read a number of posts but cannot work this one out. Its very simple and there is something I am missing. I am just trying to get CodersGuru's NewBar code to work and have spent hours searching around to solve the Compile debug problems.

Here is the code, I am sure it is a simple solution:

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

int lastbar, curbar;

bool NewBar()

{

static datetime lastbar = 0

datetime curbar = Time[0];

if(lastbar!=curbar)

{

lastbar=curbar;

return(true);

}

else

{

return(false);

}

}

return(0);

}

//+------------------------------------------------------------------+

The code is from Alert Once Per Bar | www.metatrader.info sorry its just a copy and paste job, but at the moment I just want it to print some text when a new bar forms on the chart! How hard can it be? Very hard for a new programmer!

The compiling errors I currently get are:

'(' - function definition unexpected - which is at bool NewBar() or (43, 16)

and

'}' - unbalanced parentheses - this is the last one, cant work out where I am going wrong!

Thanks so much for any simple answers, also please say if there is a better way to post code. Sorry for being lazy and asking this, but it has been frustrating me all evening and yes, I have tried searching.

Thanks so much for any answers,

Howard

Edit: Sorry, cant remember how to post code properly!? Screenshot attached.

Files:
code.jpg  23 kb
 

Hit F8 function Key and it will zero into the possible error.

Dave

 

youre missing a ";" after lastbar = 0

But here is a better function for checking only once per bar. When youre posting code wrap it with the php tags by hitting the little php button in the editor. cheers.

 

You've got the function definition inside a function.

Take the whole Newbar() definiton out and paste it below the start function. If that doesn't make any sense to you you may need to go back and re-read CodeGuru's tutorial and look at some EA code.

Good luck

Lux

 

Ha! I didn't even catch that at first glance :-p

Mach, this is how it should look. int start() is a method. make sure your method declaration is never within another method. like this.

int start(){

return(0);

}

bool NewBar(){

int lastbar, curbar;

static datetime lastbar = 0;

datetime curbar = Time[0];

if(lastbar!=curbar){

lastbar=curbar;

return(true);

}else{

return(false);

}

}
 

Hello all, thanks for your wonderful replies as I slept. To wake up this morning to this amount of help is brilliant. I had to go out this morning and did not want to reply until it was fixed, but I have now got it working fine thanks. My learning is coming on well and all of you have really helped.

Dave137: Thanks for the hint about F8, I had figured out by double clicking it does a similar thing, but this helps too.

Nicholishen: Thanks for pointing this out. As a new programmer its hard to spot problems in the most simple of codes. As well as a big thanks for posting the code to use at the end. After a few little mistakes I made when typing it out, I have now got it working well.

luxinterior: Thanks for spotting this obvious, I didnt really understand about the functions but have had another look through the tutorial and am now more confident.

Also thanks for moving this thread to the correct part of the forum - sorry I did have a look around to see where to post it, but missed the programming section!

Hope to be able to learn a little more by myself now, but what you have said has been invaluable and hopefully I will be able to sort out my own problems from now - or at least post them in the correct place, in the correct style!

Howard

Reason: