//+------------------------------------------------------------------+
//|                   MTF_Forex_Freedom_Bar  #MTF Supertrend Bar.mq4 |
//|                                      Copyright © 2006, Eli hayun |
//|  mod.2007 ForexTSD.com ki                http://www.elihayun.com |
//+------------------------------------------------------------------+
//mod2009fxtsd
#property copyright "Copyright © 2006, Eli hayun"
#property link      "http://www.elihayun.com"
//----
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 5
#property indicator_buffers 8
#property indicator_color1 MediumBlue
#property indicator_color2 Red
#property indicator_color3 MediumBlue
#property indicator_color4 Red
#property indicator_color5 MediumBlue
#property indicator_color6 Red
#property indicator_color7 MediumBlue
#property indicator_color8 Red

//---- buffers
double buf4_up[];
double buf4_down[];
double buf3_up[];
double buf3_down[];
double buf2_up[];
double buf2_down[];
double buf1_up[];
double buf1_down[];
//----


extern int  FX_FreedomBar_MODE = 1;
extern string   note_BarMode  = "1CCI 2MACD 3OsMA 4MOM 5RSI 6SAR 7STO" ;

extern int CCI_Period = 14;

extern int  MACD_Fast  = 8;
extern int  MACD_Slow  = 17;
extern int  MACD_SigMA = 9;
extern int  MACD_lineMode   = 0;

extern int Mom_Period = 14;

extern int RSI_Period = 14;

extern double SAR_Step     = 0.02;
extern double SAR_Max      = 0.2;

extern int STO_KPeriod      = 14;
extern int STO_DPeriod      = 3;
extern int STO_Slowing      = 3;
extern int STO_KD_lineMode  = 0;



extern int  price      = 0;
extern string   note_Price    = "0C 1O 2H 3L 4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6; STO&MacdLineMode: 0main,1sig";
//extern string   MA_Method_ = "SMA0 EMA1 SMMA2 LWMA3; LineMode: 0main,1sig";

extern int Period_1=5;
extern int Period_2=15;
extern int Period_3=30;
extern int Period_4=60;

extern bool   AutoDisplay     = 0;
extern string TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN|0-CurrentTF";

extern int    MaxBarsToCount = 1500;
extern double Gap=1; // Gap between the lines of bars

//----
string shortname;
string indName;

bool firstTime=true;
extern int UniqueNum=7;
extern color TextColor = SlateGray;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetAutoDisplay();
    
       {
         if (FX_FreedomBar_MODE<1)FX_FreedomBar_MODE=1;
         if (FX_FreedomBar_MODE>7)FX_FreedomBar_MODE=7;
         
         switch (FX_FreedomBar_MODE)
         {
            case 1: indName = " CCI ("+CCI_Period+") ";  break;
            case 2: indName = " MACD ("+MACD_Fast+","+ MACD_Slow+","+ MACD_SigMA+") ";   break;
            case 3: indName = " OsMA ("+MACD_Fast+","+ MACD_Slow+","+ MACD_SigMA+") ";   break;
            case 4: indName = " MOM ("+Mom_Period+")";   break;
            case 5: indName = " RSI ("+RSI_Period+") ";  break;
            case 6: indName = " SAR ("+DoubleToStr(SAR_Step,3)+","+DoubleToStr(SAR_Max,2)+")"; break;
            case 7: indName = " STO ("+STO_KPeriod+","+STO_DPeriod+","+STO_Slowing+") "; break;
         
         }

       }  
      
      shortname =  "FX_Freedom "+indName+"  ["+Period_1+","+Period_2+","+Period_3+","+Period_4+"] "+UniqueNum;
   
   firstTime=true;

   IndicatorShortName(shortname);

//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,110);
   SetIndexBuffer(0,buf1_up);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,110);
   SetIndexBuffer(1,buf1_down);
   SetIndexEmptyValue(1,0.0);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,110);
   SetIndexBuffer(2,buf2_up);
   SetIndexEmptyValue(2,0.0);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,110);
   SetIndexBuffer(3,buf2_down);
   SetIndexEmptyValue(3,0.0);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4,110);
   SetIndexBuffer(4,buf3_up);
   SetIndexEmptyValue(4,0.0);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,110);
   SetIndexBuffer(5,buf3_down);
   SetIndexEmptyValue(5,0.0);
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6,110);
   SetIndexBuffer(6,buf4_up);
   SetIndexEmptyValue(6,0.0);
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexArrow(7,110);
   SetIndexBuffer(7,buf4_down);
   SetIndexEmptyValue(7,0.0);
   SetIndexLabel(0,"");
   SetIndexLabel(1,"");
   SetIndexLabel(2,"");
   SetIndexLabel(3,"");
   SetIndexLabel(4,"");
   SetIndexLabel(5,"");
   SetIndexLabel(6,"");
   SetIndexLabel(7,"");

if (Period_1<Period()) Period_1=Period();
if (Period_2<Period()) Period_2=Period();
if (Period_3<Period()) Period_3=Period();
if (Period_4<Period()) Period_4=Period();




//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
      int i=0, y15m=0, y30m=0, y1h=0,y4h=0,  yy;
   
   int limit=Bars-counted_bars;
       limit=MathMax(limit,Period_1/Period());
       limit=MathMax(limit,Period_2/Period());
       limit=MathMax(limit,Period_3/Period());
       limit=MathMax(limit,Period_4/Period());
       limit=MathMin(limit,MaxBarsToCount);

   //-------------------------------1----------------------------------------   

   int tf;
   double dif = Time[0] - Time[1];
   for ( i=ObjectsTotal()-1; i>-1; i--)
      {
         if (StringFind(ObjectName(i),"FF_"+UniqueNum+"_") >= 0)  ObjectDelete(ObjectName(i));
      }
      
   double shift = 0.2;
   for ( tf=1; tf<=4; tf++)
      {  
         string txt = "??";
         double gp;
         switch (tf)
         {
            case 1: txt = tf2txt(Period_4);  gp = 1.2 + shift; break;
            case 2: txt = tf2txt(Period_3);  gp = 1.2 + Gap   + shift; break;
            case 3: txt = tf2txt(Period_2);  gp = 1.2 + Gap*2 + shift; break;
            case 4: txt = tf2txt(Period_1);  gp = 1.2 + Gap*3 + shift; break;
         }
        
  
         string name = "FF_"+UniqueNum+"_"+tf+"_"+txt;
         ObjectCreate (name, OBJ_TEXT, WindowFind(shortname), iTime(NULL,0,0)+dif*4, gp);
         ObjectSetText(name, txt,6,"Arial", TextColor);
      }
   
   

//
 
   datetime TimeArray_4H[], TimeArray_1H[], TimeArray_30M[], TimeArray_15M[];
   ArrayCopySeries(TimeArray_4H, MODE_TIME,Symbol(),Period_4);
   ArrayCopySeries(TimeArray_1H, MODE_TIME,Symbol(),Period_3);
   ArrayCopySeries(TimeArray_30M,MODE_TIME,Symbol(),Period_2);
   ArrayCopySeries(TimeArray_15M,MODE_TIME,Symbol(),Period_1);
//----
   for(i=0, y15m=0,  y4h=0,  y1h=0,  y30m=0;i<limit;i++)
     {
      if (Time[i]<TimeArray_4H[y4h])   y4h++;
      if (Time[i]<TimeArray_1H[y1h])   y1h++;
      if (Time[i]<TimeArray_30M[y30m]) y30m++;
      if (Time[i]<TimeArray_15M[y15m]) y15m++;
   
      for(tf=0; tf < 4; tf++)
        {
         int prd;
         switch(tf)
           {
            case 0: prd=Period_4;  yy=y4h;   break;
            case 1: prd=Period_3;  yy=y1h;   break;
            case 2: prd=Period_2;  yy=y30m;  break;
            case 3: prd=Period_1;  yy=y15m;  break;
 
 
           }
//----
   double   ind, zeroL;

   double      cci   =iCCI(NULL, prd, CCI_Period,price, yy);
   double      macd  =iMACD(NULL,prd, MACD_Fast,MACD_Slow,MACD_SigMA,price,MACD_lineMode,yy);
   double      osma  =iOsMA(NULL,prd, MACD_Fast,MACD_Slow,MACD_SigMA,price,yy);
   double      mom   =iMomentum(NULL,prd,Mom_Period,price,yy);
   double      rsi   =iRSI(NULL, prd, RSI_Period,price, yy); 
   double      sar   =iSAR(NULL, prd, SAR_Step,SAR_Max,yy);
   double      sto   =iStochastic(NULL,prd,STO_KPeriod,STO_DPeriod,STO_Slowing,0,0,STO_KD_lineMode, yy); 

   double      close =iClose(NULL,prd,yy);
        
  
         switch (FX_FreedomBar_MODE)
         {
            case 1: ind = cci;   zeroL=0;    break;
            case 2: ind = macd;  zeroL=0;    break;
            case 3: ind = osma;  zeroL=0;    break;
            case 4: ind = mom;   zeroL=100;  break;
            case 5: ind = rsi;   zeroL=50;   break;
            case 6: ind = close; zeroL=sar;  break;
            case 7: ind = sto;   zeroL=50;   break;
         }

      double dUp=EMPTY_VALUE; //   iCustom(NULL, prd, "SuperTrend", false, 0, yy);

         if (ind > zeroL) dUp=1; 

         switch(tf)
           {
            case 0: if (dUp == EMPTY_VALUE)  buf4_down[i] =1;            else buf4_up[i] =1; break;
            case 1: if (dUp == EMPTY_VALUE)  buf3_down[i] =1 + Gap * 1;  else buf3_up[i] =1 + Gap * 1; break;
            case 2: if (dUp == EMPTY_VALUE)  buf2_down[i] =1 + Gap * 2;  else buf2_up[i] =1 + Gap * 2; break;
            case 3: if (dUp == EMPTY_VALUE)  buf1_down[i] =1 + Gap * 3;  else buf1_up[i] =1 + Gap * 3; break;
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
string tf2txt(int tf)
  {
   if (tf==PERIOD_M1)     return("M1");
   if (tf==PERIOD_M5)     return("M5");
   if (tf==PERIOD_M15)    return("M15");
   if (tf==PERIOD_M30)    return("M30");
   if (tf==PERIOD_H1)     return("H1");
   if (tf==PERIOD_H4)     return("H4");
   if (tf==PERIOD_D1)     return("D1");
   if (tf==PERIOD_W1)     return("W1");
   if (tf==PERIOD_MN1)    return("MN");
   return("??");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetValues(int p1, int p2, int p3, int p4)
  {
   Period_1=p1;   Period_2=p2; Period_3=p3; Period_4=p4;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetAutoDisplay()
  {
   if (AutoDisplay)
     {
      switch(Period())
        {
         case PERIOD_M1  :  SetValues(PERIOD_M1,  PERIOD_M5, PERIOD_M15,PERIOD_M30);  break;
         case PERIOD_M5  :  SetValues(PERIOD_M5,  PERIOD_M15,PERIOD_M30,PERIOD_H1 );  break;
         case PERIOD_M15 :  SetValues(PERIOD_M15, PERIOD_M30,PERIOD_H1, PERIOD_H4 );  break;
         case PERIOD_M30 :  SetValues(PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1 );  break;
         case PERIOD_H1  :  SetValues(PERIOD_H1,  PERIOD_H4, PERIOD_D1, PERIOD_W1 );  break;
         case PERIOD_H4  :  SetValues(PERIOD_H4,  PERIOD_D1, PERIOD_W1, PERIOD_MN1);  break;
         case PERIOD_D1  :  SetValues(PERIOD_D1,  PERIOD_W1, PERIOD_MN1,PERIOD_MN1);  break;
         case PERIOD_W1  :  SetValues(PERIOD_W1,  PERIOD_MN1,PERIOD_MN1,PERIOD_MN1);  break;
         case PERIOD_MN1 :  SetValues(PERIOD_MN1, PERIOD_MN1,PERIOD_MN1,PERIOD_MN1);  break;
        }
     }
  }
  
//+------------------------------------------------------------------+


