//+------------------------------------------------------------------+
//|                                             Western Ichimoku.mq4 |
//|                                      Copyright © 2009, fruitaly. |
//|                                     http://fruitaly.wordpress.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, fruitaly."
#property link      "http://fruitaly.wordpress.com"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 YellowGreen
#property indicator_color4 Chocolate
#property indicator_color5 YellowGreen
#property indicator_color6 Chocolate
#property indicator_color7 Lime
//---- input parameters
extern int       Tenkan=10;
extern int       Kijun=20;
extern int       Senkou=50;
extern int       MAMethod=MODE_EMA;
//---- buffers
double TS[];
double KS[];
double CS[];
double SSA[];
double SSB[];
double Cloud1[];
double Cloud2[];

int begin;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,TS);
   SetIndexDrawBegin(0,Tenkan-1);
   SetIndexLabel(0,"Tenkan Sen");
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,KS);
   SetIndexDrawBegin(1,Kijun-1);
   SetIndexLabel(1,"Kijun Sen");
   
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,CS);
   SetIndexShift(6,-Kijun);
   SetIndexLabel(6,"Chinkou Span");
   
   begin=Kijun; if(begin<Tenkan) begin=Tenkan;
   
   SetIndexStyle(2,DRAW_HISTOGRAM, STYLE_DOT);
   SetIndexBuffer(2,Cloud1);
   SetIndexDrawBegin(2,Kijun+begin-1);
   SetIndexShift(2,Kijun);
   SetIndexLabel(2,NULL);   
      
   SetIndexStyle(4,DRAW_LINE, STYLE_DOT);
   SetIndexBuffer(4,SSA);
   SetIndexDrawBegin(4,Kijun+begin-1);
   SetIndexShift(4,Kijun);
   SetIndexLabel(4,"Senkou Span A");
   
   SetIndexStyle(3,DRAW_HISTOGRAM, STYLE_DOT);
   SetIndexBuffer(3,Cloud2);
   SetIndexDrawBegin(3,Kijun+Senkou-1);
   SetIndexShift(3,Kijun);
   SetIndexLabel(3,NULL);
      
   SetIndexStyle(5,DRAW_LINE, STYLE_DOT);
   SetIndexBuffer(5,SSB);
   SetIndexDrawBegin(5,Kijun+Senkou-1);
   SetIndexShift(5,Kijun);
   SetIndexLabel(5,"Senkou Span B");
   
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   int    counted_bars=IndicatorCounted();
   
   if(Bars<Tenkan || Bars<Kijun || Bars<Senkou) return(-1);
   //initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=Tenkan;i++)    TS[Bars-i]=0;
      for(i=1;i<=Kijun;i++)     KS[Bars-i]=0;
      for(i=1;i<=begin;i++) { SSA[Bars-i]=0; Cloud1[Bars-i]=0;}
      for(i=1;i<=Senkou;i++)  { SSB[Bars-i]=0; Cloud2[Bars-i]=0;}
     }
//---- Tenkan Sen
   i=Bars-Tenkan;
   if(counted_bars>Tenkan) i=Bars-counted_bars-1;
   while(i>=0)
     {
      TS[i]=iMA(NULL,0,Tenkan,0,MAMethod,PRICE_CLOSE,i);
      i--;
     }
//---- Kijun Sen
   i=Bars-Kijun;
   if(counted_bars>Kijun) i=Bars-counted_bars-1;
   while(i>=0)
     {
      KS[i]=iMA(NULL,0,Kijun,0,MAMethod,PRICE_CLOSE,i);
      i--;
     }
//---- Senkou Span A
   i=Bars-begin+1;
   if(counted_bars>begin-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      SSA[i]=(TS[i]+KS[i])/2;
      Cloud1[i]=SSA[i];
      i--;
     }
//---- Senkou Span B
   double ma;
   i=Bars-Senkou;
   if(counted_bars>Senkou) i=Bars-counted_bars-1;
   while(i>=0)
     {
      ma=iMA(NULL,0,Senkou,0,MAMethod,PRICE_CLOSE,i);
      SSB[i]=ma;
      Cloud2[i]=ma;
      i--;
     }
//---- Chinkou Span
   i=Bars-1;
   if(counted_bars>1) i=Bars-counted_bars-1;
   while(i>=0) { CS[i]=Close[i]; i--; }
//----
   return(0);
  }
//+------------------------------------------------------------------+