My first EA, it wont open trades, any help? - page 2

 
Luis Garcia:

All i did was adding that Res parameter and putting the Res= in front of OrderSend (got the idea from one of the expertes mt4 has built in)

Bad habit to get into.

int res=OrderSend(.......

will NOT make the OrderSend() magically succeed whereas before it failed. All it will do is stop the compiler from warning you that it should be checked.

Keith Watford:

There is a reason for this warning, so check it. Print all details should the OrderSend() return -1, then you may be able to pinpoint the problem.

 

After OrderSend, you should check GetLastError. MT4 will rarely show errors unless you have told it to show them.

if your code continue to open 0 orders, you need to print your errors to alert window, or to your journal, so that you can research that error.

 
Revo Trades:

After OrderSend, you should check GetLastError. MT4 will rarely show errors unless you have told it to show them.

if your code continue to open 0 orders, you need to print your errors to alert window, or to your journal, so that you can research that error.

You should check GetLastError() only if the OrderSend() returns -1.

 
Keith Watford:

Bad habit to get into.

will NOT make the OrderSend() magically succeed whereas before it failed. All it will do is stop the compiler from warning you that it should be checked.

Oh, ok Keith, i get what you are saying, I was getting that warning you told me, that Order send return should be checked (something like that) i really didnt even know what that Res command did, just saw it on another EA mt4 has built in and thought maybe it was what my EA was missing...

 
Best use the Metaeditor it will create OnInit OnTick etc for you and wont use the old style 

Plus old style u come across a lotof errors while compiling or debugging. 
Wrt Indicators 
 

You are adding and subtracting 15 from your MA values, you want to add/subtract 15 points, so 15*_Point

That is very different. The same rule apply for TP/SL.

Your code need to look like this:

 double Media = iMA (NULL,0,MA, MAShift,MAMethod, MAApliedTo,1); 


   //para comprar   

   if (Ask>Media+15*_Point)

      if(OrdersTotal()==0)

      OrderSend (Symbol (),OP_BUY,LotSize,Ask,6,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,NULL,MagicNumber,0,Green); 

   //para venta   

   if (Bid<Media-15*_Point)

       if(OrdersTotal()==0)

       OrderSend (Symbol (),OP_SELL,LotSize,Bid,6,Bid+StopLoss*_Point,Bid-TakeProfit*_Point,NULL,MagicNumber,0,Red);
       

       return (0);

In addition to this, always check OrderSend result in a boolean function, and if the function return FALSE, GetLastError().

You can find a lot of good examples on CodeBase.

 

OrderSend() is not a boolean function.

It returns the ticket number in case it succeeds and otherwise -1;

 
Marco vd Heijden:

OrderSend() is not a boolean function.

It returns the ticket number in case it succeeds and otherwise -1;

Sorry I got confused but the point is to always check returned value, as compiler suggest
 
Fabio Cavalloni:

You are adding and subtracting 15 from your MA values, you want to add/subtract 15 points, so 15*_Point

That is very different. The same rule apply for TP/SL.

Your code need to look like this:

In addition to this, always check OrderSend result in a boolean function, and if the function return FALSE, GetLastError().

You can find a lot of good examples on CodeBase.

Thanks Fabio, this was really helpful, i saw some tutorials about changing ticks size ald lot size and found it a little confusing, thanks!!

 
Marco vd Heijden:

OrderSend() is not a boolean function.

It returns the ticket number in case it succeeds and otherwise -1;

I woulnd`t know how to code such return, promise to get on it tho! thanks

Reason: