//+------------------------------------------------------------------+
//|                                           zelenko regression.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright (c) 2013, David Louisson"
#property link      "http://www.forexfactory.com/hanover"

#property indicator_chart_window

#include <hanover --- function header (np).mqh>

extern int        FontSize                   = 12;
extern string     FontID                     = "Arial";
extern color      FontColor                  = White;
extern string     RefreshPeriod              = "+0";

string     sym, IndiName;
int        dig, tmf, vis, wno, RefreshEveryXMins;
double     spr, pnt;
datetime   prev_time;
bool       FirstTime;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndiName = "@ZR-" + GetUniqueInt();
  IndicatorShortName(IndiName);

  sym     = Symbol();
  tmf     = Period();
  pnt     = MarketInfo(sym,MODE_POINT);
  dig     = MarketInfo(sym,MODE_DIGITS);
  spr     = MarketInfo(sym,MODE_SPREAD);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
  } 
  
  RefreshEveryXMins = StrToTF(RefreshPeriod);
  prev_time = -9999;
  del_obj();
  plot_obj();
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  if (RefreshEveryXMins < 0)  {
    if (FirstTime)  {
      del_obj();
      plot_obj();
    }
    FirstTime = false;      
    return(0);
  }  
  if (RefreshEveryXMins == 0) {
    del_obj();
    plot_obj();    
  } else {
    if (prev_time != iTime(sym,RefreshEveryXMins,0))  {
      del_obj();
      plot_obj();
      prev_time = iTime(sym,RefreshEveryXMins,0);
  } }      
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
void plot_obj()   {
//+------------------------------------------------------------------+
  for (int i=0; i<ObjectsTotal(); i++)  {
    string name = ObjectName(i);
    if (ObjectType(name)  != OBJ_REGRESSION)                continue;
    int bar1 = iBarShift(sym,tmf,ObjectGet(name,OBJPROP_TIME1),false);
    int bar0 = iBarShift(sym,tmf,ObjectGet(name,OBJPROP_TIME2),false);
    if (bar0<0 || bar1<0)                                   continue;
    double price1  = ObjectGet(name,OBJPROP_PRICE1);
    double price0  = ObjectGet(name,OBJPROP_PRICE2);
    int    bars    = MathAbs(bar1-bar0);
    double width   = 0;
    for (int j=0; j<=bars; j++)  {
      double median_price = price1+(price0-price1)*DivZero(j,bars);
      double close        = Close[bar1-j];
      width               = MathMax(width,MathAbs(close-median_price));
//      log(j,bar1-j,median_price,close,width);
    }
    string   text = NumberToStr(2*width/pnt,"'Channel width = 'TR-5' pips'") + NumberToStr(MathAbs(price1-price0)/pnt,"' / angle = 'TR-5' pips'");
    datetime objt = Time[(bar1+bar0)/2];
    double   objp = MathMin(price1,price0)-width;
//    ObjectSetText(name,comm,12);
    PlotText  (IndiName+NumberToStr(i,"'-'Z4"), true, 0, objt, objp, text, FontColor, FontSize, FontID, 0.0, false, 0);      // Plot text
    i++;    
  }   
  return(0);
}

//+------------------------------------------------------------------+
#include <hanover --- extensible functions (np).mqh>