//+------------------------------------------------------------------+
//|                                               _DPF_continous.mq4 |
//|                                 Copyright © 2011, Juergen Rohlfs |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Juergen Rohlfs"
#property link      ""

#property indicator_chart_window
#property  indicator_buffers 2
#property  indicator_color1  LimeGreen
#property  indicator_color2  Red
#property  indicator_color3  Blue
#property  indicator_color4  Yellow
#property  indicator_color5  White
#property  indicator_color6  Orange
#property  indicator_color7  Magenta
#property  indicator_color8  Aqua

#define NEUTRAL 0
#define BUY 1
#define SELL -1

/*extern*/ bool AutoDistance=true;
/*extern*/ int  Distance=25;
/*extern*/ double SL=30;
extern int GMTOffset=3;
extern int    BandsPeriod=20;
extern int    BandsShift=0;
extern double BandsDeviations=1.5;

double   IndBuff0[];
double   IndBuff1[];
double   IndBuff2[];
double   IndBuff3[];
double   IndBuff4[];
double   IndBuff5[];
double   IndBuff6[];
double   IndBuff7[];

int      Signal;
double   LocalPoint;
int refHour;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
string shortName;

   IndicatorBuffers(8);
   SetIndexBuffer(0,IndBuff0);
   SetIndexBuffer(1,IndBuff1);
   SetIndexBuffer(2,IndBuff2);
   SetIndexBuffer(3,IndBuff3);
   SetIndexBuffer(4,IndBuff4);
   SetIndexBuffer(5,IndBuff5);
   SetIndexBuffer(6,IndBuff6);
   SetIndexBuffer(7,IndBuff7);
//---- indicators
   IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS));
   
   SetIndexLabel(0,"Upper boundary");
   SetIndexLabel(1,"Lower boundary");
   SetIndexLabel(2,"High-SL");
   SetIndexLabel(3,"Low+SL");
   SetIndexLabel(4,"S1");
   SetIndexLabel(5,"S2");
   SetIndexLabel(6,"S3");
   SetIndexLabel(7,"Label7");
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_NONE);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_NONE);
   SetIndexStyle(6,DRAW_NONE);
   SetIndexStyle(7,DRAW_NONE);


   LocalPoint=Point*GetDigitAdjustFactor();
   SL*=LocalPoint;
   
   if(AutoDistance)
      CalcDistance();
//----
   if(GMTOffset<0)
      refHour=24+GMTOffset;
   else
      refHour=GMTOffset;
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit,counted_bars=IndicatorCounted();
   
   if(counted_bars>0) counted_bars--;
   limit=Bars-1-counted_bars;

   for(int bar=limit; bar>=0; bar--)
   {
      if(TimeHour(Time[bar])==refHour)
      {
         IndBuff0[bar]=iCustom(Symbol(),PERIOD_H1,"Bands",BandsPeriod,BandsShift,BandsDeviations,1,bar);   //High
         IndBuff1[bar]=iCustom(Symbol(),PERIOD_H1,"Bands",BandsPeriod,BandsShift,BandsDeviations,2,bar);   //Low
      }
      else
      {
         IndBuff0[bar]=IndBuff0[bar+1];   //High
         IndBuff1[bar]=IndBuff1[bar+1];   //Low
      
      }
   }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
int GetDigitAdjustFactor()
{
   if(Digits==5 || Digits==3)
      return(10);
   else
      return(1);
}

bool IsNewBar(int theBar)
{
static datetime barTime=0;

   if(barTime<Time[theBar])
   {
      barTime=Time[theBar];
      return(true);
   }
   return(false);
}

void CalcDistance()
{
   switch(Period())
   {
      case PERIOD_M1:
         Distance=5;
         break;
      case PERIOD_M5:
         Distance=5;
         break;
      case PERIOD_M15:
         Distance=7;
         break;
      case PERIOD_M30:
         Distance=10;
         break;
      case PERIOD_H1:
         Distance=20;
         break;
      case PERIOD_H4:
         Distance=50;
         break;
      case PERIOD_D1:
         Distance=100;
         break;
      case PERIOD_W1:
         Distance=100;
         break;
      default:
         Distance=100;
         break;
   }
}


