Download MetaTrader 5

Indicator doesn't draw- what am I doing wrong?

To add comments, please log in or register
All articles are available from MetaTrader 5. Contribute your own one!
Alex1703
10
Alex1703 2016.03.07 17:28 

Hello everyone :)

after having coded several EA's in mql5, I have started writing my first indicator a few days ago. I was hoping I could get some help.

It is supposed to use the fourier transform in order to extrapolate the prices into the future. At this stage I have not gotten to the extrapolation part just yet, but I am trying to plot the past price in the form of a line.

While the calculations appear to be working out well, the indicator does not plot anything. The indicator buffer therefore contains the right values (tested with printf), but no line appears. I have read several guides on indicators, but I can't find the mistake :/

I would greatly appreciate if You could point me to the mistake :)

Thanks in advance!


//+------------------------------------------------------------------+
//|                                                           FT.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Extrapolation
#property indicator_label1  "Extrapolation"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Blue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#define pi 3.14159265359
//--- indicator buffers
double ExtrapolationBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtrapolationBuffer,INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 100);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
ArrayInitialize(ExtrapolationBuffer,EMPTY_VALUE);
   double avg=0;
   int N=100;
   int n=(N/2)+1;
   double REx[], IMx[], close[];
   ArrayResize(REx, n);
   ArrayResize(IMx, n);
   
   CopyClose(_Symbol, PERIOD_M1, 0, N, close);
   
   for (int i=0;i<N;i++){
      avg=avg+close[i];
   }
   
   avg=avg/N;
   
   for (int i=0;i<n;i++){
      REx[i]=0;
      IMx[i]=0;
      
      for (int j=0;j<N;j++){
         REx[i]=REx[i]+close[j]*MathCos(2*pi*(j+1)*(i+1)/N);
         IMx[i]=IMx[i]-close[j]*MathSin(2*pi*(j+1)*(i+1)/N);
      }
   }
   
   
   for (int i=0;i<n;i++){
      REx[i]=REx[i]/(N/2);
      IMx[i]=-IMx[i]/(N/2);
   }
   REx[0]=REx[0]/2;
   REx[n-1]=REx[0]/2;
   
   for (int j=0; j<N;j++){
      ExtrapolationBuffer[j]=0;
      for (int i=0; i<n;i++){
        ExtrapolationBuffer[j]=ExtrapolationBuffer[j]+REx[i]*MathCos(2*pi*(j+1)*(i+1)/N);
        ExtrapolationBuffer[j]=ExtrapolationBuffer[j]+IMx[i]*MathSin(2*pi*(j+1)*(i+1)/N);
      }
      ExtrapolationBuffer[j]=NormalizeDouble(avg+ExtrapolationBuffer[j], _Digits);
      //printf(""+ExtrapolationBuffer[j]);      TEST CALCULATED VALUES
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Vasyl Nosal
18546
Vasyl Nosal 2016.03.07 17:47  
ArraySetAsSeries(ExtrapolationBuffer,true);
or delete this
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 100)
Alex1703
10
Alex1703 2016.03.07 18:09  
Vasyl Nosal:

Thanks for your quick reply!

Setting the array to series reverses the order of the array. I can certainly implement a loop which will compensate for that reversal, but isn't there another way of doing this?

If i delete the PLOT_DRAW_BEGIN function, I still get no line. :/

Alain Verleyen
Moderator
30737
Alain Verleyen 2016.03.07 20:21  
Alex1703:

Thanks for your quick reply!

Setting the array to series reverses the order of the array.

Your close[] array is reversed :

   CopyClose(_Symbol,_Period,0,N,close);// You have to add error processing
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(ExtrapolationBuffer,true);
To add comments, please log in or register