//+------------------------------------------------------------------+
//|                                                    Fibonacci.mq5 |
//+------------------------------------------------------------------+
//|                                                    Fibonacci.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_plots 0
//--- input parameters
input bool Y = false; 
input ENUM_TIMEFRAMES pr = PERIOD_W1;

string prefix = "Fibonacci_";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0,prefix);
   ChartRedraw(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
if (Y == false)
{
   double H1 = iHigh(_Symbol,pr,1);
   double L1 = iLow(_Symbol,pr,1);
   double O1 = iOpen(_Symbol,pr,0);
   double D1 = H1 - L1;
   ShowFib(H1,L1,D1,O1);
}
else
{
   double H2 = iHigh(_Symbol,PERIOD_MN1,iHighest(_Symbol,PERIOD_MN1,MODE_HIGH,12,Month()));
   double L2 = iLow(_Symbol,PERIOD_MN1,iLowest(_Symbol,PERIOD_MN1,MODE_LOW,12,Month()));
   double O2 = iOpen(_Symbol,PERIOD_MN1,Month()-1);
   double D2 = H2 - L2;
   ShowFib(H2,L2,D2,O2);
}  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void ShowFib(double H, double L, double D, double OP)
{
   LN (prefix + " + 300 " , H + ( 2 * D) , White );
   LN (prefix + " + 276.4 " , H + ( 1.764 * D) , White );
   LN (prefix +  " + 261.8 " , H + ( 1.618 * D) , White );
   LN (prefix +  " + 250 " , H + ( 1.5 * D) , White );
   LN (prefix +  " + 238.2 " , H + ( 1.382 * D) , White );
   LN (prefix +  " + 223.6 " , H + ( 1.236 * D) , White );
   LN (prefix +  " + 200 " , H + ( 1 * D) , White );
   LN (prefix +  " + 176.4 " , H + ( 0.764 * D) , White );
   LN (prefix +  " + 161.8 " , H + ( 0.618 * D) , White );
   LN (prefix +  " + 150 " , H + ( 0.5 * D) , White );
   LN (prefix +  " + 138.2 " , H + ( 0.382 * D) , White );
   LN (prefix +  " + 123.6 " , H + ( 0.236 * D) , White );
   
   LN (prefix +  " + 100 " , H , White );
   
   LN (prefix +  " + 76.4 " , L + ( 0.764 * D) , White );
   LN (prefix +  " + 61.8 " , L + ( 0.618 * D) , White );
   LN (prefix +  " + 50 " , L + ( 0.5 * D) , White );
   LN (prefix +  " + 38.2 " , L + ( 0.382 * D) , White );
   LN (prefix +  " + 23.6 " , L + ( 0.236 * D) , White );
   
   LN (prefix +  " + 0 " , L , White );
   LN (prefix +  " Open " , OP , White );
   
   LN (prefix +  " - 100 " , L - ( 1 * D) , White );
   LN (prefix +  " - 76.4 " , L - ( 0.764 * D) , White );
   LN (prefix +  " - 61.8 " , L - ( 0.618 * D) , White );
   LN (prefix +  " - 50 " , L - ( 0.5 * D) , White );
   LN (prefix +  " - 38.2 " , L - ( 0.382 * D) , White );
   LN (prefix +  " - 23.6 " , L - ( 0.236 * D) , White );

   
   LN (prefix +  " - 200 " , L - ( 2 * D) , White );
   LN (prefix +  " - 176.4 " , L - ( 1.764 * D) , White );
   LN (prefix +  " - 161.8 " , L - ( 1.618 * D) , White );
   LN (prefix +  " - 150 " , L - ( 1.5 * D) , White );
   LN (prefix +  " - 138.2 " , L - ( 1.382 * D) , White );
   LN (prefix +  " - 123.6 " , L - ( 1.236 * D) , White );
}
//+------------------------------------------------------------------+
void Upper(double op, double hg, double lw, double at)
{
   LN (prefix +  " R +61.8 " , lw + ( 0.618 * at )  , Orchid );
}
//+------------------------------------------------------------------+
void Lower(double op, double hg, double lw, double at)
{
   LN (prefix +  " R -61.8 " , hg - ( 0.618 * at )  , Orchid );
}
//+------------------------------------------------------------------+
void LN ( string nm , double val , color cl)
{
   if(ObjectFind(0,nm) == -1)
   ObjectCreate ( 0 , nm , OBJ_TREND , 0 , 0 , 0 ) ;
   ObjectSetDouble(0,nm,OBJPROP_PRICE,0,val);
   ObjectSetDouble(0,nm,OBJPROP_PRICE,1,val);
   ObjectSetInteger(0,nm,OBJPROP_TIME,0,iTime(_Symbol,pr,0));
   ObjectSetInteger(0,nm,OBJPROP_TIME,1,iTime(_Symbol,PERIOD_CURRENT,0)+13*PeriodSeconds(PERIOD_CURRENT));
   ObjectSetInteger(0, nm, OBJPROP_COLOR, cl);
   ObjectSetInteger(0, nm, OBJPROP_RAY, false);
   ObjectSetInteger(0, nm, OBJPROP_SELECTED, false);
   ObjectSetInteger(0, nm, OBJPROP_COLOR, clrDimGray);
   string nmL = nm + "_Label";
   string c = StringSubstr(nm, StringLen(prefix)) + " = " + DoubleToString(val, _Digits);
   if(ObjectFind(0,nmL) == -1)
   ObjectCreate ( 0 , nmL , OBJ_TEXT , 0 , iTime(_Symbol,PERIOD_CURRENT,0)+13*PeriodSeconds(PERIOD_CURRENT) , val );
   ObjectSetInteger(0, nmL, OBJPROP_TIME, 0, iTime(_Symbol,PERIOD_CURRENT,0)+13*PeriodSeconds(PERIOD_CURRENT));
   ObjectSetDouble(0, nmL, OBJPROP_PRICE, 0, val);
   ObjectSetString(0, nmL, OBJPROP_TEXT, c);
   ObjectSetString(0, nmL, OBJPROP_FONT, "Tahoma");
   ObjectSetInteger(0, nmL, OBJPROP_FONTSIZE, 12);
   ObjectSetInteger(0, nmL, OBJPROP_COLOR, clrOrchid);
      
}
//+------------------------------------------------------------------+
