#property copyright "Copyright © 2008, FXAW"
#property link      "http://fxaw.activeboard.com/forum.spark?forumID=117707&p=3&topicID=21828798"

extern string    Order_settings="----------------------------------------------------------------------";
extern int       MagicNumber                      = 886699;
extern string    TradeComment                     = "Zyxord";
extern double    DistancePips                     = 10;
extern double    DistanceAtrMultiplier            = 0;
extern double    DistanceAtrPeriod                = 14;
extern bool      ClassicSL                        = false;
extern double    ClassicSLPips                    = 0;
extern double    ClassicSLAtrMultiplier           = 0;
extern double    ClassicSLAtrPeriod               = 14;
extern bool      LurchSL                          = false;
extern double    LurchSLPips                      = 25;
extern double    LurchSLAtrMultiplier             = 0;
extern double    LurchSLAtrPeriod                 = 14;
extern bool      ChandelierExit                   = false;
extern double    ChandelierAtrMultiplier          = 3;
extern double    ChandelierAtrPeriod              = 14;
extern bool      ChandelierProfitOnly             = true;
extern double    VisibleSL                        = 0;
extern bool      PartialClosePivot                = false;
extern bool      PartialCloseTP                   = false;
extern double    TakeProfitPips                   = 0;
extern double    TakeProfitAtrMultiplier          = 1;
extern double    TakeProfitAtrPeriod              = 14;
extern double    VisibleTP                        = 0;
extern bool      BreakEvenPivot                   = false;
extern double    BreakEvenPips                    = 0;
extern double    BreakEvenAtrMultiplier           = 0;
extern double    BreakEvenAtrPeriod               = 14;
extern double    LockPips                         = 0;
extern double    LockAtrMultiplier                = 0;
extern double    LockAtrPeriod                    = 14;
extern double    TrailingStopPips                 = 0;
extern double    TrailingStopAtrMultiplier        = 0;
extern double    TrailingStopAtrPeriod            = 14;
extern double    TrailingStopStartPips            = 0;
extern double    TrailingStepPips                 = 0;
extern double    PivotDistancePips                = 30;
extern int       Slippage                         = 4;
extern bool      UseFreshSignal                   = true;
extern bool      QQEAReentrySignal                = true;
extern bool      LurchReentrySignal               = true;
extern bool      CloseIfProfit                    = false;
extern bool      CloseAtOppositeSignal            = true;
extern bool      Hedge                            = true;
extern bool      UseTradingHours                  = false;
extern string    StartHour                        = "0700";
extern string    EndHour                          = "2200";
extern bool      FridayClose                      = true;
extern string    CloseHour                        = "2150";
extern string    Money_management_settings="----------------------------------------------------------------------";
extern double    Lots                             = 0.2;
extern double    LotDivider                       = 2;
extern bool      MoneyManagement                  = false;
extern bool      AllowMicro                       = false;
extern double    LeveragePercent                  = 1.0;
extern bool      Martingale                       = false;
extern double    LotMultiplier                    = 1.3;
extern int       MaxProgression                   = 3;

extern bool      UseLurch                         = true;
extern bool      UseQQEA                          = true;
extern bool      UseGann_HiLo_Activator_v2        = true;

extern string    Lurch_settings="----------------------------------------------------------------------";
extern int       ATRPeriod                        = 9;
extern double    ATRMultiplier                    = 3;
extern string    Lurch_losing_trade_settings="----------------------------------------------------------------------";
extern int       ATRPeriod2                       = 28;
extern double    ATRMultiplier2                   = 5;

extern string    QQEA_settings="----------------------------------------------------------------------";
extern int       SF                               = 5;
extern int       RSIPeriod                        = 14;
extern double    DARFACTOR                        = 4.236;

extern string    Gann_HiLo_Activator_v2_settings="----------------------------------------------------------------------";
extern int	     Lookback		                    = 10;

extern string    Pivot_Points_settings="----------------------------------------------------------------------";
extern int	     PivotSet		                    = 1;
extern int	     DayStartHour	                    = 9;

// Variables ***********************************************************************************************************
bool LongSignal[4],ShortSignal[4],TradingTime[2],CurrentTrade[3],FreshSignal[7];
double Temp[5000],PCandles[3],ODistance[2],TP[4],SL[4],TStop[4],TStep[4],BE[4],LPips[2],PDistance[3],OLots[4],OProfit[3],QQE[5],GHLA[3],Lurch[4],PP[9],Pivot[3],MinSL,SLoss,TProfit,Spread;
string Screen[4],AccountType,gvoop[3],gvosl[3],gvotp[3];
int LongArrow[2],ShortArrow[2],OTicket[4],SHour[2],EHour[2],CHour[2],PivotNumber[3],ServerTime,mult,i,z,v,d;
datetime OTime;
// Initialization ***************************************************************************************************************
int init()
{
if (Digits==3 || Digits==5) mult=10;
if (Digits==2 || Digits==4) mult=1;

MinSL = MarketInfo(Symbol(),MODE_STOPLEVEL)/mult;
if((VisibleSL<MinSL && VisibleSL!=0) || (VisibleTP<MinSL && VisibleTP!=0))
{MessageBox("The VisibleSL and VisibleTP should be at least "+DoubleToStr(MinSL,1)+" pips");}

if(MarketInfo(Symbol(), MODE_LOTSIZE) == 10000) {AccountType = "Mini";}
if(MarketInfo(Symbol(), MODE_LOTSIZE) == 100000) {AccountType = "Standard";}
if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) d=2;
if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) d=1;

if (!UseQQEA && QQEAReentrySignal) {QQEAReentrySignal=false;}
if (!UseLurch && LurchReentrySignal) {LurchReentrySignal=false;}
if (ClassicSL && ClassicSLPips==0 && ClassicSLAtrMultiplier==0) {ClassicSL=false;}
if (ClassicSL && ClassicSLPips!=0 && ClassicSLAtrMultiplier!=0) {ClassicSLPips=0;}
if (LurchSL) {ClassicSL=false;}
if (LurchSL && LurchSLPips!=0 && LurchSLAtrMultiplier!=0) {LurchSLPips=0;}
if (ChandelierExit) {TrailingStopPips=0; TrailingStopAtrMultiplier=0;}
if (ChandelierExit && !ChandelierProfitOnly) {ClassicSL=false; LurchSL=false;}
if (BreakEvenPips>0 && BreakEvenAtrMultiplier>0) {BreakEvenPips=0;}
if (LockPips>0 && LockAtrMultiplier>0) {LockPips=0;}
if (TrailingStopPips>0 && TrailingStopAtrMultiplier>0) {TrailingStopPips=0;}
if (LotDivider<=1) {PartialCloseTP=false;}

Slippage*=mult;
ODistance[1]=NormalizeDouble(DistancePips*Point*mult,Digits);
TP[0]=NormalizeDouble(VisibleTP*Point*mult,Digits);
TP[1]=NormalizeDouble(TakeProfitPips*Point*mult,Digits);
SL[0]=NormalizeDouble(VisibleSL*Point*mult,Digits);
SL[1]=NormalizeDouble(ClassicSLPips*Point*mult,Digits);
SL[2]=NormalizeDouble(LurchSLPips*Point*mult,Digits);
BE[1]=NormalizeDouble(BreakEvenPips*Point*mult,Digits);
LPips[1]=NormalizeDouble(LockPips*Point*mult,Digits);
TStop[1]=NormalizeDouble(TrailingStopPips*Point*mult,Digits);
TStop[2]=NormalizeDouble(TrailingStopStartPips*Point*mult,Digits);
TStep[1]=NormalizeDouble(TrailingStepPips*Point*mult,Digits);
PDistance[1]=NormalizeDouble(PivotDistancePips*Point*mult,Digits);
LongArrow[1]=0x003300;
ShortArrow[1]=0x0000FF;
SHour[1]=StrToInteger(StartHour);
EHour[1]=StrToInteger(EndHour);
CHour[1]=StrToInteger(CloseHour);
gvoop[1]=StringConcatenate(Symbol(),DoubleToStr(MagicNumber,0),"oop1");
gvoop[2]=StringConcatenate(Symbol(),DoubleToStr(MagicNumber,0),"oop2");
gvosl[1]=StringConcatenate(Symbol(),DoubleToStr(MagicNumber,0),"osl1");
gvosl[2]=StringConcatenate(Symbol(),DoubleToStr(MagicNumber,0),"osl2");
gvotp[1]=StringConcatenate(Symbol(),DoubleToStr(MagicNumber,0),"otp1");
gvotp[2]=StringConcatenate(Symbol(),DoubleToStr(MagicNumber,0),"otp2");
if (TP[1]==0 && TakeProfitAtrMultiplier==0)
{if (GlobalVariableGet(gvotp[1])!=0) GlobalVariableSet(gvotp[1],0);
if (GlobalVariableGet(gvotp[2])!=0) GlobalVariableSet(gvotp[2],0);}
return(0);}

int deinit()
{return(0);}

int start()
{
// Money Management ***********************************************************************************************
if(MoneyManagement)
{if (AccountType=="Standard")
{if (AllowMicro)
Lots = MathMax(0.01,NormalizeDouble(AccountEquity()*LeveragePercent/100000.0,2));
else Lots = MathMax(0.1,NormalizeDouble(AccountEquity()*LeveragePercent/100000.0,1));}
else if (AccountType=="Mini")
{if (AllowMicro)
Lots = MathMax(0.01,NormalizeDouble(AccountEquity()*LeveragePercent/10000.0,2));
else Lots = MathMax(0.1,NormalizeDouble(AccountEquity()*LeveragePercent/10000.0,1));}}
Lots=MathMin(Lots,MarketInfo(Symbol(),MODE_MAXLOT)); OLots[3]=Lots;
// Martingale lots calculation **************************************************************************************************
if (Martingale && !CurrentTrade[1] && !CurrentTrade[2])
{z=0; v=OrdersHistoryTotal()-1; while(v>=0)
{if (OrderSelect(v,SELECT_BY_POS,MODE_HISTORY))
{if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)  
{z+=(OrderProfit()<0 && OrderComment()==TradeComment);
if (OrderProfit()>=0) break;}} v--;}
z=MathMin(z,MaxProgression); 
OLots[3]=MathMin(NormalizeDouble(Lots*MathPow(LotMultiplier,z),d),MarketInfo(Symbol(),MODE_MAXLOT));}
// Trading hours check ********************************************************************************************                                            |
ServerTime=TimeHour(TimeCurrent())*100+TimeMinute(TimeCurrent());
TradingTime[1] = true;
if (SHour[1] < EHour[1] && (ServerTime < SHour[1] || ServerTime >= EHour[1])) {TradingTime[1]=false;}
if (SHour[1] > EHour[1] && ServerTime < SHour[1] && ServerTime >= EHour[1]) {TradingTime[1]=false;}
TradingTime[1]=((TradingTime[1] || !UseTradingHours) && !(FridayClose && DayOfWeek()==5 && CHour[1]<=ServerTime));
// Order management ************************************************************************************************                                              
CurrentTrade[1]=false;
CurrentTrade[2]=false;
for(i=OrdersTotal()-1; i>=0; i--) 
{if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)  
// Partial close and Friday close ***********************************************************************************
{if (OrderType()==OP_BUY)
{if (GlobalVariableGet(gvoop[1])!=0) GlobalVariableSet(gvoop[1],0);
CurrentTrade[1]=true;
OTicket[1]=OrderTicket();
OLots[1]=OrderLots();
OProfit[1]=OrderProfit();
FreshSignal[1]=false;
if (Pivot[1]!=0 && Bid>=Pivot[1])
{if (BreakEvenPivot && GlobalVariableGet(gvosl[1])<OrderOpenPrice())      
{GlobalVariableSet(gvosl[1],NormalizeDouble(OrderOpenPrice(),Digits));}
if (PartialClosePivot && OrderComment()==TradeComment && LotDivider>=1)
{OLots[0]=MathMax(MarketInfo(Symbol(),MODE_MINLOT),NormalizeDouble(OrderLots()/LotDivider,d));
OrderClose(OrderTicket(),OLots[0],Bid,Slippage,LongArrow[1]);}}
if (FridayClose && DayOfWeek()==5 && CHour[1]<=ServerTime)
{OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,LongArrow[1]);}
z=0; while(z<=6)
{if (OrderComment()==TradeComment && Bid<PP[z] && Bid>PP[z+1] && OrderOpenPrice()+PDistance[1]<=PP[z] && Pivot[1]==0)
{Pivot[1]=PP[z];PivotNumber[1]=z;}
z++;}}
if (OrderType()==OP_SELL) 
{if (GlobalVariableGet(gvoop[2])!=0) GlobalVariableSet(gvoop[2],0);
CurrentTrade[2] = true;
OTicket[2]=OrderTicket(); 
OLots[2]=OrderLots();
OProfit[2]=OrderProfit();
FreshSignal[2]=false;
if (Pivot[2]!=0 && Bid<=Pivot[2])
{if (BreakEvenPivot && (GlobalVariableGet(gvosl[2])>OrderOpenPrice() || GlobalVariableGet(gvosl[2])==0))      
{GlobalVariableSet(gvosl[2],NormalizeDouble(OrderOpenPrice(),Digits));}
if (PartialClosePivot && OrderComment()==TradeComment && LotDivider>=1)
{OLots[0]=MathMax(MarketInfo(Symbol(),MODE_MINLOT),NormalizeDouble(OrderLots()/LotDivider,d));
OrderClose(OrderTicket(),OLots[0],Ask,Slippage,ShortArrow[1]);}}
if (FridayClose && DayOfWeek()==5 && CHour[1]<=ServerTime)
{OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,ShortArrow[1]);}
z=0; while(z<=6)
{if (OrderComment()==TradeComment && Bid<PP[z] && Bid>PP[z+1] && OrderOpenPrice()-PDistance[1]>=PP[z+1] && Pivot[2]==0)
{Pivot[2]=PP[z+1];PivotNumber[2]=z+1;}
z++;}}
// Take profit calculation ********************************************************************************************
if (TP[1]>0 || TakeProfitAtrMultiplier>0)
{if (OrderType()==OP_BUY && GlobalVariableGet(gvotp[1])==0)
{if (TP[1]>0) {GlobalVariableSet(gvotp[1],NormalizeDouble(OrderOpenPrice()+TP[1],Digits));}
if (TakeProfitAtrMultiplier>0) {GlobalVariableSet(gvotp[1],NormalizeDouble(OrderOpenPrice()+iATR(NULL,0,TakeProfitAtrPeriod,1)*TakeProfitAtrMultiplier,Digits));}}
if (OrderType()==OP_SELL && GlobalVariableGet(gvotp[2])==0)
{if (TP[1]>0) {GlobalVariableSet(gvotp[2],NormalizeDouble(OrderOpenPrice()-TP[1],Digits));}
if (TakeProfitAtrMultiplier>0) {GlobalVariableSet(gvotp[2],NormalizeDouble(OrderOpenPrice()-iATR(NULL,0,TakeProfitAtrPeriod,1)*TakeProfitAtrMultiplier,Digits));}}}
// Stop loss calculation ***********************************************************************************************
if (ClassicSL)
{if (OrderType()==OP_BUY && GlobalVariableGet(gvosl[1])==0)
{if (SL[1]>0) {SLoss=NormalizeDouble(OrderOpenPrice()-SL[1],Digits);}
if (ClassicSLAtrMultiplier>0) {SLoss=NormalizeDouble(OrderOpenPrice()-iATR(NULL,0,ClassicSLAtrPeriod,1)*ClassicSLAtrMultiplier,Digits);}
GlobalVariableSet(gvosl[1],SLoss);}
if (OrderType()==OP_SELL && GlobalVariableGet(gvosl[2])==0)
{if (SL[1]>0) {SLoss=NormalizeDouble(OrderOpenPrice()+SL[1],Digits);}
if (ClassicSLAtrMultiplier>0) {SLoss=NormalizeDouble(OrderOpenPrice()+iATR(NULL,0,ClassicSLAtrPeriod,1)*ClassicSLAtrMultiplier,Digits);}
GlobalVariableSet(gvosl[2],SLoss);}}
if (LurchSL)
{if (OrderType()==OP_BUY && GlobalVariableGet(gvosl[1])==0)
{if (LurchSLAtrMultiplier==0) {SLoss=NormalizeDouble(MathMin(Lurch[1]-SL[2],OrderOpenPrice()-SL[2]),Digits);}
if (LurchSLAtrMultiplier>0) {Temp[0]=iATR(NULL,0,LurchSLAtrPeriod,1)*LurchSLAtrMultiplier;
SLoss=NormalizeDouble(MathMin(Lurch[1]-Temp[0],OrderOpenPrice()-Temp[0]),Digits);}
GlobalVariableSet(gvosl[1],SLoss);}
if (OrderType()==OP_SELL && GlobalVariableGet(gvosl[2])==0)
{if (LurchSLAtrMultiplier==0) {SLoss=NormalizeDouble(MathMax(Lurch[1]+SL[2],OrderOpenPrice()+SL[2]),Digits);}
if (LurchSLAtrMultiplier>0) {Temp[0]=iATR(NULL,0,LurchSLAtrPeriod,1)*LurchSLAtrMultiplier;
SLoss=NormalizeDouble(MathMax(Lurch[1]+Temp[0],OrderOpenPrice()+Temp[0]),Digits);}
GlobalVariableSet(gvosl[2],SLoss);}}
// Break even ***********************************************************************************************************                                                 
if(BE[1]>0  || BreakEvenAtrMultiplier>0)
{if(BE[1]>0) BE[0]=BE[1];
if (BreakEvenAtrMultiplier>0) BE[0]=iATR(NULL,0,BreakEvenAtrPeriod,1)*BreakEvenAtrMultiplier;
if (LPips[1]>0) LPips[0]=LPips[1];
if (LockAtrMultiplier>0) LPips[0]=iATR(NULL,0,LockAtrPeriod,1)*LockAtrMultiplier;
if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>=BE[0] && GlobalVariableGet(gvosl[1])<OrderOpenPrice()+LPips[0])      
{GlobalVariableSet(gvosl[1],NormalizeDouble(OrderOpenPrice()+LPips[0],Digits));}
if(OrderType()==OP_SELL && OrderOpenPrice()-Ask>=BE[0] && GlobalVariableGet(gvosl[2])>OrderOpenPrice()-LPips[0])
{GlobalVariableSet(gvosl[2],NormalizeDouble(OrderOpenPrice()-LPips[0],Digits));}}
// Trailing stop ******************************************************************************************************** 
if (TStop[1]>0 || TrailingStopAtrMultiplier>0)
{if (TStop[1]>0) TStop[0]=TStop[1];
if (TrailingStopAtrMultiplier>0) TStop[0]=iATR(NULL,0,TrailingStopAtrPeriod,1)*TrailingStopAtrMultiplier;
if(OrderType()==OP_BUY)
{if (Bid-OrderOpenPrice()>=TStop[0] && Bid-OrderOpenPrice()>=TStop[2])
{if (GlobalVariableGet(gvosl[1])<=Bid-TStop[0]-TStep[1])
{GlobalVariableSet(gvosl[1],NormalizeDouble(Bid-TStop[0],Digits));}}}
if (OrderType()==OP_SELL)
{if (OrderOpenPrice()-Ask>=TStop[0] && OrderOpenPrice()-Ask>=TStop[2])
{if (GlobalVariableGet(gvosl[2])>=Ask+TStop[0]+TStep[1] || GlobalVariableGet(gvosl[2])==0)
{GlobalVariableSet(gvosl[2],NormalizeDouble(Ask+TStop[0],Digits));}}}}
// Chandelier exit ********************************************************************************************************
if(ChandelierExit && OTime!=Time[0] && Volume[0]>0)
{PCandles[2]=MathFloor((MathAbs(Time[0]-OrderOpenTime()))/(Period()*60)+1); 
if(OrderType()==OP_BUY)
{Temp[0]=High[1];
z=1; while(z<=PCandles[2]) 
{if (Temp[0]<High[z]) {Temp[0]=High[z];}
z++;}
Temp[0]-=iATR(NULL,0,ChandelierAtrPeriod,1)*ChandelierAtrMultiplier;
if (ChandelierProfitOnly && Temp[0]>OrderOpenPrice())
{GlobalVariableSet(gvosl[1],NormalizeDouble(Temp[0],Digits));}
if (!ChandelierProfitOnly)
{GlobalVariableSet(gvosl[1],NormalizeDouble(Temp[0],Digits));}}
if(OrderType()==OP_SELL)
{Temp[0]=Low[1];
z=1; while(z<=PCandles[2]) 
{if (Temp[0]>Low[z]) {Temp[0]=Low[z];}
z++;}
Temp[0]+=iATR(NULL,0,ChandelierAtrPeriod,1)*ChandelierAtrMultiplier;
if (ChandelierProfitOnly && Temp[0]<OrderOpenPrice())
{GlobalVariableSet(gvosl[2],NormalizeDouble(Temp[0],Digits));}
if (!ChandelierProfitOnly)
{GlobalVariableSet(gvosl[2],NormalizeDouble(Temp[0],Digits));}}}
// Stop loss and take profit order close ***********************************************************************************
if (OrderType()==OP_BUY)
{if (GlobalVariableGet(gvotp[1])!=0 && Bid>=GlobalVariableGet(gvotp[1]))
{OLots[0]=OrderLots();
if (OrderComment()==TradeComment && PartialCloseTP)
{OLots[0]=MathMax(MarketInfo(Symbol(),MODE_MINLOT),NormalizeDouble(OrderLots()/LotDivider,d));}
if (OrderComment()==TradeComment || !PartialCloseTP)
OrderClose(OrderTicket(),OLots[0],Bid,Slippage,LongArrow[1]);}
if (GlobalVariableGet(gvosl[1])!=0 && Bid<=GlobalVariableGet(gvosl[1]))
{OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,LongArrow[1]);}}
if (OrderType()==OP_SELL)
{if (GlobalVariableGet(gvotp[2])!=0 && Ask<=GlobalVariableGet(gvotp[2]))
{OLots[0]=OrderLots();
if (OrderComment()==TradeComment && PartialCloseTP)
{OLots[0]=MathMax(MarketInfo(Symbol(),MODE_MINLOT),NormalizeDouble(OrderLots()/LotDivider,d));}
if (OrderComment()==TradeComment || !PartialCloseTP)
OrderClose(OrderTicket(),OLots[0],Ask,Slippage,ShortArrow[1]);}
if (GlobalVariableGet(gvosl[2])!=0 && Ask>=GlobalVariableGet(gvosl[2]))
{OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,ShortArrow[1]);}}}}}
// Working order opening ****************************************************************************************************
if (TradingTime[1])
{if (GlobalVariableGet(gvoop[1])>0 && Bid<=GlobalVariableGet(gvoop[1]) && !CurrentTrade[1] && (!CurrentTrade[2] || Hedge) && LongSignal[3])
{SLoss=NormalizeDouble(Ask-SL[0],Digits)*(SL[0]>0);
TProfit=NormalizeDouble(Ask+TP[0],Digits)*(TP[0]>0);
OrderSend(Symbol(),OP_BUY,OLots[3],Ask,Slippage,SLoss,TProfit,TradeComment,MagicNumber,0,LongArrow[1]);}
if (GlobalVariableGet(gvoop[2])>0 && Bid>=GlobalVariableGet(gvoop[2]) && !CurrentTrade[2] && (!CurrentTrade[1] || Hedge) && ShortSignal[3])
{SLoss=NormalizeDouble(Bid+SL[0],Digits)*(SL[0]>0);
TProfit=NormalizeDouble(Bid-TP[0],Digits)*(TP[0]>0);
OrderSend(Symbol(),OP_SELL,OLots[3],Bid,Slippage,SLoss,TProfit,TradeComment,MagicNumber,0,ShortArrow[1]);}}
// Variables reset **********************************************************************************************************
if (!CurrentTrade[1]) 
{if (GlobalVariableGet(gvosl[1])!=0) GlobalVariableSet(gvosl[1],0);
if (GlobalVariableGet(gvotp[1])!=0) GlobalVariableSet(gvotp[1],0);}
if (!CurrentTrade[2]) 
{if (GlobalVariableGet(gvosl[2])!=0) GlobalVariableSet(gvosl[2],0);
if (GlobalVariableGet(gvotp[2])!=0) GlobalVariableSet(gvotp[2],0);}
if (ShortSignal[0] || !TradingTime[1])
{if (GlobalVariableGet(gvoop[1])!=0) GlobalVariableSet(gvoop[1],0);}
if (LongSignal[0] || !TradingTime[1])
{if (GlobalVariableGet(gvoop[2])!=0) GlobalVariableSet(gvoop[2],0);}
if (!CurrentTrade[1]) {Pivot[1]=0;}
if (!CurrentTrade[2]) {Pivot[2]=0;}
// Entry, reentry and fresh signal check **************************************************************************************
if(OTime!=Time[0] && Volume[0]>0)
{OTime =Time[0];
Lurch[1]=iCustom(NULL,0,"#LURCH_SRv3",ATRPeriod,ATRMultiplier,0,1);
Lurch[2]=iCustom(NULL,0,"#LURCH_SRv3",ATRPeriod,ATRMultiplier,0,2);
Lurch[3]=iCustom(NULL,0,"#LURCH_SRv3",ATRPeriod2,ATRMultiplier2,0,1);

QQE[1] = iCustom(NULL,0,"QQEA",SF,RSIPeriod,DARFACTOR,0,1);
QQE[2] = iCustom(NULL,0,"QQEA",SF,RSIPeriod,DARFACTOR,1,1);
QQE[3] = iCustom(NULL,0,"QQEA",SF,RSIPeriod,DARFACTOR,0,2);
QQE[4] = iCustom(NULL,0,"QQEA",SF,RSIPeriod,DARFACTOR,1,2);

i=0; while(i<=7) {PP[i]=iCustom(NULL,0,"Pivot Points",PivotSet,DayStartHour,i,1); i++;}
i=1; while(i<=6) {FreshSignal[i]=false; i++;}
Temp[5]=0; Temp[6]=0; PCandles[1]=0; 
i=OrdersHistoryTotal()-1; while(i>=0)
{if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && ((Temp[5]==0 && !CurrentTrade[1]) || (Temp[6]==0 && !CurrentTrade[2])))           
{Temp[5]+=(OrderType()==OP_BUY); Temp[6]+=(OrderType()==OP_SELL);
PCandles[1]=MathFloor((MathAbs(Time[0]-OrderCloseTime()))/(Period()*60))+1; 
z=1; while(z<=PCandles[1])
{Temp[1]=iCustom(NULL,0,"QQEA",SF,RSIPeriod,DARFACTOR,0,z);
Temp[2]=iCustom(NULL,0,"QQEA",SF,RSIPeriod,DARFACTOR,1,z);
Temp[3]=iCustom(NULL,0,"#LURCH_SRv3",ATRPeriod,ATRMultiplier,0,z);
Temp[4]=Close[z];
if (OrderType()==OP_BUY && Temp[5]==1 && !CurrentTrade[1])
{if (!FreshSignal[3]) {FreshSignal[3]=(Temp[1]<=Temp[2]);}
if (!FreshSignal[5]) {FreshSignal[5]=(Temp[3]>=Temp[4]);}}
if (OrderType()==OP_SELL && Temp[6]==1 && !CurrentTrade[2])
{if (!FreshSignal[4]) {FreshSignal[4]=(Temp[1]>=Temp[2]);}
if (!FreshSignal[6]) {FreshSignal[6]=(Temp[3]<=Temp[4]);}}
z++;}}
i--;}}
if (Temp[5]==0 && !CurrentTrade[1]) {FreshSignal[3]=true; FreshSignal[5]=true;}
if (Temp[6]==0 && !CurrentTrade[2]) {FreshSignal[4]=true; FreshSignal[6]=true;}
if (QQEAReentrySignal && LurchReentrySignal)
{FreshSignal[1]=(FreshSignal[3] && FreshSignal[5] && (Close[2]<=Lurch[2] || QQE[3]<=QQE[4]));
FreshSignal[2]=(FreshSignal[4] && FreshSignal[6] && (Close[2]>=Lurch[2] || QQE[3]>=QQE[4]));}
if (QQEAReentrySignal && !LurchReentrySignal)
{FreshSignal[1]=(FreshSignal[3] && (Close[2]<=Lurch[2] || QQE[3]<=QQE[4]));
FreshSignal[2]=(FreshSignal[4]  && (Close[2]>=Lurch[2] || QQE[3]>=QQE[4]));}
if (!QQEAReentrySignal && LurchReentrySignal)
{FreshSignal[1]=(FreshSignal[5]  && (Close[2]<=Lurch[2] || QQE[3]<=QQE[4]));
FreshSignal[2]=(FreshSignal[6]  && (Close[2]>=Lurch[2] || QQE[3]>=QQE[4]));}
if (!QQEAReentrySignal && !LurchReentrySignal)
{FreshSignal[1]=(Close[2]<=Lurch[2] || QQE[3]<=QQE[4]);
FreshSignal[2]=(Close[2]>=Lurch[2] || QQE[3]>=QQE[4]);}

if (!UseFreshSignal) 
{FreshSignal[1]=true;
FreshSignal[2]=true;}

LongSignal[1] = (Close[1]>Lurch[1] || !UseLurch);
ShortSignal[1] = (Close[1]<Lurch[1] || !UseLurch);

LongSignal[2] = (QQE[1]>QQE[2] || !UseQQEA);
ShortSignal[2] = (QQE[1]<QQE[2] || !UseQQEA);

if (PP[PivotNumber[1]]!=Pivot[1]) {Pivot[1]=0;}
if (PP[PivotNumber[2]]!=Pivot[2]) {Pivot[2]=0;}}

GHLA[0] = iCustom(NULL,0,"Gann_HiLo_Activator_v2",Lookback,0,0);
LongSignal[3] = (Bid>GHLA[0] || !UseGann_HiLo_Activator_v2);
ShortSignal[3] = (Bid<GHLA[0] || !UseGann_HiLo_Activator_v2);
LongSignal[0]=(LongSignal[1] && LongSignal[2] && LongSignal[3]);
ShortSignal[0]=(ShortSignal[1] && ShortSignal[2] && ShortSignal[3]);
// Order opening and closing at signal ***************************************************************************************
if (LongSignal[0])
{if (CurrentTrade[2] && CloseAtOppositeSignal)
{if (OProfit[2]>=0 || (OProfit[2]<0 && !CloseIfProfit && (Close[1]>Lurch[3] || !UseLurch)))
{OrderClose(OTicket[2],OLots[2],Ask,Slippage,ShortArrow[1]);}}
if ((!CurrentTrade[2] || Hedge) && !CurrentTrade[1] && FreshSignal[1])
{if (ODistance[1]==0 && DistanceAtrMultiplier==0 && TradingTime[1])
{SLoss=NormalizeDouble(Ask-SL[0],Digits)*(SL[0]>0);
TProfit=NormalizeDouble(Ask + TP[0],Digits)*(TP[0]>0);
OrderSend(Symbol(),OP_BUY,OLots[3],Ask,Slippage,SLoss,TProfit,TradeComment,MagicNumber,0,LongArrow[1]);}
if ((ODistance[1]>0 || DistanceAtrMultiplier>0) && TradingTime[1])
{FreshSignal[1]=false;
if (ODistance[1]>0) {ODistance[0]=ODistance[1];}
if (DistanceAtrMultiplier>0) {ODistance[0]=iATR(NULL,0,DistanceAtrPeriod,1)*DistanceAtrMultiplier;}
GlobalVariableSet(gvoop[1],NormalizeDouble(Bid-ODistance[0],Digits));}}}
if (ShortSignal[0])
{if (CurrentTrade[1] && CloseAtOppositeSignal)
{if (OProfit[1]>=0 || (OProfit[1]<0 && !CloseIfProfit && (Close[1]<Lurch[3] || !UseLurch)))
{OrderClose(OTicket[1],OLots[1],Bid,Slippage,LongArrow[1]);}}
if ((!CurrentTrade[1] || Hedge) && !CurrentTrade[2] && FreshSignal[2])
{if (ODistance[1]==0 && DistanceAtrMultiplier==0 && TradingTime[1])
{SLoss=NormalizeDouble(Bid+SL[0],Digits)*(SL[0]>0);
TProfit=NormalizeDouble(Bid-TP[0],Digits)*(TP[0]>0);
OrderSend(Symbol(),OP_SELL,OLots[3],Bid,Slippage,SLoss,TProfit,TradeComment,MagicNumber,0,ShortArrow[1]);}
if ((ODistance[1]>0 || DistanceAtrMultiplier>0) && TradingTime[1])
{FreshSignal[2]=false;
if (ODistance[1]>0) {ODistance[0]=ODistance[1];}
if (DistanceAtrMultiplier>0) {ODistance[0]=iATR(NULL,0,DistanceAtrPeriod,1)*DistanceAtrMultiplier;}
GlobalVariableSet(gvoop[2],NormalizeDouble(Bid+ODistance[0],Digits));}}}
// Comment *********************************************************************************************************************
if (TradingTime[1])
{Screen[1] = "\nWe are inside trading hours\n";}
else
{Screen[1] = "\nWe are outside trading hours\n";}
Screen[1] = StringConcatenate("\nTrading hours ",StringSubstr(StartHour,0,2),":",StringSubstr(StartHour,2,2)," to ",StringSubstr(EndHour,0,2),":",StringSubstr(EndHour,2,2),Screen[1]);
Screen[1] = StringConcatenate("\n","Server time ",StringSubstr(DoubleToStr(ServerTime+10000,0),1,2),":",StringSubstr(DoubleToStr(ServerTime+10000,0),3,2),"\n",Screen[1]);
if (!UseTradingHours) {Screen[1] = "";}
Screen[0] = "";
if (Pivot[1]!=0 && PartialClosePivot) {Screen[0] = StringConcatenate("\nLong order partial close at price level ",Pivot[1]);}
if (Pivot[2]!=0 && PartialClosePivot) {Screen[0] = StringConcatenate(Screen[0],"\nShort order partial close at price level ",Pivot[2]);}
Screen[1] = StringConcatenate(Screen[1],Screen[0]);
Screen[0] = "";
if (!CurrentTrade[1] && GlobalVariableGet(gvoop[1])>0) {Screen[0] = StringConcatenate(Screen[0],"\nLong order at price level ",GlobalVariableGet(gvoop[1]));}
if (!CurrentTrade[2] && GlobalVariableGet(gvoop[2])>0) {Screen[0] = StringConcatenate(Screen[0],"\nShort order at price level ",GlobalVariableGet(gvoop[2]));}
if (CurrentTrade[1] && GlobalVariableGet(gvotp[1])>0) {Screen[0]=StringConcatenate(Screen[0],"\nLong order hidden take profit ",GlobalVariableGet(gvotp[1]));}
if (CurrentTrade[2] && GlobalVariableGet(gvotp[2])>0) {Screen[0]=StringConcatenate(Screen[0],"\nShort order hidden take profit ",GlobalVariableGet(gvotp[2]));}
if (CurrentTrade[1] && GlobalVariableGet(gvosl[1])>0) {Screen[0]=StringConcatenate(Screen[0],"\nLong order hidden stop loss ",GlobalVariableGet(gvosl[1]));}
if (CurrentTrade[2] && GlobalVariableGet(gvosl[2])>0) {Screen[0]=StringConcatenate(Screen[0],"\nShort order hidden stop loss ",GlobalVariableGet(gvosl[2]));}
Comment(Screen[1],Screen[0]);
return(0);}

