#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_chart_window
#property indicator_buffers 7
#property strict

//
//
//

input int           Tenkan                     = 9;              // Tenkan Period
input int           Kijun                      = 26;             // Kijun Period
input int           Senkou                     = 52;             // Senkou Period
input int           KijunShift                 = 26;             // Shift
input string        __dis__01                  = "";             //.Display settings
input bool          Draw_Tenkan                = false;          // Draw Tenkan sen?
input int           inpTenkanWidth             = 1;              // Tenkan sen width
input color         inpTenkanClr               = clrLimeGreen;   // Tenkan sen color
input bool          Draw_Kijun                 = false;          // Draw Kijun sen?
input int           inpKijunWidth              = 1;              // Kijun sen width
input color         inpKijunClr                = clrRed;         // Kijun sen color
input bool          Draw_Kumo                  = true;           // Draw Kumo cloud?
input bool          inpAutoWidth               = true;           // Auto width kumo cloud?
input int           inpKumoWidth               = 5;              // Kumo cloud width if auto = false
input color         inpKumoClrUp               = clrLimeGreen;   // Bullish kumo cloud color
input color         inpKumoClrDn               = clrBrown;       // Bearish kumo cloud color
input bool          Draw_Chinkou               = false;          // Draw Chinkou?
input int           inpChinkouWidth            = 1;              // Chinkou width
input color         inpChinkouClr              = clrDarkOrange;  // Chinkou color
input bool          Draw_Span                  = true;           // Draw Kumo spans?
input int           inpSpanAWidth              = 5;              // SpanA width
input color         inpSpanAColor              = clrDarkGreen;   // SpanA color
input int           inpSpanBWidth              = 5;              // SpanB width
input color         inpSpanBColor              = clrMaroon;      // SpanB color

double ts[],ks[],spA[],spB[],chin[],spA2[],spB2[];
struct sGlobalStruct
{
   int      scale,width;
   double   tlo,thi,klo,khi,slo,shi;
};
sGlobalStruct glo;

//
//
//

int OnInit()
{
   if (inpAutoWidth)
   {
      glo.scale = int(ChartGetInteger(0,CHART_SCALE));
      switch(glo.scale) 
	   {
	      case 0: glo.width =  3; break;
	      case 1: glo.width =  3; break;
		   case 2: glo.width =  4; break;
		   case 3: glo.width =  5; break;
		   case 4: glo.width =  8; break;
		   case 5: glo.width = 16; break;
	   }
	}
	else { glo.width = inpKumoWidth; }
	
   IndicatorDigits(_Digits);
   SetIndexBuffer(0, spA, INDICATOR_DATA);  SetIndexStyle(0,Draw_Kumo    ? DRAW_HISTOGRAM : DRAW_NONE,EMPTY,glo.width      ,inpKumoClrUp);   SetIndexLabel(0, "Kumo cloud");   
   SetIndexBuffer(1, spB, INDICATOR_DATA);  SetIndexStyle(1,Draw_Kumo    ? DRAW_HISTOGRAM : DRAW_NONE,EMPTY,glo.width      ,inpKumoClrDn);   SetIndexLabel(1, "Kumo cloud"); 
   SetIndexBuffer(2, ts,  INDICATOR_DATA);  SetIndexStyle(2,Draw_Tenkan  ? DRAW_LINE      : DRAW_NONE,EMPTY,inpTenkanWidth ,inpTenkanClr);   SetIndexLabel(2, "Tenkan Sen");
   SetIndexBuffer(3, ks,  INDICATOR_DATA);  SetIndexStyle(3,Draw_Kijun   ? DRAW_LINE      : DRAW_NONE,EMPTY,inpKijunWidth  ,inpKijunClr);    SetIndexLabel(3, "Kijun Sen");
   SetIndexBuffer(4, chin,INDICATOR_DATA);  SetIndexStyle(4,Draw_Chinkou ? DRAW_LINE      : DRAW_NONE,EMPTY,inpChinkouWidth,inpChinkouClr);  SetIndexLabel(4, "Chinkou Span");
   SetIndexBuffer(5, spA2,INDICATOR_DATA);  SetIndexStyle(5,Draw_Span    ? DRAW_LINE      : DRAW_NONE,EMPTY,inpSpanAWidth  ,inpSpanAColor);  SetIndexLabel(5, "Senkou Span A");
   SetIndexBuffer(6, spB2,INDICATOR_DATA);  SetIndexStyle(6,Draw_Span    ? DRAW_LINE      : DRAW_NONE,EMPTY,inpSpanBWidth  ,inpSpanBColor);  SetIndexLabel(6, "Senkou Span B");
   
   SetIndexShift(0,KijunShift); 
   SetIndexShift(1,KijunShift);
   SetIndexShift(2,KijunShift); 
   SetIndexShift(3,KijunShift);
   SetIndexShift(4,-KijunShift);
   SetIndexShift(5,KijunShift);
   SetIndexShift(6,KijunShift);    
   
   IndicatorSetString(INDICATOR_SHORTNAME," Ichimoku"); 
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

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=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1; 
   
   //
   //
   //
     
   for (; i>=0 && !_StopFlag; i--)
   {
      glo.tlo =  low[ArrayMinimum(low, Tenkan,i)];
      glo.thi = high[ArrayMaximum(high,Tenkan,i)];
      ts[i]   = ((glo.thi+glo.tlo)!=0) ? (glo.thi+glo.tlo)*0.5 : 0;
         
      glo.klo =  low[ArrayMinimum(low, Kijun,i)];
      glo.khi = high[ArrayMaximum(high,Kijun,i)];
      ks[i]   = ((glo.khi+glo.klo)!=0) ? (glo.khi+glo.klo)*0.5 : 0;
        
      glo.slo =  low[ArrayMinimum(low, Senkou,i)];
      glo.shi = high[ArrayMaximum(high,Senkou,i)];
       
      spA[i]  = (ks[i]+ts[i])*0.5;
      spA2[i] = (ks[i]+ts[i])*0.5;
      spB[i]  = (glo.shi+glo.slo)*0.5; 
      spB2[i] = (glo.shi+glo.slo)*0.5; 
      chin[i] = close[i];   
   }
return(rates_total);
}





