
#property copyright "Don Isbell"
#property link      "disbellj@gmail.com"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2 DeepSkyBlue
#property indicator_color3 DeepSkyBlue
#property indicator_color4 Red
#property indicator_color5 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2

extern bool Colorize = False;
extern color ColorUp = DeepSkyBlue;
extern color ColorDown = Red;
extern color ColorNeutral = White;
extern int eintTimeFrame = 0;
extern int eintHalfLength = 25;
extern double edblATRMultiplier = 1.0;
extern int eintATRPeriod = 50;
extern int eintBarsToProcess = 0;
extern bool eblnAlerts = false;

double Pip;
int LotsRoundTo,RoundTo,j,k;
string shortname,LastArrow,Trend;

double Buffer[];
double lsmaua[];
double lsmaub[];
double lsmada[];
double lsmadb[];
double OPEN;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator

   IndicatorShortName("Colored Hourly Open Don");
  
   IndicatorBuffers(5);
  
   SetIndexBuffer(0,Buffer);
   if(Colorize == True) {
      SetIndexBuffer(1,lsmaua);
      SetIndexBuffer(2,lsmaub);
      SetIndexBuffer(3,lsmada);
      SetIndexBuffer(4,lsmadb);
   }
   
   //Figure out what a pip and its value is on this broker
   if(Point == 0.01 || Point == 0.0001) {
      Pip = Point;
   } else if(Point == 0.001 || Point == 0.00001) {
      Pip = Point*10;
   } 
      
   if(Point == 0.01) {
      RoundTo = 2;
   } else if(Point == 0.001) {
      RoundTo = 3;
   } else if(Point == 0.0001) {
      RoundTo = 4;
   } else if(Point == 0.00001) {
      RoundTo = 5;
   }

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll();
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   DrawDonTrendLine();   
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
void DrawDonTrendLine() {
   int      counted_bars=IndicatorCounted();
   int      limit,i,dayi;

   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit = MathMin(Bars-counted_bars,Bars-1);
   if(Colorize == True) {
      if (Buffer[limit] > Buffer[limit+1]) CleanPoint(limit,lsmaua,lsmaub);
      if (Buffer[limit] < Buffer[limit+1]) CleanPoint(limit,lsmada,lsmadb);
   }
   for(i = limit; i >= 0; i--)
   {
      dayi = iBarShift(Symbol(), PERIOD_H1, Time[i], false);
      OPEN = iOpen(Symbol(), PERIOD_H1, dayi);    
      Buffer[i] = OPEN;
      if(Colorize == True) {
         lsmaua[i] = EMPTY_VALUE;
         lsmaub[i] = EMPTY_VALUE;
         lsmada[i] = EMPTY_VALUE;
         lsmadb[i] = EMPTY_VALUE;
         if (Buffer[i] > Buffer[i+1] || (Buffer[i] == Buffer[i+1] && Close[i] > Buffer[i])) PlotPoint(i,lsmaua,lsmaub,Buffer);
         if (Buffer[i] < Buffer[i+1] || (Buffer[i] == Buffer[i+1] && Close[i] < Buffer[i])) PlotPoint(i,lsmada,lsmadb,Buffer);
      }
   }
   
}

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}