//+------------------------------------------------------------------+
//|                                                Price Channel.mq4 |
//|                                                                  |
//|                                       http://forex.kbpauk.ru/    |
//+------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  clrRed
#property indicator_color2  clrBlue
#property indicator_color3  clrDodgerBlue
#property strict

//
//
//

input int            ChannelPeriod   = 14;                // Price channel period
input string         DisplayID       = "PC";              // Button ID
input int            btn_Offset_x    = 204;               // Button horizontal position
input int            btn_Offset_y    = 20;                // Button vertical position
input int            btn_Width       = 75;                // Button width
input int            btn_Height      = 20;                // Button height
input int            btn_FontSize    = 10;                // Button font size 

double upBand[],dnBand[],miBand[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
{
   if (ObjectFind(DisplayID)!=0)
   {
      ObjectCreate(ChartID(),DisplayID,OBJ_BUTTON,0,0,0);
         ObjectSetString( ChartID(),DisplayID,OBJPROP_TEXT,"PC on");
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_FONTSIZE,btn_FontSize);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_CORNER,0);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_COLOR,clrWhite);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_BGCOLOR,clrDimGray);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_YDISTANCE,btn_Offset_y);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_XDISTANCE,btn_Offset_x);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_XSIZE,btn_Width);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_YSIZE,btn_Height);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_SELECTED,false);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_HIDDEN,true);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_STATE,true);
   }
   
   SetIndexBuffer(0,upBand,INDICATOR_DATA);
   SetIndexBuffer(1,dnBand,INDICATOR_DATA);
   SetIndexBuffer(2,miBand,INDICATOR_DATA);
   
   if (GetButtonState(DisplayID)!="off")
   {
      SetIndexStyle(0, DRAW_LINE); SetIndexLabel(0, "UpCh");
      SetIndexStyle(1, DRAW_LINE); SetIndexLabel(1, "DownCh");
      SetIndexStyle(2, DRAW_LINE); SetIndexLabel(2, "MidCh");
   }
   else for (int i=0; i<3; i++) SetIndexStyle(i,DRAW_NONE);

   IndicatorSetString(INDICATOR_SHORTNAME,"Price Channel("+(string)ChannelPeriod+")");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{ 
     switch(reason)
     {
         case REASON_PARAMETERS  :
         case REASON_CHARTCHANGE :
         case REASON_RECOMPILE   :
         case REASON_CLOSE       : break;
         default :
         {
            ObjectDelete(DisplayID);
         }                  
      }
}
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
   static string prevState ="";
   if (id==CHARTEVENT_OBJECT_CLICK && sparam==DisplayID)
   {
      string newState = GetButtonState(DisplayID);
         if (newState!=prevState)
         if (newState=="off")
         { 
           SetIndexStyle(0,DRAW_NONE); 
           SetIndexStyle(1,DRAW_NONE); 
           SetIndexStyle(2,DRAW_NONE);
           prevState=newState; 
         }
         else  
         { 
           SetIndexStyle(0,DRAW_LINE);  
           SetIndexStyle(1,DRAW_LINE);  
           SetIndexStyle(2,DRAW_LINE);  
           prevState=newState; 
         }
         ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,"PC " +newState);
   }
}  


//+------------------------------------------------------------------+
//| Price Channel                                                    |
//+------------------------------------------------------------------+

int  OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1;
   
   //
   //
   //
   
   for (; i>=0 && !_StopFlag; i--)
   {
       double lo =  low[ArrayMinimum(low, ChannelPeriod,i)];
       double hi = high[ArrayMaximum(high,ChannelPeriod,i)];
       miBand[i] = ((hi+lo)!=0) ? (hi+lo)*0.5 : 0;
       upBand[i] = hi;
       dnBand[i] = lo;
   }
return(rates_total);
}

//
//
//

string GetButtonState(string whichbutton)
{
       bool selected=ObjectGetInteger(ChartID(),whichbutton,OBJPROP_STATE);
       if (selected)
           { return ("on"); } 
       else
           { return ("off");}
}
