//+------------------------------------------------------------------+
//| The Mean Goose v1.01 MT4 conversion                              |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window
#property indicator_buffers 9

#property indicator_color1 clrYellow
#property indicator_color2 clrRed
#property indicator_color3 clrBlueViolet
#property indicator_color4 clrBlueViolet
#property indicator_color5 clrOrange
#property indicator_color6 clrOrange
#property indicator_color7 clrOrange
#property indicator_color8 clrYellow
#property indicator_color9 clrRed

extern int bars=10000;
extern int kc1_len=200;
extern double kc1_mult=9.5;
extern bool kc1_useEMA=true;
extern int kc1_atrLen=200;
extern int kc2_len=20;
extern double kc2_mult=3.5;
extern bool kc2_useEMA=false;
extern int kc2_atrLen=40;
extern int ema_len=2;
extern int ema_off=0;
extern bool useHODFilter=false;
extern int reset_hour=18;
extern int reset_min=0;

double BuyBuffer[],SellBuffer[],KC1Up[],KC1Dn[],KC2Up[],KC2Dn[],KCEMA[],dotUp[],dotDn[];

bool highTrigger=false, lowTrigger=false;
datetime lastAlert=0;

double MA(int shift,int len,bool exp){
 return iMA(NULL,0,len,0,exp?MODE_EMA:MODE_SMA,PRICE_CLOSE,shift);
}
double ATR(int shift,int len){
 return iATR(NULL,0,len,shift);
}

int init(){
 IndicatorBuffers(9);
 SetIndexBuffer(0,BuyBuffer); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,233);
 SetIndexBuffer(1,SellBuffer); SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,234);
 SetIndexBuffer(2,KC1Up); SetIndexStyle(2,DRAW_LINE);
 SetIndexBuffer(3,KC1Dn); SetIndexStyle(3,DRAW_LINE);
 SetIndexBuffer(4,KC2Up); SetIndexStyle(4,DRAW_LINE);
 SetIndexBuffer(5,KC2Dn); SetIndexStyle(5,DRAW_LINE);
 SetIndexBuffer(6,KCEMA); SetIndexStyle(6,DRAW_LINE,STYLE_SOLID,3);
 SetIndexBuffer(7,dotUp); SetIndexStyle(7,DRAW_ARROW); SetIndexArrow(7,108);
 SetIndexBuffer(8,dotDn); SetIndexStyle(8,DRAW_ARROW); SetIndexArrow(8,108);
 ArraySetAsSeries(BuyBuffer,true);ArraySetAsSeries(SellBuffer,true);
 ArraySetAsSeries(KC1Up,true);ArraySetAsSeries(KC1Dn,true);
 ArraySetAsSeries(KC2Up,true);ArraySetAsSeries(KC2Dn,true);
 ArraySetAsSeries(KCEMA,true);ArraySetAsSeries(dotUp,true);ArraySetAsSeries(dotDn,true);
 return(0);
}

int start(){
 int counted=IndicatorCounted();
 int limit=Bars-counted-2;
 if(limit>bars) limit=bars;

 for(int i=limit;i>=0;i--){
  BuyBuffer[i]=EMPTY_VALUE; SellBuffer[i]=EMPTY_VALUE;
  dotUp[i]=EMPTY_VALUE; dotDn[i]=EMPTY_VALUE;

  double kc1=MA(i,kc1_len,kc1_useEMA);
  double atr1=ATR(i,kc1_atrLen);
  double u1=kc1+atr1*kc1_mult;
  double d1=kc1-atr1*kc1_mult;

  double kc2=MA(i,kc2_len,kc2_useEMA);
  double atr2=ATR(i,kc2_atrLen);
  double u2=kc2+atr2*kc2_mult;
  double d2=kc2-atr2*kc2_mult;

  double ema=iMA(NULL,0,ema_len,ema_off,MODE_EMA,PRICE_CLOSE,i);

  KC1Up[i]=u1; KC1Dn[i]=d1;
  KC2Up[i]=u2; KC2Dn[i]=d2;
  KCEMA[i]=ema;

  bool highDot=(High[i]>u1 && High[i]>u2);
  bool lowDot=(Low[i]<d1 && Low[i]<d2);

  if(highDot) dotUp[i]=High[i]+atr1;
  if(lowDot) dotDn[i]=Low[i]-atr1;

  if(highDot) highTrigger=true;
  if(lowDot) lowTrigger=true;

  if(highTrigger && Close[i]<ema){
    SellBuffer[i]=High[i]+atr1*4;
    highTrigger=false;
  }
  if(lowTrigger && Close[i]>ema){
    BuyBuffer[i]=Low[i]-atr1*4;
    lowTrigger=false;
  }
 }
 return(0);
}
