//+------------------------------------------------------------------+
//|                                                      wicksys.mq4 |
//|                                    Copyright 2018, Fauzaan Gasim |
//|                                           https://t.me/fauzaaaan |
//|                       Revised by Sridhar K.G (skg3007@gmail.com) |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Fauzaan Gasim"
#property link      "https://t.me/fauzaaaan"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
enum Modes
  {
   HH,
   LL,
   HL,
   LH
  };

//Basic Order stuff (Completed)
extern double Lots=0.01;
extern int TP = 500;
extern int SL = 500;
extern bool RL=false; // Reverse Logic

                      //Swing (Completed)
extern bool Swingmode=true; // Use Swing mode from X candles
extern Modes Mode=HH;
extern int X=5; // Define X candles

                //Min Candle Length (Completed)
extern bool MACB=true; // Use Candle Length
extern int MAC=10; // Candle Length is atleast ____

extern bool     TradeHours=false;      //Trade on Selected Hours
extern string   StartHour     = "05:30"; //Start Hour
extern string   EndHour       = "20:00"; //End Hour
extern string   ClsFinalHour  = "21:00"; //Close All Orders @Hour

                                         ////Min Candle percent (Not coded)
//extern bool usePercent = true; // Lowerwick - upperwick percent
//extern int percent = 150; // Define Lowerwick upperwick percent

////candlesize (Not coded)
//extern bool CandleSize = true; // K * Bigger than previous Y candles
//extern int K = 150; // (Candle sizing (Multiplying number)
//extern int Y = 4; // Candle Sizing (number of candles)

datetime newcandle;
int multiple;
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits() == 3) multiple = 100;
   if(Digits() == 5) multiple = 10000;

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
void OnTick()
  {
   bool canTrade=true,os;
   if(TradeHours)
     {
      if(!TimeFilter(StartHour,EndHour)) canTrade=false;
      if(!TimeFilter(StartHour,ClsFinalHour)) CloseAll();
     }

   if(newcandle==Time[1] && canTrade)
     {
      double LW = getLowerWick(); //lowerwick
      double UW = getUpperWick(); //Higherwick (upperwick)

                                  //double LW = MathAbs(Low[1]-Close[1]); //lowerwick
      //double UW = MathAbs(High[1]-Open[1]); //Higherwick (upperwick)
      //Swing mode is false
      if(Swingmode==false)
        {
         //Original entry
         if(LW>UW && RL==false)
           {
            if(MACB == false) os=OrderSend(Symbol(),OP_BUY,Lots,Ask,50,Bid-SL*Point(),Bid+TP*Point(),NULL,123,NULL,clrBlue);
            if(MACB == true && (High[1]-Low[1])*multiple >= MAC) os=OrderSend(Symbol(),OP_BUY,Lots,Ask,50,Bid-SL*Point(),Bid+TP*Point(),NULL,123,NULL,clrBlue);
           }

         if(LW<UW && RL==false)
           {
            if(MACB == false) os=OrderSend(Symbol(),OP_SELL,Lots,Bid,50,Bid+SL*Point(),Bid-TP*Point(),NULL,1213,NULL,clrRed);
            if(MACB == true && (High[1]-Low[1])*multiple >= MAC) os=OrderSend(Symbol(),OP_SELL,Lots,Bid,50,Bid+SL*Point(),Bid-TP*Point(),NULL,1213,NULL,clrRed);
           }

         //Reverse logic   
         if(LW>UW && RL==true)
           {
            if(MACB == false) os=OrderSend(Symbol(),OP_SELL,Lots,Bid,50,Bid+SL*Point(),Bid-TP*Point(),NULL,1213,NULL,clrRed);
            if(MACB == true && (High[1]-Low[1])*multiple >= MAC) os=OrderSend(Symbol(),OP_SELL,Lots,Bid,50,Bid+SL*Point(),Bid-TP*Point(),NULL,1213,NULL,clrRed);
           }

         if(LW<UW && RL==true)
           {
            if(MACB == false) os=OrderSend(Symbol(),OP_BUY,Lots,Ask,50,Bid-SL*Point(),Bid+TP*Point(),NULL,123,NULL,clrBlue);
            if(MACB == true && (High[1]-Low[1])*multiple >= MAC) os=OrderSend(Symbol(),OP_BUY,Lots,Ask,50,Bid-SL*Point(),Bid+TP*Point(),NULL,123,NULL,clrBlue);
           }
        }

      //Swing mode is true
      if(Swingmode==true)
        {
         if(swingCheck(Mode,X)==true)
           {

            //Original entry
            if(LW>UW && RL==false)
              {
               if(MACB == false) os=OrderSend(Symbol(),OP_BUY,Lots,Ask,50,Bid-SL*Point(),Bid+TP*Point(),NULL,123,NULL,clrBlue);
               if(MACB == true && (High[1]-Low[1])*multiple >= MAC) os=OrderSend(Symbol(),OP_BUY,Lots,Ask,50,Bid-SL*Point(),Bid+TP*Point(),NULL,123,NULL,clrBlue);

              }

            if(LW<UW && RL==false)
              {
               if(MACB == false) os=OrderSend(Symbol(),OP_SELL,Lots,Bid,50,Bid+SL*Point(),Bid-TP*Point(),NULL,1213,NULL,clrRed);
               if(MACB == true && (High[1]-Low[1])*multiple >= MAC) os=OrderSend(Symbol(),OP_SELL,Lots,Bid,50,Bid+SL*Point(),Bid-TP*Point(),NULL,1213,NULL,clrRed);
              }

            //Reverse logic   
            if(LW>UW && RL==true)
              {
               if(MACB == false) os=OrderSend(Symbol(),OP_SELL,Lots,Bid,50,Bid+SL*Point(),Bid-TP*Point(),NULL,1213,NULL,clrRed);
               if(MACB == true && (High[1]-Low[1])*multiple >= MAC) os=OrderSend(Symbol(),OP_SELL,Lots,Bid,50,Bid+SL*Point(),Bid-TP*Point(),NULL,1213,NULL,clrRed);
              }

            if(LW<UW && RL==true)
              {
               if(MACB == false) os=OrderSend(Symbol(),OP_BUY,Lots,Ask,50,Bid-SL*Point(),Bid+TP*Point(),NULL,123,NULL,clrBlue);
               if(MACB == true && (High[1]-Low[1])*multiple >= MAC) os=OrderSend(Symbol(),OP_BUY,Lots,Ask,50,Bid-SL*Point(),Bid+TP*Point(),NULL,123,NULL,clrBlue);
              }
           }
        }
     }
   newcandle=Time[0];
  }
//+------------------------------------------------------------------+
bool swingCheck(Modes value,int candles)
  {
   if(value==HH)
     {
      if(High[candles+1]>High[candles+2]) return(true);
     }

   if(value==LL)
     {
      if(Low[candles+1]<Low[candles+2]) return(true);
     }

   if(value==HL)
     {
      if(Low[candles+1]>Low[candles+2]) return(true);
     }

   if(value==LH)
     {
      if(High[candles+1]<High[candles+2]) return(true);
     }

   return(false);
  }
//+------------------------------------------------------------------+
double getLowerWick()
  {
   if(Open[1]>Close[1]) return(MathAbs(Low[1]-Close[1]));

   if(Open[1]<Close[1]) return(MathAbs(Low[1]-Open[1]));
   else return(NULL);
  }
//+------------------------------------------------------------------+
double getUpperWick()
  {
   if(Open[1]>Close[1]) return(MathAbs(High[1]-Open[1]));

   if(Open[1]<Close[1]) return(MathAbs(High[1]-Close[1]));
   else return(NULL);

  }
//+------------------------------------------------------------------+
bool TimeFilter(string StartH,string EndH)
  {
   if(TimeCurrent() >= StringToTime(StartH) && TimeCurrent() <= StringToTime(EndH)) return (true);
   else return (false);
  }
//+------------------------------------------------------------------+
void CloseAll()
  {
   double askBid=0;
   bool oc;

   for(int ot=OrdersTotal(); ot>=0; ot--)
     {
      if(OrderSelect(ot,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==OP_BUY) askBid=MarketInfo(OrderSymbol(),MODE_BID);
         else if(OrderType()==OP_SELL) askBid=MarketInfo(OrderSymbol(),MODE_ASK);

         if(OrderType()<=OP_SELL) oc=OrderClose(OrderTicket(),OrderLots(),askBid,10,Yellow);
         if(OrderType()>OP_SELL) oc=OrderDelete(OrderTicket(),Yellow);
        }
     }
  }
//+------------------------------------------------------------------+
