//+------------------------------------------------------------------+
//| BOLLINGER+STARC (BollStarc-TC.mq4)                               |
//| Copyright © 2006                                                 |
//| http://www.                                                      |
//+------------------------------------------------------------------+
//+sigArrows
#include <hanover --- function header (np).mqh>

#property copyright "Copyright © 2006, "
#property link "http://www.   "

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 DimGray // Starc midband
#property indicator_color2 Yellow //Starc upper
#property indicator_color3 Yellow
#property indicator_color4 RoyalBlue //BBupper
#property indicator_color5 RoyalBlue


#property indicator_color6 Blue 
#property indicator_color7 Red
#property indicator_color8 Orange

#property  indicator_width6  1
#property  indicator_width7  1
#property  indicator_width8  2

//---- indicator parameters
extern int     BB_Period        = 20;
extern int     BB_Deviations    = 2;
extern int     MA_Period        = 13;   //6
extern int     ATR_Period       = 21;   //15
extern double  KATR             = 2;
extern int     Shift            = 0;    //1
extern double  ArrowDistance    = 0.3;
extern string  FontName         = "Verdana";
extern int     FontSize         = 14;
extern int     HorizSpacing     = 5;
extern color   FontColor1       = Yellow;
extern color   FontColor2       = RoyalBlue;
extern color   FontColor3       = Red;
extern color   FontColor4       = White;
extern string  PriceMask        = "TR4.3";
extern string  PriceMaskJPY     = "TR5.2";
extern string  DiffMask         = "TR-4.1";

//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];

double ExtMapBuffer1[];
double ExtMapBuffer2[];

double SigBufferUp[];
double SigBufferDn[];
double SigBufferWarn[];
double SigDistance;

string IndiName = "BollStarc-";
string ccy, sym;
int    dig, tf, tmf, vis, wno, RefreshEveryXMins;
double spr, pnt, tickval;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  SetIndexStyle(0,DRAW_LINE,0,1);
  SetIndexBuffer(0,MovingBuffer);
  SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1);
  SetIndexBuffer(1,UpperBuffer);
  SetIndexStyle(2,DRAW_LINE,STYLE_DOT,1);
  SetIndexBuffer(2,LowerBuffer);

  SetIndexDrawBegin(0,MA_Period+Shift);
  SetIndexDrawBegin(1,ATR_Period+Shift);
  SetIndexDrawBegin(2,ATR_Period+Shift);
  //---- BB start
  SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2);
  SetIndexBuffer(3,ExtMapBuffer1);
  SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2);
  SetIndexBuffer(4,ExtMapBuffer2);

  SetIndexDrawBegin(3,BB_Period);
  SetIndexDrawBegin(4,BB_Period);
  //---- BB ends

  SetIndexEmptyValue(0,EMPTY_VALUE);
  SetIndexEmptyValue(1,EMPTY_VALUE);
  SetIndexEmptyValue(2,EMPTY_VALUE);
  SetIndexEmptyValue(3,EMPTY_VALUE);
  SetIndexEmptyValue(4,EMPTY_VALUE);
  SetIndexEmptyValue(5,EMPTY_VALUE);
  SetIndexEmptyValue(6,EMPTY_VALUE);
  SetIndexEmptyValue(7,EMPTY_VALUE);
 
 
  SetIndexBuffer(5,SigBufferUp);
  SetIndexStyle(5, DRAW_ARROW);
  SetIndexArrow(5, 241);//233221

  SetIndexBuffer(6,SigBufferDn);
  SetIndexStyle(6, DRAW_ARROW);
  SetIndexArrow(6, 242);//234221

  SetIndexBuffer(7,SigBufferWarn);
  SetIndexStyle(7, DRAW_ARROW);
  SetIndexArrow(7, 159);//115167244221  

  ccy     = Symbol();
  tmf     = Period();
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  } 
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  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);
}

//+------------------------------------------------------------------+
int start()   {
//+------------------------------------------------------------------+
  int i,k,counted_bars=IndicatorCounted();
  if(counted_bars<0) return(-1);
   
  if (counted_bars>0) counted_bars--;
  int  limit=Bars-counted_bars;

  for (i=limit; i>=0; i--)   {
    //STARC Bands
    MovingBuffer[i] = iMA(NULL,0,MA_Period,Shift,MODE_EMA,PRICE_CLOSE,i);
    UpperBuffer[i] = MovingBuffer[i] + (KATR * iATR(NULL,0,ATR_Period,i+Shift));
    LowerBuffer[i] = MovingBuffer[i] - (KATR * iATR(NULL,0,ATR_Period,i+Shift));
    //BBands
    ExtMapBuffer1[i]=iBands(NULL,0,BB_Period,BB_Deviations,0,PRICE_CLOSE,MODE_UPPER,i);
    ExtMapBuffer2[i]=iBands(NULL,0,BB_Period,BB_Deviations,0,PRICE_CLOSE,MODE_LOWER,i);
 
    SigDistance = ArrowDistance*(KATR * iATR(NULL,0,ATR_Period,i+Shift));
 
    SigBufferUp [i] = EMPTY_VALUE;
    SigBufferDn [i] = EMPTY_VALUE;
    SigBufferWarn[i]= EMPTY_VALUE;

    if ( ExtMapBuffer1[i+1]<UpperBuffer[i+1]&& ExtMapBuffer2[i+1]>=LowerBuffer[i+1]&&
    ExtMapBuffer2[i]<LowerBuffer[i]&& MovingBuffer[i+1]<MovingBuffer[i] )     {
      SigBufferUp [i] = ExtMapBuffer2[i]-SigDistance;
      if (i==0)  
        SigBufferWarn[i]  = SigBufferUp[i];
      else
        SigBufferWarn[i] = EMPTY_VALUE;
    }

    if ( ExtMapBuffer1[i+1]<=UpperBuffer[i+1]&& ExtMapBuffer2[i+1]>LowerBuffer[i+1]&&
    ExtMapBuffer1[i]>UpperBuffer[i]&& MovingBuffer[i+1]>MovingBuffer[i])    {
      SigBufferDn [i] = ExtMapBuffer1[i]+SigDistance;
      if (i==0)  
        SigBufferWarn[i] = SigBufferDn[i];
      else
        SigBufferWarn[i] = EMPTY_VALUE;
    }
  }
  PlotTextValues();

  return(0);
}

//+------------------------------------------------------------------+
void PlotTextValues()   {
//+------------------------------------------------------------------+
  string mask = PriceMask;
  if (StringFind(ccy,"JPY")>=0)
    mask = PriceMaskJPY;
  PlotText  (IndiName+"1", true, 0, Time[0]+HorizSpacing*60*Period(), UpperBuffer[0],   NumberToStr(UpperBuffer[0],mask),   FontColor1, FontSize, FontName, 0, false, 0);      // Plot text
  PlotText  (IndiName+"2", true, 0, Time[0]+HorizSpacing*60*Period(), LowerBuffer[0],   NumberToStr(LowerBuffer[0],mask),   FontColor1, FontSize, FontName, 0, false, 0);      // Plot text
  PlotText  (IndiName+"3", true, 0, Time[0]+HorizSpacing*60*Period(), ExtMapBuffer1[0], NumberToStr(ExtMapBuffer1[0],mask), FontColor2, FontSize, FontName, 0, false, 0);      // Plot text
  PlotText  (IndiName+"4", true, 0, Time[0]+HorizSpacing*60*Period(), ExtMapBuffer2[0], NumberToStr(ExtMapBuffer2[0],mask), FontColor2, FontSize, FontName, 0, false, 0);      // Plot text

  double prc  = (UpperBuffer[0]+ExtMapBuffer1[0])/2;
  double diff = (ExtMapBuffer1[0]-UpperBuffer[0])/pnt;
  PlotText  (IndiName+"5", true, 0, Time[0]+2*HorizSpacing*60*Period(), prc, NumberToStr(diff,DiffMask), FontColor3, FontSize, FontName, 0, false, 0);      // Plot text

  prc  = (LowerBuffer[0]+ExtMapBuffer2[0])/2;
  diff = (LowerBuffer[0]-ExtMapBuffer2[0])/pnt;
  PlotText  (IndiName+"6", true, 0, Time[0]+2*HorizSpacing*60*Period(), prc, NumberToStr(diff,DiffMask), FontColor3, FontSize, FontName, 0, false, 0);      // Plot text

  prc  = UpperBuffer[0];
  diff = (UpperBuffer[0]-High[0])/pnt;
  PlotText  (IndiName+"7", true, 0, Time[0]+3*HorizSpacing*60*Period(), prc, NumberToStr(diff,DiffMask), FontColor1, FontSize, FontName, 0, false, 0);      // Plot text

  prc  = ExtMapBuffer1[0];
  diff = (ExtMapBuffer1[0]-High[0])/pnt;
  PlotText  (IndiName+"9", true, 0, Time[0]+3*HorizSpacing*60*Period(), prc, NumberToStr(diff,DiffMask), FontColor2, FontSize, FontName, 0, false, 0);      // Plot text

  prc  = LowerBuffer[0];
  diff = (Low[0]-LowerBuffer[0])/pnt;
  PlotText  (IndiName+"8", true, 0, Time[0]+3*HorizSpacing*60*Period(), prc, NumberToStr(diff,DiffMask), FontColor1, FontSize, FontName, 0, false, 0);      // Plot text

  prc  = ExtMapBuffer2[0];
  diff = (Low[0]-ExtMapBuffer2[0])/pnt;
  PlotText  (IndiName+"0", true, 0, Time[0]+3*HorizSpacing*60*Period(), prc, NumberToStr(diff,DiffMask), FontColor2, FontSize, FontName, 0, false, 0);      // Plot text

  return(0);
}

//+-----------------------------------------------------------------+
#include <hanover --- extensible functions (np).mqh>

