//+----------------------------------------------------------+
//|                    Traders Gift EA .mq4                  |
//|               Copyright © 2015, David Currey             |
//+----------------------------------------------------------+
extern int MAGICMA = 3333300;
extern string Hdr0 = ">>> EA MODES OF OPERATION";
extern string Hlp0 = "TradingMode: Autotrading=0, Advisory Only=1, Discretionary Trading=2";
extern int TradingMode = 0;
extern string Hlp0a = "AdvisoryMode: On Screen prompts=0, Email or Text Message=1";
extern int AdvisoryMode = 0;
extern string Hdr1 = ">>> MONEY MANAGEMENT";
extern string Hlp1 = "LotSize: Automoney management=0";
extern double LotSize = 0;
extern string Hlp1a = "RiskBasis: on equity=0, on stoploss=1";
extern int RiskBasis = 0;
extern double MaximumRiskPercentage = 2;
extern string Hlp1b = "StopLoss: for signal reversal or autocalculation=0";
extern int StopLoss = 10;
extern string Hlp1c = "UseTrailingStopLoss: No=0, Yes=1";
extern int UseTrailingStopLoss = 1;
extern int TakeProfit = 10;
extern string Hlp1d = "SetStopLossToBreakEvenPips: no profit protection=0";
extern string Hlp1e = "SetStopLossToBreakEvenPips: protect loss at ?? pip gain=??";
extern int SetStopLossToBreakEvenPips = 0;
extern int MaxSlippage = 1;
extern int MaxSpread = 2;
extern string Hdr2 = ">>> EMA CROSS";
extern string Hlp2 = "UseEMACross No=0, Yes=1";
extern int UseEMACross = 0;
extern int ShortEMA = 5;
extern int LongEMA = 10;
extern string Hlp2a = "The Maximum number of bars ago that the cross took place";
extern int EMAMaxBarsAgo = 1;
extern string Hdr3 = ">>> PRICE/EMA CROSS";
extern string Hlp3 = "UsePriceEMACross: No=0, Yes=1";
extern int UsePriceEMACross = 0;
extern int SingleEMA = 5;
extern int EMAShift = 2;
extern string Hlp3a = "Use Heinken Ashi Candles: No=0, Yes=1";
extern int UseHACandles = 0;
extern int PriceEMAMaxBarsAgo = 1;
extern string Hdr4 = ">>> RSI CROSS";
extern string Hlp4 = "UseRSICross: No=0, Yes=1";
extern int UseRSICross = 0;
extern int RSIPeriods = 14;
extern int RSITopLevel = 70;
extern int RSIBottomLevel = 30;
extern int RSIMaxBarsAgo = 1;
extern string Hdr5 = ">>> MACD CROSS";
extern string Hlp5 = "UseSignal/MainCross: No=0, Yes=1";
extern int UseMACDCross = 0;
extern string Hlp5a = "UseMainZeroCross: No=0, Yes=1";
extern int UseMainZeroCross = 0;
extern string Hlp5b = "UseSignalZeroCross: No=0, Yes=1";
extern int UseSignalZeroCross = 0;
extern int ShortMACD = 12;
extern int LongMACD = 26;
extern int Signal = 9;
extern int MACDMaxBarsAgo = 1;
extern string Hdr6 = ">>> PSAR CROSS";
extern string Hlp6 = "UsePSAR: No=0, Yes=1";
extern int UsePSAR = 0;
extern double PSARStop = .2;
extern double PSARIncrement = .02;
extern int PSARMaxBarsAgo = 1;
extern string Hdr7 = ">>> ZIGZAG";
extern string Hlp7 = "UseZigZag: No=0, Yes=1";
extern int UseZigZag = 0;
extern int ZZDepth = 6;
extern int ZZDeviation = 5;
extern int ZZBackstep = 3;
extern int ZZMaxBarsAgo = 1;
extern string Hdr8 = ">>> NEWS OR TIME OF DAY STRADDLE";
extern string Hlp8 = "UseStraddle: No=0, Yes=1";
extern int UseStraddle = 0;
extern int StraddleBuffer = 15;         
extern int TradeHour = 0;             
extern int TradeMinute = 0;             
extern string Hdr9 = ">>> CONTINUOUS STRADDLE";
extern string Hlp9 = "UseContinuousStraddle: No=0, Yes=1";
extern int UseContinuousStraddle = 0;
extern int ContinuousStraddleBuffer = 15;         
extern string Hdr10 = ">>> ATR BREAKOUT";
extern string Hlp10 = "UseATRBreakout: No=0, Yes=1";
extern int UseATRBreakout = 0;
extern int ATRLookbackBars = 5;
extern double MinATRPipsPerBar = 5;
extern int ATRMaxBarsAgo = 1;
extern string Hdr11 = ">>> ADX FILTER";
extern string Hlp11 = "UseADX: No=0, Yes=1";
extern int UseADX = 0;
extern string Hdr12 = ">>> TIME OF DAY FILTER";
extern string Hlp12 = "UseTimeOfDay: No=0, Yes=1";
extern int UseTimeOfDay = 0;
extern int TradingHoursStart = 0;
extern int TradingHoursEnd = 0;
extern string Hdr13 = ">>> NEWS FILTER";
extern string Hlp13 = "UseNews: No=0, Yes=1";
extern int UseNews = 0;
extern string Hlp13a = "Use MT4 Times, for no filter NewsHour?=-1";
extern int NewsHour1 = -1;
extern int NewsMinute1 = 0;
extern int NewsHour2 = -1;
extern int NewsMinute2 = 0;
extern int NewsHour3 = -1;
extern int NewsMinute3 = 0;
extern int AvoidMinutesBeforeNews = 0;
extern int AvoidMinutesAfterNews = 0; 
extern string Hdr14 = ">>> PIVOT POINT FILTER";
extern string Hlp14 = "UsePivots: No=0, Yes=1";
extern int UsePivots = 0;
extern string Hlp14a = "Number of Hours relative to MT4 End of Day for calculation";
extern int MT4Offset = 0;
extern string Hlp14b = "Number of Pips buffer away from significant levels";
extern int PPBuffer = 5;
extern string Hdr15 = ">>> FIBONACCI FILTER";
extern string Hlp15 = "UseFibonacci: No=0, Yes=1";
extern int UseFibs = 0;
extern int FibLookbackBars = 60;
extern int FibTimeFrameInMins = 60;
extern string Hlp15a = "Number of Pips buffer away from significant levels";
extern int FibsBuffer = 5;
extern string Hdr16 = ">>> SUPPORT/RESISTANCE FILTER";
extern string Hlp16 = "UseSR: No=0, Yes=1";
extern int UseSR = 0;
extern int SRMaxBarsAgo = 60;
extern string Hlp16a = "Number of Pips buffer away from Support/Resistance levels";
extern int SRBuffer = 5;
extern string Hdr17 = ">>> DIVERGENCE FILTER";
extern string Hlp17 = "Use Divergence: No=0, Yes=1";
extern int UseDivergence = 0;
extern int DivergenceLookbackBars = 60;
extern int DivergenceTimeFrameInMins = 60;
double StartRSI;
double EndRSI;
double StartMACD;
double EndMACD;
double StartPrice;
double EndPrice = 0;
extern string Hdr18 = ">>> CANDLESTICK FILTER";
extern string Hlp18 = "UseCandlesticks: No=0, Yes=1";
extern int UseCandleSticks = 0;
extern int CandleSticksMaxBarsAgo = 1;
//-------------------------------------
// Money Management variables
double TakeWinPrice;
double CurrProfitTarget;
double PointValue;
double StopLossPrice;
double StopLevel;
double CurrOpen;
double PrevOpen;
double PrevValue;
double PrevBid;
double Wager;
//-------------------------------------
// EMA Cross variables
double CurrFastEMA;
double PrevFastEMA;
double CurrSlowEMA;
double PrevSlowEMA;
int BuyEMACrossReturnValue;
int SellEMACrossReturnValue;
//-------------------------------------
// Price EMA Cross variables
double Close1;
double Close2;
int BuyPriceEMACrossReturnValue;
int SellPriceEMACrossReturnValue;
//-------------------------------------
// RSI Cross variables
double CurrRSI;
double PrevRSI;
int BuyRSIReturnValue;
int SellRSIReturnValue;
//-------------------------------------
// MACD Cross variables
double CurrMain;
double PrevMain;
double CurrSignal;
double PrevSignal;
int BuyMACDReturnValue;
int SellMACDReturnValue;
//-------------------------------------
// PSAR Cross variables
double CurrPSAR;
double PrevPSAR;
int BuyPSARReturnValue;
int SellPSARReturnValue;
//-------------------------------------
// ZigZag variables
double Prev1Zig;
double Prev2Zig;
double Prev3Zig;
double Prev4Zig;
double LastZig;
int BuyZZReturnValue;
int SellZZReturnValue;
//-------------------------------------
// Straddle variables
int StraddleReturnValue = 0;
//-------------------------------------
// ATR variables
int BuyATRReturnValue = 0;
int SellATRReturnValue = 0;
double CurrATR;
double PrevATR;
//-------------------------------------
// ADX variables
int BuyADXReturnValue;
int SellADXReturnValue;
//-------------------------------------
// Time of day variables
int TimeOfDayReturnValue;
//-------------------------------------
// News variables
int NewsReturnValue;
int NewsReturnValue1;
int NewsReturnValue2;
int NewsReturnValue3;
int StartHour1;
int EndHour1;
int StartMinute1;
int EndMinute1;
int StartHour2;
int EndHour2;
int StartMinute2;
int EndMinute2;
int StartHour3;
int EndHour3;
int StartMinute3;
int EndMinute3;
//-------------------------------------
// Pivot Point variables
double PP;
double S1;
double S2;
double S3;
double R1;
double R2;
double R3;
int BuyPivotsReturnValue;
int SellPivotsReturnValue;
//-------------------------------------
// Fibonacci variables
double F100;
double F62;
double F50;
double F38;
double F0;
int BuyFibsReturnValue;
int SellFibsReturnValue;
//-------------------------------------
// Support Resistance variables
int BuySRReturnValue;
int SellSRReturnValue;
//-------------------------------------
// Divergence variables
int BuyDivergenceReturnValue;
int SellDivergenceReturnValue;
double MaxRSIPosition;
double MinRSIPosition;
double MaxPricePosition;
double MinPricePosition;
//-------------------------------------
// CandleSticks variables
int BuyCandleSticksReturnValue;
int SellCandleSticksReturnValue;
int CandleSticksBuySignal;
int CandleSticksSellSignal;
double gPointPow = 0;
double gPointCoef = 0;
//-------------------------------------
extern string Message = "Traders Gift";
string Comment1;
string Message2;
int Buys, Sells, BuyStops, SellStops;
int NoBuyTrade, NoSellTrade;
int Shift;
int Modified;

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
  if(Bars<100 || IsTradeAllowed()==false) return;
  if(StringFind(Symbol(),"JPY") > (-1)) PointValue = .01;
  else PointValue = .0001;
  if(CalculateCurrOrders(Symbol())==0)
    { 
    CheckForOpen();
    }
  else
    { 
    if (StopLoss == 0) CheckForOpen();
    CheckForClose();
    CheckForModify();
    }
  } 
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrOrders(string symbol)
  {
  Buys=0;
  Sells=0;
  BuyStops=0;
  SellStops=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  Buys++;
         if(OrderType()==OP_SELL) Sells++;
         if(OrderType()==OP_BUYSTOP)  BuyStops++;
         if(OrderType()==OP_SELLSTOP) SellStops++;
        }
     }

//---- return orders volume
 
   if(Buys>0) return(Buys);
   else       return(-Sells);
  }

//+------------------------------------------------------------------+
//| Check for Close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
  int res, i;
    if ((SellEMACrossReturnValue == 1 || SellPriceEMACrossReturnValue == 1 || SellRSIReturnValue == 1 || SellMACDReturnValue == 1 || SellPSARReturnValue == 1 || SellATRReturnValue == 1)
    && StopLoss == 0 && Buys > 0)
       {
       for (i = OrdersTotal()-1; i >= 0; i--) 
           { 
           res = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
           if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == MAGICMA) 
              {
              res = OrderClose(OrderTicket(),OrderLots(),Bid,1,Red);
              }
           } 
       } 
    if ((BuyEMACrossReturnValue == 1 || BuyPriceEMACrossReturnValue == 1 || BuyRSIReturnValue == 1 || BuyMACDReturnValue == 1 || BuyPSARReturnValue == 1 || BuyATRReturnValue == 1
    || BuyZZReturnValue == 1) && StopLoss == 0 && Sells > 0)
       {
       for (i = OrdersTotal()-1; i >= 0; i--) 
           { 
           res = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
           if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == MAGICMA) 
              {
              res = OrderClose(OrderTicket(),OrderLots(),Ask,1,Red);
              }
           } 
       } 
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
    // ---- BUY conditions ---- 
    if (UseEMACross > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < EMAMaxBarsAgo+1; Shift++)
          {
          CalcEMA(Shift);
          ProcessBuyEMACross();
          ProcessSellEMACross();
          }
       if (BuyEMACrossReturnValue == 0 && SellEMACrossReturnValue == 0) return;
       if (BuyEMACrossReturnValue == 0) NoBuyTrade = 1;
       if (SellEMACrossReturnValue == 0) NoSellTrade = 1;
       }
    if (UsePriceEMACross > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < PriceEMAMaxBarsAgo+1; Shift++)
          {
          CalcPriceEMA(Shift);
          ProcessBuyPriceEMACross();
          ProcessSellPriceEMACross();
          }
       if (BuyPriceEMACrossReturnValue == 0 && SellPriceEMACrossReturnValue == 0) return;
       if (BuyPriceEMACrossReturnValue == 0) NoBuyTrade = 1;
       if (SellPriceEMACrossReturnValue == 0) NoSellTrade = 1;
       }
    if (UseRSICross > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < RSIMaxBarsAgo+1; Shift++)
          {
          CalcRSI(Shift);
          ProcessBuyRSICross();
          ProcessSellRSICross();
          }
       if (BuyRSIReturnValue == 0 && SellRSIReturnValue == 0) return;
       if (BuyRSIReturnValue == 0) NoBuyTrade = 1;
       if (SellRSIReturnValue == 0) NoSellTrade = 1;
       }
    if (UseMACDCross > 0 || UseMainZeroCross > 0 || UseSignalZeroCross > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < MACDMaxBarsAgo+1; Shift++)
          {
          CalcMACD(Shift);
          ProcessBuyMACDCross();
          ProcessSellMACDCross();
          }
       if (BuyMACDReturnValue == 0 && SellMACDReturnValue == 0) return;
       if (BuyMACDReturnValue == 0) NoBuyTrade = 1;
       if (SellMACDReturnValue == 0) NoSellTrade = 1;
       }
    if (UsePSAR > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < PSARMaxBarsAgo+1; Shift++)
          {
          CalcPSAR(Shift);
          ProcessBuyPSARCross();
          ProcessSellPSARCross();
          }
       if (BuyPSARReturnValue == 0 && SellPSARReturnValue == 0) return;
       if (BuyPSARReturnValue == 0) NoBuyTrade = 1;
       if (SellPSARReturnValue == 0) NoSellTrade = 1;
       }
    if (UseZigZag > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < ZZMaxBarsAgo+1; Shift++)
          {
          CalcZig(Shift);
          ProcessBuyZig();
          ProcessSellZig();
          }
       if (BuyZZReturnValue == 0 && SellZZReturnValue == 0) return;
       if (BuyZZReturnValue == 0) NoBuyTrade = 1;
       if (SellZZReturnValue == 0) NoSellTrade = 1;
       }
    if (UseStraddle > 0) 
       {
       ProcessStraddle();
       if (StraddleReturnValue == 0) return;
       }
    if (UseATRBreakout > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < ATRMaxBarsAgo+1; Shift++)
          {
          CalcATR(Shift);
          ProcessBuyATRBreakout();
          ProcessSellATRBreakout();
          }
       if (BuyATRReturnValue == 0 && SellATRReturnValue == 0) return;
       if (BuyATRReturnValue == 0) NoBuyTrade = 1;
       if (SellATRReturnValue == 0) NoSellTrade = 1;
       }
    if (UseADX > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       ProcessBuyADX();
       ProcessSellADX();
       if (BuyADXReturnValue == 0 && SellADXReturnValue == 0) return;
       if (BuyADXReturnValue == 0) NoBuyTrade = 1;
       if (SellADXReturnValue == 0) NoSellTrade = 1;
       }
    if (UseTimeOfDay > 0) 
       {
       ProcessTimeOfDay();
       if (TimeOfDayReturnValue == 0) return;
       }
    if (UseNews > 0) 
       {
       ProcessNews();
       if (NewsReturnValue == 0) return;
       }
    if (UsePivots > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       CalcPivots();
       ProcessBuyPivots();
       ProcessSellPivots();
       if (BuyPivotsReturnValue == 0 && SellPivotsReturnValue == 0) return;
       if (BuyPivotsReturnValue == 0) NoBuyTrade = 1;
       if (SellPivotsReturnValue == 0) NoSellTrade = 1;
       }
    if (UseFibs > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       CalcFibs();
       ProcessBuyFibs();
       ProcessSellFibs();
       if (BuyFibsReturnValue == 0 && SellFibsReturnValue == 0) return;
       if (BuyFibsReturnValue == 0) NoBuyTrade = 1;
       if (SellFibsReturnValue == 0) NoSellTrade = 1;
       }
    if (UseSR > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < SRMaxBarsAgo+1; Shift++)
          {
          CalcSR(Shift);
          ProcessBuySR();
          ProcessSellSR();
          }
       if (BuySRReturnValue == 0 && SellSRReturnValue == 0) return;
       if (BuySRReturnValue == 0) NoBuyTrade = 1;
       if (SellSRReturnValue == 0) NoSellTrade = 1;
       }
    if (UseDivergence > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       CalcDivergence();
       ProcessBuyDivergence();
       ProcessSellDivergence();
       if (BuyDivergenceReturnValue == 0 && SellDivergenceReturnValue == 0) return;
       if (BuyDivergenceReturnValue == 0) NoBuyTrade = 1;
       if (SellDivergenceReturnValue == 0) NoSellTrade = 1;
       }
    if (UseCandleSticks > 0) 
       {
       NoBuyTrade = 0 ;
       NoSellTrade = 0;
       for(Shift = 1; Shift < CandleSticksMaxBarsAgo+1; Shift++)
          {
          CalcCandleSticks(Shift);
          ProcessBuyCandleSticks();
          ProcessSellCandleSticks();
          }
       if (BuyCandleSticksReturnValue == 0 && SellCandleSticksReturnValue == 0) return;
       if (BuyCandleSticksReturnValue == 0) NoBuyTrade = 1;
       if (SellCandleSticksReturnValue == 0) NoSellTrade = 1;
       }
    
    if (CalculateCurrOrders(Symbol()) != 0) return;
        
    if (NoBuyTrade == 0 && NoSellTrade == 1) 
       {
       int res;
       if (TradingMode != 0)
          {
          PlaySound("alert.wav");
          Message2 = StringConcatenate("ALERT - BUY",Symbol(),"/",Period(),"min at ",TimeToString(TimeCurrent(),TIME_MINUTES));
          if (AdvisoryMode == 0) res = MessageBox(Message,Message2,MB_OKCANCEL);
          if (AdvisoryMode == 1) SendMail(Message,Message2);
          if (TradingMode == 1) return;
          if (res == 2) return;
          }
       OpenBuyOrder();
       }
    if (StraddleReturnValue == 1 || UseContinuousStraddle == 1)
       {
       if (TradingMode != 0)
          {
          PlaySound("alert.wav");
          Message2 = StringConcatenate("ALERT - BUY STOP/SELL STOP",Symbol(),"/",Period(),"min at ",TimeToString(TimeCurrent(),TIME_MINUTES));
          if (AdvisoryMode == 0) res = MessageBox(Message,Message2,MB_OKCANCEL);
          if (AdvisoryMode == 1) SendMail(Message,Message2);
          if (TradingMode == 1) return;
          if (res == 2) return;
          }
       OpenStopOrder();
       }
    if (NoSellTrade == 0 && NoBuyTrade == 1) 
       {
       if (TradingMode != 0)
          {
          PlaySound("alert.wav");
          Message2 = StringConcatenate("ALERT - SELL",Symbol(),"/",Period(),"min at ",TimeToString(TimeCurrent(),TIME_MINUTES));
          if (AdvisoryMode == 0) res = MessageBox(Message,Message2,MB_OKCANCEL);
          if (AdvisoryMode == 1) SendMail(Message,Message2);
          if (TradingMode == 1) return;
          if (res == 2) return;
          }
       OpenSellOrder();
       }
   }
//+------------------------------------------------------------------+
//| Open Buy Order                                                   |
//+------------------------------------------------------------------+
void OpenBuyOrder()
  {
       int res;
       Comment1 = StringConcatenate(Symbol()," BUY ",Message);
       Wager = LotSize;
       if (LotSize == 0  && RiskBasis == 0) Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2);
       if (LotSize == 0  && RiskBasis == 1) Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 1000 / StopLoss,2);
       if ((Ask - Bid) <= MaxSpread) 
          {
          res=OrderSend(Symbol(),OP_BUY,Wager,Ask,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          }
       // calculate stop loss and take profit levels
       if (StopLoss == 0 && BuyPriceEMACrossReturnValue == 1) StopLoss = (Close[0]-Low[2]) * PointValue;
       if (StopLoss == 0 && BuyZZReturnValue == 1) StopLossPrice = Prev1Zig - (2 * PointValue);
       if (StopLoss != 0) StopLossPrice = Bid - (StopLoss * PointValue);
       if (TakeProfit == 0 && BuyZZReturnValue == 1) TakeWinPrice = Ask + (Bid - Prev1Zig) + (2 * PointValue);
       if (TakeProfit != 0) TakeWinPrice = Ask + (TakeProfit * PointValue);   
       res = OrderModify(res,OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0,Red);
       if (res > -1)
          { 
          if (Close[0] > 1000) CurrProfitTarget = Wager * Close[0]  * .05;
          else if (Close[0] > 100) CurrProfitTarget = Wager * Close[0]  * .5;
          else if (Close[0] > 10) CurrProfitTarget = Wager * Close[0]  * 5;
          else CurrProfitTarget = Wager / Close[0]  * 50;
          Modified = 1;
          }
  }
//+------------------------------------------------------------------+
//| Open Stop Order                                                  |
//+------------------------------------------------------------------+
void OpenStopOrder()
  {
       int res;
       Comment1 = StringConcatenate(Symbol()," BUY ",Message);
       Wager = LotSize;
       if (LotSize == 0  && RiskBasis == 0) Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2);
       if (LotSize == 0  && RiskBasis == 1) Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 1000 / StopLoss,2);
       // calculate stop loss and take profit levels
       if (BuyStops == 0)
         {
         res = OrderSend(Symbol(),OP_BUYSTOP,Wager,Ask + (StraddleBuffer * Point * 10) ,MaxSlippage,0,0,"BuyStop",MAGICMA,0,Green);
         Modified = 0;
         }
       if (SellStops == 0)
         {
         res=OrderSend(Symbol(),OP_SELLSTOP,Wager,Bid - (StraddleBuffer * Point * 10) ,MaxSlippage,0,0,"SellStop",MAGICMA,0,Green);
         Modified = 0;
         }
  }
//+------------------------------------------------------------------+
//| Open Sell Order                                                  |
//+------------------------------------------------------------------+
void OpenSellOrder()
  {
       int res;
       Comment1 = StringConcatenate(Symbol()," SELL ",Message);
       Wager = LotSize;
       if (LotSize == 0  && RiskBasis == 0) Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2);
       if (LotSize == 0  && RiskBasis == 1) Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 1000 / StopLoss,2);
       if ((Ask - Bid) <= MaxSpread) 
          {
          res=OrderSend(Symbol(),OP_SELL,Wager,Bid,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          }
       // calculate stop loss and take profit levels
       if (StopLoss == 0 && SellPriceEMACrossReturnValue == 1) StopLoss = (High[2]-Close[0])* PointValue;
       if (StopLoss == 0 && SellZZReturnValue == 1) StopLossPrice = Prev1Zig - (2 * PointValue);
       if (StopLoss != 0) StopLossPrice = Ask + (StopLoss * PointValue);
       if (TakeProfit == 0 && SellZZReturnValue == 1) TakeWinPrice = Bid - (Prev1Zig - Bid) - (2 * PointValue);
       if (TakeProfit != 0) TakeWinPrice = Bid - (TakeProfit * PointValue);   
       //Print("Sell Order - Curr Price ",Close[0],", Stop Loss ",StopLossPrice,", Take Win ",TakeWinPrice);
       res = OrderModify(res,OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0,Red);
       if (res > -1)
          {
          if (Close[0] > 1000) CurrProfitTarget = Wager * Close[0]  * .05;
          else if (Close[0] > 100) CurrProfitTarget = Wager * Close[0]  * .5;
          else if (Close[0] > 10) CurrProfitTarget = Wager * Close[0]  * 5;
          else CurrProfitTarget = Wager / Close[0]  * 50;
          Modified = 1;
          }
  }    
//+------------------------------------------------------------------+
//| Check for modify order conditions                                |
//+------------------------------------------------------------------+
void CheckForModify()
  {
  int res;
     for(int i=0;i<OrdersTotal();i++)
         {
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
            {
 
            // For Straddle trades only
            if(OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP) res = OrderDelete(OrderTicket());
            if(OrderType() == OP_SELL && Buys > 0) res = OrderClose(OrderTicket(),OrderLots(),Ask,1,Red);
 
            if (Modified == 0)
               {
               if (OrderType()==OP_BUY) 
                  {
                  //Print("BUY Stop Loss Price ",StopLossPrice,", Take Win Price ",TakeWinPrice, ", Ask ",Ask);  
                  if (StopLoss != 0) StopLossPrice = Bid - (StopLoss * PointValue);
                  if (TakeProfit != 0) TakeWinPrice = Ask + (TakeProfit * PointValue); 
                  res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0,Red);
                  if (res > -1) 
                     {
                     if (Close[0] > 1000) CurrProfitTarget = Wager * Close[0]  * .05;
                     else if (Close[0] > 100) CurrProfitTarget = Wager * Close[0]  * .5;
                     else if (Close[0] > 10) CurrProfitTarget = Wager * Close[0]  * 5;
                     else CurrProfitTarget = Wager / Close[0]  * 50;
                     Modified = 1;
                     }
                  }
               if (OrderType()==OP_SELL) 
                  {
                  //Print("SELL STOP Stop Loss Price ",StopLossPrice,", Take Win Price ",TakeWinPrice,", Bid ",Bid);  
                  if (StopLoss != 0) StopLossPrice = Ask + (StopLoss * PointValue);
                  if (TakeProfit != 0) TakeWinPrice = Bid - (TakeProfit * PointValue);   
                  res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0,Red);
                  if (res > -1) 
                     {
                     if (Close[0] > 1000) CurrProfitTarget = Wager * Close[0]  * .05;
                     else if (Close[0] > 100) CurrProfitTarget = Wager * Close[0]  * .5;
                     else if (Close[0] > 10) CurrProfitTarget = Wager * Close[0]  * 5;
                     else CurrProfitTarget = Wager / Close[0]  * 50;
                     Modified = 1;
                     }
                  }
               }

            // For all trades
            if (Modified == 1)
               {
               if (OrderProfit() > CurrProfitTarget && ((OrderType()==OP_BUY && Bid > PrevBid)|| (OrderType()==OP_SELL && Bid < PrevBid))) 
                  {
                   // set the StopLoss to (Curr price - stop win)
                  if (OrderType()==OP_BUY) StopLossPrice = Bid - (StopLoss * PointValue);
                  if (OrderType()==OP_SELL) StopLossPrice = Ask + (StopLoss * PointValue);
                  res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0, Red);
                  // Set Profit Target to Curr Profit + 5%
                  if (res > -1)
                     {
                     if (Close[0] > 1000) CurrProfitTarget = CurrProfitTarget +( Wager * Close[0]  * .05);
                     else if (Close[0] > 100) CurrProfitTarget = CurrProfitTarget + (Wager * Close[0]  * .5);
                     else if (Close[0] > 10) CurrProfitTarget = CurrProfitTarget + (Wager * Close[0]  * 5);
                     else CurrProfitTarget = CurrProfitTarget + (Wager / Close[0]  * 50);
                     PrevBid = Bid;
                     break;
                     }
                  } 
               }
            }
         } 
  }     
//+------------------------------------------------------------------+
//| Calculate EMA                                                    |
//+------------------------------------------------------------------+
void CalcEMA(int ShiftVal)
  {
  CurrOpen = Open[0];
  CurrFastEMA = NormalizeDouble(iMA(Symbol(),0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,ShiftVal),7);
  PrevFastEMA = NormalizeDouble(iMA(Symbol(),0,ShortEMA,0,MODE_EMA,PRICE_CLOSE,ShiftVal+1),7);
  CurrSlowEMA = NormalizeDouble(iMA(Symbol(),0,LongEMA,0,MODE_EMA,PRICE_CLOSE,ShiftVal),7);
  PrevSlowEMA = NormalizeDouble(iMA(Symbol(),0,LongEMA,0,MODE_EMA,PRICE_CLOSE,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy EMA Cross                                            |
//+------------------------------------------------------------------+
void ProcessBuyEMACross()
  {
  if (CurrFastEMA >= CurrSlowEMA  && PrevFastEMA < PrevSlowEMA && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     BuyEMACrossReturnValue = 1;
     }
  else BuyEMACrossReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell EMA Cross                                           |
//+------------------------------------------------------------------+
void ProcessSellEMACross()
  {
  if (CurrFastEMA <= CurrSlowEMA  && PrevFastEMA > PrevSlowEMA && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     SellEMACrossReturnValue = 1;
     }
  else SellEMACrossReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calculate Price EMA                                              |
//+------------------------------------------------------------------+
void CalcPriceEMA(int ShiftVal)
  {
  CurrOpen = Open[0];
  Close1 = Close[1];
  Close2 = Close[2];
  if (UseHACandles == 1) 
    {
    Close1 = (Open[1]+High[1]+Low[1]+Close[1])/4;
    Close2 = (Open[2]+High[2]+Low[2]+Close[2])/4;
    }
  CurrFastEMA = NormalizeDouble(iMA(Symbol(),0,SingleEMA,EMAShift,MODE_EMA,PRICE_CLOSE,ShiftVal),7);
  PrevFastEMA = NormalizeDouble(iMA(Symbol(),0,SingleEMA,EMAShift,MODE_EMA,PRICE_CLOSE,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy Price EMA Cross                                      |
//+------------------------------------------------------------------+
void ProcessBuyPriceEMACross()
  {
    if (Close1 >= CurrFastEMA  && Close2 < PrevFastEMA && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     BuyPriceEMACrossReturnValue = 1;
     }
  else BuyPriceEMACrossReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell Price EMA Cross                                     |
//+------------------------------------------------------------------+
void ProcessSellPriceEMACross()
  {
    if (Close1 <= CurrFastEMA  && Close2 > PrevFastEMA && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     SellPriceEMACrossReturnValue = 1;
     }
  else SellPriceEMACrossReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calculate RSI                                                    |
//+------------------------------------------------------------------+
void CalcRSI(int ShiftVal)
  {
  CurrOpen = Open[0];
  CurrRSI = NormalizeDouble(iRSI(Symbol(),0,RSIPeriods,PRICE_CLOSE,ShiftVal),7);
  PrevRSI = NormalizeDouble(iRSI(Symbol(),0,RSIPeriods,PRICE_CLOSE,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy RSI Cross                                            |
//+------------------------------------------------------------------+
void ProcessBuyRSICross()
  {
    if (CurrRSI >= RSIBottomLevel && PrevRSI < RSIBottomLevel && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     BuyRSIReturnValue = 1;
     Print("Curr RSI ", CurrRSI,", Prev RSI ",PrevRSI);    
     }
  else BuyRSIReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell RSI Cross                                           |
//+------------------------------------------------------------------+
void ProcessSellRSICross()
  {
    if (CurrRSI <= RSITopLevel && PrevRSI > RSITopLevel && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     SellRSIReturnValue = 1;
     }
  else SellRSIReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calculate MACD                                                   |
//+------------------------------------------------------------------+
void CalcMACD(int ShiftVal)
  {
  CurrOpen = Open[0];
  CurrMain = iMACD(NULL,0,ShortMACD,LongMACD,Signal,PRICE_CLOSE,MODE_MAIN,ShiftVal);
  PrevMain = iMACD(NULL,0,ShortMACD,LongMACD,Signal,PRICE_CLOSE,MODE_MAIN,ShiftVal+1);
  CurrSignal = iMACD(NULL,0,ShortMACD,LongMACD,Signal,PRICE_CLOSE,MODE_SIGNAL,ShiftVal);
  PrevSignal = iMACD(NULL,0,ShortMACD,LongMACD,Signal,PRICE_CLOSE,MODE_SIGNAL,ShiftVal+1);
  }
//+------------------------------------------------------------------+
//| Process Buy MACD Cross                                           |
//+------------------------------------------------------------------+
void ProcessBuyMACDCross()
  {
    if ((CurrMain < 0 && CurrMain >= CurrSignal && PrevMain <= PrevSignal && UseMACDCross == 1 && CurrOpen != PrevOpen)
    ||  (CurrMain >= 0 && PrevMain <= 0 && UseMainZeroCross == 1 &&CurrOpen != PrevOpen)
    ||  (CurrSignal >= 0 && PrevSignal <= 0 && UseSignalZeroCross == 1 &&CurrOpen != PrevOpen))
     {
     PrevOpen = CurrOpen;
     BuyMACDReturnValue = 1;
     }
  else BuyMACDReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell MACD Cross                                          |
//+------------------------------------------------------------------+
void ProcessSellMACDCross()
  {
    if ((CurrMain > 0 && CurrMain <= CurrSignal && PrevMain >= PrevSignal && UseMACDCross == 1 && CurrOpen != PrevOpen)
    ||  (CurrMain <= 0 && PrevMain >= 0 && UseMainZeroCross == 1 &&CurrOpen != PrevOpen)
    ||  (CurrSignal <= 0 && PrevSignal >= 0 && UseSignalZeroCross == 1 &&CurrOpen != PrevOpen))
     {
     PrevOpen = CurrOpen;
     SellMACDReturnValue = 1;
     }
  else SellMACDReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calculate PSAR                                                   |
//+------------------------------------------------------------------+
void CalcPSAR(int ShiftVal)
  {
  CurrOpen = Open[0];
  CurrPSAR = NormalizeDouble(iSAR(Symbol(),0,PSARIncrement,PSARStop,ShiftVal),7);
  PrevPSAR = NormalizeDouble(iSAR(Symbol(),0,PSARIncrement,PSARStop,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy PSAR Cross                                           |
//+------------------------------------------------------------------+
void ProcessBuyPSARCross()
  {
    if (Close[1] >= CurrPSAR && Close[2] < PrevPSAR && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     BuyPSARReturnValue = 1;
     }
  else BuyPSARReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell PSAR Cross                                          |
//+------------------------------------------------------------------+
void ProcessSellPSARCross()
  {
    if (Close[1] <= CurrPSAR && Close[2]> PrevPSAR && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     SellPSARReturnValue = 1;
     }
  else SellPSARReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calculate ZigZag                                                 |
//+------------------------------------------------------------------+
void CalcZig(int ShiftVal)
  {
  int n, i;
  if (ZZDeviation >= ZZDepth) return;
  if (ZZBackstep >= ZZDeviation) return;
  double Zig;
  n=0;
  i = ShiftVal;
  while(n<5)
     {
     Zig=iCustom(NULL,0,"ZigZag",ZZDepth,ZZDeviation,ZZBackstep,0,i);
     if (Zig > 0)
        {
        if (n==1) Prev1Zig = Zig;
        if (n==2) Prev2Zig = Zig;
        if (n==3) Prev3Zig = Zig;
        if (n==4) Prev4Zig = Zig;
        n+=1;
        }
     i++;  
     } 
  }
//+------------------------------------------------------------------+
//| Process Buy Zig                                                  |
//+------------------------------------------------------------------+
void ProcessBuyZig()
  {
  if (Prev1Zig > Prev3Zig && Prev2Zig > Prev4Zig && Prev2Zig > Prev1Zig && Close[0] > Prev2Zig && LastZig != Prev3Zig)
     {
     LastZig = Prev3Zig;
     BuyZZReturnValue = 1;
     }
  else BuyZZReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell Zig                                                 |
//+------------------------------------------------------------------+
void ProcessSellZig()
  {
    if (Prev1Zig < Prev3Zig && Prev2Zig < Prev4Zig && Prev2Zig < Prev1Zig && Close[0] < Prev2Zig && LastZig != Prev3Zig)
     {
     LastZig = Prev3Zig;
     SellZZReturnValue = 1;
     }
  else SellZZReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Straddle                                                |
//+------------------------------------------------------------------+
void ProcessStraddle()
  {
    if (Hour() == TradeHour && Minute() == TradeMinute)
     {
     StraddleReturnValue = 1;
     }
  else StraddleReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calc ATR Breakout                                                      |
//+------------------------------------------------------------------+
void CalcATR(int ShiftVal)
  {
  CurrOpen = Open[0];
  CurrATR = NormalizeDouble(iATR(Symbol(),0,ATRLookbackBars,ShiftVal),7);
  PrevATR = NormalizeDouble(iATR(Symbol(),0,ATRLookbackBars,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy ATR Breakout                                         |
//+------------------------------------------------------------------+
void ProcessBuyATRBreakout()
  {
    if ((CurrATR - PrevATR) >= MinATRPipsPerBar/10000 && Close[0] > Close[1] && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     BuyATRReturnValue = 1;
     }
  else BuyATRReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell ATR Breakout                                        |
//+------------------------------------------------------------------+
void ProcessSellATRBreakout()
  {
    if ((CurrATR - PrevATR) >= MinATRPipsPerBar/10000 && Close[0] < Close[1] && CurrOpen != PrevOpen)
     {
     PrevOpen = CurrOpen;
     SellATRReturnValue = 1;
     }
  else SellATRReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Buy ADX                                                  |
//+------------------------------------------------------------------+
void ProcessBuyADX()
  {
    if (iADX(Symbol(),0,14,PRICE_CLOSE,MODE_MAIN,0) > 25)
     {
     BuyADXReturnValue = 1;
     }
  else BuyADXReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell ADX                                                 |
//+------------------------------------------------------------------+
void ProcessSellADX()
  {
    if (iADX(Symbol(),0,14,PRICE_CLOSE,MODE_MAIN,0) > 25)
     {
     SellADXReturnValue = 1;
     }
  else SellADXReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Buy Time Of day                                          |
//+------------------------------------------------------------------+
void ProcessTimeOfDay()
  {
    if ((Hour() >= TradingHoursStart && Hour() <= TradingHoursEnd) || (TradingHoursStart == 0  && TradingHoursEnd == 0))
     {
     TimeOfDayReturnValue = 1;
     }
  else TimeOfDayReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process News                                                     |
//+------------------------------------------------------------------+
void ProcessNews()
  {
       if (NewsHour1 != -1)
         {
         StartHour1 = NewsHour1;
         StartMinute1 = NewsMinute1 - AvoidMinutesBeforeNews;
         if (StartMinute1 < 0)
           {
           StartMinute1 = StartMinute1 + 60;
           StartHour1 = StartHour1 - 1;
           if (StartHour1 < 0) StartHour1 = StartHour1 + 24;
           }
         EndHour1 = NewsHour1; 
         EndMinute1 = NewsMinute1 + AvoidMinutesAfterNews; 
         if (EndMinute1 > 59)
           {
           EndMinute1 = EndMinute1 - 60;
           EndHour1 = EndHour1 + 1;
           }
         if (EndHour1 > 23) EndHour1 = EndHour1 - 24;
         if (Hour() >= StartHour1 && Minute() >= StartMinute1)
           {
           if (Hour() == EndHour1 && Minute() <= EndMinute1) return; 
           }
         NewsReturnValue1 = 1;  
         }

       if (NewsHour2 != -1)
         {
         StartHour2 = NewsHour2;
         StartMinute2 = NewsMinute2 - AvoidMinutesBeforeNews;
         if (StartMinute2 < 0)
           {
           StartMinute2 = StartMinute2 + 60;
           StartHour2 = StartHour2 - 1;
           if (StartHour2 < 0) StartHour2 = StartHour2 + 24;
           }
         EndHour2 = NewsHour2; 
         EndMinute2 = NewsMinute2 + AvoidMinutesAfterNews; 
         if (EndMinute2 > 59)
           {
           EndMinute2 = EndMinute2 - 60;
           EndHour2 = EndHour2 + 1;
           }
         if (EndHour2 > 23) EndHour2 = EndHour2 - 24;
         if (Hour() >= StartHour2 && Minute() >= StartMinute2)
           {
           if (Hour() == EndHour2 && Minute() <= EndMinute2) return; 
           }
         NewsReturnValue2 = 1;  
         }

       if (NewsHour3 != -1)
         {
         StartHour3 = NewsHour3;
         StartMinute3 = NewsMinute3 - AvoidMinutesBeforeNews;
         if (StartMinute3 < 0)
           {
           StartMinute3 = StartMinute3 + 60;
           StartHour3 = StartHour3 - 1;
           if (StartHour3 < 0) StartHour3 = StartHour3 + 24;
           }
         EndHour3 = NewsHour3; 
         EndMinute3 = NewsMinute3 + AvoidMinutesAfterNews; 
         if (EndMinute3 > 59)
           {
           EndMinute3 = EndMinute3 - 60;
           EndHour3 = EndHour3 + 1;
           }
         if (EndHour3 > 23) EndHour3 = EndHour2 - 24;
         if (Hour() >= StartHour3 && Minute() >= StartMinute3)
           {
           if (Hour() == EndHour3 && Minute() <= EndMinute3) return; 
           }
         NewsReturnValue3 = 1;  
         }

       if (NewsReturnValue1 == 0 && NewsReturnValue2 == 0 && NewsReturnValue3 ==0) NewsReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calc Pivots                                                      |
//+------------------------------------------------------------------+
void CalcPivots()
  {
  double max, min,close;
  min = 9999;
  max = 0;
  for(int i= 24 + Hour() + MT4Offset; i > Hour() + MT4Offset; i--)
     {
      if (iHigh(Symbol(),60,i) > max) max = iHigh(Symbol(),60,i);
      if (iLow(Symbol(),60,i) < min) min = iLow(Symbol(),60,i);
      if (i==MT4Offset + Hour()+ 1) close = iClose(Symbol(),60,i);
     }
  PP = (max + min + close) / 3; 
  R1 = (2 * PP) - min;
  S1 = (2 * PP) - max;
  R2 = (PP - S1) + R1;
  S2 = PP - (R1 - S1);
  R3 = (PP - S2) + R2;
  S3 = PP - (R2 - S2); 
  }
//+------------------------------------------------------------------+
//| Process Buy Pivots Breakout                                      |
//+------------------------------------------------------------------+
void ProcessBuyPivots()
  {
    if ((Close[0] < R1 - (PPBuffer * PointValue) && Close[0] > PP) || (Close[0] < R2 - (PPBuffer * PointValue) && Close[0] > R1)
    ||  (Close[0] < R3 - (PPBuffer * PointValue) && Close[0] > R2) 
    ||  (Close[0] < PP - (PPBuffer * PointValue)) || (Close[0] > R3 + (PPBuffer * PointValue)))
     {
     PrevOpen = CurrOpen;
     BuyPivotsReturnValue = 1;
     }
  else BuyPivotsReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell Pivots                                              |
//+------------------------------------------------------------------+
void ProcessSellPivots()
  {
    if ((Close[0] > S1 + (PPBuffer * PointValue) && Close[0] < PP) || (Close[0] > S2 + (PPBuffer * PointValue) && Close[0] < S1)
    ||  (Close[0] > S3 - (PPBuffer * PointValue) && Close[0] < S2)  
    ||  (Close[0] > PP + (PPBuffer * PointValue)) || (Close[0] < S3 - (PPBuffer * PointValue)))
     {
     PrevOpen = CurrOpen;
     SellPivotsReturnValue = 1;
     }
  else SellPivotsReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calc Fibs                                                        |
//+------------------------------------------------------------------+
void CalcFibs()
  {
  double max, min;
  min = 9999;
  max = 0;
  for(int i= 0; i < FibLookbackBars ; i++)
     {
      if (iHigh(Symbol(),FibTimeFrameInMins,i) > max) max = iHigh(Symbol(),FibTimeFrameInMins,i);
      if (iLow(Symbol(),FibTimeFrameInMins,i) < min) min = iLow(Symbol(),FibTimeFrameInMins,i);
     }
  F0 = max;
  F38 = max - (max - min) *38.2 / 100; 
  F50 = max - (max - min) *50 / 100; 
  F38 = max - (max - min) *61.8 / 100;
  F100 = min; 
  }
//+------------------------------------------------------------------+
//| Process Buy Fibs Breakout                                        |
//+------------------------------------------------------------------+
void ProcessBuyFibs()
  {
    if ((Close[0] < F0 + (FibsBuffer * PointValue) && Close[0] > F38) || (Close[0] < F38 - (FibsBuffer * PointValue) && Close[0] > F50) 
    || (Close[0] < F50 - (FibsBuffer * PointValue) && Close[0] > F62) || (Close[0] < F62 - (FibsBuffer * PointValue) && Close[0] > F100)
    || (Close[0] < F100 - (FibsBuffer * PointValue) || Close[0] > F0 + (FibsBuffer * PointValue)))
     {
     PrevOpen = CurrOpen;
     BuyFibsReturnValue = 1;
     }
  else BuyFibsReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell Fibs                                                |
//+------------------------------------------------------------------+
void ProcessSellFibs()
  {
    if ((Close[0] > F100 + (FibsBuffer * PointValue) && Close[0] < F62) || (Close[0] > F62 + (FibsBuffer * PointValue) && Close[0] < F50) 
    || (Close[0] > F50 + (FibsBuffer * PointValue) && Close[0] < F38) || (Close[0] < F38 + (FibsBuffer * PointValue) && Close[0] < F0)
    || (Close[0] > F0 + (FibsBuffer * PointValue) || Close[0] < F100 - (FibsBuffer * PointValue)))
     {
     PrevOpen = CurrOpen;
     SellFibsReturnValue = 1;
     }
  else SellFibsReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calc Support Resistance                                          |
//+------------------------------------------------------------------+
void CalcSR(int ShiftVal)
  {
  int n, i;
  if (ZZDeviation >= ZZDepth) return;
  if (ZZBackstep >= ZZDeviation) return;
  double Zig;
  n=0;
  i = ShiftVal;
  while(n<5)
     {
     Zig=iCustom(NULL,0,"ZigZag",ZZDepth,ZZDeviation,ZZBackstep,0,i);
     if (Zig > 0)
        {
        if (n==1) Prev1Zig = Zig;
        if (n==2) Prev2Zig = Zig;
        if (n==3) Prev3Zig = Zig;
        if (n==4) Prev4Zig = Zig;
        n+=1;
        }
     i++;  
     } 
  }
//+------------------------------------------------------------------+
//| Process Buy SR Breakout                                          |
//+------------------------------------------------------------------+
void ProcessBuySR()
  {
  if (Prev1Zig > Prev3Zig && Prev2Zig > Prev4Zig && Prev2Zig > Prev1Zig && Close[0] > Prev2Zig - SRBuffer && LastZig != Prev3Zig)
     {
     LastZig = Prev3Zig;
     BuySRReturnValue = 1;
     }
  else BuySRReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell SR                                                  |
//+------------------------------------------------------------------+
void ProcessSellSR()
  {
    if (Prev1Zig < Prev3Zig && Prev2Zig < Prev4Zig && Prev2Zig < Prev1Zig && Close[0] < Prev2Zig + SRBuffer && LastZig != Prev3Zig)
     {
     LastZig = Prev3Zig;
     SellSRReturnValue = 1;
     }
  else SellSRReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Calc Divergence                                                  |
//+------------------------------------------------------------------+
void CalcDivergence()
  {
  for(int i= 0; i < DivergenceLookbackBars ; i++)
     {
     StartRSI = iRSI(Symbol(),DivergenceTimeFrameInMins,14,PRICE_CLOSE,DivergenceLookbackBars);
     EndRSI = iRSI(Symbol(),DivergenceTimeFrameInMins,14,PRICE_CLOSE,1);
     StartMACD = iMACD(NULL,DivergenceTimeFrameInMins,12,26,19,PRICE_CLOSE,MODE_MAIN,DivergenceLookbackBars);
     EndMACD = iMACD(NULL,DivergenceTimeFrameInMins,12,26,19,PRICE_CLOSE,MODE_MAIN,1);
     StartPrice = iClose(Symbol(),DivergenceTimeFrameInMins,DivergenceLookbackBars);
     EndPrice = iClose(Symbol(),DivergenceTimeFrameInMins,1);
     }
  }
//+------------------------------------------------------------------+
//| Process Buy Divergence                                           |
//+------------------------------------------------------------------+
void ProcessBuyDivergence()
  {
  if ((StartRSI > EndRSI || StartMACD > EndMACD) && StartPrice < EndPrice) BuyDivergenceReturnValue = 1;
  else BuyDivergenceReturnValue = 0;
  }
//+------------------------------------------------------------------+
//| Process Sell Divergence                                          |
//+------------------------------------------------------------------+
void ProcessSellDivergence()
  {
  if ((StartRSI < EndRSI || StartMACD < EndMACD) && StartPrice > EndPrice) SellDivergenceReturnValue = 1;
  else SellDivergenceReturnValue = 0;
  }
//+------------------------------------------------------------------+
//| Calc CandleSticks                                                |
//+------------------------------------------------------------------+
void CalcCandleSticks(int ShiftVal)
  {
  CandleSticksBuySignal = 0;
  CandleSticksSellSignal = 0;
  if(candlePatternBullishEngulfing(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternDoji(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternHammer(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternPiercingLine(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBearishEngulfing(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternShootingStar(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternDarkCloudCover(ShiftVal)== true) CandleSticksSellSignal = 1;
  }
//+------------------------------------------------------------------+
//| Process Buy CandleSticks                                         |
//+------------------------------------------------------------------+
void ProcessBuyCandleSticks()
  {
  if (CandleSticksBuySignal == 1) BuyCandleSticksReturnValue = 1;  
  else BuyCandleSticksReturnValue = 0;
  }
//+------------------------------------------------------------------+
//| Process Sell CandleSticks                                        |
//+------------------------------------------------------------------+
void ProcessSellCandleSticks()
  {
  if (CandleSticksSellSignal == 1) SellCandleSticksReturnValue = 1;  
  else SellCandleSticksReturnValue = 0;
  }
//+------------------------------------------------------------------+
//| Candlestick Pattern Functions                                    |
//+------------------------------------------------------------------+

bool candlePatternBearishEngulfing(int ShiftV) {
   double O = Open[ShiftV];
   double O1 = Open[ShiftV+1];
   double C = Close[ShiftV];
   double C1 = Close[ShiftV+1];

   if ((C1>O1)&&(O>C)&&(O>=C1)&&(O1>=C)&&((O-C)>(C1-O1))) {
      return(true);
   }

   return(false);
}


bool candlePatternBullishEngulfing(int ShiftV) {
   double O = Open[ShiftV];
   double O1 = Open[ShiftV+1];
   double C = Close[ShiftV];
   double C1 = Close[ShiftV+1];

   if ((O1>C1)&&(C>O)&&(C>=O1)&&(C1>=O)&&((C-O)>(O1-C1))) {
      return(true);
   }

   return(false);
}

//+------------------------------------------------------------------+

bool candlePatternDarkCloudCover(int ShiftV) {
   double L = Low[ShiftV];
   double H = High[ShiftV];

   double O = Open[ShiftV];
   double O1 = Open[ShiftV+1];
   double C = Close[ShiftV];
   double C1 = Close[ShiftV+1];
   double CL = H-L;

   double OC_HL;
   if((H - L) != 0) {
      OC_HL = (O-C)/(H-L);
   } else {
      OC_HL = 0;
   }

   double Piercing_Line_Ratio = 0.5;
   double Piercing_Candle_Length = 10;

   if ((C1>O1)&&(((C1+O1)/2)>C)&&(O>C)&&(C>O1)&&(OC_HL>Piercing_Line_Ratio)&&((CL>=Piercing_Candle_Length*gPointCoef))) {
      return(true);
   }

   return(false);
}

//+------------------------------------------------------------------+

bool candlePatternDoji(int ShiftV) {
   if(MathAbs(Open[ShiftV] - Close[ShiftV])*gPointPow < 0.6) {
      return(true);
   }
   return(false);
}

//+------------------------------------------------------------------+

bool candlePatternHammer(int ShiftV) {
   double H = High[ShiftV];
   double L = Low[ShiftV];
   double L1 = Low[ShiftV+1];
   double L2 = Low[ShiftV+2];
   double L3 = Low[ShiftV+3];

   double O = Open[ShiftV];
   double C = Close[ShiftV];
   double CL = H-L;

   double BodyLow, BodyHigh;
   double Candle_WickBody_Percent = 0.9;
   double CandleLength = 12;

   if (O > C) {
      BodyHigh = O;
      BodyLow = C;
   } else {
      BodyHigh = C;
      BodyLow = O;
   }

   double LW = BodyLow-L;
   double UW = H-BodyHigh;
   double BLa = MathAbs(O-C);
   double BL90 = BLa*Candle_WickBody_Percent;

   double pipValue = gPointCoef;

   if ((L<=L1)&&(L<L2)&&(L<L3))  {
      if (((LW/2)>UW)&&(LW>BL90)&&(CL>=(CandleLength*pipValue))&&(O!=C)&&((LW/3)<=UW)&&((LW/4)<=UW)/*&&(H<H1)&&(H<H2)*/)  {
         return(true);
      }
      if (((LW/3)>UW)&&(LW>BL90)&&(CL>=(CandleLength*pipValue))&&(O!=C)&&((LW/4)<=UW)/*&&(H<H1)&&(H<H2)*/)  {
         return(true);
      }
      if (((LW/4)>UW)&&(LW>BL90)&&(CL>=(CandleLength*pipValue))&&(O!=C)/*&&(H<H1)&&(H<H2)*/)  {
         return(true);
      }
   }

   return(false);
}

//+------------------------------------------------------------------+

bool candlePatternPiercingLine(int ShiftV) {
   double L = Low[ShiftV];
   double H = High[ShiftV];

   double O = Open[ShiftV];
   double O1 = Open[ShiftV+1];
   double C = Close[ShiftV];
   double C1 = Close[ShiftV+1];
   double CL = H-L;

   double CO_HL;
   if((H - L) != 0) {
      CO_HL = (C-O)/(H-L);
   } else {
      CO_HL = 0;
   }

   double Piercing_Line_Ratio = 0.5;
   double Piercing_Candle_Length = 10;

   if ((C1<O1)&&(((O1+C1)/2)<C)&&(O<C) && (CO_HL>Piercing_Line_Ratio)&&(CL>=(Piercing_Candle_Length*gPointCoef))) {
      return(true);
   }

   return(false);
}

//+------------------------------------------------------------------+

bool candlePatternShootingStar(int ShiftV) {
   double L = Low[ShiftV];
   double H = High[ShiftV];
   double H1 = High[ShiftV+1];
   double H2 = High[ShiftV+2];
   double H3 = High[ShiftV+3];

   double O = Open[ShiftV];
   double C = Close[ShiftV];
   double CL = H-L;

   double BodyLow, BodyHigh;
   double Candle_WickBody_Percent = 0.9;
   double CandleLength = 12;

   if (O > C) {
      BodyHigh = O;
      BodyLow = C;
   } else {
      BodyHigh = C;
      BodyLow = O;
   }

   double LW = BodyLow-L;
   double UW = H-BodyHigh;
   double BLa = MathAbs(O-C);
   double BL90 = BLa*Candle_WickBody_Percent;

   double pipValue = gPointCoef;

   if ((H>=H1)&&(H>H2)&&(H>H3))  {
      if (((UW/2)>LW)&&(UW>(2*BL90))&&(CL>=(CandleLength*pipValue))&&(O!=C)&&((UW/3)<=LW)&&((UW/4)<=LW)/*&&(L>L1)&&(L>L2)*/)  {
         return(true);
      }
      if (((UW/3)>LW)&&(UW>(2*BL90))&&(CL>=(CandleLength*pipValue))&&(O!=C)&&((UW/4)<=LW)/*&&(L>L1)&&(L>L2)*/)  {
         return(true);
      }
      if (((UW/4)>LW)&&(UW>(2*BL90))&&(CL>=(CandleLength*pipValue))&&(O!=C)/*&&(L>L1)&&(L>L2)*/)  {
         return(true);
      }
   }

   return(false);
}
//+------------------------------------------------------------------+
