// Id: 16823
//+------------------------------------------------------------------+
//|                                                   BTF_Source.mq4 |
//|                             Copyright (c) 2016, Gehtsoft USA LLC | 
//|                                            http://fxcodebase.com |
//|                                   Paypal: https://goo.gl/9Rj74e  | 
//+------------------------------------------------------------------+
//|                                      Developed by : Mario Jemic  |                    
//|                                          mario.jemic@gmail.com   |
//|                   BitCoin : 15VCJTLaz12Amr7adHSBtL9v8XomURo9RF   |
//+------------------------------------------------------------------+

#property indicator_chart_window

enum e_cycles{ Min_5=1, Min_15=2, Min_30=3, Min_60=4, Min_240=5, Daily=6, Weekly=7, Monthly=8 };
enum e_method{ Current=1, Previous = 2 };

input        e_cycles BTF          = Daily;
input        e_method Method       = Current;
extern int   Number_Of_BTF_Candles = 20;
extern bool  Show_Labels           = true;
extern bool  Draw_Cycles_Separator = true;
extern color Open_Color            = clrDarkGray;
extern color High_Color            = clrRed;
extern color Low_Color             = clrLime;
extern color Close_Color           = clrBlue;
extern int   Lines_Style           = 0;
extern int   Lines_Width           = 2;
extern color BTF_Separator         = clrDimGray;

int Periodo, Minutes;

string IndicatorName;
string IndicatorObjPrefix;

string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}

int init(){
   IndicatorName = GenerateIndicatorName("Bigger TF Source");
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(IndicatorName);
   if (Check()) Alert("The Bigger TF Source selected for this Time Frame cannot be calculated");
   
   return(0);
}

int deinit(){
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
   return(0);
}

int start()
  {
   
   int i, bar;
   double OPEN, HIGH, LOW, CLOSE;
   datetime Line_Start, Line_End;
   bool Draw_Label;
   
   if (Check()==false){
   
   for(i=Number_Of_BTF_Candles; i>=0; i--){
      
      if (BTF==1){ Periodo = PERIOD_M5;  Minutes = 5;     }
      if (BTF==2){ Periodo = PERIOD_M15; Minutes = 15;    }
      if (BTF==3){ Periodo = PERIOD_M30; Minutes = 30;    }
      if (BTF==4){ Periodo = PERIOD_H1;  Minutes = 60;    }
      if (BTF==5){ Periodo = PERIOD_H4;  Minutes = 240;   }
      if (BTF==6){ Periodo = PERIOD_D1;  Minutes = 1440;  }
      if (BTF==7){ Periodo = PERIOD_W1;  Minutes = 10080; }
      if (BTF==8){ Periodo = PERIOD_MN1; Minutes = 43200; }
      
      if (Method==1) bar = i; else bar = i+1;
      
      // Calculations
      OPEN = iOpen(NULL,Periodo,bar);
      HIGH = iHigh(NULL,Periodo,bar);
      LOW = iLow(NULL,Periodo,bar);
      CLOSE = iClose(NULL,Periodo,bar);
      
      // Drawing the Lines
      Line_Start = iTime(NULL,Periodo,i);
      if (i==0){
         Line_End = iTime(NULL,Periodo,i)+(1*(Minutes*60));
         Draw_Label = true;
      }else{
         Line_End = iTime(NULL,Periodo,(i-1));
         Draw_Label = false;
      }
      
      Pivot("OPEN"+i,Line_Start,OPEN,Line_End, Open_Color,3, STYLE_SOLID,Draw_Label);
      Pivot("HIGH"+i,Line_Start,HIGH,Line_End, High_Color,2, STYLE_SOLID,Draw_Label);
      Pivot("LOW"+i,Line_Start,LOW,Line_End, Low_Color,2, STYLE_SOLID,Draw_Label);
      Pivot("CLOSE"+i,Line_Start,CLOSE,Line_End, Close_Color,2, STYLE_SOLID,Draw_Label);
      
      if (Draw_Cycles_Separator) Separator("Sep"+i, Line_Start, BTF_Separator, 0, STYLE_DOT);
      
   }
   
   } // if Check==false
   
//----
   return(0);
}

void Pivot(string Nombre, datetime tiempo1, double precio1, datetime tiempo2, color bpcolor, int ancho, int style, bool draw_text){
   ObjectDelete(IndicatorObjPrefix + Nombre);
   ObjectCreate(IndicatorObjPrefix + Nombre, OBJ_TREND, 0, tiempo1, precio1, tiempo2, precio1);
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_COLOR, bpcolor);
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_STYLE, style);
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_WIDTH, ancho);
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_RAY, False);
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_BACK, true );
   if (Show_Labels && draw_text){
      ObjectDelete(IndicatorObjPrefix + "T"+Nombre);
      ObjectCreate(IndicatorObjPrefix + "T"+Nombre, OBJ_TEXT, 0, tiempo2+(2*Period()*60), precio1 );
      ObjectSetText(IndicatorObjPrefix + "T"+Nombre, StringSubstr(Nombre,0,StringLen(Nombre)-1), 10, "Arial", bpcolor );
      ObjectSet(IndicatorObjPrefix + "T"+Nombre, OBJPROP_TIME1, tiempo2+(2*Period()*60));
      ObjectSet(IndicatorObjPrefix + "T"+Nombre, OBJPROP_PRICE1, precio1);
   }
}

// Draw Separator
void Separator(string Nombre, datetime tiempo1, color sesscolor, int ancho, int style){
   ObjectDelete(IndicatorObjPrefix + Nombre);
   ObjectCreate(IndicatorObjPrefix + Nombre, OBJ_VLINE, 0, tiempo1, WindowPriceMax());
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_COLOR, sesscolor);
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_STYLE, style);
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_WIDTH, ancho);
   ObjectSet(IndicatorObjPrefix + Nombre, OBJPROP_BACK, True);
}

bool Check (){
   
   bool wrong_tf = false;
   
   if (Period()==5     && BTF<2) wrong_tf = true;
   if (Period()==15    && BTF<3) wrong_tf = true;
   if (Period()==30    && BTF<4) wrong_tf = true;
   if (Period()==60    && BTF<5) wrong_tf = true;
   if (Period()==240   && BTF<6) wrong_tf = true;
   if (Period()==1440  && BTF<7) wrong_tf = true;
   if (Period()==10080 && BTF<8) wrong_tf = true;
   if (Period()==43200)          wrong_tf = true;
   
   return(wrong_tf);
   
}