//+----------------------------------------------------------+
//|                    Traders Gift EA .mq4                  |
//|               Copyright Waived 2015, David Currey        |
//+----------------------------------------------------------+
extern int MAGICMA = 3333300;
extern string Hdr0 = ">>> EA MODES OF OPERATION";
extern string Hlp0 = "TradingMode: Autotrading=0, Advisory Only=1, Discretionary Trading=2";
extern int TradingMode = 0;
extern string Hlp0a = "AdvisoryMode: On Screen prompts=0, Email or Text Message=1";
extern int AdvisoryMode = 0;
extern string Hdr1 = ">>> MONEY MANAGEMENT";
extern string Hlp1 = "LotSize: Automoney management=0";
extern double LotSize = 0;
extern string Hlp1a = "RiskBasis: on equity=0, on stoploss=1";
extern int RiskBasis = 0;
extern double MaximumRiskPercentage = 2;
extern string Hlp1b = "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 string Hdr21 = ">>> Minimum Pip Gain To Reset StopLossMultiplier";
extern int MinPipGainReset = 5;
extern string Hdr22 = ">>> Decrease Stop Loss % On Profit Gain Of 5%";
extern double DecrStopLossPercent = 0;
extern int MaxSlippage = 1;
extern int MaxSpread = 5;
extern string Hdr20 = ">>> Timeframe For Backtest";
extern int ReqTimeFrame = 0;
extern string Hlp20 = "Leave EA Panel Visible on Exit: No=0, Yes=1";
extern int PanelVisibleOnExit = 0;
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 int EMATimeFrame = 0;
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 int PriceEMATimeFrame = 0;
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 RSIMaxBarsAgo = 1;
extern string Hdr5 = ">>> MACD CROSS";
extern int UseMACDCross = 0;
extern int ShortMACD = 12;
extern int LongMACD = 26;
extern int Signal = 9;
extern int MACDTimeFrame = 0;
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 int STOCTimeFrame = 0;
extern int STOCMaxBarsAgo = 1;
extern string Hdr24 = ">>> BOLLINGER BANDS";
extern string Hlp24 = "UseBollBands: No=0, Yes=1";
extern int UseBollBands = 0;
extern int BBPeriod = 20;
extern double BBDeviations = 2;
extern int BBTimeFrame = 0;
extern int BBMaxBarsAgo = 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 PSARTimeFrame = 0;
extern int PSARMaxBarsAgo = 1;
extern string Hdr25 = ">>> COMMODITY CHANNEL INDEX";
extern string Hlp25 = "UseCCI: No=0, Yes=1";
extern int UseCCI = 0;
extern double CCIPeriods = 30;
extern int CCITimeFrame = 0;
extern int CCIMaxBarsAgo = 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 ZZTimeFrame = 0;
extern int ZZMaxBarsAgo = 1;
extern string Hdr8 = ">>> NEWS, TIME OF DAY, PRICE or INDICATOR";
extern string Hlp8 = "UseStraddle: No=0, Yes=1";
extern int UseStraddle = 0;
extern string Hlp8a = "StraddleType: Time=0, Price=1, Indicator=2";
extern int StraddleType = 0;
extern int StraddleBuffer = 5;         
extern int TradeHour = 0;             
extern int TradeMinute = 0;             
extern double TradePrice = 0;
extern string Hdr9 = ">>> CONTINUOUS STRADDLE";
extern string Hlp9 = "UseContinuousStraddle: No=0, Yes=1";
extern int UseContinuousStraddle = 0;
extern int ContinuousStraddleBuffer = 15;         
extern string Hdr10 = ">>> ATR BREAKOUT";
extern string Hlp10 = "UseATRBreakout: No=0, Yes=1";
extern int UseATRBreakout = 0;
extern int ATRLookbackBars = 5;
extern double MinATRPipsPerBar = 5;
extern int ATRTimeFrame = 0;
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 ADXTimeFrame = 0;
extern int ADXMaxBarsAgo = 1;
extern string Hdr12 = ">>> TIME and DAY FILTER";
extern string Hlp12 = "UseTime and Day: No=0, Yes=1";
extern int UseTimeAndDay = 0;
extern int TradingHoursStart = 0;
extern int TradingHoursEnd = 23;
extern string Hlp12a = "Sunday=0, Friday=5";
extern int TradingDaysStart = 0;
extern int TradingDaysEnd = 5;
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 FibTimeFrame = 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 SRTimeFrame = 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 DivergenceTimeFrame = 60;
extern string Hdr18 = ">>> CANDLESTICK FILTER";
extern string Hlp18 = "UseCandlesticks: No=0, Yes=1";
extern int UseCandleSticks = 0;
extern int CSTimeFrame = 0;
extern int CSMaxBarsAgo = 1;
extern string Hdr23 = ">>> VOLUME FILTER";
extern string Hlp23 = "UseVolume: No=0, Yes=1";
extern int UseVolume;
//-------------------------------------
// Money Management variables
double TakeWinPrice;
double CurrProfitTarget;
double PointValue;
double StopLossPrice;
double StopLevel;
double PrevOpen;
double PrevValue;
double PrevBid;
double PrevStopLossPrice;
double Wager;
int PrevEquity;
double PrevWager;
int ConsecLosses;
double Spread;
double BreakEvenPrice;
int CapReached;
//-------------------------------------
// EMA Cross variables
double CurrFastEMA;
double PrevFastEMA;
double CurrSlowEMA;
double PrevSlowEMA;
int BuyEMACrossReturnValue;
int SellEMACrossReturnValue;
//-------------------------------------
// Price EMA Cross variables
double Close1;
double Close2;
int BuyPriceEMACrossReturnValue;
int SellPriceEMACrossReturnValue;
//-------------------------------------
// RSI Cross variables
double CurrRSI;
double PrevRSI;
int BuyRSIReturnValue;
int SellRSIReturnValue;
//-------------------------------------
// MACD Cross variables
double CurrMain;
double PrevMain;
double CurrSignal;
double PrevSignal;
int BuyMACDReturnValue;
int SellMACDReturnValue;
//-------------------------------------
// STOC Cross variables
double CurrentSlowD;
double PreviousSlowD;
double CurrentSlowK;
double PreviousSlowK;
int BuySTOCReturnValue;
int SellSTOCReturnValue;
//-------------------------------------
// Bollinger Bands variables
double CurrBBValue;
double PrevBBValue;
int BuyBBReturnValue;
int SellBBReturnValue;
//-------------------------------------
// PSAR Cross variables
double CurrPSAR;
double PrevPSAR;
int BuyPSARReturnValue;
int SellPSARReturnValue;
//-------------------------------------
// CCI Cross variables
double CurrCCI;
double PrevCCI;
int BuyCCIReturnValue;
int SellCCIReturnValue;
//-------------------------------------
// 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 TimeAndDayReturnValue;
//-------------------------------------
// 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;
double StartRSI;
double EndRSI;
double StartMACD;
double EndMACD;
double StartPrice;
double EndPrice = 0;
//-------------------------------------
// CandleSticks variables
int BuyCandleSticksReturnValue;
int SellCandleSticksReturnValue;
int CandleSticksBuySignal;
int CandleSticksSellSignal;
//-------------------------------------
// Volume variables
int BuyVolumeReturnValue;
int SellVolumeReturnValue;
//-------------------------------------
// 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;
int CurrentMinute;
int CurrentHour;
int CurrentDay;
int CurrentBarPass;
int AdjStopLoss;
//+------------------------------------------------------------------+
//| 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);
     }
  if (StopLossATRPeriods == 0 && StopLossMultiplier > 1)
     {
     ObjectCount = ObjectCount + 1;
     ObjectCreate("ObjMinPipGainReset", OBJ_LABEL, 0, 0, 0);
     ObjectSetText("ObjMinPipGainReset","Minimum Pip Gain to Reset SL Multiplier " + MinPipGainReset,7, "Verdana", clrMaroon);
     ObjectSet("ObjMinPipGainReset", OBJPROP_CORNER, 0);
     ObjectSet("ObjMinPipGainReset", OBJPROP_XDISTANCE, 10);
     ObjectSet("ObjMinPipGainReset", 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 (UseMACDCross > 0)CreateObject("ObjMACDCross",MACDMaxBarsAgo,"Using MACDCross",ShortMACD,LongMACD,Signal,"");
  if (UseSTOCCross > 0)CreateObject("ObjSTOCCross",STOCMaxBarsAgo,"Using STOCCross",KPeriod,Slowing,DPeriod,"");
  if (UseBollBands > 0)CreateObject("ObjBollBands",BBMaxBarsAgo,"Using Bollinger Bands",BBPeriod,BBDeviations,"","");
  if (UsePSAR > 0)CreateObject("ObjPSAR",PSARMaxBarsAgo,"Using PSAR",PSARStop,PSARIncrement,"","");
  if (UseCCI > 0)CreateObject("ObjPSAR",CCIMaxBarsAgo,"Using CCI",CCIPeriods,"","","");
  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 (UseTimeAndDay > 0)CreateObject("ObjTimeAndDay","","Using Time and Day",TradingHoursStart,TradingHoursEnd,TradingDaysStart,TradingDaysEnd);
  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",DivergenceLookbackBars,"Using Divergence","","","","");
  if (UseCandleSticks > 0)CreateObject("ObjCandleSticks",CSMaxBarsAgo,"Using CandleSticks","","","","");
  if (UseVolume > 0)CreateObject("ObjVolume","","Using Volume","","","","");
  ObjectSet("ObjRectangle", OBJPROP_YSIZE,(ObjectCount-1) * 10 +5);
  }
//+------------------------------------------------------------------+
//| CreateObject function                                                   |
//+------------------------------------------------------------------+
void CreateObject(string Name,string Lookback,string Descr,string Value1,string Value2,string Value3, string Value4)
  {
  ObjectCount = ObjectCount + 1;
  ObjectCreate(Name, OBJ_LABEL, 0, 0, 0);
  ObjectSetText(Name,"(" + Lookback + ")   " + Descr + "   (" + Value1 + ", " + Value2 + ", " + Value3 + ", " + Value4 + ")",7, "Verdana", clrBlue);
  ObjectSet(Name, OBJPROP_CORNER, 0);
  ObjectSet(Name, OBJPROP_XDISTANCE, 10);
  ObjectSet(Name, OBJPROP_YDISTANCE, (ObjectCount*10));
  }
//+------------------------------------------------------------------+
//| Deinit function                                                   |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  if (PanelVisibleOnExit == 0) ObjectsDeleteAll();
  }
//+------------------------------------------------------------------+
//| 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 if(StringFind(Symbol(),"XAU") > (-1)) PointValue = .1;
  else if(StringFind(Symbol(),"GOLD") > (-1)) PointValue = .1;
  else if(StringFind(Symbol(),"XAG") > (-1)) PointValue = .01;
  else if(StringFind(Symbol(),"SILVER") > (-1)) PointValue = .01;
  else PointValue = .0001;
  DisableTrading = 0;
  for(i=0;i<OrdersTotal();i++)
     {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
     if(OrderType()==OP_BUYLIMIT) DisableTrading = 1;
     } 
  if(CalculateCurrOrders(Symbol())== 0)
    {
    if (DisableTrading == 0)CheckForOpen();
    }
  else
    { 
    if (TradeClosureMode == 1 && DisableTrading == 0) CheckForOpen();
    if (DisableTrading == 0)CheckForClose();
    if (DisableTrading == 0)CheckForModify();
    }
  } 
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrOrders(string symbol)
  {
  Buys=0;
  Sells=0;
  BuyStops=0;
  SellStops=0;
//----
   for(i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  Buys++;
         if(OrderType()==OP_SELL) Sells++;
         if(OrderType()==OP_BUYSTOP)  BuyStops++;
         if(OrderType()==OP_SELLSTOP) SellStops++;
        }
     }

//---- return orders volume
 
   if(Buys>0) return(Buys);
   else       return(-Sells);
  }

//+------------------------------------------------------------------+
//| Check for Close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
    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 && 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 (BuyMACDReturnValue == 0  && UseMACDCross == 1) return;
              if (BuySTOCReturnValue == 0  && UseSTOCCross == 1) return;
              if (BuyBBReturnValue == 0  && UseBollBands == 1) return;
              if (BuyPSARReturnValue == 0  && UsePSAR == 1) return;
              if (BuyCCIReturnValue == 0  && UseCCI == 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;
              if (BuyVolumeReturnValue == 0  && UseVolume == 1) return;
              res = OrderClose(OrderTicket(),OrderLots(),Ask,1,clrMaroon);
              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 (SellMACDReturnValue == 0  && UseMACDCross == 1) return;
              if (SellSTOCReturnValue == 0  && UseSTOCCross == 1) return;
              if (SellBBReturnValue == 0  && UseBollBands == 1) return;
              if (SellPSARReturnValue == 0  && UsePSAR == 1) return;
              if (SellCCIReturnValue == 0  && UseCCI == 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;
              if (SellVolumeReturnValue == 0  && UseVolume == 1) return;
              res = OrderClose(OrderTicket(),OrderLots(),Bid,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;
       PrevPrice = 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;
       PrevPrice = 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;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 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) 
       {
       BuyMACDReturnValue = 0;
       SellMACDReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 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;
       PrevPrice = 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 (UseBollBands > 0) 
       {
       BuyBBReturnValue = 0;
       SellBBReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 0;
       for (Shift = BBMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcBB(Shift);
          ProcessBuyBB(Shift);
          ProcessSellBB(Shift);
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellBBReturnValue = 0;
           BuyBBReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyBBReturnValue = 0;
           SellBBReturnValue = 1;
           }
       if (BuyBBReturnValue == 0 && SellBBReturnValue == 0) return;
       }
    if (UsePSAR > 0) 
       {
       BuyPSARReturnValue = 0;
       SellPSARReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 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 (UseCCI > 0) 
       {
       BuyCCIReturnValue = 0;
       SellCCIReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 0;
       for (Shift = CCIMaxBarsAgo; Shift > 0; Shift--)
          {
          CalcCCI(Shift);
          ProcessBuyCCICross();
          ProcessSellCCICross();
          }
       if  (BuyBarCross < SellBarCross && BuyBarCross > 0) 
           {
           SellCCIReturnValue = 0;
           BuyCCIReturnValue = 1;
           }
       else if (SellBarCross < BuyBarCross && SellBarCross > 0) 
           {
           BuyCCIReturnValue = 0;
           SellCCIReturnValue = 1;
           }
       if (BuyCCIReturnValue == 0 && SellCCIReturnValue == 0) return;
       }
    if (UseZigZag > 0) 
       {
       BuyZZReturnValue = 0;
       SellZZReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 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;
       PrevPrice = 0;
       ProcessStraddle();
       }
    if (UseATRBreakout > 0) 
       {
       BuyATRReturnValue = 0;
       SellATRReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 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;
       PrevPrice = 0;
       ProcessCalcADX(Shift);
       ProcessBuyADX();
       ProcessSellADX();
       }
    if (UseTimeAndDay > 0) 
       {
       TimeAndDayReturnValue = 0;
       PrevPrice = 0;
       ProcessTimeAndDay();
       }
    if (UseNews > 0) 
       {
       PrevPrice = 0;
       NewsReturnValue = 0;
       ProcessNews();
       }
    if (UsePivots > 0) 
       {
       BuyPivotsReturnValue = 0;
       SellPivotsReturnValue = 0;
       PrevPrice = 0;
       CalcPivots();
       ProcessBuyPivots();
       ProcessSellPivots();
       }
    if (UseFibs > 0) 
       {
       BuyFibsReturnValue = 0;
       SellFibsReturnValue = 0;
       PrevPrice = 0;
       CalcFibs();
       ProcessBuyFibs();
       ProcessSellFibs();
       }
    if (UseSR > 0) 
       {
       BuySRReturnValue = 0;
       SellSRReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 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;
       PrevPrice = 0;
       CalcDivergence();
       ProcessBuyDivergence();
       ProcessSellDivergence();
       }
    if (UseCandleSticks > 0) 
       {
       BuyCandleSticksReturnValue = 0;
       SellCandleSticksReturnValue = 0;
       BuyBarCross = 0;
       SellBarCross = 0;
       PrevPrice = 0;
       for (Shift = CSMaxBarsAgo; 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 (UseVolume > 0) 
       {
       CalcVolume();
       BuyVolumeReturnValue = 0;
       SellVolumeReturnValue = 0;
       PrevPrice = 0;
       ProcessBuyVolume();
       ProcessSellVolume();
       }

    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 (UseStraddle == 1)return;
    if (UseContinuousStraddle == 1)return;
    if (UseEMACross == 0
    && UsePriceEMAEvent == 0
    && UseRSICross == 0
    && UseMACDCross == 0
    && UseSTOCCross == 0
    && UseBollBands == 0
    && UsePSAR == 0
    && UseCCI == 0
    && UseZigZag == 0
    && UseATRBreakout == 0
    && UseStraddle == 0
    && UseContinuousStraddle == 0
    && UseADX == 0
    && UseTimeAndDay == 0
    && UseNews == 0
    && UsePivots == 0
    && UseFibs == 0
    && UseSR == 0
    && UseDivergence == 0
    && UseCandleSticks == 0
    && UseVolume == 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 (BuyMACDReturnValue == 0  && UseMACDCross == 1) return;
    if (BuySTOCReturnValue == 0  && UseSTOCCross == 1) return;
    if (BuyBBReturnValue == 0  && UseBollBands == 1) return;
    if (BuyPSARReturnValue == 0  && UsePSAR == 1) return;
    if (BuyCCIReturnValue == 0  && UseCCI == 1) return;
    if (BuyZZReturnValue == 0  && UseZigZag == 1) return;
    if (BuyATRReturnValue == 0  && UseATRBreakout == 1) return;
    if (BuyPivotsReturnValue == 0  && UsePivots == 1) return;
    if (TimeAndDayReturnValue == 0  && UseTimeAndDay == 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 (BuyVolumeReturnValue == 0  && UseVolume == 1) return;

    if (UseADX == 0 || BuyADXReturnValue == 1) 
       {
       CurrPrice = iOpen(Symbol(),60,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 (UseEMACross == 0
    && UsePriceEMAEvent == 0
    && UseRSICross == 0
    && UseMACDCross == 0
    && UseSTOCCross == 0
    && UseBollBands == 0
    && UsePSAR == 0
    && UseCCI == 0
    && UseZigZag == 0
    && UseATRBreakout == 0
    && UseStraddle == 0
    && UseContinuousStraddle == 0
    && UseADX == 0
    && UseTimeAndDay == 0
    && UseNews == 0
    && UsePivots == 0
    && UseFibs == 0
    && UseSR == 0
    && UseDivergence == 0
    && UseCandleSticks == 0
    && UseVolume == 0) return;
    if (BuyEMACrossReturnValue == 0  && SellEMACrossReturnValue == 0 && UseEMACross == 1) return;
    if (BuyPriceEMACrossReturnValue == 0  && SellPriceEMACrossReturnValue == 0 && (UsePriceEMAEvent == 1 || UsePriceEMAEvent == 2)) return;
    if (BuyRSIReturnValue == 0 && SellRSIReturnValue == 0 && RSIPeriods != 0 && UseRSICross == 1) return;
    if (BuyMACDReturnValue == 0  && SellMACDReturnValue == 0 && UseMACDCross == 1) return;
    if (BuySTOCReturnValue == 0  && SellSTOCReturnValue == 0 && UseSTOCCross == 1) return;
    if (BuyBBReturnValue == 0  && SellBBReturnValue == 0 && UseBollBands == 1) return;
    if (BuyPSARReturnValue == 0  && SellPSARReturnValue == 0 && UsePSAR == 1) return;
    if (BuyCCIReturnValue == 0  && SellCCIReturnValue == 0 && UseCCI == 1) return;
    if (BuyZZReturnValue == 0  && SellZZReturnValue == 0 && UseZigZag == 1) return;
    if (BuyATRReturnValue == 0  && SellATRReturnValue == 0 && UseATRBreakout == 1) return;
    if (BuyPivotsReturnValue == 0  && SellPivotsReturnValue == 0 && UsePivots == 1) return;
    if (TimeAndDayReturnValue == 0  && UseTimeAndDay == 1) return;
    if (NewsReturnValue == 0 && UseNews == 1) return;
    if (BuyFibsReturnValue == 0  && SellFibsReturnValue == 0 && UseFibs == 1) return;
    if (BuySRReturnValue == 0  && SellSRReturnValue == 0 && UseSR == 1) return;
    if (BuyDivergenceReturnValue == 0  && SellDivergenceReturnValue == 0 && UseDivergence == 1) return;
    if (BuyCandleSticksReturnValue == 0  && SellCandleSticksReturnValue == 0 && UseCandleSticks == 1) return;
    if (BuyVolumeReturnValue == 0  && SellVolumeReturnValue == 0 && UseVolume == 1) 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 (UseStraddle == 1)return;
    if (UseContinuousStraddle == 1)return;
    if (UseEMACross == 0
    && UsePriceEMAEvent == 0
    && UseRSICross == 0
    && UseMACDCross == 0
    && UseSTOCCross == 0
    && UseBollBands == 0
    && UsePSAR == 0
    && UseCCI == 0
    && UseZigZag == 0
    && UseATRBreakout == 0
    && UseStraddle == 0 
    && UseContinuousStraddle == 0 
    && UseADX == 0
    && UseTimeAndDay == 0
    && UseNews == 0
    && UsePivots == 0
    && UseFibs == 0
    && UseSR == 0
    && UseDivergence == 0
    && UseCandleSticks == 0
    && UseVolume == 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 (SellMACDReturnValue == 0  && UseMACDCross == 1) return;
    if (SellSTOCReturnValue == 0  && UseSTOCCross == 1) return;
    if (SellBBReturnValue == 0  && UseBollBands == 1) return;
    if (SellPSARReturnValue == 0  && UsePSAR == 1) return;
    if (SellCCIReturnValue == 0  && UseCCI == 1) return;
    if (SellZZReturnValue == 0  && UseZigZag == 1) return;
    if (SellATRReturnValue == 0  && UseATRBreakout == 1) return;
    if (TimeAndDayReturnValue == 0  && UseTimeAndDay == 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 (SellVolumeReturnValue == 0  && UseVolume == 1) return;

    if (UseADX == 0 || SellADXReturnValue == 1) 
       {
       CurrPrice = iOpen(Symbol(),60,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;
       }
   }
//+------------------------------------------------------------------+
//| Set Wager                                                        |
//+------------------------------------------------------------------+
void SetWager()
   {
   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 (StopLossATRPeriods == 0 && StopLossMultiplier != 0 && PrevWager != 0  && AccountEquity() < PrevEquity + (MinPipGainReset * Wager * 10) && PrevEquity != 0) 
      {
      Wager = PrevWager * StopLossMultiplier;
      ConsecLosses = ConsecLosses + 1;
      }
   else  ConsecLosses = 0;

   if (TakeProfitATRPeriods == 0 && TakeProfitMultiplier != 0) TakeProfit = StopLoss * TakeProfitMultiplier;

   // Set Wager to Original
   if (CapReached == 1) 
      {
      Wager =  NormalizeDouble(AccountEquity() * MaximumRiskPercentage / 100000,2); 
      CapReached = 0;
      ConsecLosses = 0;
      }

   // Cap at 20% Equity
   if ((Wager > NormalizeDouble(AccountEquity() * 20 / 100000,2)) &&  LotSize == 0  && RiskBasis == 0)
      {
      Wager = NormalizeDouble(AccountEquity() * 20 / 100000,2);
      CapReached = 1;
      }

   PrevWager = Wager;
   PrevEquity = AccountEquity();
   AdjStopLoss = StopLoss;
   }
//+------------------------------------------------------------------+
//| Open Buy Order                                                   |
//+------------------------------------------------------------------+
void OpenBuyOrder()
   {
       Comment1 = StringConcatenate(Symbol()," BUY ",Message);
       if ((Ask - Bid) > (MaxSpread * PointValue)) return; 
       SetWager();
       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()
  {
       if (BuyStops == 0)
          {
          SetWager();
          Comment1 = StringConcatenate(Symbol()," BUY ",Message);
          if (UseStraddle == 1 && DisableTrading == 0) res = OrderSend(Symbol(),OP_BUYSTOP,Wager,Ask + (StraddleBuffer * PointValue) ,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          if (UseContinuousStraddle == 1 && DisableTrading == 0) 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 && DisableTrading == 0) res=OrderSend(Symbol(),OP_SELLSTOP,Wager,Bid - (StraddleBuffer * PointValue) ,MaxSlippage,0,0,Comment1,MAGICMA,0,Green);
          if (UseContinuousStraddle == 1 && DisableTrading == 0) 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);
       if ((Ask - Bid) > (MaxSpread * PointValue)) return; 
       SetWager();
       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;
                  return;
                  }
               }
            if (OrderType()==OP_SELL) 
               {
               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;
                  return;
                  }
               }
            }

         // For all trades
         if (Modified == 1)
            {
            Spread = Ask - Bid;
            if (OrderType()==OP_BUY && SetStopLossToBreakEvenPips != 0 
            && OrderProfit() >= Wager * SetStopLossToBreakEvenPips * 10
            && 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 * 10
            && 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 - (AdjStopLoss * PointValue) < BreakEvenPrice) return;   
            if (BreakEvenCompleted == 1 && OrderType()==OP_SELL && Ask + (AdjStopLoss * PointValue) > BreakEvenPrice) return;   
            if (OrderProfit() > CurrProfitTarget && ((OrderType()==OP_BUY)|| (OrderType()==OP_SELL))) 
               {
               if (OrderType()==OP_BUY && UseTrailingStopLoss == 1 && TradeClosureMode == 0) StopLossPrice = Bid - (AdjStopLoss * PointValue);
               if (OrderType()==OP_SELL && UseTrailingStopLoss == 1 && TradeClosureMode == 0) StopLossPrice = Ask + (AdjStopLoss * PointValue); 
               if (OrderType()==OP_BUY && StopLossPrice < PrevStopLossPrice + (3 * PointValue)) return;
               if (OrderType()==OP_SELL && StopLossPrice > PrevStopLossPrice - (3 * PointValue)) return;
               PrevStopLossPrice = StopLossPrice;
               if (UseTrailingStopLoss == 1 && TradeClosureMode == 0) res = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossPrice, TakeWinPrice, 0, clrMaroon);
               if (res > -1) 
                  {
                  CurrProfitTarget = OrderProfit() + (Wager / Close[0]  * .005 / PointValue);
                  if (AdjStopLoss > 5) AdjStopLoss = MathRound(AdjStopLoss * (100-DecrStopLossPercent) / 100);
                  }
               } 
            }
         }
      } 
  }     
//+------------------------------------------------------------------+
//| GetLastClose                                                     |
//+------------------------------------------------------------------+
void GetLastClose()
  {
  if (ReqTimeFrame == 0)
     {
     CurrentBarPass = 1;
     return;
     }
  int last_trade=HistoryTotal();
  if(last_trade>0)
     {
     if (OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
        {
        CurrentMinute = TimeMinute(OrderCloseTime());
        CurrentHour = TimeHour(OrderCloseTime());
        CurrentDay = TimeDay(OrderCloseTime());
        }
     }
  CurrentBarPass = 0;
  if (ReqTimeFrame == 1440 && TimeDay(TimeCurrent()) != CurrentDay) CurrentBarPass = 1;
  if ((ReqTimeFrame == 240 && TimeHour(TimeCurrent())/4 != CurrentHour /4) || (TimeDay(TimeCurrent()) != CurrentDay)) CurrentBarPass = 1;
  if (ReqTimeFrame == 60 && (TimeHour(TimeCurrent())!= CurrentHour || TimeDay(TimeCurrent()) != CurrentDay)) CurrentBarPass = 1;
  if (ReqTimeFrame == 30 && (MathRound(TimeMinute(TimeCurrent())/30) != MathRound(CurrentMinute/30) || TimeHour(TimeCurrent())!= CurrentHour || TimeDay(TimeCurrent()) != CurrentDay)) CurrentBarPass = 1;
  if (ReqTimeFrame == 15 && (MathRound(TimeMinute(TimeCurrent())/15) != MathRound(CurrentMinute/15) || TimeHour(TimeCurrent())!= CurrentHour || TimeDay(TimeCurrent()) != CurrentDay)) CurrentBarPass = 1;
  if (ReqTimeFrame == 5 && (MathRound(TimeMinute(TimeCurrent())/5) != MathRound(CurrentMinute/5) || TimeHour(TimeCurrent())!= CurrentHour || TimeDay(TimeCurrent()) != CurrentDay)) CurrentBarPass = 1;
  if (ReqTimeFrame == 1 && (TimeMinute(TimeCurrent()) != CurrentMinute || TimeHour(TimeCurrent())!= CurrentHour || TimeDay(TimeCurrent()) != CurrentDay)) CurrentBarPass = 1;
  }  
   
//+------------------------------------------------------------------+
//| Calculate EMA                                                    |
//+------------------------------------------------------------------+
void CalcEMA(int ShiftVal)
  {
  GetLastClose();
  if (EMATimeFrame == 0) EMATimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),EMATimeFrame,0);
  CurrFastEMA = NormalizeDouble(iMA(Symbol(),ReqTimeFrame,ShortEMA,0,MODE_EMA,PRICE_CLOSE,ShiftVal),7);
  PrevFastEMA = NormalizeDouble(iMA(Symbol(),ReqTimeFrame,ShortEMA,0,MODE_EMA,PRICE_CLOSE,ShiftVal+1),7);
  CurrSlowEMA = NormalizeDouble(iMA(Symbol(),ReqTimeFrame,LongEMA,0,MODE_EMA,PRICE_CLOSE,ShiftVal),7);
  PrevSlowEMA = NormalizeDouble(iMA(Symbol(),ReqTimeFrame,LongEMA,0,MODE_EMA,PRICE_CLOSE,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy EMA Cross                                            |
//+------------------------------------------------------------------+
void ProcessBuyEMACross()
  {
  if (CurrFastEMA >= CurrSlowEMA  && PrevFastEMA < PrevSlowEMA
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     BuyEMACrossReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell EMA Cross                                           |
//+------------------------------------------------------------------+
void ProcessSellEMACross()
  {
  if (CurrFastEMA <= CurrSlowEMA  && PrevFastEMA > PrevSlowEMA 
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     SellEMACrossReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate Price EMA                                              |
//+------------------------------------------------------------------+
void CalcPriceEMA(int ShiftVal)
  {
  GetLastClose();
  if (PriceEMATimeFrame == 0) PriceEMATimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),PriceEMATimeFrame,0);
  Close1 = iClose(Symbol(),PriceEMATimeFrame,ShiftVal);
  Close2 = iClose(Symbol(),PriceEMATimeFrame,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(),PriceEMATimeFrame,SingleEMA,EMAShift,MODE_EMA,PRICE_CLOSE,ShiftVal),7);
  PrevFastEMA = NormalizeDouble(iMA(Symbol(),PriceEMATimeFrame,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 && CurrentBarPass == 1) 
     {
     BuyPriceEMACrossReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = iOpen(Symbol(),ReqTimeFrame,0);
     }
  if (Close1 > CurrFastEMA && UsePriceEMAEvent == 2) 
     {
     BuyPriceEMACrossReturnValue = 1;
     BuyBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell Price EMA Cross                                     |
//+------------------------------------------------------------------+
void ProcessSellPriceEMACross()
  {
  if (Close1 <= CurrFastEMA  && Close2 > PrevFastEMA && UsePriceEMAEvent == 1 
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     SellPriceEMACrossReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = iOpen(Symbol(),ReqTimeFrame,0);
     }
  if (Close1 < CurrFastEMA && UsePriceEMAEvent == 2)
     {
     SellPriceEMACrossReturnValue = 1;
     SellBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Calculate RSI                                                    |
//+------------------------------------------------------------------+
void CalcRSI(int ShiftVal)
  {
  GetLastClose();
  CurrPrice = iOpen(Symbol(),ReqTimeFrame,0);
  if (RSITimeFrame == 0) RSITimeFrame = ReqTimeFrame;
  if (RSIPeriods != 0)
     {
     CurrRSI = NormalizeDouble(iRSI(Symbol(),RSITimeFrame,RSIPeriods,PRICE_CLOSE,ShiftVal),7);
     PrevRSI = NormalizeDouble(iRSI(Symbol(),RSITimeFrame,RSIPeriods,PRICE_CLOSE,ShiftVal+1),7);
     }    
  }
//+------------------------------------------------------------------+
//| Process Buy RSI Cross                                            |
//+------------------------------------------------------------------+
void ProcessBuyRSICross()
  {
  if (CurrRSI >= RSIBottomLevel && PrevRSI < RSIBottomLevel 
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     BuyRSIReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell RSI Cross                                           |
//+------------------------------------------------------------------+
void ProcessSellRSICross()
  {
  if (CurrRSI <= RSITopLevel && PrevRSI > RSITopLevel
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     SellRSIReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate MACD                                                   |
//+------------------------------------------------------------------+
void CalcMACD(int ShiftVal)
  {
  GetLastClose();
  if (MACDTimeFrame == 0) MACDTimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),MACDTimeFrame,0);
  CurrMain = iMACD(NULL,MACDTimeFrame,ShortMACD,LongMACD,Signal,PRICE_CLOSE,MODE_MAIN,ShiftVal);
  PrevMain = iMACD(NULL,MACDTimeFrame,ShortMACD,LongMACD,Signal,PRICE_CLOSE,MODE_MAIN,ShiftVal+1);
  CurrSignal = iMACD(NULL,MACDTimeFrame,ShortMACD,LongMACD,Signal,PRICE_CLOSE,MODE_SIGNAL,ShiftVal);
  PrevSignal = iMACD(NULL,MACDTimeFrame,ShortMACD,LongMACD,Signal,PRICE_CLOSE,MODE_SIGNAL,ShiftVal+1);
  }
//+------------------------------------------------------------------+
//| Process Buy MACD Cross                                           |
//+------------------------------------------------------------------+
void ProcessBuyMACDCross()
  {
  if (CurrMain < 0 && CurrMain >= CurrSignal && PrevMain <= PrevSignal
  && PrevPrice != CurrPrice && CurrentBarPass == 1)
     {
     BuyMACDReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell MACD Cross                                          |
//+------------------------------------------------------------------+
void ProcessSellMACDCross()
  {
  if (CurrMain > 0 && CurrMain <= CurrSignal && PrevMain >= PrevSignal
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     SellMACDReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate STOC                                                   |
//+------------------------------------------------------------------+
void CalcSTOC(int ShiftVal)
  {
  GetLastClose();
  if (STOCTimeFrame == 0) STOCTimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),STOCTimeFrame,0);
  CurrentSlowD =  iStochastic(Symbol(),STOCTimeFrame,KPeriod,DPeriod,Slowing,0,1,MODE_SIGNAL,ShiftVal);
  PreviousSlowD = iStochastic(Symbol(),STOCTimeFrame,KPeriod,DPeriod,Slowing,0,1,MODE_SIGNAL,ShiftVal+1);
  CurrentSlowK = iStochastic(Symbol(),STOCTimeFrame,KPeriod,DPeriod,Slowing,0,1,MODE_MAIN,ShiftVal);
  PreviousSlowK = iStochastic(Symbol(),STOCTimeFrame,KPeriod,DPeriod,Slowing,0,1,MODE_MAIN,ShiftVal+1);
  }
//+------------------------------------------------------------------+
//| Process Buy STOC Cross                                           |
//+------------------------------------------------------------------+
void ProcessBuySTOCCross()
  {
  if (CurrentSlowD <= 20 && CurrentSlowK <= 20 && CurrentSlowD <= CurrentSlowK && PreviousSlowD > PreviousSlowK 
  && UseSTOCCross == 1 && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     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 && CurrentBarPass == 1) 
     {
     SellSTOCReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate BollingerBands                                                    |
//+------------------------------------------------------------------+
void CalcBB(int ShiftVal)
  {
  GetLastClose();
  if (BBTimeFrame == 0) BBTimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),BBTimeFrame,0);
  }
//+------------------------------------------------------------------+
//| Process Buy Bollinger Bands                                          |
//+------------------------------------------------------------------+
void ProcessBuyBB(int ShiftVal)
  {
  CurrBBValue = NormalizeDouble(iBands(Symbol(),BBTimeFrame,BBPeriod,BBDeviations,0,PRICE_CLOSE,1,ShiftVal),7);
  PrevBBValue = NormalizeDouble(iBands(Symbol(),BBTimeFrame,BBPeriod,BBDeviations,0,PRICE_CLOSE,1,ShiftVal+1),7);
  if (Close[Shift] >= CurrBBValue  && Close[Shift + 1] < PrevBBValue 
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     BuyBBReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell Bollinger Bands                                          |
//+------------------------------------------------------------------+
void ProcessSellBB(int ShiftVal)
  {
  CurrBBValue = NormalizeDouble(iBands(Symbol(),BBTimeFrame,BBPeriod,BBDeviations,0,PRICE_CLOSE,2,ShiftVal),7);
  PrevBBValue = NormalizeDouble(iBands(Symbol(),BBTimeFrame,BBPeriod,BBDeviations,0,PRICE_CLOSE,2,ShiftVal+1),7);
  if (Close[Shift] <= CurrBBValue  && Close[Shift + 1] > PrevBBValue 
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     SellBBReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate PSAR                                                   |
//+------------------------------------------------------------------+
void CalcPSAR(int ShiftVal)
  {
  GetLastClose();
  if (PSARTimeFrame == 0) PSARTimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),PSARTimeFrame,0);
  CurrPSAR = NormalizeDouble(iSAR(Symbol(),PSARTimeFrame,PSARIncrement,PSARStop,ShiftVal),7);
  PrevPSAR = NormalizeDouble(iSAR(Symbol(),PSARTimeFrame,PSARIncrement,PSARStop,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy PSAR Cross                                           |
//+------------------------------------------------------------------+
void ProcessBuyPSARCross()
  {
  if (Close[1] >= CurrPSAR && Close[2] < PrevPSAR && PrevPrice != CurrPrice 
  && CurrentBarPass == 1)
     {
     BuyPSARReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell PSAR Cross                                          |
//+------------------------------------------------------------------+
void ProcessSellPSARCross()
  {
  if (Close[1] <= CurrPSAR && Close[2]> PrevPSAR && PrevPrice != CurrPrice 
  && CurrentBarPass == 1)
     {
     SellPSARReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calculate CCI                                                    |
//+------------------------------------------------------------------+
void CalcCCI(int ShiftVal)
  {
  GetLastClose();
  CurrPrice = iOpen(Symbol(),ReqTimeFrame,0);
  if (CCITimeFrame == 0) CCITimeFrame = ReqTimeFrame;
  if (CCIPeriods != 0)
     {
     CurrCCI = NormalizeDouble(iCCI(Symbol(),CCITimeFrame,CCIPeriods,PRICE_CLOSE,ShiftVal),7);
     PrevCCI = NormalizeDouble(iCCI(Symbol(),CCITimeFrame,CCIPeriods,PRICE_CLOSE,ShiftVal+1),7);
     }    
  }
//+------------------------------------------------------------------+
//| Process Buy CCI Cross                                            |
//+------------------------------------------------------------------+
void ProcessBuyCCICross()
  {
  if (CurrCCI >= -100 && PrevCCI < -100 
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     BuyCCIReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell CCI Cross                                           |
//+------------------------------------------------------------------+
void ProcessSellCCICross()
  {
  if (CurrCCI <= 100 && PrevCCI > 100
  && PrevPrice != CurrPrice && CurrentBarPass == 1) 
     {
     SellCCIReturnValue = 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,ZZTimeFrame,"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)
     {
     BuyZZReturnValue = 1;
     BuyBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell Zig                                                 |
//+------------------------------------------------------------------+
void ProcessSellZig()
  {
  if (Prev1Zig < Prev3Zig && Prev2Zig < Prev4Zig && Prev2Zig < Prev1Zig && Close[0] < Prev2Zig)
     {
     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)
  || (StraddleType == 2)) StraddleReturnValue = 1;
  }
//+------------------------------------------------------------------+
//| Calc ATR Breakout                                                      |
//+------------------------------------------------------------------+
void CalcATR(int ShiftVal)
  {
  GetLastClose();
  if (ATRTimeFrame == 0) ATRTimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),ATRTimeFrame,0);
  CurrATR = NormalizeDouble(iATR(Symbol(),ATRTimeFrame,ATRLookbackBars,ShiftVal),7);
  PrevATR = NormalizeDouble(iATR(Symbol(),ATRTimeFrame,ATRLookbackBars,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy ATR Breakout                                         |
//+------------------------------------------------------------------+
void ProcessBuyATRBreakout()
  {
  if ((CurrATR - PrevATR) >= MinATRPipsPerBar/10000 && Close[0] > Close[1] && PrevPrice != CurrPrice)
     {
     BuyATRReturnValue = 1;
     PrevPrice = CurrPrice;
     BuyBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell ATR Breakout                                        |
//+------------------------------------------------------------------+
void ProcessSellATRBreakout()
  {
  if ((CurrATR - PrevATR) >= MinATRPipsPerBar/10000 && Close[0] < Close[1] && PrevPrice != CurrPrice)
     {
     SellATRReturnValue = 1;
     PrevPrice = CurrPrice;
     SellBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Calc ADX                                                  |
//+------------------------------------------------------------------+
void ProcessCalcADX(int ShiftVal)
  {
  GetLastClose();
  if (ADXTimeFrame == 0) ADXTimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),ADXTimeFrame,0);
  CurrADX = NormalizeDouble(iADX(Symbol(),ADXTimeFrame,ADXPeriods,PRICE_CLOSE,MODE_MAIN,ShiftVal),7);
  PrevADX = NormalizeDouble(iADX(Symbol(),ADXTimeFrame,ADXPeriods,PRICE_CLOSE,MODE_MAIN,ShiftVal+1),7);
  }
//+------------------------------------------------------------------+
//| Process Buy ADX                                                  |
//+------------------------------------------------------------------+
void ProcessBuyADX()
  {
  if (CurrADX > PrevADX && UseADX == 1 && PrevPrice != CurrPrice)
     {
     BuyADXReturnValue = 1;
     PrevPrice = CurrPrice;
     BuyBarCross = Shift;
     }
  if (iADX(Symbol(),0,ADXPeriods,PRICE_CLOSE,MODE_MAIN,0) > ADXLevel && UseADX == 2 && PrevPrice != CurrPrice)
     { 
     BuyADXReturnValue = 1;
     PrevPrice = CurrPrice;
     BuyBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell ADX                                                 |
//+------------------------------------------------------------------+
void ProcessSellADX()
  {
  if (CurrADX < PrevADX && UseADX == 1 && PrevPrice != CurrPrice) 
     {
     SellADXReturnValue = 1;
     PrevPrice = CurrPrice;
     SellBarCross = Shift;
     }
  if (iADX(Symbol(),0,ADXPeriods,PRICE_CLOSE,MODE_MAIN,0) > ADXLevel && UseADX == 2 && PrevPrice != CurrPrice)
     {
     SellADXReturnValue = 1;
     PrevPrice = CurrPrice;
     SellBarCross = Shift;
     }
  }
//+------------------------------------------------------------------+
//| Process Time and day                                          |
//+------------------------------------------------------------------+
void ProcessTimeAndDay()
  {
  if (Hour() >= TradingHoursStart && Hour() <= TradingHoursEnd 
  && DayOfWeek() >= TradingDaysStart && DayOfWeek() <= TradingDaysEnd) TimeAndDayReturnValue = 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;
  if (FibTimeFrame == 0) FibTimeFrame = ReqTimeFrame;
  min = 9999;
  max = 0;
  for(i= 0; i < FibLookbackBars ; i++)
     {
      if (iHigh(Symbol(),FibTimeFrame,i) > max) max = iHigh(Symbol(),FibTimeFrame,i);
      if (iLow(Symbol(),FibTimeFrame,i) < min) min = iLow(Symbol(),FibTimeFrame,i);
     }
  F0 = max;
  F38 = max - (max - min) *38.2 / 100; 
  F50 = max - (max - min) *50 / 100; 
  F62 = 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 (SRTimeFrame == 0) SRTimeFrame = ReqTimeFrame;
  if (ZZDeviation >= ZZDepth) return;
  if (ZZBackstep >= ZZDeviation) return;
  double Zig;
  n=0;
  i = ShiftVal;
  while(n<5)
     {
     Zig=iCustom(NULL,SRTimeFrame,"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()
  {
  if (DivergenceTimeFrame == 0) DivergenceTimeFrame = ReqTimeFrame;
  for(i= 0; i < DivergenceLookbackBars ; i++)
     {
     StartRSI = iRSI(Symbol(),DivergenceTimeFrame,14,PRICE_CLOSE,DivergenceLookbackBars);
     EndRSI = iRSI(Symbol(),DivergenceTimeFrame,14,PRICE_CLOSE,1);
     StartMACD = iMACD(NULL,DivergenceTimeFrame,12,26,19,PRICE_CLOSE,MODE_MAIN,DivergenceLookbackBars);
     EndMACD = iMACD(NULL,DivergenceTimeFrame,12,26,19,PRICE_CLOSE,MODE_MAIN,1);
     StartPrice = iClose(Symbol(),DivergenceTimeFrame,DivergenceLookbackBars);
     EndPrice = iClose(Symbol(),DivergenceTimeFrame,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)
  {
  GetLastClose();
  if (CSTimeFrame == 0) CSTimeFrame = ReqTimeFrame;
  CurrPrice = iOpen(Symbol(),CSTimeFrame,0);
  CandleSticksBuySignal = 0;
  CandleSticksSellSignal = 0;
  if(candlePatternBullishEngulfing(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBullishPiercingLine(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBullishKicker(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBullishHarami(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBullishAbandonedBaby(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBullishMorningStarDoji(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBullishInvertedHammer(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBullishThreeWhiteSoldiers(ShiftVal)== true) CandleSticksBuySignal = 1;
  if(candlePatternBearishEngulfing(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternBearishDarkCloudCover(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternBearishKicker(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternBearishHarami(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternBearishAbandonedBaby(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternBearishEveningStarDoji(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternBearishShootingStar(ShiftVal)== true) CandleSticksSellSignal = 1;
  if(candlePatternBearishThreeBlackCrows(ShiftVal)== true) CandleSticksSellSignal = 1;
  }
//+------------------------------------------------------------------+
//| Process Buy CandleSticks                                         |
//+------------------------------------------------------------------+
void ProcessBuyCandleSticks()
  {
  if (CandleSticksBuySignal == 1 && CurrPrice != PrevPrice && CurrentBarPass == 1)
     {
     BuyCandleSticksReturnValue = 1;
     BuyBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell CandleSticks                                        |
//+------------------------------------------------------------------+
void ProcessSellCandleSticks()
  {
  if (CandleSticksSellSignal == 1 && CurrPrice != PrevPrice && CurrentBarPass == 1)
     {
     SellCandleSticksReturnValue = 1;
     SellBarCross = Shift;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Calc Volume                                              |
//+------------------------------------------------------------------+
void CalcVolume()
  {
  GetLastClose();
  CurrPrice = iOpen(Symbol(),ReqTimeFrame,0);
  }
//+------------------------------------------------------------------+
//| Process Buy Volume                                           |
//+------------------------------------------------------------------+
void ProcessBuyVolume()
  {
  if (Close[1] > Close[2] && Volume[1] > Volume[2] && CurrPrice != PrevPrice) 
     {
     BuyVolumeReturnValue = 1;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Process Sell Volume                                       |
//+------------------------------------------------------------------+
void ProcessSellVolume()
  {
  if (Close[1] < Close[2] && Volume[1] > Volume[2] && CurrPrice != PrevPrice)
     { 
     SellVolumeReturnValue = 1;
     PrevPrice = CurrPrice;
     }
  }
//+------------------------------------------------------------------+
//| Candlestick Pattern Functions                                    |
//+------------------------------------------------------------------+

bool candlePatternBullishEngulfing(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);

   if ((O1 > C1) && (C > O) && (C >= O1) && (C1 >= O) && (( C - O) > (O1 - C1))) return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBullishPiercingLine(int ShiftV)
   {
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);

   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   if ((C1 < O1) && (((O1 + C1) / 2) < C) && (O < C) && (O < C1) && (C < O1) && ((C - O) / (.001 + (H - L)) > 0.6)) return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBullishKicker(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   
   if ((O1 > C1) && (O >= O1) && (C > O))return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBullishHarami(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   
   if (((O1 > C1) && (C > O) && (C <= O1) && (C1 <= O) && ((C - O) < (O1 - C1))))return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBullishAbandonedBaby(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double O2 =  iOpen(Symbol(),CSTimeFrame,ShiftV+2);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   double C2 =  iClose(Symbol(),CSTimeFrame,ShiftV+2);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);
   double H1 =  iHigh(Symbol(),CSTimeFrame,ShiftV+1);
   double H2 =  iHigh(Symbol(),CSTimeFrame,ShiftV+2);
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double L1 =  iLow(Symbol(),CSTimeFrame,ShiftV+1);
   double L2 =  iLow(Symbol(),CSTimeFrame,ShiftV+2);
   
   if (C2 < O2 && MathAbs(C2 - O2) > (H2 - L2) * 0.50 && H1 > L2 && (C1 + O1) /2 >= ((H1 + L1) / 2) * 0.40
   && (C1 + O1) / 2 <= ((H1 + L1) / 2) * 0.60 && MathAbs (C1 - O1) <= (H1 - L1) * 10 && L > H1 && C < O)return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBullishMorningStarDoji(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double O2 =  iOpen(Symbol(),CSTimeFrame,ShiftV+2);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   double C2 =  iClose(Symbol(),CSTimeFrame,ShiftV+2);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);
   double H1 =  iHigh(Symbol(),CSTimeFrame,ShiftV+1);
   double H2 =  iHigh(Symbol(),CSTimeFrame,ShiftV+2);
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double L1 =  iLow(Symbol(),CSTimeFrame,ShiftV+1);
   double L2 =  iLow(Symbol(),CSTimeFrame,ShiftV+2);
   
   if (C2<O2 && MathAbs(C2 - O2) > (H2 - L2) * 0.50 && H1 < L2 && ((C1 + O1) / 2) - L > (H - L) * 0.40
   && ((C1 + O1) / 2) - L < (H - L) * 0.60 && MathAbs(C1 - O1) < (H1 - L1) * 0.20 && C > O) return(true);
   return(false);
   }

///+------------------------------------------------------------------+
bool candlePatternBullishInvertedHammer(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   
   if ((((H - L) > 3 * (O - C)) && ((H - C) / (.001 + H - L) > 0.6) && ((H - O) / (.001 + H - L) > 0.6)))return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBullishThreeWhiteSoldiers(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double O2 =  iOpen(Symbol(),CSTimeFrame,ShiftV+2);
   double O3 =  iOpen(Symbol(),CSTimeFrame,ShiftV+3);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   double C2 =  iClose(Symbol(),CSTimeFrame,ShiftV+2);
   double C3 =  iClose(Symbol(),CSTimeFrame,ShiftV+3);
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double L1 =  iLow(Symbol(),CSTimeFrame,ShiftV+1);
   double L2 =  iLow(Symbol(),CSTimeFrame,ShiftV+2);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);
   double H1 =  iHigh(Symbol(),CSTimeFrame,ShiftV+1);
   double H2 =  iHigh(Symbol(),CSTimeFrame,ShiftV+2);
   
   if ((C > O* 1.01) && (C1 > O1 * 1.01) &&(C2 > O2 * 1.01) && (C > C1) && (C1 > C2) &&(O < C1 && O > O1)
   &&(O1 < C2 && O1 > O2) &&(((H - C) / (H - L)) < .2) && (((H1 - C1) / (H1 - L1)) < .2)
   &&(((H2 - C2) / (H2 - L2)) < .2)) return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBearishEngulfing(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);

   if ((C1>O1)&&(O>C)&&(O>=C1)&&(O1>=C)&&((O-C)>(C1-O1))) return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBearishDarkCloudCover(int ShiftV)
   {
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);

   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,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) && (O > C1) && (C > O1) && ((O - C) / (.001 + (H - L)) > .6))return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBearishKicker(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   
   if ((O1 < C1) && (O <= O1) && (C <= O))return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBearishHarami(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   
   if (((C1 > O1) && (O > C) && (O <= C1) && (O1 <= C) && ((O - C) < (C1 - O1))))return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBearishAbandonedBaby(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double O2 =  iOpen(Symbol(),CSTimeFrame,ShiftV+2);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   double C2 =  iClose(Symbol(),CSTimeFrame,ShiftV+2);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);
   double H1 =  iHigh(Symbol(),CSTimeFrame,ShiftV+1);
   double H2 =  iHigh(Symbol(),CSTimeFrame,ShiftV+2);
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double L1 =  iLow(Symbol(),CSTimeFrame,ShiftV+1);
   double L2 =  iLow(Symbol(),CSTimeFrame,ShiftV+2);
   
   if (MathAbs(C2 - O2) > (H2 - L2) * 0.50 && C2 > O2 && MathAbs(C1 - O1) <= (H1-L1) * 0.05
   && ((C1 + O1) / 2) - L1 >= (H1 - L1) * 0.40 && ((C1 + O1) / 2) - L1 <= (H1 - L1) *0.60
   && L1 > H2 && C < O && H < L1 && O > C2 && (L > O2 || C < L2))return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBearishEveningStarDoji(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double O2 =  iOpen(Symbol(),CSTimeFrame,ShiftV+2);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   double C2 =  iClose(Symbol(),CSTimeFrame,ShiftV+2);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);
   double H1 =  iHigh(Symbol(),CSTimeFrame,ShiftV+1);
   double H2 =  iHigh(Symbol(),CSTimeFrame,ShiftV+2);
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double L1 =  iLow(Symbol(),CSTimeFrame,ShiftV+1);
   double L2 =  iLow(Symbol(),CSTimeFrame,ShiftV+2);
   
   if (((C2 > O2) && ((C2 - O2)/(.001 + H2 - L2) > .6)&&(C2 < O1) && (C1 > O1)
   &&((H1 - L1) > (3 * (C1 - O1))) && (O > C) && (O < O1))) return(true);
   return(false);
   }

///+------------------------------------------------------------------+
bool candlePatternBearishShootingStar(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double H =  iHigh(Symbol(),CSTimeFrame,ShiftV);
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   
   if ((((H - L) > 4 * (O - C)) && ((H - C) / (.001 + H - L) >= 0.75) && ((H - O) / (.001 + H - L) >= 0.75)))return(true);
   return(false);
   }

//+------------------------------------------------------------------+
bool candlePatternBearishThreeBlackCrows(int ShiftV)
   {
   double O =  iOpen(Symbol(),CSTimeFrame,ShiftV);
   double O1 =  iOpen(Symbol(),CSTimeFrame,ShiftV+1);
   double O2 =  iOpen(Symbol(),CSTimeFrame,ShiftV+2);
   double C =  iClose(Symbol(),CSTimeFrame,ShiftV);
   double C1 =  iClose(Symbol(),CSTimeFrame,ShiftV+1);
   double C2 =  iClose(Symbol(),CSTimeFrame,ShiftV+2);
   double L =  iLow(Symbol(),CSTimeFrame,ShiftV);
   double L1 =  iLow(Symbol(),CSTimeFrame,ShiftV+1);
   double L2 =  iLow(Symbol(),CSTimeFrame,ShiftV+2);
   
   if (O1 < O2 && O1 > C2 && O < O1 && O > C1 && C1 < L2 && C < L1 && C2 < L2 * 1.05 && C1 < L1 * 1.05 && C < L * 1.05) return(true);
   return(false);
   }
