Unexpected result in Strategy Tester

 

Hi,

I'm just starting to learn MQL4.

I want to make the classic example of looking for EMA crosses:


//+------------------------------------------------------------------+
//| cross_alert.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"

//---- input parameters
extern int ShortPeriod=5;
extern int LongPeriod=10;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
double MA_short_cur=iMA(Symbol(),0,ShortPeriod,0,MODE_EMA,PRICE_CLOSE,0);
double MA_long_cur=iMA(Symbol(),0,LongPeriod,0,MODE_EMA,PRICE_CLOSE,0);
double MA_short_prev=iMA(Symbol(),0,ShortPeriod,0,MODE_EMA,PRICE_CLOSE,1);
double MA_long_prev=iMA(Symbol(),0,LongPeriod,0,MODE_EMA,PRICE_CLOSE,1);

// check cross up
if (MA_short_prevMA_long_cur)
{
Print("Cross UP");
}

// check cross down
if (MA_short_prev>MA_long_prev && MA_short_cur<MA_long_cur)
{
Print("Cross DOWN");
}

//----
return(0);
}
//+------------------------------------------------------------------+
I run it on GBPUSD on the daily chart and it detects the crosses well but the output in the Journal tab is pretty weird. For example there is a cross on 2007.07.27 and this is what I see in the Journal (it is the same at any other cross as well):


2007.08.23 22:04:04 2007.07.27 23:59 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 22:40 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 22:00 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 21:20 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 20:40 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 20:00 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 19:59 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 18:40 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 18:00 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 17:20 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 16:40 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 16:00 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 15:59 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 14:40 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 14:00 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 13:20 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 12:00 cross_alert GBPUSD,Daily: Cross DOWN
2007.08.23 22:04:04 2007.07.27 10:00 cross_alert GBPUSD,Daily: Cross DOWN
I have no intention to use this code to execute any order but if I did would it execute 18 orders???

How can I fix this to have only one single entry for each cross?

Thanks for your help!

Alvo

 

Dear Alvo,

Would you please advice me, where the links is, if I want to learn MQL4, too. I thank you in advance.

Reason: