//+------------------------------------------------------------------+
//|                                                   mb_Fibo_v1.mq4 |
//|                                           Copyright 2013, Пашка. |
//|                                         http://marketbreath.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, Пашка."
#property link      "http://marketbreath.com/"

#property indicator_chart_window

extern double Depo = 100;
extern double Risk = 5;
extern color  col_fibo = Orange;
extern bool   lev_23_6 = False;
extern bool   lev_61_8 = False;

double lot;
double price0, price100;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  if (iAO(Symbol(),0,1)<0)
  {
  double pr1 = iHigh(Symbol(), 0, (iHighest(Symbol(), 0, MODE_HIGH, 50, 1)));
  double pr2 = iLow(Symbol(), 0, (iLowest(Symbol(), 0, MODE_LOW, 50, 1)));
  }
  else
  {
  pr2 = iHigh(Symbol(), 0, (iHighest(Symbol(), 0, MODE_HIGH, 50, 1)));
  pr1 = iLow(Symbol(), 0, (iLowest(Symbol(), 0, MODE_LOW, 50, 1)));
  }
  if (ObjectFind("mb_FIbo") == -1)
  {
   ObjectCreate("mb_Fibo", OBJ_FIBO, 0, Time[0], pr2, Time[50], pr1);
   ObjectSet("mb_Fibo", OBJPROP_RAY, false);
   if (lev_23_6 == false && lev_61_8 == false)ObjectSet("mb_Fibo",OBJPROP_FIBOLEVELS,4 );
   if (lev_23_6 == true && lev_61_8 == false)ObjectSet("mb_Fibo",OBJPROP_FIBOLEVELS,5 );
   if (lev_23_6 == false && lev_61_8 == true)ObjectSet("mb_Fibo",OBJPROP_FIBOLEVELS,5 );
   if (lev_23_6 == true && lev_61_8 == true)ObjectSet("mb_Fibo",OBJPROP_FIBOLEVELS,6 );
   ObjectSet                ("mb_Fibo" , OBJPROP_FIRSTLEVEL + 0 , 0);
   ObjectSetFiboDescription ("mb_Fibo" , 0, "(%$)    0   ");
   ObjectSet                ("mb_Fibo" , OBJPROP_FIRSTLEVEL + 1 , 1);
   ObjectSetFiboDescription ("mb_Fibo" , 1, "(%$)    100   ");
   ObjectSet                ("mb_Fibo" , OBJPROP_FIRSTLEVEL + 2 , 1.61);
   ObjectSetFiboDescription ("mb_Fibo" , 2, "(%$)    161   ");
   ObjectSet                ("mb_Fibo" , OBJPROP_FIRSTLEVEL + 3 , 2);
   ObjectSetFiboDescription ("mb_Fibo" , 3, "(%$)    200   ");
   if (lev_23_6 == true)
   {
    ObjectSet                ("mb_Fibo" , OBJPROP_FIRSTLEVEL + 4 , 0.236);
    ObjectSetFiboDescription ("mb_Fibo" , 4, "    23.6   ");
   }
   if (lev_61_8 == true && lev_23_6 == false)
   {
    ObjectSet                ("mb_Fibo" , OBJPROP_FIRSTLEVEL + 4 , 0.618);
    ObjectSetFiboDescription ("mb_Fibo" , 4, "    61.8   ");
   }
   if (lev_61_8 == true && lev_23_6 == true)
   {
    ObjectSet                ("mb_Fibo" , OBJPROP_FIRSTLEVEL + 5 , 0.618);
    ObjectSetFiboDescription ("mb_Fibo" , 5, "    61.8   ");
   }
   ObjectSet("mb_Fibo", OBJPROP_LEVELCOLOR, col_fibo);
  }
  
  if (ObjectFind("link")==-1)
  {
   ObjectCreate("link", OBJ_LABEL, 0, 0,0,0);
   ObjectSetText("link", "marketbreath.com", 8 , "Verdana" , Green);
   ObjectSet("link", OBJPROP_XDISTANCE, 5);
   ObjectSet("link", OBJPROP_YDISTANCE, 5);
   ObjectSet("link", OBJPROP_CORNER, 2);
  }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   if (ObjectFind("mb_FIbo") != -1) ObjectDelete("mb_Fibo");
   if (ObjectFind("link") != -1) ObjectDelete ("link");
   Comment ("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double p0, p100, tik;
   int point;
//----
   if (ObjectFind("mb_FIbo") != -1)
   {
    p0 = ObjectGet("mb_Fibo", OBJPROP_PRICE1);
    p100 = ObjectGet("mb_Fibo", OBJPROP_PRICE2);
    
    if (p0 != price0 || p100 != price100)
    {
     double l161, l200, r161, r200;
     price0 = p0;
     price100 = p100;
     tik = MarketInfo(Symbol(),MODE_TICKVALUE);
     if (price0>price100) point = (price0-price100)/Point;
     if (price100>price0) point = (price100-price0)/Point;
     lot = NormalizeDouble((Risk*(Depo/100))/(tik*point), 2);
     double l = MathMod(lot/MarketInfo(Symbol(),MODE_MINLOT), 2);
     if (l!=1)
     {
      l161 = NormalizeDouble(lot/2, 2);
      l200 = l161;
     }
     if (l==1)
     {
      l161 = NormalizeDouble((lot+(l*MarketInfo(Symbol(),MODE_MINLOT)))/2, 2);
      l200 = NormalizeDouble(l161-(l*MarketInfo(Symbol(),MODE_MINLOT)), 2);
     }
     double r    = ((tik*point*lot)/Depo)*100;
            r161 = ((tik*point*l161)/Depo)*100;
            r200 = ((tik*point*l200)/Depo)*100;
     Comment("Суммарный лот =  ", DoubleToStr(lot, 2), "    ",
             "Риск =  ", DoubleToStr(r, 2), " %", "\n", 
             "Лот1 =  ", DoubleToStr(l161, 2) , "    ", 
             "Риск =  ", DoubleToStr(r161, 2), " %", "\n",
             "Лот2 =  ", DoubleToStr(l200, 2) , "    ", 
             "Риск =  ", DoubleToStr(r200, 2), " %"
             );
    }
    
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+