Navigation´╝ÜHome > Content >

NightScalperEA_v0.5.mq4

Time: 2010-03-04 | Download file:NightScalperEA_v0.5.mq4

//////////////////////////////////////////////////////
//        if you make money from this EA            //
//   please make donnation to my paypal Account     //
//           nikolaospantzos@gmail.com              //
//////////////////////////////////////////////////////
#property copyright "Copyright 2013 by PanNik"
//=================================================================================================================================================//
#import "Kernel32.dll"
void GetSystemTime(int& TimeArray[]);
#import
//=================================================================================================================================================//
extern string S0="-------General Sets-------";
extern string EA_Comment          = "";     
extern string MagicInfo           = "if MAGIC=0, a unique value will be generated by EA";  
extern int    Magic               = 0;
extern bool   New_Trade           = true;
extern int    Slippage            = 3;
extern bool   SendMailInfo        = false;   
extern bool   Hide_SL_TP          = false;
extern string S1="-------Lot and Risk-------";
extern double ManualLot           = 0;
extern double RiskFactor          = 5;
extern string S2="-------Spread Limits-------";
extern string SpreandInfo         = "if Max_Spread=0, EA will use internal settings";
extern double MaxSpread           = 0;
extern int    LastTicksCount      = 30;
extern int    PlusSpreadPerCent   = 25;
extern string S3="-------Hours Limits-------";
extern string HoursInfo           = "if Open_Hour=0 or/and Close_Hour=0, EA will use internal settings";
extern int    Open_Hour           = 0;
extern int    Close_Hour          = 0;
extern string S4="-------Times and Days-------";
extern int    GMT_Offset          = 99;
extern bool   TradeOnFriday       = true;
extern int    Friday_Hour         = 24;
extern string S5="-------Manage Loss Trade-------";
extern bool   Trade_Pause_On_Loss = false;
extern string Pause_TypeStr       = "Puase type: 1=Use Minutes, 2=Use Pips";
extern int    Pause_Type          = 1;
extern int    Trade_Pause_Minutes = 60;
extern double Trade_Pause_Pips    = 20;
extern string S6="-------Second Trade-------";
extern bool   Allow_Second_Trade  = false;
extern double Distance            = 15;
extern double Lot_Factor          = 1;
extern string S7="-------Auto Sets-------";
extern string InfoAutoSets        = "!!! Below this line works sets if UseAutoSets=false !!!";
extern bool   UseAutoSets         = true;
extern string S8="-------TakeProfit and StopLoss-------";
extern double TakeProfit          = 60;
extern double StopLoss            = 50;
extern string S9="-------CCI Sets-------";
extern bool   Use_CCI             = true;
extern int    CCI_Period          = 10;
extern double CCI_LevelEntry      = 200;
extern bool   Use_CCI_Exit        = true;
extern double CCI_LevelExit       = 140;
extern string CCI_ExitInfo        = "1:exit in profit, 2:exit in loss, 3:exit in profit or loss";
extern int    CCI_ExitMode        = 3;
extern string S10="-------WPR Sets-------";
extern bool   Use_WPR             = true;
extern int    WPR_Period          = 10;
extern double WPR_LevelEntry      = 91;
extern bool   Use_WPR_Exit        = true;
extern double WPR_LevelExit       = 40;
extern string WPR_ExitInfo        = "1:exit in profit, 2:exit in loss, 3:exit in profit or loss";
extern int    WPR_ExitMode        = 3;
extern string S11="-------BB Sets-------";
extern bool   Use_BB              = true;
extern int    BB_Period           = 20;
extern int    BB_Deviation        = 2;
extern bool   Use_BB_Exit         = true;
extern string BB_ExitInfo         = "1:exit in profit, 2:exit in loss, 3:exit in profit or loss";
extern int    BB_ExitMode         = 3;
extern string S12="-------Envelopes Sets-------";
extern bool   Use_Envelopes       = true;
extern int    Envelope_Period     = 36;
extern double Envelope_Deviation  = 0.05;
extern int    Breakout_MA_Period  = 16;
extern bool   Use_Envelopes_Exit  = true;
extern string Envelopes_ExitInfo  = "1:exit in profit, 2:exit in loss, 3:exit in profit or loss";
extern int    Envelopes_ExitMode  = 3;
extern string S13="-------Fractals Sets-------";
extern bool   Use_Fractals        = true;
extern bool   Use_Fractals_Exit   = true;
extern string Fractals_ExitInfo   = "1:exit in profit, 2:exit in loss, 3:exit in profit or loss";
extern int    Fractals_ExitMode   = 3;
//=================================================================================================================================================//
bool Trade;
bool WarningPair;
datetime Last_Time;
string SessionInfo="";
string SpreadInfo="";
string EA_Name;
//--------------------------
int CountTickSpreadPair;
int CountTickSpread=0;
int TotalAddMeter=0;
int CntTick=0;
int LastOpenOrders=0;
int SpreadSampleSize=10;  
int CCI_Buy_Sig;
int CCI_Sell_Sig;
int WPR_Buy_Sig;
int WPR_Sell_Sig;
int Fractals_Buy_Sig;
int Fractals_Sell_Sig;  
int CCI_Exit_Buy_Sig;
int CCI_Exit_Sell_Sig;
int WPR_Exit_Buy_Sig;
int WPR_Exit_Sell_Sig; 
int BB_Buy_Exit_Sig;
int BB_Sell_Exit_Sig;
int Envelopes_Buy_Exit_Sig;
int Envelopes_Sell_Exit_Sig;
int Fractals_Buy_Exit_Sig;
int Fractals_Sell_Exit_Sig;
int BB_Buy_Sig;               
int BB_Sell_Sig;
int Envelopes_Buy_Sig;               
int Envelopes_Sell_Sig;
int Trade_Pause_Buy_Sig;
int Trade_Pause_Sell_Sig;
int TotalOpenOrders;
int LastOpenType;
int LastClosedType;
int LastClosedTime;
int MultiplierDigit;
int DSTprv=0;
int DSTcur=0;
int DSTextern=0;
int StarHour=0;
int EndHour=0;
int FridayHour=0;
int FirstSignalBuy=0;
int FirstSignalSell=0;
int TickCountMeter=0;
int LoopCount;
int BarsTotal;
//--------------------------
double Close1;
double ArraySpreadPair[300];
double AvgSpread;
double Lot;
double LastUpFractal;
double LastDownFractal;
double midFractal;
double Spread[0];
double CCI_1; 
double WPR_1; 
double PointsDec;
double LastBuyPrice;
double LastSellPrice;
double SumProfit;
double HistoryProfits;
double LastClosedProfit;
double LastClosedPrice;
double StopLevel;
double LotStep;
double MinLot;
double MaxLot;
double TickValue;
double TickSize;
double PairSpread;
double MaximumSpread;
//=================================================================================================================================================//
int init()
{
  WarningPair=false;
  EA_Name=WindowExpertName();
  if(EA_Comment=="") EA_Comment=EA_Name+"_"+Symbol();
  //-----------------------------------------------------------------
  //Digits points
  PointsDec=Point;
  MultiplierDigit=1;
  if((MarketInfo(Symbol(),MODE_DIGITS)==5.0)||(MarketInfo(Symbol(),MODE_DIGITS)==3.0)) MultiplierDigit=10;
  PointsDec*=MultiplierDigit;
  Slippage*=MultiplierDigit;
  //-----------------------------------------------------------------
  //Lots info
  LotStep=MarketInfo(Symbol(),MODE_LOTSTEP);
  MinLot=MarketInfo(Symbol(),MODE_MINLOT);
  MaxLot=MarketInfo(Symbol(),MODE_MAXLOT);
  TickValue=MarketInfo(Symbol(),MODE_TICKVALUE);
  TickSize=MarketInfo(Symbol(),MODE_TICKSIZE);
  //-----------------------------------------------------------------
  //Background
  if((!IsTesting())&&(!IsOptimization()))
  {
  if(ObjectFind("Background1")==-1)
  {
  ObjectCreate("Background1",OBJ_LABEL,0,0,0);
  ObjectSet("Background1",OBJPROP_CORNER,0);
  ObjectSet("Background1",OBJPROP_BACK,false);
  ObjectSet("Background1",OBJPROP_YDISTANCE,18);
  ObjectSet("Background1",OBJPROP_XDISTANCE,0);
  ObjectSetText("Background1","g",125,"Webdings",Black);
  }
  //---
  if(ObjectFind("Background2")==-1)
  {
  ObjectCreate("Background2",OBJ_LABEL,0,0,0);
  ObjectSet("Background2",OBJPROP_CORNER,0);
  ObjectSet("Background2",OBJPROP_BACK,false);
  ObjectSet("Background2",OBJPROP_YDISTANCE,144);
  ObjectSet("Background2",OBJPROP_XDISTANCE,0);
  ObjectSetText("Background2","g",125,"Webdings",Black);
  }
  }
  //-----------------------------------------------------------------
  //Set Magic Number
  if(Magic==0) Magic=MagicNumber();
  //-----------------------------------------------------------------
  //Auto Sets
  if(StringSubstr(Symbol(),0,6)=="EURCAD")
  {
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=60; TakeProfit=60;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=120;
  Use_WPR=true; WPR_Period=10; WPR_LevelEntry=80; WPR_LevelExit=40;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="EURGBP")
  {
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=60; TakeProfit=60;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=120;
  Use_WPR=true; WPR_Period=11; WPR_LevelEntry=80; WPR_LevelExit=40;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="USDCAD")
  {
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=60; TakeProfit=60;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=125;
  Use_WPR=true; WPR_Period=10; WPR_LevelEntry=80; WPR_LevelExit=35;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="EURCHF")
  { 
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=40; TakeProfit=40;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=130;
  Use_WPR=true; WPR_Period=10; WPR_LevelEntry=80; WPR_LevelExit=30;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="USDCHF")
  { 
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=40; TakeProfit=40;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=120;
  Use_WPR=true; WPR_Period=10; WPR_LevelEntry=80; WPR_LevelExit=40;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="GBPCHF")
  { 
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=50; TakeProfit=50;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=130;
  Use_WPR=true; WPR_Period=10; WPR_LevelEntry=80; WPR_LevelExit=30;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="GBPCAD")
  {  
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=50; TakeProfit=50;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=130;
  Use_WPR=true; WPR_Period=10; WPR_LevelEntry=80; WPR_LevelExit=30;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="EURUSD")
  {  
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=40; TakeProfit=40;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=120;
  Use_WPR=true; WPR_Period=8; WPR_LevelEntry=80; WPR_LevelExit=42;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="CADCHF")
  {
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=50; TakeProfit=50;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=130;
  Use_WPR=true; WPR_Period=10; WPR_LevelEntry=80; WPR_LevelExit=30;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  if(StringSubstr(Symbol(),0,6)=="GBPUSD")
  {  
  if(Open_Hour==0) Open_Hour=18; if(Close_Hour==0) Close_Hour=24;
  if(UseAutoSets==true)
  {
  StopLoss=40; TakeProfit=40;
  Use_CCI=true; CCI_Period=10; CCI_LevelEntry=90; CCI_LevelExit=125;
  Use_WPR=true; WPR_Period=12; WPR_LevelEntry=80; WPR_LevelExit=38;
  Use_BB=true; BB_Period=20; BB_Deviation=2;
  Use_Envelopes=true; Envelope_Period=36; Envelope_Deviation=0.05; Breakout_MA_Period=16;
  Use_Fractals=true;
  Use_CCI_Exit=true; CCI_ExitMode=3;
  Use_WPR_Exit=true; WPR_ExitMode=3;
  Use_BB_Exit=true; BB_ExitMode=3;
  Use_Envelopes_Exit=true; Envelopes_ExitMode=3;
  Use_Fractals_Exit=true; Fractals_ExitMode=3;
  }
  }
  else
  WarningPair=true;
  //-----------------------------------------------------------------
  //Confirm range
  StopLevel=MathMax(MarketInfo(Symbol(),MODE_FREEZELEVEL)/MultiplierDigit,MarketInfo(Symbol(),MODE_STOPLEVEL)/MultiplierDigit);
  if(TakeProfit300) LastTicksCount=300;
  ArrayInitialize(ArraySpreadPair,0);
  //-----------------------------------------------------------------
  //Detect current DST
  if((IsTesting())||(IsOptimization()))
  {
  int GetMonth=Month();
  int GetDayOfMonth=Day();
  int GetDayOfWeek=DayOfWeek();
  if(GetDayOfWeek==0) GetDayOfWeek=7;
  //---
  if((GetMonth>3)&&(GetMonth<10)) DSTcur=1;
  if((GetMonth<3)&&(GetMonth>10)) DSTcur=0;
  if((GetMonth==3)&&(GetDayOfMonth<=24)) DSTcur=0;
  if((GetMonth==10)&&(GetDayOfMonth<=24)) DSTcur=1;
  //---
  if((GetMonth==3)&&(GetDayOfMonth>24))//detect last week
  {
  if((GetDayOfMonth==31)&&(GetDayOfWeek==7)) DSTcur=1;
  if((GetDayOfMonth==30)&&(GetDayOfWeek==7)) DSTcur=1;
  if((GetDayOfMonth==29)&&(GetDayOfWeek==7)) DSTcur=1;
  if((GetDayOfMonth==28)&&(GetDayOfWeek==7)) DSTcur=1;
  if((GetDayOfMonth==27)&&(GetDayOfWeek==7)) DSTcur=1;
  if((GetDayOfMonth==26)&&(GetDayOfWeek==7)) DSTcur=1;
  if((GetDayOfMonth==25)&&(GetDayOfWeek==7)) DSTcur=1;
  }
  if((GetMonth==10)&&(GetDayOfMonth>24))//detect last week
  {
  if((GetDayOfMonth==31)&&(GetDayOfWeek==7)) DSTcur=0;
  if((GetDayOfMonth==30)&&(GetDayOfWeek==7)) DSTcur=0;
  if((GetDayOfMonth==29)&&(GetDayOfWeek==7)) DSTcur=0;
  if((GetDayOfMonth==28)&&(GetDayOfWeek==7)) DSTcur=0;
  if((GetDayOfMonth==27)&&(GetDayOfWeek==7)) DSTcur=0;
  if((GetDayOfMonth==26)&&(GetDayOfWeek==7)) DSTcur=0;
  if((GetDayOfMonth==25)&&(GetDayOfWeek==7)) DSTcur=0;
  }
  }
  //-----------------------------------------------------------------
  start();
  return (0);
}
//=================================================================================================================================================//
int deinit()
{
  ObjectDelete("Background1");
  ObjectDelete("Background2");
  Comment("");//clear comments
  return (0);
}
//=================================================================================================================================================//
int start()
{ 
  Trade=true;
  CCI_Buy_Sig=0;
  CCI_Sell_Sig=0;  
  WPR_Buy_Sig=0;
  WPR_Sell_Sig=0;
  BB_Buy_Sig=0;
  BB_Sell_Sig=0;
  Envelopes_Buy_Sig=0;
  Envelopes_Sell_Sig=0;
  Fractals_Buy_Sig=0;
  Fractals_Sell_Sig=0;
  LastUpFractal=0;
  LastDownFractal=0;
  Trade_Pause_Buy_Sig=1;
  Trade_Pause_Sell_Sig=1; 
  CCI_Exit_Buy_Sig=0;
  CCI_Exit_Sell_Sig=0;
  WPR_Exit_Buy_Sig=0;
  WPR_Exit_Sell_Sig=0;
  BB_Buy_Exit_Sig=0;
  BB_Sell_Exit_Sig=0;
  Envelopes_Buy_Exit_Sig=0;
  Envelopes_Sell_Exit_Sig=0;
  Fractals_Buy_Exit_Sig=0;
  Fractals_Sell_Exit_Sig=0;
  FirstSignalBuy=0;
  FirstSignalSell=0;
  AvgSpread=0;
  MaximumSpread=0;
  Close1=Close[1];
  BarsTotal=Bars;
  //-----------------------------------------------------------------
  //Not acceptable pair
  if((WarningPair==true)&&(UseAutoSets==true))
  {
  Print("Please attach/use expert in following chart/pair: EURCAD, EURGBP, USDCAD, EURCHF, USDCHF, GBPCHF, GBPCAD, EURUSD, CADCHF, GBPUSD");
  if((!IsTesting())&&(!IsOptimization()))
  Comment( 
          "\n*=====================*",   
          "\n   "+EA_Name+ " - By PanNik",
          "\n*=====================*",
          "\n   Not acceptable pair ! ! !",
          "\n   Please select to attach expert",
          "\n   in chart / pair of bellow.",
          "\n*=====================*",
          "\n   EURCAD",
          "\n   EURGBP",
          "\n   USDCAD",
          "\n   EURCHF",
          "\n   USDCHF",
          "\n   GBPCHF",
          "\n   GBPCAD",
          "\n   EURUSD",
          "\n   CADCHF",
          "\n   GBPUSD",
          "\n*=====================*"
         );
  Sleep(60000);       
  return(0);
  }
  //-----------------------------------------------------------------
  //Confirm use indicators
  if((Use_CCI==false)&&(Use_WPR==false)&&(Use_Fractals==false)&&(Use_BB==false)&&(Use_Envelopes==false))
  {
  Print("You must use at least one signal (CCI, WPR, Bands, Envelopes or Fractals");
  Sleep(60000);       
  return(0);
  } 
  //-----------------------------------------------------------------
  //Time Offset
  if((IsTesting())||(IsOptimization()))
  {
  if(GMT_Offset==99) {Alert("Please set GMT Offset in the EA inputs"); Sleep(60000);return(0);}
  DST();
  if((DSTcur==1)&&(DSTprv==0)) DSTextern=-1;
  if((DSTcur==1)&&(DSTprv==1)) DSTextern=0;
  if((DSTcur==0)&&(DSTprv==0)) DSTextern=0;
  if((DSTcur==0)&&(DSTprv==1)) DSTextern=1;
  }
  else DSTextern=0;
  //---
  if((!IsTesting())&&(!IsOptimization())) GMT_Offset=GMTOffset();
  //---
  StarHour=Open_Hour+GMT_Offset+DSTextern;
  EndHour=Close_Hour+GMT_Offset+DSTextern;
  FridayHour=Friday_Hour+GMT_Offset+DSTextern;
  if(StarHour>=24) StarHour=StarHour-24;
  if(EndHour>=24) EndHour=EndHour-24;
  //-----------------------------------------------------------------
  //Count orders
  if(Trade_Pause_On_Loss==true) CountHistoryOrders();
  CountCurrentOrders();
  //-----------------------------------------------------------------
  //Set spread
  PairSpread=MarketInfo(Symbol(),MODE_SPREAD)/MultiplierDigit;
  //---avrg spread
  LoopCount=LastTicksCount-1;
  if(TickCountMeter0) AvgSpread/=TotalAddMeter;
  //---max spread
  if(MaxSpread==0) MaximumSpread=AvgSpread+((AvgSpread/100)*PlusSpreadPerCent);
  if(MaxSpread>0) MaximumSpread=MaxSpread;
  //-----------------------------------------------------------------
  //Trade time
  if((TradeOnFriday==false)&&(TimeDayOfWeek(TimeCurrent()-GMT_Offset*3600)==5)) Trade=false;
  if((TradeOnFriday==true)&&(DayOfWeek()==5)&&(TimeHour(TimeCurrent())>FridayHour)) Trade=false;
  if((StarHour=EndHour))) Trade=false;
  if((StarHour>EndHour)&&(TimeHour(TimeCurrent())=EndHour)) Trade=false;
  if((Month()==12)&&(Day()>22)) Trade=false; 
  if((Month()==1)&&(Day()<5)) Trade=false; 
  //-----------------------------------------------------------------
  //Info session and spread
  if(!IsTesting()&&!IsOptimization()) 
  {
  if(Trade==true) SessionInfo="Session Open";
  if(Trade==false) SessionInfo="Session Closed";
  if(Ask-Bid<=MaximumSpread*PointsDec) SpreadInfo="Spread Normal";
  if(Ask-Bid>MaximumSpread*PointsDec) SpreadInfo="Spread High!";
  Display();//Add Display
  if((Hide_SL_TP==false)&&(TotalOpenOrders>0)) ModifyAll();//Double check to make sure all trades have TP and SL 
  }//end if((!IsTesting())&&(!IsOptimization())) 
  //-----------------------------------------------------------------
  //Set signals
  if(Use_CCI==false)
  {
  CCI_Buy_Sig=1;
  CCI_Sell_Sig=1;
  }
  if(Use_WPR==false)
  {
  WPR_Buy_Sig=1;
  WPR_Sell_Sig=1;
  }  
  if(Use_Fractals==false)
  {
  Fractals_Buy_Sig=1;
  Fractals_Sell_Sig=1;
  }
  if(Use_BB==false)
  {
  BB_Buy_Sig=1;
  BB_Sell_Sig=1;
  }
  if(Use_Envelopes==false)
  {
  Envelopes_Buy_Sig=1;
  Envelopes_Sell_Sig=1;
  }
  //-----------------------------------------------------------------
  //Start check for signal entry or exit
  if(((Trade==true)&&(TotalOpenOrders==0)&&(New_Trade==true))||(TotalOpenOrders>0))
  {
  //-----------------------------------------------------------------
  //Count iCCI levels
  if((Use_CCI==true)&&((TotalOpenOrders==0)||((Use_CCI_Exit==true)&&(TotalOpenOrders>0))))
  {
  CCI_1=iCCI(NULL,PERIOD_M15,CCI_Period,PRICE_CLOSE,0);  
  if(CCI_1<-CCI_LevelEntry) CCI_Buy_Sig=1;
  if(CCI_1>CCI_LevelEntry) CCI_Sell_Sig=1;
  }//end if((Use_CCI==true)...
  //-----------------------------------------------------------------
  //Count iWPR levels
  if(((CCI_Buy_Sig==1)||(CCI_Sell_Sig==1))||((Use_WPR_Exit==true)&&(TotalOpenOrders>0)))
  if((Use_WPR==true)&&((TotalOpenOrders==0)||((Use_WPR_Exit==true)&&(TotalOpenOrders>0))))
  {
  WPR_1=iWPR(NULL,PERIOD_M15,WPR_Period,0);
  if(WPR_1<-WPR_LevelEntry) WPR_Buy_Sig=1;
  if(WPR_1>-(100-WPR_LevelEntry)) WPR_Sell_Sig=1;
  }//end if((Use_WPR==true)...
  //-----------------------------------------------------------------
  //Count iFractals signals
  if(((WPR_Buy_Sig==1)||(WPR_Sell_Sig==1))||((Use_Fractals_Exit==true)&&(TotalOpenOrders>0)))
  if((Use_Fractals==true)&&((TotalOpenOrders==0)||((Use_Fractals_Exit==true)&&(TotalOpenOrders>0))))
  {
  for(int cnt1=1; cnt1LastDownFractal)&&(LastDownFractal!=0)) Fractals_Buy_Sig=1;
  if((Close10)))
  if((Use_BB==true)&&((TotalOpenOrders==0)||((Use_BB_Exit==true)&&(TotalOpenOrders>0))))
  {
  for(int cnt2=1; cnt2iBands(NULL,PERIOD_M15,BB_Period,BB_Deviation,0,PRICE_CLOSE,MODE_UPPER,cnt2))
  {
  BB_Buy_Sig=1;
  break;
  }
  if(Close[cnt2]iMA(NULL,PERIOD_M15,BB_Period,0,0,PRICE_CLOSE,1))&&(BB_Buy_Sig==1)) BB_Buy_Sig=1; else BB_Buy_Sig=0;
  if((Close[1]0)))
  if((Use_Envelopes==true)&&((TotalOpenOrders==0)||((Use_Envelopes_Exit==true)&&(TotalOpenOrders>0))))
  {
  for(int cnt3=1; cnt3iEnvelopes(NULL,PERIOD_M15,Envelope_Period,0,0,PRICE_CLOSE,Envelope_Deviation,MODE_UPPER,cnt3))
  {
  Envelopes_Buy_Sig=1;
  break;
  }
  if(iMA(NULL,PERIOD_M15,Breakout_MA_Period,0,0,PRICE_CLOSE,cnt3)iEnvelopes(NULL,PERIOD_M15,Envelope_Period,0,0,PRICE_CLOSE,Envelope_Deviation,MODE_UPPER,1))&&(Envelopes_Buy_Sig==1)) Envelopes_Buy_Sig=1; else Envelopes_Buy_Sig=0;
  if((Close[1]0)
  {
  if((Use_CCI==true)&&(Use_CCI_Exit==true)&&(((SumProfit>0)&&(CCI_ExitMode==1))||((SumProfit<0)&&(CCI_ExitMode==2))||(CCI_ExitMode==3)))
  {
  if(CCI_1>CCI_LevelExit) CCI_Exit_Buy_Sig=1;
  if(CCI_1<-CCI_LevelExit) CCI_Exit_Sell_Sig=1;
  }  
  if((Use_WPR==true)&&(Use_WPR_Exit==true)&&(((SumProfit>0)&&(WPR_ExitMode==1))||((SumProfit<0)&&(WPR_ExitMode==2))||(WPR_ExitMode==3)))
  {
  if(WPR_1>-WPR_LevelExit) WPR_Exit_Buy_Sig=1;
  if(WPR_1<(-100+WPR_LevelExit)) WPR_Exit_Sell_Sig=1;
  }
  if((Use_BB==true)&&(Use_BB_Exit==true)&&(((SumProfit>0)&&(BB_ExitMode==1))||((SumProfit<0)&&(BB_ExitMode==2))||(BB_ExitMode==3)))
  {
  if(BB_Sell_Sig==1) BB_Buy_Exit_Sig=1;
  if(BB_Buy_Sig==1) BB_Sell_Exit_Sig=1;
  }
  if((Use_Envelopes==true)&&(Use_Envelopes_Exit==true)&&(((SumProfit>0)&&(Envelopes_ExitMode==1))||((SumProfit<0)&&(Envelopes_ExitMode==2))||(Envelopes_ExitMode==3)))
  {
  if(Envelopes_Sell_Sig==1) Envelopes_Buy_Exit_Sig=1;
  if(Envelopes_Buy_Sig==1) Envelopes_Sell_Exit_Sig=1;
  }
  if((Use_Fractals==true)&&(Use_Fractals_Exit==true)&&(((SumProfit>0)&&(Fractals_ExitMode==1))||((SumProfit<0)&&(Fractals_ExitMode==2))||(Fractals_ExitMode==3)))
  {
  if((Close1>LastUpFractal)&&(LastUpFractal!=0)) Fractals_Buy_Exit_Sig=1;
  if((Close10)
  //-----------------------------------------------------------------
  //Open trades
  if(TotalOpenOrders==0)
  {
  //---First buy trade
  if(FirstSignalBuy==1) 
  {
  Lot=CalculateLots(RiskFactor);
  int Ticket_1=0;
  Ticket_1=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,EA_Comment,Magic,0,Blue);
  if(Ticket_1>0)
  {
  Last_Time=iTime(NULL,0,0);
  if(Hide_SL_TP==false) ModifyAll();
  if(SendMailInfo==true)  SendMail("Open First Buy","Open Buy First Order "+Symbol()+", No Of Ticket: "+Ticket_1+", Order Open Price: "+Ask);
  }
  }
  //---First sell trade   
  if(FirstSignalSell==1) 
  {
  Lot=CalculateLots(RiskFactor);
  int Ticket_2=0;
  Ticket_2=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,0,0,EA_Comment,Magic,0,Red);
  if(Ticket_2>0)
  {
  Last_Time=iTime(NULL,0,0);
  if(Hide_SL_TP==false) ModifyAll();
  if(SendMailInfo==true)  SendMail("Open First Sell","Open Sell First Order "+Symbol()+", No Of Ticket: "+Ticket_1+", Order Open Price: "+Bid);
  }
  }
  }//end if(TotalOpenOrders==0)
  //---Second buy trade
  if((TotalOpenOrders==1)&&(Allow_Second_Trade==true))
  {
  if((Time[0]!=Last_Time)&&(Trade_Pause_Buy_Sig==1)&&(LastOpenType==0)&&(Ask<=LastBuyPrice-(Distance*PointsDec)))
  {
  Lot=CalculateLots(RiskFactor*Lot_Factor);
  Ticket_1=0;
  Ticket_1=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,EA_Comment,Magic,0,Blue);
  if(Ticket_1>0)
  {
  Last_Time=iTime(NULL,0,0);
  if(Hide_SL_TP==false) ModifyAll();
  if(SendMailInfo==true)  SendMail("Open Second Buy","Open Buy Second Order "+Symbol()+", No Of Ticket: "+Ticket_1+", Order Open Price: "+Ask);
  }
  //---
  }
  //---Second sell trade   
  if((Time[0]!=Last_Time)&&(Trade_Pause_Sell_Sig==1)&&(LastOpenType==1)&&(Bid>=LastSellPrice+(Distance*PointsDec)))
  {
  Lot=CalculateLots(RiskFactor*Lot_Factor);
  Ticket_2=0;
  Ticket_2=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,0,0,EA_Comment,Magic,0,Red);
  if(Ticket_2>0)
  {
  Last_Time=iTime(NULL,0,0);
  if(Hide_SL_TP==false) ModifyAll();
  if(SendMailInfo==true)  SendMail("Open Second Sell","Open Sell Second Order "+Symbol()+", No Of Ticket: "+Ticket_1+", Order Open Price: "+Bid);
  }
  //---
  }
  }//end if((TotalOpenOrders==1)&&(Allow_Second_Trade==true))
  }//end if(((Trade==true)&&(TotalOpenOrders==0)&&... 
  //-----------------------------------------------------------------
  //Count history orders
  if((!IsTesting())&&(!IsOptimization()))
  {
  if(CntTick<4)//count first times
  {
  CntTick++;
  if(CntTick<3) CountHistoryOrders();
  }
  //---
  if(LastOpenOrders!=TotalOpenOrders)//count if open or close order
  {
  if(LastOpenOrders>TotalOpenOrders) CountHistoryOrders();
  LastOpenOrders=TotalOpenOrders;
  }
  }
  //-----------------------------------------------------------------
  return (0);
}
//=================================================================================================================================================//
void CountCurrentOrders()
{
  int total=OrdersTotal();
  TotalOpenOrders=0;
  LastBuyPrice=0;
  LastSellPrice=0;
  LastOpenType=-1;
  SumProfit=0;
  //-----------------------------------------------------------------
  for(int cnt=0; cnt=TakeProfit*PointsDec)||(OrderOpenPrice()-Ask>=StopLoss*PointsDec)) CloseAll(OrderTicket(),OrderType());
  }
  if(OrderType()==OP_SELL)
  {
  LastSellPrice=OrderOpenPrice();
  LastOpenType=OP_SELL;
  if((Hide_SL_TP==true)&&(OrderOpenPrice()-Bid>=TakeProfit*PointsDec)||(Bid-OrderOpenPrice()>=StopLoss*PointsDec)) CloseAll(OrderTicket(),OrderType());
  }
  TotalOpenOrders++;
  SumProfit+=OrderProfit()+OrderCommission()+OrderSwap();
  }
  }
  }
  //-----------------------------------------------------------------
  return(0);
}
//=================================================================================================================================================//
void CountHistoryOrders()
{
  int total=OrdersHistoryTotal();
  HistoryProfits=0;
  LastClosedProfit=0;
  LastClosedPrice=0;
  LastClosedType=-1;
  LastClosedTime=0;
  //-----------------------------------------------------------------
  for(int cnt=0; cnt=0; cnt--) 
  { 
  if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
  {
  if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==Magic)&&(OrderCloseTime()==0)) 
  {
  //--- close buy
  if((OrderType()==OP_BUY)&&(OrderType()==OrdrType)&&((OrderTicket()==OrdrTicket)||(OrdrTicket==0)))
  {
  WasOrderClosed=false;
  WasOrderClosed=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Aquamarine);
  if(WasOrderClosed>0) if(SendMailInfo==true)  SendMail("Close Buy","Close Buy Order "+Symbol()+", No Of Ticket: "+OrderTicket()+", Order Close Price: "+Bid+". Profit Order: "+OrderProfit());
  }
  //--- close sell
  if((OrderType()==OP_SELL)&&(OrderType()==OrdrType)&&((OrderTicket()==OrdrTicket)||(OrdrTicket==0)))
  {
  WasOrderClosed=false;
  WasOrderClosed=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Coral);
  if(WasOrderClosed>0) if(SendMailInfo==true)  SendMail("Close Sell","Close Sell Order "+Symbol()+", No Of Ticket: "+OrderTicket()+", Order Close Price: "+Ask+". Profit Order: "+OrderProfit());
  }
  //---
  } 
  }
  }
  return(0); 
}
//=================================================================================================================================================//
double CalculateLots(double Risk) 
{
  double LotSize;
  //---
  if(ManualLot>0) LotSize=ManualLot;//manual
  else
  LotSize=(AccountFreeMargin()*(Risk/100))/(StopLoss*(TickValue/(TickSize/PointsDec)));//auto
  //---
  return(MathMin(MathMax(MinLot,MathRound(LotSize/LotStep)*LotStep),MaxLot));
}
//=================================================================================================================================================//
void ModifyAll() 
{ 
  for (int cnt=OrdersTotal()-1 ; cnt>=0; cnt--) 
  { 
  if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false) continue;
  if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
  { 
  if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==Magic)) 
  { 
  if((OrderStopLoss()==0)||(OrderTakeProfit()==0))
  {
  if(OrderType()==OP_BUY) OrderModify(OrderTicket(),OrderOpenPrice(),ND(Bid-PointsDec*StopLoss),ND(Ask+TakeProfit*PointsDec),0,Blue); 
  if(OrderType()==OP_SELL) OrderModify(OrderTicket(),OrderOpenPrice(),ND(Ask+PointsDec*StopLoss),ND(Bid-TakeProfit*PointsDec),0,Red);
  }  
  } 
  }
  }
  return(0);
}
//=================================================================================================================================================//
int MagicNumber()
{
  //------------------------------------------------------
  //Calculate symbol
  string SymbolName=Symbol();
  int SymbolNumber=StringLen(SymbolName);
  int LocalChart=0;
  int LocalCounter;
  int Magig;
  //------------------------------------------------------
  //Calculate account number
  for(LocalCounter=0; LocalCounter3)&&(GetMonth<10)) DSTprv=1;
  if((GetMonth<3)&&(GetMonth>10)) DSTprv=0;
  if((GetMonth==3)&&(GetDayOfMonth<=24)) DSTprv=0;
  if((GetMonth==10)&&(GetDayOfMonth<=24)) DSTprv=1;
  //---
  if((GetMonth==3)&&(GetDayOfMonth>24))//detect last week
  {
  if((GetDayOfMonth==31)&&(GetDayOfWeek==7)) DSTprv=1;
  if((GetDayOfMonth==30)&&(GetDayOfWeek==7)) DSTprv=1;
  if((GetDayOfMonth==29)&&(GetDayOfWeek==7)) DSTprv=1;
  if((GetDayOfMonth==28)&&(GetDayOfWeek==7)) DSTprv=1;
  if((GetDayOfMonth==27)&&(GetDayOfWeek==7)) DSTprv=1;
  if((GetDayOfMonth==26)&&(GetDayOfWeek==7)) DSTprv=1;
  if((GetDayOfMonth==25)&&(GetDayOfWeek==7)) DSTprv=1;
  }
  if((GetMonth==10)&&(GetDayOfMonth>24))//detect last week
  {
  if((GetDayOfMonth==31)&&(GetDayOfWeek==7)) DSTprv=0;
  if((GetDayOfMonth==30)&&(GetDayOfWeek==7)) DSTprv=0;
  if((GetDayOfMonth==29)&&(GetDayOfWeek==7)) DSTprv=0;
  if((GetDayOfMonth==28)&&(GetDayOfWeek==7)) DSTprv=0;
  if((GetDayOfMonth==27)&&(GetDayOfWeek==7)) DSTprv=0;
  if((GetDayOfMonth==26)&&(GetDayOfWeek==7)) DSTprv=0;
  if((GetDayOfMonth==25)&&(GetDayOfWeek==7)) DSTprv=0;
  }
  //---
  return(0);
} 
//=================================================================================================================================================//
int GMTOffset()
{
  int TimeArray[4];
  int TZInfoArray[43];
  int nYear,nMonth,nDay,nHour,nMin,nSec,nMilliSec;
  string sMilliSec;
  GetSystemTime(TimeArray);
  nYear=TimeArray[0]&0x0000FFFF;
  nMonth=TimeArray[0]>>16;
  nDay=TimeArray[1]>>16;
  nHour=TimeArray[2]&0x0000FFFF;
  nMin=TimeArray[2]>>16;
  nSec=TimeArray[3]&0x0000FFFF;
  nMilliSec=TimeArray[3]>>16;
  string LocalTimeS=FormatDateTime(nYear,nMonth,nDay,nHour,nMin,nSec);
  double GMTdiff=TimeCurrent()-StrToTime(LocalTimeS);
  return(MathRound(GMTdiff/3600.0));
}
//=================================================================================================================================================//
string FormatDateTime(int nYear,int nMonth,int nDay,int nHour,int nMin,int nSec)
{
  string sMonth,sDay,sHour,sMin,sSec;
  sMonth=100+nMonth;
  sMonth=StringSubstr(sMonth,1);
  sDay=100+nDay;
  sDay=StringSubstr(sDay,1);
  sHour=100+nHour;
  sHour=StringSubstr(sHour,1);
  sMin=100+nMin;
  sMin=StringSubstr(sMin,1);
  sSec=100+nSec;
  sSec=StringSubstr(sSec,1);
  return(StringConcatenate(nYear,".",sMonth,".",sDay," ",sHour,":",sMin,":",sSec));
}
//=================================================================================================================================================//
      

Recommend