//+------------------------------------------------------------------+
//|Хэдж индикатор с выбором периода выравнивания     HedgePeriod.mq4 |
///инструменты сравниваются на прошлой свече выбранного периода      |
//|                               Copyright © 2010, Vladimir Hlystov |
//|                                         http://cmillion.narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Vladimir Hlystov"
#property link      "http://cmillion.narod.ru"

#property indicator_separate_window
//#property indicator_minimum -100
//#property indicator_maximum 100
#property indicator_level1	0
#property indicator_levelcolor	Silver
#property indicator_levelwidth	0
#property indicator_level2	100
#property indicator_level3	-100
#property indicator_levelstyle	2
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_style1 0
#property indicator_width1 2  
#property indicator_color2 Blue
#property indicator_style2 2
#property indicator_width2 1  
#property indicator_color3 Red
#property indicator_style3 2
#property indicator_width3 1  
//+------------------------------------------------------------------+
extern string SYMBOL2         = "";
extern string _period_        = "0-H1  1-H4  2-D1  3-W1  4-MN1";
extern int Period_Comparison  = 2;                                //период на котором происходит сравнение инструментов час день неделя месяц
extern string _Draw_          = "0-no  1-Candle  2-Line";
extern int Draw               = 0;                                //1-свечи 2-стредняя  0-ничего не рисовать
extern int averaging          = 5;                                //усреднение по N свечей
//+------------------------------------------------------------------+
string SYMBOL1;
double A[];
double B[];
double Delta[];
string Name;
int DigitS1,DigitS2,PeriodComparison;
datetime TimeBar;
//+------------------------------------------------------------------+
int init()
  {
   SYMBOL1 = Symbol();
   switch(Period_Comparison)
   {
   case 0:
      PeriodComparison=60;
      break;
   case 1:
      PeriodComparison=240;
      break;
   case 2:
      PeriodComparison=1440;
      break;
   case 3:
      PeriodComparison=10080;
      break;
   case 4:
      PeriodComparison=43200;
      break;
   default:
      PeriodComparison=43200;
      Alert("NOT PeriodComparison. it is exposed MN1");
      break;
   }
   //+------------------------------------------------------------------+
   DigitS1 = MarketInfo(SYMBOL1,MODE_DIGITS);
   DigitS2 = MarketInfo(SYMBOL2,MODE_DIGITS);
   IndicatorBuffers(3);
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
   SetIndexBuffer(0, Delta);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1, A);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2, B);
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);
   IndicatorDigits(1);
   Name = StringConcatenate("HedgePeriod (",SYMBOL1," ",SYMBOL2," ",string_p(PeriodComparison),") ");
   IndicatorShortName(Name);
   return(0);
  }
//+------------------------------------------------------------------+
int deinit()
  {
   if (Draw!=0) ObjectsAllDelete(SYMBOL2);
   return(0);
  }
//+------------------------------------------------------------------+
int start()
{
   double High_Win,Low_Win,H,L;
   double OpenBar,CloseBar,LowBar,HighBar;
   string nameCandle;
   int Digit = MarketInfo(SYMBOL2,MODE_DIGITS);
   if (Draw!=0) ObjectsAllDelete(SYMBOL2);
   double d,A_old;
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   int limit=WindowFirstVisibleBar();//Bars-counted_bars;
   for(int i=limit; i>=0; i--)
   {
      High_Win = iHigh(SYMBOL1,PeriodComparison,iBarShift(SYMBOL1,PeriodComparison,iTime(SYMBOL1,0,i),false)+1);
      Low_Win  = iLow (SYMBOL1,PeriodComparison,iBarShift(SYMBOL1,PeriodComparison,iTime(SYMBOL1,0,i),false)+1);
      H        = iHigh(SYMBOL2,PeriodComparison,iBarShift(SYMBOL2,PeriodComparison,iTime(SYMBOL2,0,i),false)+1);
      L        = iLow (SYMBOL2,PeriodComparison,iBarShift(SYMBOL2,PeriodComparison,iTime(SYMBOL2,0,i),false)+1);
      
      A[i]= (iMA(SYMBOL2,0,averaging,0,MODE_SMA,PRICE_WEIGHTED,i) -L)/(H-L)*200-100; 
      B[i]= (iMA(SYMBOL1,0,averaging,0,MODE_SMA,PRICE_WEIGHTED,i) -Low_Win)/(High_Win-Low_Win)*200-100;
      Delta[i]=A[i]-B[i];
      if (i<=WindowFirstVisibleBar())
      {
         if (Draw!=0)
         {
            OpenBar = iOpen (SYMBOL2,0,i);
            CloseBar= iClose(SYMBOL2,0,i);
            LowBar  = iLow  (SYMBOL2,0,i);
            HighBar = iHigh (SYMBOL2,0,i);
            if (i==0) nameCandle=SYMBOL2;
            else nameCandle=StringConcatenate(SYMBOL2," ",TimeToStr(Time[i],TIME_MINUTES),"\n",
                                             "O ",DoubleToStr(OpenBar,Digit),"\n",
                                             "H ",DoubleToStr(HighBar,Digit),"\n",
                                             "L ",DoubleToStr(LowBar,Digit),"\n",
                                             "C ",DoubleToStr(CloseBar,Digit));
            OpenBar =(OpenBar -L)/(H-L)*(High_Win-Low_Win)+Low_Win;
            CloseBar=(CloseBar-L)/(H-L)*(High_Win-Low_Win)+Low_Win;
            LowBar  =(LowBar  -L)/(H-L)*(High_Win-Low_Win)+Low_Win;
            HighBar =(HighBar -L)/(H-L)*(High_Win-Low_Win)+Low_Win;
            if (Draw==1)
            {
               ObjectCreate(nameCandle, OBJ_TREND,0,Time[i],LowBar,Time[i],HighBar,0,0);
               ObjectSet   (nameCandle, OBJPROP_WIDTH, 1);
               if (OpenBar>CloseBar) ObjectSet   (nameCandle, OBJPROP_COLOR, indicator_color2);  
               else ObjectSet   (nameCandle, OBJPROP_COLOR, indicator_color3);  
               ObjectSet   (nameCandle, OBJPROP_RAY,   false);
               nameCandle=StringConcatenate(nameCandle," Body");
               //ObjectDelete(nameCandle);
               ObjectCreate(nameCandle, OBJ_TREND,0,Time[i],OpenBar,Time[i],CloseBar,0,0);
               ObjectSet   (nameCandle, OBJPROP_WIDTH, 4);
               if (OpenBar>CloseBar) ObjectSet   (nameCandle, OBJPROP_COLOR, indicator_color2);  
               else ObjectSet   (nameCandle, OBJPROP_COLOR, indicator_color3);  
               ObjectSet   (nameCandle, OBJPROP_RAY,   false);
            }
            if (Draw==2)
            {
               double A_tek=(OpenBar+CloseBar+LowBar+HighBar)/4;
               if (A_old!=0)
               {
                  ObjectCreate(nameCandle, OBJ_TREND,0,Time[i+1],A_old,Time[i],A_tek,0,0);
                  ObjectSet   (nameCandle, OBJPROP_WIDTH, 1);
                  ObjectSet   (nameCandle, OBJPROP_COLOR, indicator_color2);  
                  ObjectSet   (nameCandle, OBJPROP_RAY,   false);
               }
               A_old=A_tek;
            }
         }
      }
   }
   return(0);

}
//+------------------------------------------------------------------+
int ObjectsAllDelete(string txt)
{
   for(int n=ObjectsTotal()-1; n>=0; n--) 
     {
      string Obj_Name=ObjectName(n);
      if (StringFind(Obj_Name,txt,0) != -1)
      {
         ObjectDelete(Obj_Name);
      }
   }
   return;
}//+------------------------------------------------------------------+
string string_p(int per)
{
   if (per == 60)    return(" H1 ");
   if (per == 240)   return(" H4 ");
   if (per == 1440)  return(" D1 ");
   if (per == 10080) return(" W1 ");
   if (per == 43200) return(" MN1 ");
return("error period");
}
//+------------------------------------------------------------------+