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

To add comments, please log in or register
Fabio Cavalloni
7563
Fabio Cavalloni  
Luis Garcia:

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

It so simple:

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

if(OrdersTotal()==0) {

    int res = 0;   

    if (Ask>Media+15*_Point)
      res = OrderSend (Symbol (),OP_BUY,LotSize,Ask,6,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,NULL,MagicNumber,0,Green); 
 
    if (Bid<Media-15*_Point)
      res = OrderSend (Symbol (),OP_SELL,LotSize,Bid,6,Bid+StopLoss*_Point,Bid-TakeProfit*_Point,NULL,MagicNumber,0,Red);
       
    if( res<0 ) GetLastError();
}

Keep in mind that OrdersTotal() return total of orders (both market and pending on your account). Using this variable  can be right only in backtest.

Always better to create a custom function checking if there are opened orders with Magic Number.

bool orderOpen()  {
   int total=OrdersTotal();
   for(int i=0; i<total; i++)  {
      if( !OrderSelect(i,SELECT_BY_POS) || OrderMagicNumber()!=magicNumber) continue;
      return(true);
   }
   return(false);
}
So your 
OrdersTotal()==0

Will become

!OrderOpen()
Anyway, as I already suggested, you can learn by the infinite amount of free code found on Codebase, instead of opening thread related only to your bad coding skills.
Luis Garcia
177
Luis Garcia  
Fabio Cavalloni:

It so simple:

Keep in mind that OrdersTotal() return total of orders (both market and pending on your account). Using this variable  can be right only in backtest.

Always better to create a custom function checking if there are opened orders with Magic Number.

So your 

Will become

Anyway, as I already suggested, you can learn by the infinite amount of free code found on Codebase, instead of opening thread related only to your bad coding skills.

Thanks man!!

123
To add comments, please log in or register