Help with plotting basic Ichimoku Indicator using built in function

 

Hi,


I am new to MQL and am trying to get my head around how it works.

Analyzing the default Ichimoku Indicator, I wrote my own version trying to use the "iIchimoku" built-in function, but even though it compiles without error it just doesn't work. This is what I have the doesn't work. Can someone please explain what I am doing wrong here?

//+------------------------------------------------------------------+
//|                                     _Test-Indicator-Ichimoku.mq4 |
//|                                       Copyright Meta Quotes Ltd. |
//|                                             https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright Meta Quotes Ltd."
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Red          // Tenkan-sen
#property indicator_color2 Blue         // Kijun-sen
#property indicator_color3 SandyBrown   // Up Kumo
#property indicator_color4 Thistle      // Down Kumo
#property indicator_color5 Lime         // Chikou Span
#property indicator_color6 SandyBrown   // Up Kumo bounding line
#property indicator_color7 Thistle      // Down Kumo bounding line

//--- Inputs
extern int InpTenkanPer = 9;
extern int InpKijunPer  = 26;
extern int InpSenkouPer = 52;
extern int price_field  = PRICE_CLOSE;
//--- buffer indices
int TenkanIdx = 0;
int KijunIdx = 1;
int SenkouAIdx = 2;
int SenkouA2Idx = 5;
int SenkouBIdx = 3;
int SenkouB2Idx = 6;
int ChikouIdx = 4;
//--- indicator array buffers
double ExtBufferTenkan[];
double ExtBufferKijun[];
double ExtBufferSenkouA[];
double ExtBufferSenkouA2[];
double ExtBufferSenkouB[];
double ExtBufferSenkouB2[];
double ExtBufferChikou[];
//--- draw begin
int    ExtBegin;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorDigits(Digits);
//--- map indicator buffers
   SetIndexBuffer(TenkanIdx,ExtBufferTenkan);
   SetIndexBuffer(KijunIdx,ExtBufferKijun);
   SetIndexBuffer(SenkouAIdx,ExtBufferSenkouA);
   SetIndexBuffer(SenkouA2Idx,ExtBufferSenkouA2);
   SetIndexBuffer(SenkouBIdx,ExtBufferSenkouB);
   SetIndexBuffer(SenkouB2Idx,ExtBufferSenkouB2);
   SetIndexBuffer(ChikouIdx,ExtBufferChikou);
//--- indicator drawing styles
   SetIndexStyle(TenkanIdx,DRAW_LINE);
   SetIndexStyle(KijunIdx,DRAW_LINE);
   SetIndexStyle(SenkouAIdx,DRAW_HISTOGRAM,STYLE_DOT);
   SetIndexStyle(SenkouA2Idx,DRAW_LINE,STYLE_DOT);
   SetIndexStyle(SenkouBIdx,DRAW_HISTOGRAM,STYLE_DOT);
   SetIndexStyle(SenkouB2Idx,DRAW_LINE,STYLE_DOT);
   SetIndexStyle(ChikouIdx,DRAW_LINE);
//--- indicator start bars
   SetIndexDrawBegin(TenkanIdx,InpTenkanPer-1);
   SetIndexDrawBegin(KijunIdx,InpKijunPer-1);
   ExtBegin=InpKijunPer;
   if(ExtBegin<InpTenkanPer)
      ExtBegin=InpTenkanPer;
   SetIndexDrawBegin(SenkouAIdx,InpKijunPer+ExtBegin-1);
   SetIndexDrawBegin(SenkouA2Idx,InpKijunPer+ExtBegin-1);
   SetIndexDrawBegin(SenkouBIdx,InpKijunPer+InpSenkouPer-1);
   SetIndexDrawBegin(SenkouB2Idx,InpKijunPer+InpSenkouPer-1);
//--- indicator shift bars
   SetIndexShift(SenkouAIdx,InpKijunPer);
   SetIndexShift(SenkouA2Idx,InpKijunPer);
   SetIndexShift(SenkouBIdx,InpKijunPer);
   SetIndexShift(SenkouB2Idx,InpKijunPer);
   SetIndexShift(ChikouIdx,-InpKijunPer);
//--- indicator chart labels
   SetIndexLabel(TenkanIdx,"Tenkan Sen");
   SetIndexLabel(KijunIdx,"Kijun Sen");
   SetIndexLabel(SenkouAIdx,NULL);
   SetIndexLabel(SenkouA2Idx,"Senkou Span A");
   SetIndexLabel(SenkouBIdx,NULL);
   SetIndexLabel(SenkouB2Idx,"Senkou Span B");
   SetIndexLabel(ChikouIdx,"Chikou Span");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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;
//--- validate periods
   if(rates_total<=InpTenkanPer || rates_total<=InpKijunPer || rates_total<=InpSenkouPer)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtBufferTenkan,false);
   ArraySetAsSeries(ExtBufferKijun,false);
   ArraySetAsSeries(ExtBufferSenkouA,false);
   ArraySetAsSeries(ExtBufferSenkouB,false);
   ArraySetAsSeries(ExtBufferChikou,false);
   ArraySetAsSeries(ExtBufferSenkouA2,false);
   ArraySetAsSeries(ExtBufferSenkouB2,false);
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
//--- initial zero
   if(prev_calculated<1)
     {
      for(i=0; i<InpTenkanPer; i++)
         ExtBufferTenkan[i]=0.0;
      for(i=0; i<InpKijunPer; i++)
         ExtBufferKijun[i]=0.0;
      for(i=0; i<ExtBegin; i++)
        {
         ExtBufferSenkouA[i]=0.0;
         ExtBufferSenkouA2[i]=0.0;
        }
      for(i=0; i<InpSenkouPer; i++)
        {
         ExtBufferSenkouB[i]=0.0;
         ExtBufferSenkouB2[i]=0.0;
        }
     }
//--- main loop
   for (i=InpSenkouPer; i<rates_total; i++)
     {
      ExtBufferTenkan[i]  = iIchimoku(_Symbol,price_field,InpTenkanPer,InpKijunPer,InpSenkouPer,MODE_TENKANSEN,1);
      ExtBufferKijun[i]   = iIchimoku(_Symbol,price_field,InpTenkanPer,InpKijunPer,InpSenkouPer,MODE_KIJUNSEN,1);
      ExtBufferSenkouA[i] = iIchimoku(_Symbol,price_field,InpTenkanPer,InpKijunPer,InpSenkouPer,MODE_SENKOUSPANA,1);
      ExtBufferSenkouB[i] = iIchimoku(_Symbol,price_field,InpTenkanPer,InpKijunPer,InpSenkouPer,MODE_SENKOUSPANB,1);
      ExtBufferChikou[i]  = iIchimoku(_Symbol,price_field,InpTenkanPer,InpKijunPer,InpSenkouPer,MODE_CHIKOUSPAN,1);
     }   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

"ArraySetAsSeries" must be "true" when using "iIchimoku". (This also changes "for loop" as well.)

Change 1 to i.

ExtBufferTenkan[i]  = iIchimoku(_Symbol,price_field,InpTenkanPer,InpKijunPer,InpSenkouPer,MODE_TENKANSEN,i);




 
Nagisa Unada:

"ArraySetAsSeries" must be "true" when using "iIchimoku". (This also changes "for loop" as well.)

Change 1 to i.




Oh dear...oops. I should have picked that up! Thank you :)