Lesson 16 - Your First Expert Advisor (Part 4)

 
Files:
lesson16.pdf  41 kb
 

Fantastic! Your work is invaluable!! Many many thanks!

 

hi

i aggree with beer.your work souns greate.in my opinion it is MQL4 bible.thanks a lot

 

Codersguru,

You Da Man!

Thank you for posting and all your hard work.

 

You're welcome!

beerhunter, kamyar, allan.zorro,

You're welcome! I hope you've enjoyed the lessons.

 

Thanks for the great course. It is very useful.

How can I use a custom indicator like pricechannelstop in the EA?

 

Excellent

Hi Guru,

Yep, excellent work, thanks for all this amazing help. It's going to take a few days to get to grips with all this but it's been really helpful to have your lessons.

Thanks again !

El Choco

 

Dear Sir

you have decleared the two static variables inside the block of the function. then every time start function recieves a quote it will call the crossed function,and every time when start calls crossed, crossed will first initilize the two varibles to zero,so how could you compare current_direction with last_direction?

int Crossed (double line1 , double line2)

{

static int last_direction = 0;

static int current_dirction = 0;

if(line1>line2)current_dirction = 1; //up

if(line1<line2)current_dirction = 2; //down

if(current_dirction != last_direction) //changed

{

last_direction = current_dirction;

return (last_direction);

}

else

{

return (0);

}

}

 

Static variables

suffic369:
Dear Sir

you have decleared the two static variables inside the block of the function. then every time start function recieves a quote it will call the crossed function,and every time when start calls crossed, crossed will first initilize the two varibles to zero,so how could you compare current_direction with last_direction?

int Crossed (double line1 , double line2)

{

static int last_direction = 0;

static int current_dirction = 0;

if(line1>line2)current_dirction = 1; //up

if(line1<line2)current_dirction = 2; //down

if(current_dirction != last_direction) //changed

{

last_direction = current_dirction;

return (last_direction);

}

else

{

return (0);

}

}

suffic369,

Thanks for the question!

There are two kinds of variables in MQL4, normal variables and static variables;

The static variables are very like the normal variables with only one extra feature:

The initializationof the static variables occurs only one time.

Which means if you write a line of code like that:

static int last_direction = 0;

You mean:

If this is the firstcall of the program, initializethe variable to 0, else(if it's the second, third, fourth etc call) don't initialize the variable again.

But if you used a normal variable instead of static variable, every time you call your program the initialization of the variable will be occurred.

 

Hi, at start i want say thank you for your great job you're doing to help us to learn programming MQL4.

I've got problem. My First EA is not working correct. When MA lines getting crossed, MT try to open order but such error occurs every time:

2006.04.19 13:14:12 My_First_EA EURUSD,M1: Error opening SELL order : 4109

It is the same with Long position.

#define ERR_TRADE_NOT_ALLOWED 4109

I know it is connect with these lines of code:

if(isCrossed == 2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

return(0);

}

But i can't get it why this error occurs. I would be pleased if you could help me. Best Wishes.

 

Simultaneous Orders

Hi Guru,

Could you make a small tutorial on how to open orders at the same time when eA is attached to different pairs? (the tutorial trades only one at a time.) I would like to trade many at a time, is this possible?

Reason: