Download MetaTrader 5
To add comments, please log in or register
Deposit to your account via Visa QIWI Wallet
Konstantinos Maleas
146
Konstantinos Maleas 2015.10.25 20:26 

Hello everybody,

I made this simple indicator couple of months before and it working fine from the very first time, until yesterday that i recompile it.

For some reason the signal  array  seems that calculates only the first time and then keeps the same numbers for all others loops.

Like the iMAOnArray() Function isn't working properly.

Can anyone help me?

Thanks in Advance

#property copyright "Konstantinos Maleas"
#property link      ""
#property version   "1.00"
#property strict

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Green
#property indicator_level1 0.0
#property indicator_levelcolor Gray
#property indicator_levelstyle STYLE_DOT

   ///--- input parameter
  extern int scen = 0;
         int main_period;// = 12;
         int signal_period = 3;

   ///--- buffers
   double main[10000], signal[10000], dif[10000];
   
int OnInit()
{
   string short_name;

   main_period = impulse_ma_period(scen,ChartPeriod());

   SetIndexStyle(0,DRAW_LINE,EMPTY,2,clrGreen);
   SetIndexBuffer(0,dif);

   short_name = "Dif ("+IntegerToString(main_period,2,' ')+")";

   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);

   if(main_period<=0)
   {
      Print("Wrong input parameter Dif Period = ",main_period);
      return(INIT_FAILED);
   }

   SetIndexDrawBegin(0,main_period);

   return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int i;
   
   if ( rates_total <= main_period )
      return(0);

   ArraySetAsSeries(main,true);
   ArraySetAsSeries(signal,true);
   ArraySetAsSeries(dif,true);
   
   
   for(i=0; i<rates_total; i++)
   {
     main[i] =  NormalizeDouble(iMA(Symbol(),0,main_period,0,MODE_LWMA,PRICE_MEDIAN,i),6);
   }

   for(i=0; i<rates_total-main_period; i--)
   {
      signal[i] =  NormalizeDouble(iMAOnArray(main, 0, signal_period, 0, MODE_LWMA, i),6);
   }

   for(i=0; i<900; i++)
   {
      dif[i] = NormalizeDouble((main[i] - signal[i]) * 1000,6);
   }
   //Alert("dif[",dif[i],"] main[",main[i],"] - signal[",signal[i],"] [",i,"]");
   //SetIndexStyle(0,DRAW_LINE,EMPTY,2,clrGreen);
   //SetIndexBuffer(0,dif);
   return(rates_total);
}

///----- Moving Average Installation -----------------------------------------------------------------------------------------------
int impulse_ma_period(int ma_imp, int tf)
{
   if ( ma_imp == 0 )
      switch(tf)
      {
         case PERIOD_M1 :  main_period = 15; break;
         case PERIOD_M5 :  main_period = 12; break;
         case PERIOD_M15 :  main_period = 16; break;
         case PERIOD_M30 :  main_period = 12; break;
         case PERIOD_H1 :  main_period = 12; break;
         case PERIOD_H4 :  main_period = 15; break;
         case PERIOD_D1 :  main_period = 22; break;
         case PERIOD_W1 :  main_period = 13; break;
         case PERIOD_MN1 :  main_period = 12; break;
      }
   if ( ma_imp == 1 )
      switch(tf)
      {
         case 1 :  main_period = 10; break;
         case 5 :  main_period = 8; break;
         case 15 :  main_period = 11; break;
         case 30 :  main_period = 8; break;
         case 60 :  main_period = 8; break;
         case 240 :  main_period = 10; break;
         case 1440 :  main_period = 15; break;
         case 10080 :  main_period = 9; break;
         case 43200 :  main_period = 8; break;
      }
   if ( ma_imp == 2 )
      switch(tf)
      {
         case 1 :  main_period = 7; break;
         case 5 :  main_period = 6; break;
         case 15 :  main_period = 8; break;
         case 30 :  main_period = 6; break;
         case 60 :  main_period = 6; break;
         case 240 :  main_period = 7; break;
         case 1440 :  main_period = 6; break;
         case 10080 :  main_period = 7; break;
         case 43200 :  main_period = 7; break;
      }
   if ( ma_imp == 3 )
      switch(tf)
      {
         case 1 :  main_period = 5; break;
         case 5 :  main_period = 4; break;
         case 15 :  main_period = 5; break;
         case 30 :  main_period = 4; break;
         case 60 :  main_period = 4; break;
         case 240 :  main_period = 5; break;
         case 1440 :  main_period = 5; break;
         case 10080 :  main_period = 4; break;
         case 43200 :  main_period = 4; break;
      }
   if ( ma_imp >= 4 )
      switch(tf)
      {
         case 1 :  main_period = 30; break;
         case 5 :  main_period = 24; break;
         case 15 :  main_period = 16; break;
         case 30 :  main_period = 10; break;
         case 60 :  main_period = 8; break;
         case 240 :  main_period = 6; break;
         case 1440 :  main_period = 5; break;
         case 10080 :  main_period = 4; break;
         case 43200 :  main_period = 3; break;
      }
   return(main_period);
}
Keith Watford
Moderator
8028
Keith Watford 2015.10.25 21:03  
I would think that you are getting an array out of range error with main and signal, you have not set them as buffers.
Konstantinos Maleas
146
Konstantinos Maleas 2015.10.25 21:37  
GumRai:
I would think that you are getting an array out of range error with main and signal, you have not set them as buffers.

Thank you for having a look, but i don't get errors and i use all the arrays as  buffers.

Only the 3d one and i have set it as buffer.

Keith Watford
Moderator
8028
Keith Watford 2015.10.26 00:27  
   for(i=0; i<rates_total-main_period; i--)
   {
      signal[i] =  NormalizeDouble(iMAOnArray(main, 0, signal_period, 0, MODE_LWMA, i),6);
   }
I think that should be i++
Konstantinos Maleas
146
Konstantinos Maleas 2015.10.26 07:59  
GumRai:
I think that should be i++


Thanks again GumRai.

Although you are right this thing isn't working.

I really think it has to do, somehow, with the iMAOnarray() function and the latest update of MT4.

Cause it was working properly but after a recompilation stop working properly.

The key word is properly. No errors, no nothing.

It is still working but the data that it returns are false.

Have a look at the previous one.

They are almost identical. The small changes came from my tries to repair it, just like the i-- that i forgot to reverse it back again to i++ :) .

#property copyright "Konstantinos Maleas"
#property link      ""
#property version   "1.00"
#property strict

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Green

   ///--- input parameter
  extern int scen = 0;
         int main_period = 12;
  extern int signal_period = 3;

   ///--- buffers
   double main[100000], signal[100000], dif[1000];


int OnInit()
{
   string short_name, short_name1, short_name2, short_name3;

   impulse_ma_period(scen,ChartPeriod());
   
  ///--- indicator line
   SetIndexStyle(0,DRAW_LINE,EMPTY,2,clrGreen);
   SetIndexBuffer(0,dif);
   
  ///--- name for DataWindow and indicator subwindow label
   short_name1 = "Dif ("+main_period+")";
   
   IndicatorShortName(short_name1);
   SetIndexLabel(0,short_name1);
  
  /////--- check for input parameter
   if(main_period<=0)
   {
      Print("Wrong input parameter Momentum Period = ",main_period);
      return(INIT_FAILED);
   }

   SetIndexDrawBegin(0,main_period);
   SetIndexDrawBegin(1,signal_period);

   return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int i,limit, pos = main_period;

   ////--- check for bars count and input parameter
   if(rates_total<=main_period || signal_period<=0)
      return(0);
   ///--- counting from 0 to rates_total
   ArraySetAsSeries(main,true);
   ArraySetAsSeries(signal,true);
   ArraySetAsSeries(dif,true);
   
   ///--- initial zero
   if(prev_calculated<=0)
   {
      for(i=0; i<main_period; i++)
         main[i]=0.0;
      limit=main_period;
   }
   else
      limit=prev_calculated-1;
      
   for(i=0; i<rates_total; i++)
   {
     main[i] =  NormalizeDouble(iMA(Symbol(),0,main_period,0,MODE_LWMA,PRICE_MEDIAN,i),6);
   }
   
   for(i=0; i<rates_total; i++)
   {
      signal[i] =  NormalizeDouble(iMAOnArray(main, 0, signal_period, 0, MODE_LWMA, i),6);
   }
   
   for(i=0; i<rates_total; i++)
   {
      dif[i] = NormalizeDouble((main[i] - signal[i]) * 1000,6);
   }
   return(rates_total);
}

///----- Moving Average Installation -----------------------------------------------------------------------------------------------
int impulse_ma_period(int ma_imp, int tf)
{
   if ( ma_imp == 0 )
      switch(tf)
      {
         case 1 :  main_period = 15; break;
         case 5 :  main_period = 12; break;
         case 15 :  main_period = 16; break;
         case 30 :  main_period = 12; break;
         case 60 :  main_period = 12; break;
         case 240 :  main_period = 15; break;
         case 1440 :  main_period = 22; break;
         case 10080 :  main_period = 13; break;
         case 43200 :  main_period = 12; break;
      }
   if ( ma_imp == 1 )
      switch(tf)
      {
         case 1 :  main_period = 10; break;
         case 5 :  main_period = 8; break;
         case 15 :  main_period = 11; break;
         case 30 :  main_period = 8; break;
         case 60 :  main_period = 8; break;
         case 240 :  main_period = 10; break;
         case 1440 :  main_period = 15; break;
         case 10080 :  main_period = 9; break;
         case 43200 :  main_period = 8; break;
      }
   if ( ma_imp == 2 )
      switch(tf)
      {
         case 1 :  main_period = 7; break;
         case 5 :  main_period = 6; break;
         case 15 :  main_period = 8; break;
         case 30 :  main_period = 6; break;
         case 60 :  main_period = 6; break;
         case 240 :  main_period = 7; break;
         case 1440 :  main_period = 6; break;
         case 10080 :  main_period = 7; break;
         case 43200 :  main_period = 7; break;
      }
   if ( ma_imp == 3 )
      switch(tf)
      {
         case 1 :  main_period = 5; break;
         case 5 :  main_period = 4; break;
         case 15 :  main_period = 5; break;
         case 30 :  main_period = 4; break;
         case 60 :  main_period = 4; break;
         case 240 :  main_period = 5; break;
         case 1440 :  main_period = 5; break;
         case 10080 :  main_period = 4; break;
         case 43200 :  main_period = 4; break;
      }
   if ( ma_imp >= 4 )
      switch(tf)
      {
         case 1 :  main_period = 3; break;
         case 5 :  main_period = 2; break;
         case 15 :  main_period = 3; break;
         case 30 :  main_period = 2; break;
         case 60 :  main_period = 2; break;
         case 240 :  main_period = 3; break;
         case 1440 :  main_period = 3; break;
         case 10080 :  main_period = 3; break;
         case 43200 :  main_period = 2; break;
      }
   return(main_period);
}


nina
60
nina 2016.02.24 16:52  
   ///--- buffers
   double main[100000], signal[100000], dif[1000];

   ///--- counting from 0 to rates_total
   ArraySetAsSeries(main,true);
   ArraySetAsSeries(signal,true);
   ArraySetAsSeries(dif,true);

The AS_SERIES flag can't be set for multi-dimensional arrays or static arrays (arrays, whose size in square brackets is preset already on the compilation stage).  ;) 

/
To add comments, please log in or register