My EA don't trade

 

Hi all,

I wrote a simple EA on the MA example provide by Metaquote. The problem is that it works fine in backtesting, but when I put it on a char don't open any trade.

I'm sure that all settings, like "Allow trade", or something else is checked. Can someone help me?

Thanks.

The core of the code is:

int start()
{
        //trade on new bar
        if(Volume[0]<10 && Bars>6)
        {
                ...
                ema0 = iMA(NULL,0,EMACalcPeriod,EMAGraphShift,MODE_EMA,EMAApplPrice,0);
                
                ...
                //Buy conditions
                if(Close[0] > ema0)
                {
                        if(OrdersTotal()>0)
                        {
                                for(j=0;j<OrdersTotal();j++)
                                {
                                        OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
                                        if(OrderType()==OP_SELL)
                                        {
                                                OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
                                        }
                                }
                        }
                        else
                        {
                                OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",12345,0,Blue);
                        }
                }
                //Sell conditions
                else if(Close[0] < ema0)
                {
                        if(OrdersTotal()>0)
                        {
                                for(j=0;j<OrdersTotal();j++)
                                {
                                        OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
                                        if(OrderType()==OP_BUY)
                                        {
                                                OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
                                        }
                                }
                        }
                        else
                        {
                                OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",12345,0,Red);
                        }
                }
                else
                {
                        if(OrdersTotal()>0)
                        {
                                OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
                                for(j=0;j<OrdersTotal();j++)
                                {
                                        if(OrderType()==OP_BUY)
                                        {
                                                OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
                                        }
                                        if(OrderType()==OP_SELL)
                                        {
                                                OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
                                        }
                                }
                        }
                }
        }
        return(0);
}
 
 

A) I don't like your new bar check

//trade on new bar
        if(Volume[0]<10 && Bars>6)

B) I don't like your failure to check return codes from Order... functions

C) I don't like the way that you assume all orders are from your EA. Consider using Magic Numbers

D) I don't like your hard coded 3 in the Order... functions. For a 3/5 digit broker, such a low number could cause problems; make it a variable (maybe extern).

But please don't take these comments personally!

 
  1. Those+
  2. if(OrdersTotal()>0)
                            {
                                    for(j=0;j<OrdersTotal();j++)
                                    {
                                            OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
                                            if(OrderType()==OP_SELL)
                                            {
                                                    OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
                                            }
                                    }
                            }
    
    You MUST count down when closing in the presence of multiple order (multiple charts) and always check return codes. Must only close order on that chart, must use magic number. No need for the if(OrderTotal()>0), no order the for() does nothing
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() 
    &&  OrderType()         == OP_SELL){                // and my pair.
       OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
    }
    

  3. On a 5 digit broker you must adjust TP, SL, AND slippage. On an ECN broker you must send the order, then set TP/SL.
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  4. Always check return codes
    int ticket=OrderSend(...)
    if (ticket < 0) Alert("OrderSend failed: ",GetLastError());

  5. //trade on new bar
            if(Volume[0]<10 && Bars>6)
    
    Volume is unreliable and that won't be a new bar for v=2 .. v=9, Always use time.
    int     start(){
        static datetime Time0; if (Time0 == Time[0]) return(0); Time0 = Time[0];
    

 
WHRoeder:
  1. Those+
  2. You MUST count down when closing in the presence of multiple order (multiple charts) and always check return codes. Must only close order on that chart, must use magic number. No need for the if(OrderTotal()>0), no order the for() does nothing
  3. On a 5 digit broker you must adjust TP, SL, AND slippage. On an ECN broker you must send the order, then set TP/SL.
  4. Always check return codes
  5. Volume is unreliable and that won't be a new bar for v=2 .. v=9, Always use time.

I understand many things seeing your examples, thank you.

But can you explain me the meaning of Digits.pips and Slippage.Pips in this code?

//++++ These are adjusted for 5 digit brokers.
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){
    if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl

Can you also tell me how to trade in a particular hour and minute of the day?

Thanks in advance.

Reason: