MQL4 - automated forex trading   /  

Forum

Unexpected result in Strategy Tester

Back to topics list To post a new topic, please log in or register

avatar
4
Alvo 2007.08.24 00:17 
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_prev<MA_long_prev && MA_short_cur>MA_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



article

Equivolume Charting Revisited

The article dwells on the method of constructing charts, at which each bar consists of the equal number of ticks.


avatar
1515
DxdCn 2007.08.24 05:22 

datetime lasttime = NULL;

int start()
{

if (Time[0] == lasttime ) return;

lasttime = Time[0];

.......................................
//----
double MA_short_cur=iMA(Symbol(),0,ShortPeriod,0,MODE_EMA,PRICE_CLOSE,0);
........................................


avatar
364
Irtron 2007.08.24 09:30 
DxdCn wrote:

datetime lasttime = NULL;


if (Time[0] == lasttime ) return;


Correction
datetime lasttime = 0;
 
int start()
{ 
    if (Time[0] >= lasttime ) return (0);
 
    lasttime = Time[0];

avatar
4
Alvo 2007.08.24 12:11 
Thanks for your help.

Unfortunately it didn't fully solved the problem.
With the code of DxdCn now I have only one entry for each cross but many of them have been missed (for example the one I used in the original post) as you can see below. And there shouldn't be 3 cross up in a row, the algorithm certainly missed the cross downs in between them.

With the correction of Irtron I do not get any result. If I understand well the code, I guess the present time is always >= the last time so this statement is always true and the main part of the code is never actually executed.

Any ideas?

Thanks for your time.

Alvo


Output with the code of DxdCn:
2007.08.24 10:09:32    2007.08.05 00:00  cross_alert GBPUSD,Daily: Cross UP
2007.08.24 10:09:32    2007.06.08 00:00  cross_alert GBPUSD,Daily: Cross DOWN
2007.08.24 10:09:32    2007.05.24 00:00  cross_alert GBPUSD,Daily: Cross UP
2007.08.24 10:09:32    2007.04.27 00:00  cross_alert GBPUSD,Daily: Cross DOWN
2007.08.24 10:09:32    2007.03.18 00:00  cross_alert GBPUSD,Daily: Cross UP
2007.08.24 10:09:32    2007.02.23 00:00  cross_alert GBPUSD,Daily: Cross UP
2007.08.24 10:09:32    2007.02.02 00:00  cross_alert GBPUSD,Daily: Cross UP
2007.08.24 10:09:32    2006.12.25 00:00  cross_alert GBPUSD,Daily: Cross DOWN



avatar
364
Irtron 2007.08.24 12:19 
This is because you use previous and current bars for ma calculations. Although the current bar is being formed and its close price changes.
Use most recent complete bars #1 and #2 for ma crossing calculation.


avatar
4
Alvo 2007.08.24 12:38 
Hi Irtron,

Using if (Time[0] == lasttime ) and bar #1 and #2 did the trick!

Thanks again!

Alvo
Back to topics list  

To add comments, please log in or register