//+------------------------------------------------------------------+
//|Squalou and Steve's Trade Signals.mq4
//+------------------------------------------------------------------+
#property copyright "SQ&SteveHopwood"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Yellow
#property indicator_width1 2
#property indicator_width2 2

//

extern string  gss="----Sixths inputs----";
extern int     BarCount=50;
extern string  smacd="----StochasticMacdSqualou inputs----";
extern int     FastEMA   =12;
extern int     SlowEMA   =26;
extern int     SignalSMA =9;
extern int     StochasticPeriod =120; //4 weeks of H4 bars = 4*5*24/4 = 120

//

double signalUP[];
double signalDN[];

#define  up "Up"
#define  down "Down"
#define  none "None"

//6ths variable.
double         BottomGoldLine;//Bottom, gold line
double         BottomGreenLine;//Bottom, green line
double         MiddleWhiteLine;//Middle, white line
double         TopGreenLine;//Top, green line
double         TopGoldLine;//Top, gold line

//Stoch variables
double         StochWhite;
double         StochBlue;

//Macd
double         MacdVal;

//BB variables
double         BbUpper, BbLower;

//Trade direction
string         TradeDirection=none;


//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,signalUP); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,233);
   SetIndexBuffer(1,signalDN); SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,234);
   return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
   int limit;
   string dir;
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit=MathMin(Bars-counted_bars,Bars-1);

   for(int i=limit; i>=0; i--)
   {

      ReadIndicatorValues(i);
      dir = GetTradeDirection(i);
      
      signalUP[i] = EMPTY_VALUE;
      signalDN[i] = EMPTY_VALUE;

      if (dir == up)   signalUP[i] = Low[i] -100*Point;
      if (dir == down) signalDN[i] = High[i]+100*Point;
   }

   return(0);
}


void GetSixths(int shift)
{
   /*
   These are declared in the general section and count the lines from the bottom upwards
   double         BottomGoldLine;//Bottom, gold line
   double         BottomGreenLine;//Bottom, green line
   double         MiddleWhiteLine;//Middle, white line
   double         TopGreenLine;//Top, green line
   double         TopGoldLine;//Top, gold line

   */

   //double value = WindowPriceMax(0)-WindowPriceMin(0);      //value top of the chart - value buttem
   double low   = Low[iLowest(NULL,0,MODE_LOW,BarCount,shift+1)];
   double value = High[iHighest(NULL,0,MODE_HIGH,BarCount,shift+1)] - low; //value top of the chart - value bottom
   double sixth = value/6;
   double valueS = (value*(MathPow(10,Digits)));
   double sixthS = (sixth*(MathPow(10,Digits)));
   
   BottomGoldLine  = NormalizeDouble(low+1*sixth, Digits);
   BottomGreenLine = NormalizeDouble(low+2*sixth, Digits);
   MiddleWhiteLine = NormalizeDouble(low+3*sixth, Digits);
   TopGreenLine    = NormalizeDouble(low+4*sixth, Digits);
   TopGoldLine     = NormalizeDouble(low+5*sixth, Digits);

}//End void GetSixths()



void GetStoch(int shift)
{
//double         StochWhite;
//double         StochRed;

   StochWhite = iStochastic(NULL, 0, 7, 3, 3, MODE_LWMA, 1, MODE_MAIN, shift);
   StochBlue = iStochastic(NULL, 0, 14, 3, 5, MODE_LWMA, 1, MODE_MAIN, shift);
   

}//void GetStoch();

double GetStochMACD(int fast_ema_period, int slow_ema_period, int signal_period, int StochasticPeriod, int shift)
{
  // get macd signal line
  double macdSignal;
  int j;
  // get highest/lowest of macdSignal over StochasticPeriod
  double ll = 100000, hh = -100000, dif;
  for(j=shift; j<shift+StochasticPeriod; j++)
  {
    macdSignal = iMACD(NULL,0,fast_ema_period, slow_ema_period, signal_period, PRICE_CLOSE,MODE_SIGNAL,j);
    hh = MathMax(hh,macdSignal);
    ll = MathMin(ll,macdSignal);
  }

  // normalize to 0..100

  dif = hh-ll;
  if (dif==0) return (0);
  else return (100 * (iMACD(NULL,0,fast_ema_period, slow_ema_period, signal_period, PRICE_CLOSE,MODE_SIGNAL,shift)-ll)/dif); 
}//End double GetStochMACD(int fast_ema_period, int slow_ema_period, int signal_period, int StochasticPeriod, int shift)

void GetBB(int shift)
{
   //Reads BB figures into BbUpper, BbLower
   
   
   BbUpper = iBands(NULL, 0, 25, 2, 0, PRICE_CLOSE, MODE_UPPER, shift);
   BbLower = iBands(NULL, 0, 25, 2, 0, PRICE_CLOSE, MODE_LOWER, shift);
   
}//void GetBb()


void ReadIndicatorValues(int shift)
{

   GetBB(shift);
   GetSixths(shift);
   GetStoch(shift);
   MacdVal = GetStochMACD(FastEMA, SlowEMA, SignalSMA, StochasticPeriod , shift);

}//End void ReadIndicatorValues()


string GetTradeDirection(int shift)
{
   
   //Is market in the killing zone
   if (Close[shift] <= TopGreenLine && Close[shift] >= BottomGreenLine)
   {
      TradeDirection = none;
      return(none);
   }//if (Ask <= TopGreenLine && Ask >= BottomGreenLine Bid <= TopGreenLine && Bid >= BottomGreenLine)
   
   //Check for short direction trade setup
   if (Close[shift] > TopGreenLine)
   {
      TradeDirection = down;
      if (StochBlue < 85) TradeDirection = none;
      if (StochWhite > StochBlue) TradeDirection = none;
      if (MacdVal < 85) TradeDirection = none;
      if (BbUpper < TopGreenLine) TradeDirection = none;  
   }//if (Ask > TopGoldLine)

   //Check for short direction trade setup
   if (Close[shift] < BottomGreenLine)
   {
      TradeDirection = up;
      if (StochBlue > 15 ) TradeDirection = none;
      if (StochWhite < StochBlue) TradeDirection = none;
      if (MacdVal > 15 ) TradeDirection = none;
      if (BbLower > BottomGreenLine) TradeDirection = none;
   }//if (Ask > TopGoldLine)

  return (TradeDirection);

}//End void GetTradeDirection()


//end

