//+------------------------------------------------------------------+
//|                                                 MTF_Ichimoku.mq4 |
//|                      Copyright © 2013, Victor Nikolaev aka Vinin |
//|                                                    Vinin@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Victor Nikolaev aka Vinin"
#property link      "Vinin@mail.ru"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Blue
#property indicator_color4 Red

#property indicator_color5 Lime
#property indicator_color6 Yellow
#property indicator_color7 Yellow

//---- input parameters
extern int TimeFrame=0;  // Используемый таймфрейм
extern int Tenkan=9;     // Период Tenkan
extern int Kijun=26;     // Период Kijun
extern int Senkou=52;    // Период Senkou
//---- buffers
double Tenkan_Buffer[];
double Kijun_Buffer[];
double SpanA_Buffer[];
double SpanB_Buffer[];
double Chinkou_Buffer[];
double SpanA2_Buffer[];
double SpanB2_Buffer[];
//----
int a_begin;
int k=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
   k=1;
   if (TimeFrame>0)
      k=TimeFrame/Period();
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Tenkan_Buffer);
   SetIndexDrawBegin(0,Tenkan-1);
   SetIndexLabel(0,"Tenkan Sen");
//----
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Kijun_Buffer);
   SetIndexDrawBegin(1,Kijun-1);
   SetIndexLabel(1,"Kijun Sen");
//----
   a_begin=Kijun; if(a_begin<Tenkan) a_begin=Tenkan;
   
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_DOT);
   SetIndexBuffer(2,SpanA_Buffer);
   SetIndexDrawBegin(2,Kijun+a_begin-1);
   SetIndexShift(2,Kijun*k);
   SetIndexLabel(2,NULL);
   
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,SpanA2_Buffer);
   SetIndexDrawBegin(5,Kijun+a_begin-1);
   SetIndexShift(5,Kijun*k);
   SetIndexLabel(5,"Senkou Span A");
//----
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_DOT);
   SetIndexBuffer(3,SpanB_Buffer);
   SetIndexDrawBegin(3,Kijun+Senkou-1);
   SetIndexShift(3,Kijun*k);
   SetIndexLabel(3,NULL);
   
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,SpanB2_Buffer);
   SetIndexDrawBegin(6,Kijun+Senkou-1);
   SetIndexShift(6,Kijun*k);
   SetIndexLabel(6,"Senkou Span B");
//----
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,Chinkou_Buffer);
   SetIndexShift(4,-Kijun*k);
   SetIndexLabel(4,"Chinkou Span");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Ichimoku Kinko Hyo                                               |
//+------------------------------------------------------------------+
int start()
  {
   int    limit, i,pos;
   int    counted_bars=IndicatorCounted();
   double high,low,price;
//----
   if(Bars<=Tenkan || Bars<=Kijun || Bars<=Senkou) return(0);



//---- Tenkan Sen
   limit=Bars-counted_bars-1;
   if (limit>1) {
      limit=Bars-Tenkan-1;
   }
   for (i=limit;i>=0;i--) {
      pos=iBarShift(NULL, TimeFrame, Time[i]);
      high=iHigh(NULL, TimeFrame,iHighest(NULL,TimeFrame,MODE_HIGH, Tenkan, pos));
      low =iLow(NULL,TimeFrame, iLowest( NULL,TimeFrame,MODE_LOW,  Tenkan, pos));
      price=(high+low)*0.5;
      Tenkan_Buffer[i]=price;
   }

//---- Kijun Sen
   limit=Bars-counted_bars-1;
   if (limit>1) {
      limit=Bars-Kijun-1;
   }
   for (i=limit;i>=0;i--) {
      pos=iBarShift(NULL, TimeFrame, Time[i]);
      high=iHigh(NULL, TimeFrame,iHighest(NULL,TimeFrame,MODE_HIGH, Kijun, pos));
      low =iLow(NULL,TimeFrame, iLowest( NULL,TimeFrame,MODE_LOW,  Kijun, pos));
      price=(high+low)*0.5;
      Kijun_Buffer[i]=price;
     }

//---- Senkou Span A
   limit=Bars-counted_bars-1;
   if (limit>1) {
      limit=Bars-MathMax(Tenkan,Kijun)-1;
   }
   for (i=limit;i>=0;i--) {
      pos=iBarShift(NULL, TimeFrame, Time[i]);
      price=(Kijun_Buffer[i]+Tenkan_Buffer[i])*0.5;
      SpanA_Buffer[i]=price;
      SpanA2_Buffer[i]=price;
     }
//---- Senkou Span B
   limit=Bars-counted_bars-1;
   if (limit>1) {
      limit=Bars-Senkou-1;
   }

   for (i=limit;i>=0;i--) {
      pos=iBarShift(NULL, TimeFrame, Time[i]);
      high=iHigh(NULL, TimeFrame,iHighest(NULL,TimeFrame,MODE_HIGH, Senkou, pos));
      low =iLow(NULL,TimeFrame, iLowest( NULL,TimeFrame,MODE_LOW,  Senkou, pos));
      price=(high+low)*0.5;
      SpanB_Buffer[i]=price;
      SpanB2_Buffer[i]=price;
   }

//---- Chinkou Span
   limit=Bars-counted_bars-1;
   if (limit>1) {
      limit=Bars-1;
   }
   for (i=limit;i>=0;i--) { 
      pos=iBarShift(NULL, TimeFrame, Time[i]);
      Chinkou_Buffer[i]=iClose(NULL,TimeFrame,pos); 
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+