//+----------------------------------------------------------+
//|                    Traders Gift EA .mq4                  |
//|               Copyright © 2015, David Currey             |
//+----------------------------------------------------------+
extern int MAGICMA = 3333300;
//extern string Hdr00 = ">>> EA INFO BOX";
//extern int XPosition = 5;
//extern int YPosition = 20;
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 = "TradeClosureMode: Use StopLoss=0, Use Signal Reversal=1, Use TradersDynamicIndex=2";
extern int TradeClosureMode = 0;
extern string Hlp1c = "If Trade Closure Mode=1 or 2, or for StopLoss using ATR, use StopLoss=0";
extern int StopLoss = 10;
extern int StopLossATRPeriods = 0;
extern double StopLossMultiplier = 0;
extern string Hlp1d = "UseTrailingStopLoss: No=0, Yes=1";
extern int UseTrailingStopLoss = 1;
extern string Hlp1e = "For TakeProfit using ATR, use TakeProfit=0";
extern int TakeProfit = 10;
extern int TakeProfitATRPeriods = 0;
extern double TakeProfitMultiplier = 0;
extern string Hlp1f = "SetStopLossToBreakEvenPips: no profit protection=0, 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 = 30;
extern string Hlp2a = "The Maximum number of bars ago that the cross took place";
extern int EMAMaxBarsAgo = 1;
extern string Hdr3 = ">>> PRICE/EMA Event";
extern string Hlp3 = "UsePriceEMAEvent: No=0, Cross=1, Position=2";
extern int UsePriceEMAEvent = 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 RSITimeFrame = 0;
extern int RSIPeriods2 = 0;
extern int RSITopLevel2 = 0;
extern int RSIBottomLevel2 = 0;
extern int RSITimeFrame2 = 0;
extern int RSIPeriods3 = 0;
extern int RSITopLevel3 = 0;
extern int RSIBottomLevel3 = 0;
extern int RSITimeFrame3 = 0;
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 Hdr19 = ">>> STOC CROSS";
extern string Hlp19 = "UseStocCross: No=0, Yes=1";
extern int UseSTOCCross = 0;
extern int KPeriod = 5;
extern int Slowing = 3;
extern int DPeriod = 3;
extern string Hlp19a = "CalculationMethod: Simple=0, Exponential=1";
extern int CalculationMethod = 0;
extern string Hlp19b = "CalculationMode: Low/High=0, Close=1";
extern int CalculationMode = 1;
extern int STOCMaxBarsAgo = 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, TIME OF DAY, PRICE or EMA STRADDLE";
extern string Hlp8 = "UseStraddle: No=0, Yes=1";
extern int UseStraddle = 0;
extern string Hlp8a = "StraddleType: Time=0, Price=1, EMA Cross=2";
extern int StraddleType = 0;
extern int StraddleBuffer = 5;         
extern int TradeHour = 0;             
extern int TradeMinute = 0;             
extern double TradePrice = 0;
extern int EMAPeriod = 200;             
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, Cross=1, Position=2";
extern int UseADX = 0;
extern int ADXPeriods = 14;
extern int ADXLevel = 25;
extern int ADXMaxBarsAgo = 1;
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 PrevOpen;
double PrevValue;
double PrevBid;
double Wager;
int PrevEquity;
double PrevWager;
int ConsecLosses;
double Spread;
double BreakEvenPrice;
//-------------------------------------
// 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;
double CurrRSI2;
double PrevRSI2;
double CurrRSI3;
double PrevRSI3;
int BuyRSIReturnValue;
int SellRSIReturnValue;
int BuyRSIReturnValue2;
int SellRSIReturnValue2;
int BuyRSIReturnValue3;
int SellRSIReturnValue3;
//-------------------------------------
// MACD Cross variables
double CurrMain;
double PrevMain;
double CurrSignal;
double PrevSignal;
int BuyMACDReturnValue;
int SellMACDReturnValue;
//-------------------------------------
// STOC Cross variables
double CurrentSlowD;
double PreviousSlowD;
double CurrentSlowK;
double PreviousSlowK;
int BuySTOCReturnValue;
int SellSTOCReturnValue;
//-------------------------------------
// 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;
double CurrADX;
double PrevADX;
//-------------------------------------
// 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;
//-------------------------------------
// TDI variables
int BuyTDIReturnValue;
int SellTDIReturnValue;
int TDIBuySignal;
int TDISellSignal;
double RSIBuffer[9];
double TDIBuffer[8];
//-------------------------------------
// General Variables
extern string Message = "Traders Gift";
string Comment1;
string Message2;
int Buys, Sells, BuyStops, SellStops;
int Shift;
int Modified;
int BreakEvenCompleted;
double CurrPrice;
double PrevPrice;
int res, i;
int BuyBarCross, SellBarCross;
int ObjectCount;
string EAMode;
string ClosureMode;
string WagerMode;
int DisableTrading;
//+------------------------------------------------------------------+
//| Init function                                                   |
//+------------------------------------------------------------------+
void init()
  {
  ObjectCreate("ObjRectangle", OBJ_RECTANGLE_LABEL, 0, 0, 0);
  ObjectSet("ObjRectangle", OBJPROP_CORNER, 0);
  ObjectSet("ObjRectangle", OBJPROP_XDISTANCE,5);
  ObjectSet("ObjRectangle", OBJPROP_YDISTANCE,20);
  ObjectSet("ObjRectangle", OBJPROP_XSIZE,240);
  //ObjectSet("ObjRectangle", OBJPROP_YSIZE,(ObjectCount + 2) * 10);
  ObjectSet("ObjRectangle", OBJPROP_BGCOLOR,clrWhite);
  ObjectSet("ObjRectangle", OBJPROP_BORDER_TYPE,BORDER_FLAT);

  ObjectCreate("ObjEA", OBJ_LABEL, 0, 0, 0);
  TextSetFont("Verdana",7,FW_BOLD,0);
  ObjectSetText("ObjEA","                    TRADERS GIFT EA",7, "Verdana", clrBlue);
  TextSetFont("Verdana",7,FW_REGULAR,0);
  ObjectSet("ObjEA", OBJPROP_CORNER, 0);
  ObjectSet("ObjEA", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjEA", OBJPROP_YDISTANCE, 20);

  ObjectCount = ObjectCount + 1;
  ObjectCreate("ObjUnderline1", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjUnderline1","                    ==============",7, "Verdana", clrBlue);
  ObjectSet("ObjUnderline1", OBJPROP_CORNER, 0);
  ObjectSet("ObjUnderline1", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjUnderline1", OBJPROP_YDISTANCE, 30);


  ObjectCreate("ObjSpread", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjSpread","Spread " + (Ask-Bid) ,7, "Verdana", clrMaroon);
  ObjectSet("ObjSpread", OBJPROP_CORNER, 0);
  ObjectSet("ObjSpread", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjSpread", OBJPROP_YDISTANCE, 40);
  
  if (TradingMode == 0) EAMode = "Automatic";
  if (TradingMode == 1) EAMode = "Advisory";
  if (TradingMode == 2) EAMode = "Discretionary";
  ObjectCreate("ObjEAMode", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjEAMode","Trading Mode - " + EAMode ,7, "Verdana", clrMaroon);
  ObjectSet("ObjEAMode", OBJPROP_CORNER, 0);
  ObjectSet("ObjEAMode", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjEAMode", OBJPROP_YDISTANCE, 50);

  if (LotSize != 0) WagerMode = "Fixed Lots";
  if (RiskBasis == 0 && LotSize == 0) WagerMode = "On Equity";
  if (RiskBasis == 1 && LotSize == 0) WagerMode = "On Stop Loss";
  ObjectCreate("ObjWagerMode", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjWagerMode","Money Management Mode - " + WagerMode ,7, "Verdana", clrMaroon);
  ObjectSet("ObjWagerMode", OBJPROP_CORNER, 0);
  ObjectSet("ObjWagerMode", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjWagerMode", OBJPROP_YDISTANCE, 60);

  if (TradeClosureMode == 0) ClosureMode = "Stop Loss or Take Profit";
  if (TradeClosureMode == 1) ClosureMode = "Signal Reversal";
  if (TradeClosureMode == 2) ClosureMode = "Traders Dynamic Index";
  ObjectCreate("ObjTradeClosureMode", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjTradeClosureMode","TradeClosureMode - " + ClosureMode ,7, "Verdana", clrMaroon);
  ObjectSet("ObjTradeClosureMode", OBJPROP_CORNER, 0);
  ObjectSet("ObjTradeClosureMode", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjTradeClosureMode", OBJPROP_YDISTANCE, 70);

  ObjectCreate("ObjStopLossMultiplier", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjStopLossMultiplier","Stop Loss Multiplier " + StopLossMultiplier ,7, "Verdana", clrMaroon);
  ObjectSet("ObjStopLossMultiplier", OBJPROP_CORNER, 0);
  ObjectSet("ObjStopLossMultiplier", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjStopLossMultiplier", OBJPROP_YDISTANCE, 80);

  ObjectCreate("ObjStopLoss", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjStopLoss","Stop Loss " + StopLoss ,7, "Verdana", clrMaroon);
  ObjectSet("ObjStopLoss", OBJPROP_CORNER, 0);
  ObjectSet("ObjStopLoss", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjStopLoss", OBJPROP_YDISTANCE, 90);
    
  ObjectCreate("ObjTakeProfitMultiplier", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjTakeProfitMultiplier","Take Profit Multiplier " + TakeProfitMultiplier ,7, "Verdana", clrMaroon);
  ObjectSet("ObjTakeProfitMultiplier", OBJPROP_CORNER, 0);
  ObjectSet("ObjTakeProfitMultiplier", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjTakeProfitMultiplier", OBJPROP_YDISTANCE,100);
    
  if (TakeProfitATRPeriods == 0 && TakeProfitMultiplier != 0) TakeProfit = StopLoss * TakeProfitMultiplier;
  ObjectCreate("ObjTakeProfit", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjTakeProfit","Take Profit " + TakeProfit ,7, "Verdana", clrMaroon);
  ObjectSet("ObjTakeProfit", OBJPROP_CORNER, 0);
  ObjectSet("ObjTakeProfit", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjTakeProfit", OBJPROP_YDISTANCE,110);
    
  ObjectCount = 11;
  if (SetStopLossToBreakEvenPips > 0)
     {
     ObjectCount = ObjectCount + 1;
     ObjectCreate("ObjSetStopLossToBreakEvenPips", OBJ_LABEL, 0, 0, 0);
     ObjectSetText("ObjSetStopLossToBreakEvenPips","Set StopLoss To BreakEven " + SetStopLossToBreakEvenPips,7, "Verdana", clrMaroon);
     ObjectSet("ObjSetStopLossToBreakEvenPips", OBJPROP_CORNER, 0);
     ObjectSet("ObjSetStopLossToBreakEvenPips", OBJPROP_XDISTANCE, 10);
     ObjectSet("ObjSetStopLossToBreakEvenPips", OBJPROP_YDISTANCE, ObjectCount*10);
     }
    
  ObjectCount = ObjectCount + 3;
  ObjectCreate("ObjActiveStrategies", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjActiveStrategies","ACTIVE STRATEGIES",7, "Verdana", clrBlue);
  ObjectSet("ObjActiveStrategies", OBJPROP_CORNER, 0);
  ObjectSet("ObjActiveStrategies", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjActiveStrategies", OBJPROP_YDISTANCE, ObjectCount*10);
    
  ObjectCount = ObjectCount + 1;
  ObjectCreate("ObjUnderline2", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjUnderline2","------------------------",7, "Verdana", clrBlue);
  ObjectSet("ObjUnderline2", OBJPROP_CORNER, 0);
  ObjectSet("ObjUnderline2", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjUnderline2", OBJPROP_YDISTANCE, ObjectCount*10);

  ObjectCount = ObjectCount + 1;
  ObjectCreate("ObjParameters", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjParameters","(Lookback)    Strategy    (Other Parameters)",7, "Verdana", clrMaroon);
  ObjectSet("ObjParameters", OBJPROP_CORNER, 0);
  ObjectSet("ObjParameters", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjParameters", OBJPROP_YDISTANCE, ObjectCount*10);
  ObjectCount = ObjectCount + 1;

  if (UseEMACross > 0)CreateObject("ObjEMACross",EMAMaxBarsAgo,"Using EMACross",ShortEMA,LongEMA,"");
  if (UsePriceEMAEvent > 0)CreateObject("ObjPriceEMAEvent",PriceEMAMaxBarsAgo,"Using EMACross",SingleEMA,"","");
  if (UseRSICross > 0)CreateObject("ObjRSICross",RSIMaxBarsAgo,"Using RSICross",RSIPeriods,RSITopLevel,RSIBottomLevel);
  if (UseRSICross > 0 && RSIPeriods2 != 0) CreateObject("ObjRSICross2",RSIMaxBarsAgo,"Using RSICross2",RSIPeriods2,RSITopLevel2,RSIBottomLevel2);
  if (UseRSICross > 0 && RSIPeriods3 != 0) CreateObject("ObjRSICross3",RSIMaxBarsAgo,"Using RSICross3",RSIPeriods3,RSITopLevel3,RSIBottomLevel3);
  if (UseMACDCross > 0)CreateObject("ObjMACDCross",MACDMaxBarsAgo,"Using MACDCross",ShortMACD,LongMACD,Signal);
  if (UseSTOCCross > 0)CreateObject("ObjSTOCCross",STOCMaxBarsAgo,"Using STOCCross",KPeriod,Slowing,DPeriod);
  if (UsePSAR > 0)CreateObject("ObjPSAR",PSARMaxBarsAgo,"Using PSAR",PSARStop,PSARIncrement,"");
  if (UseZigZag > 0)CreateObject("ObjZigZag",ZZMaxBarsAgo,"Using ZigZag",ZZDepth,ZZDeviation,ZZBackstep);
  if (UseStraddle > 0)CreateObject("ObjStraddle","","Using Straddle",StraddleType,StraddleBuffer,"");
  if (UseContinuousStraddle > 0)CreateObject("ObjContStraddle","","Using Continuous Straddle",ContinuousStraddleBuffer,"","");
  if (UseATRBreakout > 0)CreateObject("ObjATRBreakout",ATRMaxBarsAgo,"Using ATR Breakout",ATRLookbackBars,MinATRPipsPerBar,"");
  if (UseADX > 0)CreateObject("ObjADX",ADXMaxBarsAgo,"Using ADX",ADXPeriods,ADXLevel,"");
  if (UseTimeOfDay > 0)CreateObject("ObjTimeOfDay","","Using Time Of Day",TradingHoursStart,TradingHoursEnd,"");
  if (UseNews > 0)CreateObject("ObjNews","","Using News",NewsHour1 + ":" + NewsMinute1,AvoidMinutesBeforeNews,AvoidMinutesAfterNews);
  if (UseNews > 0 && NewsHour2 > -1)CreateObject("ObjNews2","","Using News2",NewsHour2 + ":" + NewsMinute2,AvoidMinutesBeforeNews,AvoidMinutesAfterNews);
  if (UseNews > 0 && NewsHour3 > -1)CreateObject("ObjNews3","","Using News3",NewsHour3 + ":" + NewsMinute3,AvoidMinutesBeforeNews,AvoidMinutesAfterNews);
  if (UsePivots > 0)CreateObject("ObjPivots","","Using Pivots","","","");
  if (UseFibs > 0)CreateObject("ObjFibs","","Using Fibonacci","","","");
  if (UseSR > 0)CreateObject("ObjSR","","Using Support / Resistance","","","");
  if (UseDivergence > 0)CreateObject("ObjDivergence","","Using Divergence","","","");
  if (UseCandleSticks > 0)CreateObject("ObjCandleSticks",CandleSticksMaxBarsAgo,"Using CandleSticks","","","");
  ObjectSet("ObjRectangle", OBJPROP_YSIZE,(ObjectCount-1) * 10 +5);
  }
//+------------------------------------------------------------------+
//| CreateObject function                                                   |
//+------------------------------------------------------------------+
void CreateObject(string Name,string Lookback,string Descr,string Value1,string Value2,string Value3)
  {
  ObjectCount = ObjectCount + 1;
  ObjectCreate(Name, OBJ_LABEL, 0, 0, 0);
  ObjectSetText(Name,"(" + Lookback + ")   " + Descr + "   (" + Value1 + ", " + Value2 + ", " + Value3 + ")",7, "Verdana", clrBlue);
  ObjectSet(Name, OBJPROP_CORNER, 0);
  ObjectSet(Name, OBJPROP_XDISTANCE, 10);
  ObjectSet(Name, OBJPROP_YDISTANCE, (ObjectCount*10));
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
  if(Bars<100 || IsTradeAllowed()==false) return;
  ObjectCreate("ObjSpread", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjSpread","Spread " + (Ask-Bid) ,7, "Verdana", clrMaroon);
  ObjectSet("ObjSpread", OBJPROP_CORNER, 0);
  ObjectSet("ObjSpread", OBJPROP_XDISTANCE, 10);
  ObjectSet("ObjSpread", OBJPROP_YDISTANCE, 40);
  if(TradeClosureMode > 0)
    {
    StopLoss = 0;
    TakeProfit = 0;
    }
  if(StringFind(Symbol(),"JPY") > (-1)) PointValue = .01;
  else PointValue = .0001;
  if(CalculateCurrOrders(Symbol())==0)
    {
    if (DisableTrading == 0)CheckForOpen();
    }
  else
    { 
    if (TradeClosureMode == 1 && DisableTrading == 0) CheckForOpen();
    if (DisableTrading == 0)CheckForClose();
    if (DisableTrading == 0)CheckForModify();
    }
  } 
//+------------------------------------------------------------------+
//| Deinit function                                                   |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  ObjectDelete("ObjRectangle");
  ObjectDelete("ObjEA");
  ObjectDelete("ObjUnderline1");
  ObjectDelete("ObjSpread");
  ObjectDelete("ObjEAMode");
  ObjectDelete("ObjWagerMode");
  ObjectDelete("ObjTradeClosureMode");
  ObjectDelete("ObjStopLossMultiplier");
  ObjectDelete("ObjStopLoss");
  ObjectDelete("ObjTakeProfitMultiplier");
  ObjectDelete("ObjTakeProfit");
  ObjectDelete("ObjSetStopLossToBreakEvenPips");
  ObjectDelete("ObjActiveStrategies");
  ObjectDelete("ObjUnderline2");
  ObjectDelete("ObjParameters");
  ObjectDelete("ObjEMACross");
  ObjectDelete("ObjPriceEMAEvent");
  ObjectDelete("ObjRSICross");
  ObjectDelete("ObjRSICross2");
  ObjectDelete("ObjRSICross3");
  ObjectDelete("ObjMACDCross");
  ObjectDelete("ObjSTOCCross");
  ObjectDelete("ObjPSAR");
  ObjectDelete("ObjZigZag");
  ObjectDelete("ObjStraddle");
  ObjectDelete("ObjATRBreakout");
  ObjectDelete("ObjADX");
  ObjectDelete("ObjTimeOfDay");
  ObjectDelete("ObjNews");
  ObjectDelete("ObjNews2");
  ObjectDelete("ObjNews3");
  ObjectDelete("ObjPivots");
  ObjectDelete("ObjFibs");
  ObjectDelete("ObjSR");
  ObjectDelete("ObjDivergence");
  ObjectDelete("ObjCandleSticks");
  }
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrOrders(string symbol)
  {
  Buys=0;
  Sells=0;
  BuyStops=0;
  SellStops=0;
  DisableTrading = 0;
//----
   for(i=0;i<OrdersTotal();i++)
     {
      if(OrderType()==OP_BUYLIMIT) DisableTrading=1;
      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()
  {
    if (TradeClosureMode == 2 && (Buys > 0 || Sells > 0)) 
       {
       for(Shift = 1; Shift < 2; Shift++)
          {
          CalcTDI(Shift);
          ProcessBuyTDICross();
          ProcessSellTDICross();
          }
       if (BuyTDIReturnValue == 0 && SellTDIReturnValue == 0) return;
       }
    if ((TradeClosureMode == 1 || (TradeClosureMode == 2 && SellTDIReturnValue == 1))  && 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) 
              {
              if (SellEMACrossReturnValue == 0  && UseEMACross == 1) return;
              if (SellPriceEMACrossReturnValue == 0  && (UsePriceEMAEvent == 1 || UsePriceEMAEvent == 2)) return;
              if (SellRSIReturnValue == 0 && RSIPeriods != 0 && UseRSICross == 1) return;
              if (SellRSIReturnValue2 == 0 && RSIPeriods2 != 0 && UseRSICross == 1) return;
              if (SellRSIReturnValue3 == 0 && RSIPeriods3 != 0 && UseRSICross == 1) return;
              if (SellMACDReturnValue == 0  && UseMACDCross == 1) return;
              if (SellMACDReturnValue == 0  && UseMainZeroCross == 1) return;
              if (SellMACDReturnValue == 0  && UseSignalZeroCross == 1) return;
              if (SellSTOCReturnValue == 0  && UseSTOCCross == 1) return;
              if (SellPSARReturnValue == 0  && UsePSAR == 1) return;
              if (SellZZReturnValue == 0  && UseZigZag == 1) return;
              if (SellATRReturnValue == 0  && UseATRBreakout == 1) return;
              if (SellPivotsReturnValue == 0  && UsePivots == 1) return;
              if (SellFibsReturnValue == 0  && UseFibs == 1) return;
              if (SellSRReturnValue == 0  && UseSR == 1) return;
              if (SellDivergenceReturnValue == 0  && UseDivergence == 1) return;
              if (SellCandleSticksReturnValue == 0  && UseCandleSticks == 1) return;
              res = OrderClose(OrderTicket(),OrderLots(),Bid,1,clrMaroon);
              return;
              }
           } 
       } 
    if ((TradeClosureMode == 1 || (TradeClosureMode == 2 && BuyTDIReturnValue == 1))  && 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) 
              {
              if (BuyEMACrossReturnValue == 0  && UseEMACross == 1) return;
              if (BuyPriceEMACrossReturnValue == 0  && (UsePriceEMAEvent == 1 || UsePriceEMAEvent == 2)) return;
              if (BuyRSIReturnValue == 0 && RSIPeriods != 0 && UseRSICross == 1) return;
              if (BuyRSIReturnValue2 == 0 && RSIPeriods2 != 0 && UseRSICross == 1) return;
              if (BuyRSIReturnValue3 == 0 && RSIPeriods3 != 0 && UseRSICross == 1) return;
              if (BuyMACDReturnValue == 0  && UseMACDCross == 1) return;
              if (BuyMACDReturnValue == 0  && UseMainZeroCross == 1) return;
              if (BuyMACDReturnValue == 0  && UseSignalZeroCross == 1) return;
              if (BuySTOCReturnValue == 0  && UseSTOCCross == 1) return;
              if (BuyPSARReturnValue == 0  && UsePSAR == 1) return;
              if (BuyZZReturnValue == 0  && UseZigZag == 1) return;
              if (BuyATRReturnValue == 0  && UseATRBreakout == 1) return;
              if (BuyPivotsReturnValue == 0  && UsePivots == 1) return;
              if (BuyFibsReturnValue == 0  && UseFibs == 1) return;
              if (BuySRReturnValue == 0  && UseSR == 1) return;
              if (BuyDivergenceReturnValue == 0  && UseDivergence == 1) return;
              if (BuyCandleSticksReturnValue == 0  && UseCandleSticks == 1) return;
              res = OrderClose(OrderTicket(),OrderLots(),Ask,1,clrMaroon);
              return;
              }
           } 
       } 
  }
//+------------------------------------------------------------------+
//| Calculate TDI                                                    |
//+------------------------------------------------------------------+
void CalcTDI(int ShiftVal)
  {
  for (i=9; i>=0; i--)
      {
      RSIBuffer[i] = (iRSI(NULL,0,13,PRICE_CLOSE,i)); 
      } 
  for (i=8; i>=0; i--)
      {
      TDIBuffer[i] = (iMAOnArray(RSIBuffer,0,7,0,MODE_SMA,i)); 
      }  
  }
//+------------------------------------------------------------------+
//| Process Buy TDI Cross                                            |
//+------------------------------------------------------------------+
void ProcessBuyTDICross()
  {
  if ((TDIBuffer[1] > 50)) BuyTDIReturnValue = 1;
  else BuyTDIReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Process Sell TDI Cross                                           |
//+------------------------------------------------------------------+
void ProcessSellTDICross()
  {
  if ((TDIBuffer[1] < 50)) SellTDIReturnValue = 1;
  else SellTDIReturnValue = 0;  
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
    // ---- Buy and Sell conditions ---- 
    ObjectCount = 7;
    if (UseEMACross > 0) 
       {
       BuyEMACrossReturnValue = 0;
       SellEMACrossReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = EMAMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcEMA(Shift);
          ProcessBuyEMACross();
          ProcessSellEMACross();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellEMACrossReturnValue = 0;
           BuyEMACrossReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyEMACrossReturnValue = 0;
           SellEMACrossReturnValue = 1;
           }
       if (BuyEMACrossReturnValue == 0 && SellEMACrossReturnValue == 0) return;
       }
    if (UsePriceEMAEvent > 0) 
       {
       BuyPriceEMACrossReturnValue = 0;
       SellPriceEMACrossReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = PriceEMAMaxBarsAgo; Shift > 0; Shift--)
           {
           CalcPriceEMA(Shift);
           ProcessBuyPriceEMACross();
           ProcessSellPriceEMACross();
           }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellPriceEMACrossReturnValue = 0;
           BuyPriceEMACrossReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyPriceEMACrossReturnValue = 0;
           SellPriceEMACrossReturnValue = 1;
           }
       if (BuyPriceEMACrossReturnValue == 0 && SellPriceEMACrossReturnValue == 0) return;
       }
    if (UseRSICross > 0) 
       {
       BuyRSIReturnValue = 0;
       SellRSIReturnValue = 0;
       BuyRSIReturnValue2 = 0;
       SellRSIReturnValue2 = 0;
       BuyRSIReturnValue3 = 0;
       SellRSIReturnValue3 = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = RSIMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcRSI(Shift);
          ProcessBuyRSICross();
          ProcessSellRSICross();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellRSIReturnValue = 0;
           BuyRSIReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyRSIReturnValue = 0;
           SellRSIReturnValue = 1;
           }
       if (BuyRSIReturnValue == 0 && SellRSIReturnValue == 0) return;
       }
    if (UseMACDCross > 0 || UseMainZeroCross > 0 || UseSignalZeroCross > 0) 
       {
       BuyMACDReturnValue = 0;
       SellMACDReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = MACDMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcMACD(Shift);
          ProcessBuyMACDCross();
          ProcessSellMACDCross();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellMACDReturnValue = 0;
           BuyMACDReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyMACDReturnValue = 0;
           SellMACDReturnValue = 1;
           }
       if (BuyMACDReturnValue == 0 && SellMACDReturnValue == 0) return;
       }
    if (UseSTOCCross > 0) 
       {
       BuySTOCReturnValue = 0;
       SellSTOCReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = STOCMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcSTOC(Shift);
          ProcessBuySTOCCross();
          ProcessSellSTOCCross();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellSTOCReturnValue = 0;
           BuySTOCReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuySTOCReturnValue = 0;
           SellSTOCReturnValue = 1;
           }
       if (BuySTOCReturnValue == 0 && SellSTOCReturnValue == 0) return;
       }
    if (UsePSAR > 0) 
       {
       BuyPSARReturnValue = 0;
       SellPSARReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = PSARMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcPSAR(Shift);
          ProcessBuyPSARCross();
          ProcessSellPSARCross();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellPSARReturnValue = 0;
           BuyPSARReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyPSARReturnValue = 0;
           SellPSARReturnValue = 1;
           }
       if (BuyPSARReturnValue == 0 && SellPSARReturnValue == 0) return;
       }
    if (UseZigZag > 0) 
       {
       BuyZZReturnValue = 0;
       SellZZReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = ZZMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcZig(Shift);
          ProcessBuyZig();
          ProcessSellZig();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellZZReturnValue = 0;
           BuyZZReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyZZReturnValue = 0;
           SellZZReturnValue = 1;
           }
       if (BuyZZReturnValue == 0 && SellZZReturnValue == 0) return;
       }
    if (UseStraddle > 0) 
       {
       StraddleReturnValue = 0;
       ProcessStraddle();
       }
    if (UseATRBreakout > 0) 
       {
       BuyATRReturnValue = 0;
       SellATRReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = ATRMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcATR(Shift);
          ProcessBuyATRBreakout();
          ProcessSellATRBreakout();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellATRReturnValue = 0;
           BuyATRReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyATRReturnValue = 0;
           SellATRReturnValue = 1;
           }
       if (BuyATRReturnValue == 0 && SellATRReturnValue == 0) return;
       }
    if (UseADX > 0) 
       {
       BuyADXReturnValue = 0;
       SellADXReturnValue = 0;
       ProcessCalcADX(Shift);
       ProcessBuyADX();
       ProcessSellADX();
       }
    if (UseTimeOfDay > 0) 
       {
       TimeOfDayReturnValue = 0;
       ProcessTimeOfDay();
       }
    if (UseNews > 0) 
       {
       NewsReturnValue = 0;
       ProcessNews();
       }
    if (UsePivots > 0) 
       {
       BuyPivotsReturnValue = 0;
       SellPivotsReturnValue = 0;
       CalcPivots();
       ProcessBuyPivots();
       ProcessSellPivots();
       }
    if (UseFibs > 0) 
       {
       BuyFibsReturnValue = 0;
       SellFibsReturnValue = 0;
       CalcFibs();
       ProcessBuyFibs();
       ProcessSellFibs();
       }
    if (UseSR > 0) 
       {
       BuySRReturnValue = 0;
       SellSRReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = SRMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcSR(Shift);
          ProcessBuySR();
          ProcessSellSR();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellSRReturnValue = 0;
           BuySRReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuySRReturnValue = 0;
           SellSRReturnValue = 1;
           }
       if (BuySRReturnValue == 0 && SellSRReturnValue == 0) return;
       }
    if (UseDivergence > 0) 
       {
       BuyDivergenceReturnValue = 0;
       SellDivergenceReturnValue = 0;
       CalcDivergence();
       ProcessBuyDivergence();
       ProcessSellDivergence();
       }
    if (UseCandleSticks > 0) 
       {
       BuyCandleSticksReturnValue = 0;
       SellCandleSticksReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       for (Shift = CandleSticksMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcCandleSticks(Shift);
          ProcessBuyCandleSticks();
          ProcessSellCandleSticks();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellCandleSticksReturnValue = 0;
           BuyCandleSticksReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyCandleSticksReturnValue = 0;
           SellCandleSticksReturnValue = 1;
           }
       if (BuyCandleSticksReturnValue == 0 && SellCandleSticksReturnValue == 0) return;
       }

    if (TradeClosureMode > 0 && (Buys > 0 || Sells > 0)) CheckForClose();
    
    CalculateCurrOrders(Symbol());
    
    if (Buys > 0 || Sells > 0) return;
    
    ProcessBuyOrder();
    ProcessStopOrder();
    ProcessSellOrder();
    }
//+------------------------------------------------------------------+
//| Process Buy Order conditions                                  |
//+------------------------------------------------------------------+
void ProcessBuyOrder()
    {   
    if (UseContinuousStraddle == 1)return;
    if (UseEMACross == 0
    && UsePriceEMAEvent == 0
    && UseRSICross == 0
    && UseMACDCross == 0
    && UseMainZeroCross == 0
    && UseSignalZeroCross == 0
    && UseSTOCCross == 0
    && UsePSAR == 0
    && UseZigZag == 0
    && UseATRBreakout == 0
    && (UseStraddle == 0 || UseStraddle == 1)
    && (UseContinuousStraddle == 0)
    && UseADX == 0
    && UseTimeOfDay == 0
    && UseNews == 0
    && UsePivots == 0
    && UseFibs == 0
    && UseSR == 0
    && UseDivergence == 0
    && UseCandleSticks == 0) return;

    if (BuyEMACrossReturnValue == 0  && UseEMACross == 1) return;
    if (BuyPriceEMACrossReturnValue == 0  && (UsePriceEMAEvent == 1 || UsePriceEMAEvent == 2)) return;
    if (BuyRSIReturnValue == 0 && RSIPeriods != 0 && UseRSICross == 1) return;
    if (BuyRSIReturnValue2 == 0 && RSIPeriods2 != 0 && UseRSICross == 1) return;
    if (BuyRSIReturnValue3 == 0 && RSIPeriods3 != 0 && UseRSICross == 1) return;
    if (BuyMACDReturnValue == 0  && UseMACDCross == 1) return;
    if (BuyMACDReturnValue == 0  && UseMainZeroCross == 1) return;
    if (BuyMACDReturnValue == 0  && UseSignalZeroCross == 1) return;
    if (BuySTOCReturnValue == 0  && UseSTOCCross == 1) return;
    if (BuyPSARReturnValue == 0  && UsePSAR == 1) return;
    if (BuyZZReturnValue == 0  && UseZigZag == 1) return;
    if (BuyATRReturnValue == 0  && UseATRBreakout == 1) return;
    if (BuyPivotsReturnValue == 0  && UsePivots == 1) return;
    if (TimeOfDayReturnValue == 0  && UseTimeOfDay == 1) return;
    if (NewsReturnValue == 0 && UseNews == 1) return;
    if (BuyFibsReturnValue == 0  && UseFibs == 1) return;
    if (BuySRReturnValue == 0  && UseSR == 1) return;
    if (BuyDivergenceReturnValue == 0  && UseDivergence == 1) return;
    if (BuyCandleSticksReturnValue == 0  && UseCandleSticks == 1) return;

    if (UseADX == 0 || BuyADXReturnValue == 1) 
       {
       CurrPrice = Open[0];
       if (TradingMode != 0 && PrevPrice != CurrPrice)
          {
          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);
          PrevPrice = CurrPrice;
          }
       if (res != 2 && (TradingMode == 0 || TradingMode == 2)) OpenBuyOrder();
       return;
       }
    } 
//+------------------------------------------------------------------+
//| Process Stop Order                                               |
//+------------------------------------------------------------------+
void ProcessStopOrder()
    {
    if (UseTimeOfDay == 1 && TimeOfDayReturnValue ==0) return;
    if (StraddleReturnValue == 1 || UseContinuousStraddle == 1)
       {
       CurrPrice = Open[0];
       if (TradingMode != 0 && PrevPrice != CurrPrice)
          {
          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);
          PrevPrice = CurrPrice;
          }
       if (res != 2 && (TradingMode == 0 || TradingMode == 2)) OpenStopOrder();
       return;
       }
    }
//+------------------------------------------------------------------+
//| Process Sell Order                                               |
//+------------------------------------------------------------------+
void ProcessSellOrder()
    {
    if (UseContinuousStraddle == 1)return;
    if (UseEMACross == 0
    && UsePriceEMAEvent == 0
    && UseRSICross == 0
    && UseMACDCross == 0
    && UseMainZeroCross == 0
    && UseSignalZeroCross == 0
    && UseSTOCCross == 0
    && UsePSAR == 0
    && UseZigZag == 0
    && UseATRBreakout == 0
    && (UseStraddle == 0 || UseStraddle == 1)
    && (UseContinuousStraddle == 0 || UseContinuousStraddle == 1)
    && UseADX == 0
    && UseTimeOfDay == 0
    && UseNews == 0
    && UsePivots == 0
    && UseFibs == 0
    && UseSR == 0
    && UseDivergence == 0
    && UseCandleSticks == 0) return;

    if (SellEMACrossReturnValue == 0  && UseEMACross == 1) return;
    if (SellPriceEMACrossReturnValue == 0  && (UsePriceEMAEvent == 1 || UsePriceEMAEvent == 2)) return;
    if (SellRSIReturnValue == 0 && RSIPeriods != 0 && UseRSICross == 1) return;
    if (SellRSIReturnValue2 == 0 && RSIPeriods2 != 0 && UseRSICross == 1) return;
    if (SellRSIReturnValue3 == 0 && RSIPeriods3 != 0 && UseRSICross == 1) return;
    if (SellMACDReturnValue == 0  && UseMACDCross == 1) return;
    if (SellMACDReturnValue == 0  && UseMainZeroCross == 1) return;
    if (SellMACDReturnValue == 0  && UseSignalZeroCross == 1) return;
    if (SellSTOCReturnValue == 0  && UseSTOCCross == 1) return;
    if (SellPSARReturnValue == 0  && UsePSAR == 1) return;
    if (SellZZReturnValue == 0  && UseZigZag == 1) return;
    if (SellATRReturnValue == 0  && UseATRBreakout == 1) return;
    if (TimeOfDayReturnValue == 0  && UseTimeOfDay == 1) return;
    if (NewsReturnValue == 0 && UseNews == 1) return;
    if (SellPivotsReturnValue == 0  && UsePivots == 1) return;
    if (SellFibsReturnValue == 0  && UseFibs == 1) return;
    if (SellSRReturnValue == 0  && UseSR == 1) return;
    if (SellDivergenceReturnValue == 0  && UseDivergence == 1) return;
    if (SellCandleSticksReturnValue == 0  && UseCandleSticks == 1) return;

    if (UseADX == 0 || SellADXReturnValue == 1) 
       {
       CurrPrice = Open[0];
       if (TradingMode != 0 && PrevPrice != CurrPrice)
          {
          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);
          PrevPrice = CurrPrice;
          }
       if (res != 2 && (TradingMode == 0 || TradingMode == 2)) OpenSellOrder();
       return;
       }
   }
//+------------------------------------------------------------------+
//| Open Buy Order                                                   |
//+------------------------------------------------------------------+
void OpenBuyOrder()
  {
       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) 
          {
          if (StopLossATRPeriods == 0 && StopLossMultiplier != 0 && PrevWager != 0  && PrevEquity > AccountEquity() && PrevEquity != 0) Wager = PrevWager * StopLossMultiplier;
          if (TakeProfitATRPeriods == 0 && TakeProfitMultiplier != 0) TakeProfit = StopLoss * TakeProfitMultiplier;

          // Cap on Consecutive Stop Losses
          if ((Wager > NormalizeDouble(AccountEquity() * 20 / 100000,2) && ConsecLosses < 13) &&  LotSize == 0  && RiskBasis == 0)
          Wager = NormalizeDouble(AccountEquity() * 20 / 100000,2);
          if ((Wager > NormalizeDouble(AccountEquity() * 20 / 100000,2) || ConsecLosses > 12) &&  LotSize == 0  && RiskBasis == 0) 
          Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2);
          if (PrevWager == NormalizeDouble(AccountEquity() * 20 / 100000,2) && LotSize == 0  && RiskBasis == 0) 
          Wager =  NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2); 

          PrevWager = Wager;
          PrevEquity = AccountEquity();
          res=OrderSend(Symbol(),OP_BUY,Wager,Ask,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          }
       // calculate stop loss and take profit levels
       StopLossPrice = 0;
       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 (StopLossATRPeriods != 0) StopLossPrice = Bid - (iATR(Symbol(),0,StopLossATRPeriods,0)* StopLossMultiplier);
       if (TakeProfit == 0 && BuyZZReturnValue == 1) TakeWinPrice = Ask + (Bid - Prev1Zig) + (2 * PointValue);
       if (TakeProfit != 0) TakeWinPrice = Ask + (TakeProfit * PointValue); 
       if (TakeProfitATRPeriods != 0) TakeWinPrice = Ask + (iATR(Symbol(),0,TakeProfitATRPeriods,0)* TakeProfitMultiplier);
       res = OrderModify(res,OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0,clrMaroon);
       if (res > -1)
          { 
          CurrProfitTarget = Wager / Close[0]  * .005 / PointValue;
          Modified = 1;
          BreakEvenCompleted = 0;
          }
  }
//+------------------------------------------------------------------+
//| Open Stop Order                                                  |
//+------------------------------------------------------------------+
void OpenStopOrder()
  {
       Wager = LotSize;
       if (UseContinuousStraddle == 1) Message = "Continuous Straddle";
       if (LotSize == 0  && RiskBasis == 0) Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2);
       if (LotSize == 0  && RiskBasis == 1) Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 1000 / StopLoss,2);
       if (BuyStops == 0)
          {
          if (StopLossATRPeriods == 0 && StopLossMultiplier != 0 && PrevWager != 0  && PrevEquity > AccountEquity()
          &&  PrevEquity != 0 &&  LotSize == 0  && RiskBasis == 0) 
             { 
             Wager = PrevWager * StopLossMultiplier;
             ConsecLosses = ConsecLosses +1;
             }
          if (StopLossATRPeriods == 0 && StopLossMultiplier != 0 && PrevWager != 0  && PrevEquity <= AccountEquity() && PrevEquity != 0) ConsecLosses = 0; 
          if (TakeProfitATRPeriods == 0 && TakeProfitMultiplier != 0) TakeProfit = StopLoss * TakeProfitMultiplier;

          // Cap on Consecutive Stop Losses
          if ((Wager > NormalizeDouble(AccountEquity() * 20 / 100000,2) && ConsecLosses < 13) &&  LotSize == 0  && RiskBasis == 0)
          Wager = NormalizeDouble(AccountEquity() * 20 / 100000,2);
          if ((Wager > NormalizeDouble(AccountEquity() * 20 / 100000,2) || ConsecLosses > 12) &&  LotSize == 0  && RiskBasis == 0) 
          Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2);
          if (PrevWager == NormalizeDouble(AccountEquity() * 20 / 100000,2) && LotSize == 0  && RiskBasis == 0) 
          Wager =  NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2); 

          PrevWager = Wager;
          PrevEquity = AccountEquity();
          Comment1 = StringConcatenate(Symbol()," BUY ",Message);
          if (UseStraddle == 1) res = OrderSend(Symbol(),OP_BUYSTOP,Wager,Ask + (StraddleBuffer * PointValue) ,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          if (UseContinuousStraddle == 1) res = OrderSend(Symbol(),OP_BUYSTOP,Wager,Ask + (ContinuousStraddleBuffer * PointValue) ,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          BreakEvenCompleted = 0;
          Modified = 0;
          }
       if (SellStops == 0)
          {
          Comment1 = StringConcatenate(Symbol()," SELL ",Message);
          if (UseStraddle == 1) res=OrderSend(Symbol(),OP_SELLSTOP,Wager,Bid - (StraddleBuffer * PointValue) ,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          if (UseContinuousStraddle == 1) res=OrderSend(Symbol(),OP_SELLSTOP,Wager,Bid - (ContinuousStraddleBuffer * PointValue) ,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          BreakEvenCompleted = 0;
          Modified = 0;
          }
  }
//+------------------------------------------------------------------+
//| Open Sell Order                                                  |
//+------------------------------------------------------------------+
void OpenSellOrder()
  {
       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) 
          {
          if (StopLossATRPeriods == 0 && StopLossMultiplier != 0 && PrevWager != 0  && PrevEquity > AccountEquity() && PrevEquity != 0) Wager = PrevWager * StopLossMultiplier;
          if (TakeProfitATRPeriods == 0 && TakeProfitMultiplier != 0) TakeProfit = StopLoss * TakeProfitMultiplier;

          // Cap on Consecutive Stop Losses
          if ((Wager > NormalizeDouble(AccountEquity() * 20 / 100000,2) && ConsecLosses < 13) &&  LotSize == 0  && RiskBasis == 0)
          Wager = NormalizeDouble(AccountEquity() * 20 / 100000,2);
          if ((Wager > NormalizeDouble(AccountEquity() * 20 / 100000,2) || ConsecLosses > 12) &&  LotSize == 0  && RiskBasis == 0) 
          Wager = NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2);
          if (PrevWager == NormalizeDouble(AccountEquity() * 20 / 100000,2) && LotSize == 0  && RiskBasis == 0) 
          Wager =  NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2);

          PrevWager = Wager;
          PrevEquity = AccountEquity();
          res=OrderSend(Symbol(),OP_SELL,Wager,Bid,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          }
       // calculate stop loss and take profit levels
       StopLossPrice = 0;
       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 (StopLossATRPeriods != 0) StopLossPrice = Ask + (iATR(Symbol(),0,StopLossATRPeriods,0)* StopLossMultiplier);
       if (TakeProfit == 0 && SellZZReturnValue == 1) TakeWinPrice = Bid - (Prev1Zig - Bid) - (2 * PointValue);
       if (TakeProfit != 0) TakeWinPrice = Bid - (TakeProfit * PointValue);
       if (TakeProfitATRPeriods != 0) TakeWinPrice = Bid - (iATR(Symbol(),0,TakeProfitATRPeriods,0)* TakeProfitMultiplier);
       res = OrderModify(res,OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0,clrMaroon);
       if (res > -1)
          {
          CurrProfitTarget = Wager / Close[0]  * .005 / PointValue;
          Modified = 1;
          BreakEvenCompleted = 0;
          }
  }    
//+------------------------------------------------------------------+
//| Check for modify order conditions                                |
//+------------------------------------------------------------------+
void CheckForModify()
  {
  for(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 && (UseStraddle == 1 || UseContinuousStraddle == 1) && Buys > 0) res = OrderClose(OrderTicket(),OrderLots(),Ask,1,clrMaroon);
         }
      }   
 
  for(i=0;i<OrdersTotal();i++)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
         {
         if (Modified == 0)
            {
            if (OrderType()==OP_BUY) 
               {
               if (StopLoss != 0) StopLossPrice = Bid - (StopLoss * PointValue);
               if (StopLossATRPeriods != 0) StopLossPrice = Bid - (iATR(Symbol(),0,StopLossATRPeriods,0)* StopLossMultiplier);
               if (TakeProfit != 0) TakeWinPrice = Ask + (TakeProfit * PointValue); 
               if (TakeProfitATRPeriods != 0) TakeWinPrice = Ask + (iATR(Symbol(),0,TakeProfitATRPeriods,0)* TakeProfitMultiplier);
               res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0,clrMaroon);
               if (res > -1) 
                  {
                  CurrProfitTarget = Wager / Close[0]  * .005 / PointValue;
                  Modified = 1;
                  }
               }
            if (OrderType()==OP_SELL) 
               {
               Print(">>>>>>>>>>>>>>>>>>>>>>>>>");
               if (StopLoss != 0) StopLossPrice = Ask + (StopLoss * PointValue);
               if (StopLossATRPeriods != 0) StopLossPrice = Ask + (iATR(Symbol(),0,StopLossATRPeriods,0)* StopLossMultiplier);
               if (TakeProfit != 0) TakeWinPrice = Bid - (TakeProfit * PointValue);   
               if (TakeProfitATRPeriods != 0) TakeWinPrice = Bid - (iATR(Symbol(),0,TakeProfitATRPeriods,0)* TakeProfitMultiplier);
               res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0,clrMaroon);
               if (res > -1) 
                  {
                  CurrProfitTarget = Wager / Close[0]  * .005 / PointValue;
                  Modified = 1;
                  }
               }
            }

         // For all trades
         if (Modified == 1)
            {
            Spread = Ask - Bid;
            if (OrderType()==OP_BUY && SetStopLossToBreakEvenPips != 0 
            && OrderProfit() >= Wager * ((SetStopLossToBreakEvenPips / PointValue / 1000))
            && BreakEvenCompleted == 0)
               {
               StopLossPrice = OrderOpenPrice() + Spread + PointValue;
               res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0, clrMaroon);
               if (res > -1) 
                  {
                  CurrProfitTarget = OrderProfit() + (Wager / Close[0]  * .005 / PointValue);
                  BreakEvenCompleted = 1;
                  BreakEvenPrice = StopLossPrice;
                  }
               }
            if (OrderType()==OP_SELL && SetStopLossToBreakEvenPips != 0 
            && OrderProfit() >= Wager * ((SetStopLossToBreakEvenPips / PointValue / 1000))
            && BreakEvenCompleted == 0)
               {
               StopLossPrice = OrderOpenPrice() - Spread - PointValue;
               res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0, clrMaroon);
               if (res > -1) 
                  {
                  CurrProfitTarget = OrderProfit() + Wager / Close[0]  * .005 / PointValue;
                  BreakEvenCompleted = 1;
                  BreakEvenPrice = StopLossPrice;
                  }
               }
            if (BreakEvenCompleted == 1 && OrderType()==OP_BUY && Bid - (StopLoss * PointValue) < BreakEvenPrice) return;   
            if (BreakEvenCompleted == 1 && OrderType()==OP_SELL && Ask + (StopLoss * PointValue) > BreakEvenPrice) return;   
            if (OrderProfit() > CurrProfitTarget && ((OrderType()==OP_BUY)|| (OrderType()==OP_SELL))) 
               {
               if (OrderType()==OP_BUY && UseTrailingStopLoss == 1 && TradeClosureMode == 0) StopLossPrice = Bid - (StopLoss * PointValue);
               if (OrderType()==OP_SELL && UseTrailingStopLoss == 1 && TradeClosureMode == 0) StopLossPrice = Ask + (StopLoss * PointValue); 
               if (UseTrailingStopLoss == 1 && TradeClosureMode == 0) res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0, clrMaroon);
               if (res > -1) CurrProfitTarget = OrderProfit() + (Wager / Close[0]  * .005 / PointValue);
               } 
            }
         }
      } 
  }     
//+------------------------------------------------------------------+
//| Calculate EMA                                                    |
//+------------------------------------------------------------------+
void CalcEMA(int ShiftVal)
  {
  CurrPrice = 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 && PrevPrice != CurrPrice)
     {
     BuyEMACrossReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell EMA Cross                                           |
//+------------------------------------------------------------------+
void ProcessSellEMACross()
  {
  if (CurrFastEMA <= CurrSlowEMA  && PrevFastEMA > PrevSlowEMA && PrevPrice != CurrPrice)
     {
     SellEMACrossReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate Price EMA                                              |
//+------------------------------------------------------------------+
void CalcPriceEMA(int ShiftVal)
  {
  CurrPrice = Open[0];
  Close1 = Close[ShiftVal];
  Close2 = Close[ShiftVal+1];
  if (UseHACandles == 1) 
    {
    Close1 = (Open[ShiftVal]+High[ShiftVal]+Low[ShiftVal]+Close[ShiftVal])/4;
    Close2 = (Open[ShiftVal+1]+High[ShiftVal+1]+Low[ShiftVal+1]+Close[ShiftVal+1])/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 && UsePriceEMAEvent == 1 && PrevPrice != CurrPrice) 
     {
     BuyPriceEMACrossReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  if (Close1 > CurrFastEMA && UsePriceEMAEvent == 2 && PrevPrice != CurrPrice) 
     {
     BuyPriceEMACrossReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell Price EMA Cross                                     |
//+------------------------------------------------------------------+
void ProcessSellPriceEMACross()
  {
  if (Close1 <= CurrFastEMA  && Close2 > PrevFastEMA && UsePriceEMAEvent == 1 && PrevPrice != CurrPrice)
     {
     SellPriceEMACrossReturnValue = 1;
     SellBarCross = Shift;
     }
  if (Close1 < CurrFastEMA && UsePriceEMAEvent == 2)
     {
     SellPriceEMACrossReturnValue = 1;
     SellBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Calculate RSI                                                    |
//+------------------------------------------------------------------+
void CalcRSI(int ShiftVal)
  {
  CurrPrice = Open[0];
  if (RSIPeriods != 0)
     {
     CurrRSI = NormalizeDouble(iRSI(Symbol(),RSITimeFrame,RSIPeriods,PRICE_CLOSE,ShiftVal),7);
     PrevRSI = NormalizeDouble(iRSI(Symbol(),RSITimeFrame,RSIPeriods,PRICE_CLOSE,ShiftVal+1),7);
     }    
  if (RSIPeriods2 != 0)
     {
     CurrRSI2 = NormalizeDouble(iRSI(Symbol(),RSITimeFrame2,RSIPeriods2,PRICE_CLOSE,ShiftVal),7);
     PrevRSI2 = NormalizeDouble(iRSI(Symbol(),RSITimeFrame2,RSIPeriods2,PRICE_CLOSE,ShiftVal+1),7);
     }
  if (RSIPeriods3 != 0)
     {
     CurrRSI3 = NormalizeDouble(iRSI(Symbol(),RSITimeFrame3,RSIPeriods3,PRICE_CLOSE,ShiftVal),7);
     PrevRSI3 = NormalizeDouble(iRSI(Symbol(),RSITimeFrame3,RSIPeriods3,PRICE_CLOSE,ShiftVal+1),7);
     }
  }
//+------------------------------------------------------------------+
//| Process Buy RSI Cross                                            |
//+------------------------------------------------------------------+
void ProcessBuyRSICross()
  {
  if (CurrRSI >= RSIBottomLevel && PrevRSI < RSIBottomLevel && PrevPrice != CurrPrice)
     {
     BuyRSIReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  if (CurrRSI2 >= RSIBottomLevel2 && PrevRSI2 < RSIBottomLevel2 && RSIPeriods2 != 0 && PrevPrice != CurrPrice)
     {
     BuyRSIReturnValue2 = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  if (CurrRSI3 >= RSIBottomLevel3 && PrevRSI3 < RSIBottomLevel3 && RSIPeriods3 != 0 && PrevPrice != CurrPrice)
     {
     BuyRSIReturnValue3 = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell RSI Cross                                           |
//+------------------------------------------------------------------+
void ProcessSellRSICross()
  {
  if (CurrRSI <= RSITopLevel && PrevRSI > RSITopLevel && PrevPrice != CurrPrice)
     {
     SellRSIReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  if (CurrRSI2 <= RSITopLevel2 && PrevRSI2 > RSITopLevel2 && RSIPeriods2 != 0 && PrevPrice != CurrPrice)
     {
     SellRSIReturnValue2 = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  if (CurrRSI3 <= RSITopLevel3 && PrevRSI3 > RSITopLevel3 && RSIPeriods3 != 0 && PrevPrice != CurrPrice)
     {
     SellRSIReturnValue3 = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate MACD                                                   |
//+------------------------------------------------------------------+
void CalcMACD(int ShiftVal)
  {
  CurrPrice = 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 && PrevPrice != CurrPrice)
  ||  (CurrMain >= 0 && PrevMain <= 0 && UseMainZeroCross == 1 && PrevPrice != CurrPrice)
  ||  (CurrSignal >= 0 && PrevSignal <= 0 && UseSignalZeroCross == 1 && PrevPrice != CurrPrice))
     {
     BuyMACDReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell MACD Cross                                          |
//+------------------------------------------------------------------+
void ProcessSellMACDCross()
  {
  if ((CurrMain > 0 && CurrMain <= CurrSignal && PrevMain >= PrevSignal && UseMACDCross == 1 && PrevPrice != CurrPrice)
  ||  (CurrMain <= 0 && PrevMain >= 0 && UseMainZeroCross == 1 && PrevPrice != CurrPrice)
  ||  (CurrSignal <= 0 && PrevSignal >= 0 && UseSignalZeroCross == 1 && PrevPrice != CurrPrice))
     {
     SellMACDReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate STOC                                                   |
//+------------------------------------------------------------------+
void CalcSTOC(int ShiftVal)
  {
  CurrPrice = Open[0];
  CurrentSlowD =  iStochastic(Symbol(),0,KPeriod,DPeriod,Slowing,CalculationMethod,CalculationMode,MODE_SIGNAL,ShiftVal);
  PreviousSlowD = iStochastic(Symbol(),0,KPeriod,DPeriod,Slowing,CalculationMethod,CalculationMode,MODE_SIGNAL,ShiftVal+1);
  CurrentSlowK = iStochastic(Symbol(),0,KPeriod,DPeriod,Slowing,CalculationMethod,CalculationMode,MODE_MAIN,ShiftVal);
  PreviousSlowK = iStochastic(Symbol(),0,KPeriod,DPeriod,Slowing,CalculationMethod,CalculationMode,MODE_MAIN,ShiftVal+1);
  }
//+------------------------------------------------------------------+
//| Process Buy STOC Cross                                           |
//+------------------------------------------------------------------+
void ProcessBuySTOCCross()
  {
  if (CurrentSlowD <= 20 && CurrentSlowK <= 20 && CurrentSlowD <= CurrentSlowK && PreviousSlowD > PreviousSlowK 
  && UseSTOCCross == 1 && PrevPrice != CurrPrice)
     {
     BuySTOCReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell STOC Cross                                          |
//+------------------------------------------------------------------+
void ProcessSellSTOCCross()
  {
  if (CurrentSlowD >= 80 && CurrentSlowK >= 80 && CurrentSlowD >= CurrentSlowK && PreviousSlowD < PreviousSlowK
  && UseSTOCCross == 1 && PrevPrice != CurrPrice)
     {
     SellSTOCReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate PSAR                                                   |
//+------------------------------------------------------------------+
void CalcPSAR(int ShiftVal)
  {
  CurrPrice = 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 && PrevPrice != CurrPrice)
     {
     BuyPSARReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell PSAR Cross                                          |
//+------------------------------------------------------------------+
void ProcessSellPSARCross()
  {
  if (Close[1] <= CurrPSAR && Close[2]> PrevPSAR && PrevPrice != CurrPrice)
     {
     SellPSARReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate ZigZag                                                 |
//+------------------------------------------------------------------+
void CalcZig(int ShiftVal)
  {
  int n;
  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 && PrevPrice != CurrPrice)
     {
     BuyZZReturnValue = 1;
     BuyBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell Zig                                                 |
//+------------------------------------------------------------------+
void ProcessSellZig()
  {
  if (Prev1Zig < Prev3Zig && Prev2Zig < Prev4Zig && Prev2Zig < Prev1Zig && Close[0] < Prev2Zig && PrevPrice != CurrPrice)
     {
     SellZZReturnValue = 1;
     SellBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Straddle                                                |
//+------------------------------------------------------------------+
void ProcessStraddle()
  {
  if ((Hour() == TradeHour && Minute() == TradeMinute && StraddleType == 0)
  || (Close[2] <= TradePrice && Close[1] >= TradePrice && StraddleType == 1)
  || (Close[2] >= TradePrice && Close[1] <= TradePrice && StraddleType == 1)
  || (iMA(Symbol(),0,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,2) <= Close[2] && 
      iMA(Symbol(),0,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,1) >= Close[1] && StraddleType == 2)
  || (iMA(Symbol(),0,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,2) >= Close[2] && 
      iMA(Symbol(),0,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,1) <= Close[1] && StraddleType == 2)) StraddleReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| Calc ATR Breakout                                                      |
//+------------------------------------------------------------------+
void CalcATR(int ShiftVal)
  {
  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])
     {
     BuyATRReturnValue = 1;
     BuyBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell ATR Breakout                                        |
//+------------------------------------------------------------------+
void ProcessSellATRBreakout()
  {
  if ((CurrATR - PrevATR) >= MinATRPipsPerBar/10000 && Close[0] < Close[1])
     {
     SellATRReturnValue = 1;
     SellBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Calc ADX                                                  |
//+------------------------------------------------------------------+
void ProcessCalcADX(int ShiftVal)
  {
  CurrADX = NormalizeDouble(iADX(Symbol(),0,ADXPeriods,PRICE_CLOSE,MODE_MAIN,ShiftVal),7);
  PrevADX = NormalizeDouble(iADX(Symbol(),0,ADXPeriods,PRICE_CLOSE,MODE_MAIN,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy ADX                                                  |
//+------------------------------------------------------------------+
void ProcessBuyADX()
  {
  if (CurrADX > PrevADX && UseADX == 1 && PrevPrice != CurrPrice) BuyADXReturnValue = 1;
  if (iADX(Symbol(),0,ADXPeriods,PRICE_CLOSE,MODE_MAIN,0) > ADXLevel && UseADX == 2 && PrevPrice != CurrPrice) BuyADXReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| Process Sell ADX                                                 |
//+------------------------------------------------------------------+
void ProcessSellADX()
  {
  if (CurrADX < PrevADX && UseADX == 1 && PrevPrice != CurrPrice) SellADXReturnValue = 1;
  if (iADX(Symbol(),0,ADXPeriods,PRICE_CLOSE,MODE_MAIN,0) > ADXLevel && UseADX == 2 && PrevPrice != CurrPrice) SellADXReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| Process Buy Time Of day                                          |
//+------------------------------------------------------------------+
void ProcessTimeOfDay()
  {
  if ((Hour() >= TradingHoursStart && Hour() <= TradingHoursEnd) || (TradingHoursStart == 0  && TradingHoursEnd == 0)) TimeOfDayReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| 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;  
         }
  }
//+------------------------------------------------------------------+
//| Calc Pivots                                                      |
//+------------------------------------------------------------------+
void CalcPivots()
  {
  double max, min,close;
  min = 9999;
  max = 0;
  for(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))) BuyPivotsReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| 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)))  SellPivotsReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| Calc Fibs                                                        |
//+------------------------------------------------------------------+
void CalcFibs()
  {
  double max, min;
  min = 9999;
  max = 0;
  for(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))) BuyFibsReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| 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))) SellFibsReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| Calc Support Resistance                                          |
//+------------------------------------------------------------------+
void CalcSR(int ShiftVal)
  {
  int n;
  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)
     {
     BuySRReturnValue = 1;
     BuyBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell SR                                                  |
//+------------------------------------------------------------------+
void ProcessSellSR()
  {
  if (Prev1Zig < Prev3Zig && Prev2Zig < Prev4Zig && Prev2Zig < Prev1Zig && Close[0] < Prev2Zig + SRBuffer)
     {
     SellSRReturnValue = 1;
     SellBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Calc Divergence                                                  |
//+------------------------------------------------------------------+
void CalcDivergence()
  {
  for(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;
  }
//+------------------------------------------------------------------+
//| Process Sell Divergence                                          |
//+------------------------------------------------------------------+
void ProcessSellDivergence()
  {
  if ((StartRSI < EndRSI || StartMACD < EndMACD) && StartPrice > EndPrice) SellDivergenceReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| 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;
     BuyBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell CandleSticks                                        |
//+------------------------------------------------------------------+
void ProcessSellCandleSticks()
  {
  if (CandleSticksSellSignal == 1)
     {
     SellCandleSticksReturnValue = 1;
     SellBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+
