Problem using EA for live trading on a demo account

 

I attached an EA to the EUR/USD 15 mins chart that's supposed to initiate a BUY order when a bullish engulfing candle is formed. (The right hand corner shows a smiley face, and the settings is set to "Allow Live Trading")

However, I realised that no BUY order was opened on 14th Mar 10:15am even though a bullish engulfing candle was formed. When I run the EA on the strategy tester, it indicates that the order should be opened. What am I doing wrongly?


My code is as follows:

if (Time0 != Time[0])
{

//Bullish Engulfing
if (Open[1] <= Close[2] && Close[1] >= Open[2] && Open[2] - Close[2] > 10*Point && Close[1] - Open[1] > 10*Point)
{

int n, i;
double zag, zig;
i=0;
while(n<2)
{
if(zig>0) zag=zig;
zig=iCustom(NULL, 0, "ZigZag", 0, i);
if(zig>0) n+=1;
i++;
}


if (zag < zig)

OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss*Point, Ask + TakeProfit*Point, "CANDLESTICKS", 12345, 0, Green);
}

Any help is greatly appreciated. Thanks.

 

FA

Dont know why the Strategy Tester would open an order.

A problem I think is in the code above - I dont see variable n being initialised - so the zig/zag block wont evaluate, leading to

 if (zag < zig)

not evaluating


So... add Print statements to your code and look in the Journal of the Strategy Tester, eg

Print("Value for n: ", n)

FWIW

Below is a bunch of common functions I've thrown together so the forum can pick the bones out of them :)

Good luck

-BB-

 
int IsMorningStar()
{int retval=0;
if(
 (Body(3) > Body(2)) &&  // Star body smaller than the previous one
 
 (Body(1) > Body(2)) && // Body of star smaller than bodies of first and last candles
 
 (Close[3] < Open[3]) && // First is a down candle
 
 (Close[1] > Open[1]) && // Third is an up candle
 
 (Close[1] > (BodyLo(3) + Body(3)*0.5)) // The third candle closes above the midpoint of the first candle
 
  )
  retval=1;
 return (retval);
}
int IsEveningStar()
{int retval=0;
if(
 (Body(3) > Body(2)) &&  // Star body smaller than the previous one
 
 (Body(1) > Body(2)) && // Body of star smaller than bodies of first and last candles
 
 (Close[3] > Open[3]) && // First is an up candle
 
 (Close[1] < Open[1]) && // Third is a down candle
 
 (Close[1] < (BodyHi(3) - Body(3)*0.5)) // The third candle closes below the midpoint of the first candle
 
  )
  retval=1;
 return (retval);
}
int IsBullishEngulfing()
{int retval=0;
if(
 (Close[2] < Open[2]) && // First is a down candle
 
 (Close[1] > Open[1]) && // Second is an up candle
 
 (Body(2) < Body(1)) // First engulfed by second
  )
  retval=1;
 return (retval);
}
int IsBearishEngulfing()
{int retval=0;
if(
 (Close[2] > Open[2]) && // First is an up candle
 
 (Close[1] < Open[1]) && // Second is a down candle
 
 (Body(2) < Body(1)) // First engulfed by second
  )
  retval=1;
 return (retval);
}
int IsDojiCandle()
{int retval=0;
if(
   (Body(1) < ((High[1] - Low[1])/iDoji))
  )
  retval=1;
 return (retval);
}
 
double Body (int iCandle)
{ double CandleOpen, CandleClose;
CandleOpen=Open[iCandle];
CandleClose=Close[iCandle];
return (MathMax(CandleOpen, CandleClose)-(MathMin(CandleOpen, CandleClose)));
}
 
double BodyLo (int iCandle)
{ 
return (MathMin(Open[iCandle], Close[iCandle]));
}
 
double BodyHi (int iCandle)
{ 
return (MathMax(Open[iCandle], Close[iCandle]));
}
 
I added the Print Statement

int n, i;
         double zag, zig;
         i=0;
         
         Print("Value for n: ", n);
         while(n<2)

It gave the following comment in my journal.....

2008.03.17 21:42:51 2008.03.14 10:15 candlesticks EURUSD,M15: Value for n: 0

So I guess n is automatically initialized to 0....
 

Next step is to add Print in the later code


if (zag < zig) 
  {
    Print("Lots: ", Lots, "SL: ", StopLoss*Point, "TP: ", TakeProfit*Point)
    OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss*Point, Ask + TakeProfit*Point, "CANDLESTICKS", 12345, 0, Green); 
  }

If you dont get this output you know where to look :)

If you do, look for an OrderSend error in the Journal

-BB-

 
Added this new print statement... output is shown.... and there's no OrderSend error in the journal....

I've got a very stupid question: Does MetaTrader have to be running in order for trades to open? Can the EA open orders even after I exit from MetaTrader or shut down my PC? Sorry... Ultimate newbie here :)
 
I think I found my answer here

http://www.fxpromaker.com/faq.html#qu6

So, does it mean that we gotta leave MetaTrader and our PC on 24 hours a day?

Sorry, wasted your time because of a stupid mistake on my part... thanks anyway :)
 

FA

> So, does it mean that we gotta leave MetaTrader and our PC on 24 hours a day?

Yes, during market hours - roughly 22:00 GMT on Sunday to 23:00 GMT Friday (broker service & timezones vary)

> stupid mistake?

Not really - we all had to start somewhere.

And, like the rest of us, we just have to keep trying!

-BB-

Reason: