//+------------------------------------------------------------------+
//|                                                   Candle SnR.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link "http://derptrader.com"
#property description "Ichimoku Levels"
#property indicator_buffers 4
#property indicator_chart_window

extern color   KijunDColor       = clrBeige;
extern color   SenkouAColor      = clrPaleGreen;
extern color   SenkouBColor      = clrPlum;
extern color   Kijun60Color      = clrDeepSkyBlue;
extern int     Days              = 20;
extern int     KS60Time          = 08;
extern int     Tenkan            = 9;                                     // Tenkan period
extern int     Kijun             = 26;                                    // Kijun period
extern int     Senkou            = 52;                                    // Senkou period
extern int     BarToTest         = 0;                                     // Bar to test 

// Indicator buffers
double KDBuffer[];
double SADBuffer[];
double SBDBuffer[];
double K60Buffer[];

// Indicator data
string msIndicatorName;

int      BarShift=0;
double   ratio;
int      TimeFrame;

string Name;

bool mbRunOnce;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
  
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,KDBuffer);
   SetIndexLabel(0,"Daily KS");
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,SBDBuffer);
   SetIndexLabel(1, "Daily SA");
   SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,SBDBuffer);
   SetIndexLabel(2, "Daily SB");
   SetIndexEmptyValue(2,EMPTY_VALUE);   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,K60Buffer);
   SetIndexLabel(3, "60 KS");
   SetIndexEmptyValue(3,EMPTY_VALUE);  

   //---- Set Indicator Name
   msIndicatorName = "Ichimoku Levels ";
   
   mbRunOnce=false;

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   DelLine();
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
//double Range=HighPrice - LowPrice;
   datetime TimeStart;
   datetime TimeEnd;
   double DayOpen;

   if(Volume[0]<3) DelLine();

   for(int x=0; x<Days+1; x++)
     {
      TimeStart=iTime(Symbol(),TimeFrame,x-1);
      TimeEnd=iTime(Symbol(),TimeFrame,x-2);

      double KD   = iIchimoku(Symbol(),PERIOD_D1,Tenkan,Kijun,Senkou,MODE_KIJUNSEN,BarToTest);
      double SAD  = iIchimoku(Symbol(),PERIOD_D1,Tenkan,Kijun,Senkou,MODE_SENKOUSPANA,BarToTest);
      double SAB  = iIchimoku(Symbol(),PERIOD_D1,Tenkan,Kijun,Senkou,MODE_SENKOUSPANB,BarToTest);
      double K60  = iIchimoku(Symbol(),PERIOD_H1,Tenkan,Kijun,Senkou,MODE_KIJUNSEN,BarToTest);

      if(x==1) TimeEnd=TimeStart+60*100*60;

      DayOpen=iOpen(Symbol(),TimeFrame,x-1);

      if(x==1) int ray=1; else ray=0;

       draw(" Daily KS "+x,TimeStart,KD,TimeEnd,KijunDColor,1,ray);
      draw(" Daily SA "+x,TimeStart,SAD,TimeEnd,SenkouAColor,1,ray);
      draw(" Daily SB "+x,TimeStart,SAB,TimeEnd,SenkouBColor,1,ray);
      draw(" 60 KS "+x,TimeStart,K60,TimeEnd,Kijun60Color,1,ray);
     }
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void draw(string Line,datetime TimeStart,double Price,datetime TimeEnd,color line_clr,int line_width,int ray)
  {
   ObjectCreate(Line,OBJ_TREND,0,TimeStart,Price,TimeEnd,Price);
   ObjectSet(Line,OBJPROP_COLOR,line_clr);
   ObjectSet(Line,OBJPROP_STYLE,STYLE_DOT);
   ObjectSet(Line,OBJPROP_WIDTH,line_width);
   ObjectSet(Line,OBJPROP_BACK,True);
   ObjectSet(Line,OBJPROP_RAY,ray);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void stats(string tname,string word,int fsize,string ftype,color tcolor,int posxy,int posx,int posy)
  {
   ObjectCreate(tname,OBJ_LABEL,0,0,0);
   ObjectSetText(tname,word,fsize,ftype,tcolor);
   ObjectSet(tname,OBJPROP_CORNER,posxy);
   ObjectSet(tname,OBJPROP_XDISTANCE,posx);
   ObjectSet(tname,OBJPROP_YDISTANCE,posy);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DelLine()
  {
   for(int x=1; x<Days+1; x++)
     {
      ObjectDelete("OPEN"+x);

      ObjectDelete(" Daily KS "+x);
      ObjectDelete(" Daily SA "+x);
      ObjectDelete(" Daily SB "+x);
      ObjectDelete(" 60KS "+x);
     }
  }
//+------------------------------------------------------------------+

