Having wired Moving Average problem while creating EA..

 

what am I doing wrong?

there is a screen-shot of a part of a code responsible for moving average. I replaced all variables with values so you can see the settings. the other screen-shot shows the difference in drawing the MA and the MA indicator thrown on the chart (or rather all 4 types of them).

The Light Blue is actually the Smoothed one and is nowhere near the MA generated in the tester.
The closest one to generated MA is Simple, yet it does not match it.

What the hell is going on?

Files:
ma_problem1.png  154 kb
ma_problem2.png  80 kb
 
angreeee:

what am I doing wrong?

there is a screen-shot of a part of a code responsible for moving average. I replaced all variables with values so you can see the settings. the other screen-shot shows the difference in drawing the MA and the MA indicator thrown on the chart (or rather all 4 types of them).

The Light Blue is actually the Smoothed one and is nowhere near the MA generated in the tester.
The closest one to generated MA is Simple, yet it does not match it.

What the hell is going on?

And what value did you get ? I can't see it nowhere.
 
angevoyageur:
And what value did you get ? I can't see it nowhere.

compare the 2 windows. one on the left is the MA generated by strategy tester. it goes exactly between the trades. on the right you have all types of MA370 (SMA, EMA, SSMA, and LWMA) and none of them matches the MA 370 on the left. the SSMA is the light blue one and is nowhere near the MA generated in back-test.

Attaching another screen-shot with added debug alert line of current "ma" variable value (displayed last MA value in the log, and on the right the last value of Light Blue Smoothed 370 MA "thrown" on the chart)

 

I noticed the MAs on the chart refer to close price while the code ma refers to the open price, but as i changed the moving averages on the chart it did not change them visually in any significant way. they still look the same as on the attached screen-shot. 

Files:
ma_problem3.png  142 kb
 
its not that ea does not work. it works good, but when i was debugging it i noticed that the MA generated is just totally different of what it should be. I know I am doing something wrong, but i still dont know what it is. I attach another screenshot of the settings of the light blue smoothed ea, so you have complete picture.
Files:
ma_problem4.png  66 kb
 
the most similar MA that I have found is Smoothed MA 220, screenshot attached. It does not make any sense, :( but maybe it is a clue of some kind..
Files:
ma_problem5.png  57 kb
 

i isolated the issue to a separate EA. 

#property copyright   "Grzegorz Korycki"
#property version     "1.0"
#property description "MA TEST"


#include <Trade/Trade.mqh>

   #define MAGICMA  20131002

double Bid;
double Ask;

   
double OnTester()
{
    double custommax = TesterStatistics(STAT_EQUITY_DDREL_PERCENT);
    return (custommax);
}

CTrade  trade;
   
void OnTick()
{
   MqlTick last_tick;
   if(SymbolInfoTick(Symbol(),last_tick))
     {
      Bid = last_tick.bid;
      Ask = last_tick.ask;
     }
   start();
}
  
int OnInit()
{
   trade.SetExpertMagicNumber(MAGICMA);
   int deviation=99;
   trade.SetDeviationInPoints(deviation);
   trade.SetAsyncMode(false);
   return(0);
}  
  
      
      
      
void trend1()
{

   int ma_temp;
   double ma_buffer[20];

   double ma;
   ma_temp=iMA(Symbol(),PERIOD_D1,370,0,MODE_SMMA,PRICE_OPEN);
   CopyBuffer(ma_temp,0,0,1,ma_buffer);
   ma=ma_buffer[0];
   
   Alert("ma=", ma);

}

      


void start()
{
   trend1();
}

test pair is GBP/NZD.
Files:
ma_test.mq5  2 kb
 

the problem is getting more and more strange. When i test the above EA from 2012 it gives slightly different results for MA (in the journal log you can see actual value) than when i test it from 2013. None of them is close to the 370 SSMA put on the chart.

Maybe it is something in my initialization that makes EA act like this? 

including screen-shots. Notice the lines drawn are at different levels when comparing both screen-shots and it is the same EA.

Files:
 

Looks like a basic mistake: elements are not indexed like in timeseries (reverse order).

To fix it, you need call this function ...

ArraySetAsSeries(ma_buffer,true);
 
figurelli:

Looks like a basic mistake: elements are not indexed like in timeseries (reverse order).

To fix it, you need call this function ...

when i add it i get:

cannot be used for static allocated array       ma_test.mq5     50      4

but i only use 1 frame of this array. does that matter? And why the warning?

After adding this line nothing changed (both warning and back-tests indicate that adding it had no effect) 

the documentation states that it should be used only with constants:

[...]

const datetime &time[],

[...]

ArraySetAsSeries(time,true); 

and my ma_buffer is a variable array:

 double ma_buffer[20];
 
angreeee:

when i add it i get:

but i only use 1 frame of this array. does that matter? And why the warning?

After adding this line nothing changed (both warning and back-tests indicate that adding it had no effect) 

You are right if you copy just one frame, and this is not the root cause (your declaration of ma_buffer[20] diverted my attention to this fact).

However I suggest you move line below to Init() and the handle to global, because this is really a source of problems (after do this please test it again).

ma_temp=iMA(Symbol(),PERIOD_D1,370,0,MODE_SMMA,PRICE_OPEN);
 
figurelli:

You are right if you copy just one frame, and this is not the root cause (your declaration of ma_buffer[20] diverted my attention to this fact).

However I suggest you move line below to Init() and the handle to global, because this is really a source of problems (after do this please test it again).

i have modified the script according to your guidelines and the problem still persists :(

now it looks like this:

#property copyright   "Grzegorz Korycki"
#property version     "1.0"
#property description "MA TEST"


#include <Trade/Trade.mqh>

   #define MAGICMA  20131002

double Bid;
double Ask;

   int ma_temp;
   double ma_buffer[20];

   
double OnTester()
{
    double custommax = TesterStatistics(STAT_EQUITY_DDREL_PERCENT);
    return (custommax);
}

   CTrade  trade;
   

   void OnTick()
  {
   MqlTick last_tick;
   if(SymbolInfoTick(Symbol(),last_tick))
     {
      Bid = last_tick.bid;
      Ask = last_tick.ask;
     }
   start();
  }
  
int OnInit()
  {
   trade.SetExpertMagicNumber(MAGICMA);
   int deviation=99;
   trade.SetDeviationInPoints(deviation);
   trade.SetAsyncMode(false);
   ma_temp=iMA(Symbol(),PERIOD_D1,370,0,MODE_SMMA,PRICE_OPEN);


   return(0);
   }  
  
      
      
      
void trend1()
{

   double ma;
   CopyBuffer(ma_temp,0,0,1,ma_buffer);
   ma=ma_buffer[0];
   
   Alert("ma=", ma);
}

void start()
{
         trend1();

}

 maybe someone can create draft EA how he would create such MA value generator and I could compare both - test it and isolate the error. (if someone has few minutes to spare)

I am going to check in a sec and reply if the problem is the same on other pairs or is it only this one pair, as this is very strange. 

Reason: