//+------------------------------------------------------------------+
//|                                    Copyright 2018 YutakaYokouchi |
//|                           https://www.mql5.com/en/users/abundant |
//+------------------------------------------------------------------+
//| Constants and enumeration for migration                          |
//+------------------------------------------------------------------+
#define EMPTY -1
//+------------------------------------------------------------------+
//|                                               Converted by BRiCK |
//|                           https://www.mql5.com/en/users/abundant |
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|                                                     Ichimoku.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Ichimoku Kinko Hyo"
#property strict

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Yellow          // Tenkan-sen
#property indicator_color2 Aqua         // Kijun-sen
#property indicator_color3 Green   // Up Kumo
#property indicator_color4 Red      // Down Kumo
#property indicator_color5 Lime         // Chikou Span
#property indicator_color6 Magenta   // Up Kumo bounding line
#property indicator_color7 Red      // Down Kumo bounding line
//--- input parameters
input int InpTenkan=9;   // Tenkan-sen
input int InpKijun=26;   // Kijun-sen
input int InpSenkou=52;  // Senkou Span B
//--- buffers
double ExtTenkanBuffer[];
double ExtKijunBuffer[];
double ExtSpanA_Buffer[];
double ExtSpanB_Buffer[];
double ExtChikouBuffer[];
double ExtSpanA2_Buffer[];
double ExtSpanB2_Buffer[];
//---
int    ExtBegin;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit(void)
  {
   BRiCK();

   IndicatorDigits(Digits);
//---
   SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(0,ExtTenkanBuffer);
   SetIndexDrawBegin(0,InpTenkan-1);
   SetIndexLabel(0,"Tenkan Sen");
//---
   SetIndexStyle(1,DRAW_NONE);
   SetIndexBuffer(1,ExtKijunBuffer);
   SetIndexDrawBegin(1,InpKijun-1);
   SetIndexLabel(1,"Kijun Sen");
//---
   ExtBegin=InpKijun;
   if(ExtBegin<InpTenkan)
      ExtBegin=InpTenkan;
//---
   SetIndexStyle(2,DRAW_NONE);
   SetIndexBuffer(2,ExtSpanA_Buffer);

   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,ExtSpanA2_Buffer);

   SetIndexLabel(5,"Senkou Span A");
//---
   SetIndexStyle(3,DRAW_NONE);
   SetIndexBuffer(3,ExtSpanB_Buffer);

   SetIndexStyle(6,DRAW_NONE);
   SetIndexBuffer(6,ExtSpanB2_Buffer);

   SetIndexLabel(6,"Senkou Span B");
//---
   SetIndexStyle(4,DRAW_NONE);
   SetIndexShift(4,-InpKijun);
   SetIndexLabel(4,"Chikou Span");
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Ichimoku Kinko Hyo                                               |
//+------------------------------------------------------------------+
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[])
  {
   BRiCK();

   int    i,k,pos;
   double high_value,low_value;
//---
   if(rates_total<=InpTenkan || rates_total<=InpKijun || rates_total<=InpSenkou)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtTenkanBuffer,false);
   ArraySetAsSeries(ExtKijunBuffer,false);
   ArraySetAsSeries(ExtSpanA_Buffer,false);
   ArraySetAsSeries(ExtSpanB_Buffer,false);
   ArraySetAsSeries(ExtChikouBuffer,false);
   ArraySetAsSeries(ExtSpanA2_Buffer,false);
   ArraySetAsSeries(ExtSpanB2_Buffer,false);
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
//--- initial zero
   if(prev_calculated<1)
     {
      for(i=0; i<InpTenkan; i++)
         ExtTenkanBuffer[i]=0.0;
      for(i=0; i<InpKijun; i++)
         ExtKijunBuffer[i]=0.0;
      for(i=0; i<ExtBegin; i++)
        {
         ExtSpanA_Buffer[i]=0.0;
         ExtSpanA2_Buffer[i]=0.0;
        }
      for(i=0; i<InpSenkou; i++)
        {
         ExtSpanB_Buffer[i]=0.0;
         ExtSpanB2_Buffer[i]=0.0;
        }
     }
//--- Tenkan Sen
   pos=InpTenkan-1;
   if(prev_calculated>InpTenkan)
      pos=prev_calculated-1;
   for(i=pos; i<rates_total; i++)
     {
      high_value=high[i];
      low_value=low[i];
      k=i+1-InpTenkan;
      while(k<=i)
        {
         if(high_value<high[k])
            high_value=high[k];
         if(low_value>low[k])
            low_value=low[k];
         k++;
        }
      ExtTenkanBuffer[i]=(high_value+low_value)/2;
     }
//--- Kijun Sen
   pos=InpKijun-1;
   if(prev_calculated>InpKijun)
      pos=prev_calculated-1;
   for(i=pos; i<rates_total; i++)
     {
      high_value=high[i];
      low_value=low[i];
      k=i+1-InpKijun;
      while(k<=i)
        {
         if(high_value<high[k])
            high_value=high[k];
         if(low_value>low[k])
            low_value=low[k];
         k++;
        }
      ExtKijunBuffer[i]=(high_value+low_value)/2;
     }
//--- Senkou Span A
   pos=ExtBegin-1;
   if(prev_calculated>ExtBegin)
      pos=prev_calculated-1;
   for(i=pos; i<rates_total; i++)
     {
      ExtSpanA_Buffer[i]=(ExtKijunBuffer[i]+ExtTenkanBuffer[i])/2;
      ExtSpanA2_Buffer[i]=ExtSpanA_Buffer[i];
     }
//--- Senkou Span B
   pos=InpSenkou-1;
   if(prev_calculated>InpSenkou)
      pos=prev_calculated-1;
   for(i=pos; i<rates_total; i++)
     {
      high_value=high[i];
      low_value=low[i];
      k=i+1-InpSenkou;
      while(k<=i)
        {
         if(high_value<high[k])
            high_value=high[k];
         if(low_value>low[k])
            low_value=low[k];
         k++;
        }
      ExtSpanB_Buffer[i]=(high_value+low_value)/2;
      ExtSpanB2_Buffer[i]=ExtSpanB_Buffer[i];
     }
//--- Chikou Span
   pos=0;
   if(prev_calculated>1)
      pos=prev_calculated-1;
   for(i=pos; i<rates_total; i++)
      ExtChikouBuffer[i]=close[i];
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                    Copyright 2018 YutakaYokouchi |
//|                           https://www.mql5.com/en/users/abundant |
//+------------------------------------------------------------------+
//| Functions for migration                                          |
//+------------------------------------------------------------------+
double Ask=0;
int Bars=0;
double Bid=0;
int Digits=0;
double Point=0;
double Close[];
double High[];
double Low[];
double Open[];
datetime Time[];
long Volume[];
void BRiCK()
{
   Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   Bars=Bars(_Symbol, _Period);
   Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   Digits=(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
   Point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);

   ArraySetAsSeries(Close, true);
   ArraySetAsSeries(High, true);
   ArraySetAsSeries(Low, true);
   ArraySetAsSeries(Open, true);
   ArraySetAsSeries(Time, true);
   ArraySetAsSeries(Volume, true);

   int count = Bars(_Symbol, _Period);

   ArrayResize(Close, count);
   ArrayResize(High, count);
   ArrayResize(Low, count);
   ArrayResize(Open, count);
   ArrayResize(Time, count);
   ArrayResize(Volume, count);

   CopyClose(_Symbol, _Period, 0, Bars, Close);
   CopyHigh(_Symbol, _Period, 0, Bars, High);
   CopyLow(_Symbol, _Period, 0, Bars, Low);
   CopyOpen(_Symbol, _Period, 0, Bars, Open);
   CopyTime(_Symbol, _Period, 0, Bars, Time);
   CopyTickVolume(_Symbol, _Period, 0, Bars, Volume);
}


void IndicatorDigits(int digits)
{
  return;
}
void SetIndexDrawBegin(int index, int begin)
{
  return;
}
void SetIndexLabel(int index, string text)
{
  return;
}
void SetIndexShift(int index, int shift)
{
  return;
}
void SetIndexStyle(int index, int type, int style=EMPTY, int width=EMPTY, color clr=clrNONE)
{
  return;
}
bool SetIndexBuffer(int index, double &buffer[])
  return(false);
}
//+------------------------------------------------------------------+
//|                                               Converted by BRiCK |
//|                           https://www.mql5.com/en/users/abundant |
//+------------------------------------------------------------------+