//+------------------------------------------------------------------+
//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
//------------------------------------------------------------------

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  clrDeepSkyBlue
#property strict

//
//
//
//
//

extern ENUM_TIMEFRAMES TimeFrame     = PERIOD_CURRENT;    // Timeframe to use
extern int             Kijun         = 26;                // Kijun period
extern int             KijunShift    = 3;                 // Kijun shift
extern int             barsToDisplay = 0;                 // Bars to display 0 = all bars
 
double kj[],count[];
string indicatorFileName;
#define _mtfCall(_buf,_ind) iCustom(NULL,TimeFrame,indicatorFileName,0,Kijun,0,barsToDisplay,_buf,_ind)

//
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//

int init()
{
     for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);
     IndicatorBuffers(2);
     SetIndexBuffer(0,kj); SetIndexLabel(0,"Kijun Sen");
     SetIndexBuffer(1,count);
     
     indicatorFileName = WindowExpertName();
     TimeFrame         = fmax(TimeFrame,_Period);
     for (int i=0; i<7; i++) SetIndexShift(i,KijunShift*TimeFrame/Period());
     
     IndicatorShortName(timeFrameToString(TimeFrame)+"  Kijun-Sen");    
return(0);
}

int deinit() {  return(0);}
                      
//
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//

int start()
{
   int i,k,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
            int limit = fmin(Bars-counted_bars,Bars-2); count[0] = limit;
            SetIndexDrawBegin(0,(barsToDisplay<=0?0:Bars-barsToDisplay+1));
            if (TimeFrame != _Period)
            {
               limit = (int)fmax(limit,fmin(Bars-1,_mtfCall(1,0)*TimeFrame/Period()));
               for (i=limit; i>=0; i--)
               {
                  int y = iBarShift(NULL,TimeFrame,Time[i]);
                     kj[i] = _mtfCall(0,y);
               }
     return(0);
     }
   
     //
     //
     //
     //
     //
   
     for (i=limit; i>=0; i--)
     {
        double khi = High[i];
        double klo = Low[i];
        double kprice = 0;
        if  (i >= Bars - Kijun) continue;
        for (k = 1; k < Kijun && (i+k)<Bars; k++)
        {
           if(khi < High[i+k]) khi = High[i+k];
           if(klo >  Low[i+k]) klo =  Low[i+k];
        }
        if ((khi+klo) > 0.0) 
             kj[i] = (khi + klo)/2; 
        else kj[i] = 0;
            
     }
return(0);
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

     
   