Navigation´╝ÜHome > Content >

ANS_v0.2.mq4

Time: 2014-07-24 | Download file:ANS_v0.2.mq4

//=================================================================================================================================================//
extern string S1="-------General Sets-------";
extern string EA_Comment          = "";     
extern string S2                  = "if MAGIC=0, a unique value will be generated by EA";  
extern bool   SendMailInfo        = false;   
extern int    Magic               = 0;
extern bool   New_Trade           = true;
extern int    Slippage            = 3;
extern bool   TradeSessionBox     = true;
extern color  SessionBoxColor     = Khaki;
extern int    DayShowSessionBox   = 1;
extern string S3="-------Lot and Risk-------";
extern bool   CountRiskBaseSL     = false;
extern double lot                 = 0;
extern double Risk                = 7.5;
extern string S4="-------Spread Limits-------";
extern string SpreandInfo         = "if Max_Spread=0, EA will use internal settings";
extern double Max_Spread_Open     = 0.0;
extern double Max_Spread_Close    = 0.0;
extern string S5="-------Time Over-------";
extern bool   UseTimeOver         = true;
extern int    MaxMinutesOpen      = 90;
extern string S6="-------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 S7="-------Times and Days-------";
extern int    GMT_Offset          = 99;
extern int    DelaySessionMinutes = 0;
extern bool   TradeOnFriday       = true;
extern int    Open_Hour_Friday    = 0;
extern int    Close_Hour_Friday   = 0;
extern bool   TradeInSession      = false;
extern string S8="-------ATR and PriceLevel Filter-------";
extern bool   UseATRfilter        = false;
extern int    Atr_Period          = 14;
extern string TypeATRfilterStr    = "1=Lower Level, 2=Higher Level";
extern int    Type_ATR_level      = 1;        
extern double AtrLevel            = 0.0006;
extern bool   UsePriceLevelFilter = false;
extern int    PrvHoursCheck1      = 0;
extern int    PrvHoursCheck2      = 3;
extern string S9="-------News Filter-------";
extern bool   Use_NewsFilter      = false;
extern bool   IncludeHigh         = true;
extern int    MinsBeforeHigh      = 360;  // mins before an event to stay out of trading
extern int    MinsAfterHigh       = 240;  // mins after  an event to stay out of trading
extern bool   IncludeMedium       = true;
extern int    MinsBeforeMedium    = 180;
extern int    MinsAfterMedium     = 120;
extern bool   IncludeLow          = true;
extern int    MinsBeforeLow       = 180;
extern int    MinsAfterLow        = 30;
extern bool   IncludeSpeaks       = false; // news items with "Speaks" in them have different characteristics
extern int    MinsBeforeSpeaks    = 360;
extern int    MinsAfterSpeaks     = 240;
extern bool   ReportAllForUSD     = true;
extern string S10="-------Manage Loss Trade-------";
extern bool   PauseOnLoss         = true;
extern double PauseMinPips        = 5.0;
extern int    PauseMaxMinutes     = 180;
extern string S11="-------Second Trade-------";
extern bool   Allow_Second_Trade  = true;
extern bool   SecondInOpenSession = false;
extern double Distance            = 12;
extern double Lot_Factor          = 0.5;
extern string InfoSets            = "!!! Below this line works sets if UseAutoSets=false !!!";
extern bool   UseAutoSets         = true;
extern string S12="-------TakeProfit and StopLoss-------";
extern bool   Hide_SL_TP          = false;
extern double TakeProfit          = 60;
extern double StopLoss            = 50;
extern bool   UseTrailingSL       = false;
extern double TrailingPips        = 4;
extern double TrailingAfter       = 9;
extern bool   UseDynamicSLTP      = false;
extern int    MinutesStep         = 30;
extern double PipsStep            = 5;
extern double MinimumPips         = 15;
extern string S13="-------CCI Sets-------";
extern bool   Use_CCI             = false;
extern int    CCI_Period          = 10;
extern double CCI_Entry           = 200;
extern double CCI_Exit            = 140;
extern string S14="-------WPR Sets-------";
extern bool   Use_WPR             = false;
extern int    WPR_Period          = 10;
extern double WPR_Entry           = 91;
extern double WPR_Exit            = 40;
extern string S15="-------RSI Sets-------";
extern bool   Use_RSI             = false;
extern int    RSI_Period          = 70;
extern double RSI_Entry           = 50;
extern string S16="-------Fractals Sets-------";
extern bool   Use_Fractals        = false;
extern double MidFractalDist      = 5;
extern double OppositFractalDist  = 10;
extern bool   Show_Fractal_Levels = false;
extern string S17="-------MA Sets-------";
extern bool   Use_MA_Dist         = false;
extern double MA_Dist_Period      = 4;
extern string Mode                = "0= Simple, 1=Exponential, 2=Smoothed, 3=Linear Weighted";
extern double MA_Dist_Mode        = 1;
extern double Dist_to_MA          = 4;
extern string S18="-------MA1 Sets-------";
extern bool   Use_MA1             = false;
extern int    MA_Period1          = 75;
extern string Mode1               = "0= Simple, 1=Exponential, 2=Smoothed, 3=Linear Weighted";
extern int    MA_Mode1            = 2;
extern string S19="-------MA2 Sets-------";
extern bool   Use_MA2             = false;
extern int    MA_Period2          = 19;
extern string Mode2               = "0= Simple, 1=Exponential, 2=Smoothed, 3=Linear Weighted";
extern int    MA_Mode2            = 2;
extern string S20="-------MA2 Slope Sets-------";
extern bool   Use_MA2_Slope       = false;
extern double MA_Slope            = 1;
extern string S21="-------BB Sets-------";
extern bool   Use_BB              = false;
extern int    BB_Period           = 10;
extern int    BB_Dev              = 2;
extern int    BB_Range            = 12;
extern int    BB_Penetration      = 2;
extern bool   Use_BB_Direction    = false;
extern string S22="-------Volatility Sets-------";
extern bool   Use_Volatility      = true;
extern int    Vis_atr             = 20;
extern int    Vis_std             = 25;
extern int    Sed_atr             = 55;
extern int    Sed_std             = 135;
extern double Threshold_level     = 2;
extern bool   lag_supressor       = true;
extern int    MinutesWaitToOpen   = 30;
extern bool   CloseInReverseSignal= false;
extern int    MinutesWaitToClose  = 5;
extern double PipsLossToClose     = 2;
extern string S23="-------HFT Sets-------";
extern bool   Use_HFT             = false;
extern string S24="-------Instant_Trendline_Filter Sets-------";
extern bool   Use_Trendline       = false;
extern double alpha               = 0.07;
//=================================================================================================================================================//
bool Trade;
bool Trade_Pause_Buy_Sig;
bool Trade_Pause_Sell_Sig;
string EA_Name;
string Session="";
datetime Last_Time;
//--------------------------
int LastTicksCount=30;
int TickCountMeter=0;
int TotalAddMeter=0;
int LoopCount;
int Count;
int CCI_Buy_Sig;
int CCI_Sell_Sig;
int WPR_Buy_Sig;
int WPR_Sell_Sig;
int MA_Buy_Sig1;
int MA_Sell_Sig1;
int MA_Buy_Sig2;
int MA_Sell_Sig2;
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 MA_Slope_Buy_Sig;               
int MA_Slope_Sell_Sig;
int BB_Buy_Sig;               
int BB_Sell_Sig;
int MA_Dist_Buy_Sig;               
int MA_Dist_Sell_Sig;
int RSI_Buy_Sig;
int RSI_Sell_Sig;
int VolatilityEntry;
int CrossUp;
int CrossDn;
int FilterATR;
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 StarHour_Friday=0;
int EndHour_Friday=0;
int CheckLevelPriceBuy=0;
int CheckLevelPriceSell=0;
int FirstSignalBuy=0;
int FirstSignalSell=0;
int BuyHFT;
int SellHFT;
int TrendlineBuy;
int TrendlineSell;
int TimeToOpen;
int TimeToTrailing=0;
int TimeToStart;
int TimeToEnd;
int PlusDays;
int FirstOrderTimeOpen;
//--------------------------
double ArraySpread[30];
double Lot;
double LastUpFractal;
double LastDownFractal;
double midFractal;
double CCI_1; 
double WPR_1; 
double MA_1_1; 
double MA_1_2; 
double MA_2_1; 
double MA_2_2; 
double BB_U_1; 
double BB_U_2; 
double BB_U_3; 
double BB_L_1; 
double BB_L_2; 
double BB_L_3; 
double MA_3_1; 
double RSI_1; 
double VolBuf0;
double VolBuf2;
double VolBuf0_i;
double VolBuf2_i;
double PointsDec;
double LastBuyPrice;
double LastSellPrice;
double SumProfit;
double NetProfit;
double HistoryProfits;
double LastClosedProfit;
double LastClosedPrice;
double LastClosedPips;
double StopLevel;
double LotStep;
double MinLot;
double MaxLot;
double TickValue;
double TickSize;
double FractalsUp2;
double FractalsUp3;
double FractalsLw2;
double FractalsLw3;
double TrendUp;
double TrendDn;
//=================================================================================================================================================//
void init()
{
  //-----------------------------------------------------------------
  //Expert name
  EA_Name=WindowExpertName();
  //-----------------------------------------------------------------
  //Set time start
  TimeToTrailing=Time[0];
  //-----------------------------------------------------------------
  //digits points
  PointsDec=MarketInfo(Symbol(), MODE_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,180);
  ObjectSet("Background2",OBJPROP_XDISTANCE,0);
  ObjectSetText("Background2","g",125,"Webdings",Black);
  }
  }
  //-----------------------------------------------------------------
  //Box trade session
  if((!IsTesting())&&(!IsOptimization())&&(TradeSessionBox==true)) for(int i=0; i59)) DelaySessionMinutes=0;
  if((Type_ATR_level<1)||(Type_ATR_level>2)) Type_ATR_level=1;
  if(TrailingAfter3)&&(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);
}
//=================================================================================================================================================//
void deinit()
{
  ObjectsDeleteAll();
  Comment("");//clear comments
  //return (0);
}
//=================================================================================================================================================//
void start()
{ 
  Trade=true;
  FilterATR=0;
  VolatilityEntry=0;
  CCI_Buy_Sig=0;
  CCI_Sell_Sig=0;  
  WPR_Buy_Sig=0;
  WPR_Sell_Sig=0;
  MA_Buy_Sig1=0;
  MA_Sell_Sig1=0;
  MA_Buy_Sig2=0;
  MA_Sell_Sig2=0;  
  MA_Slope_Buy_Sig=0;
  MA_Slope_Sell_Sig=0;  
  Fractals_Buy_Sig=0;
  Fractals_Sell_Sig=0;
  BB_Buy_Sig=0;
  BB_Sell_Sig=0;
  Trade_Pause_Buy_Sig=true;
  Trade_Pause_Sell_Sig=true; 
  MA_Dist_Buy_Sig=0;
  MA_Dist_Sell_Sig=0;   
  RSI_Buy_Sig=0;
  RSI_Sell_Sig=0;    
  CCI_Exit_Buy_Sig=0;
  CCI_Exit_Sell_Sig=0;  
  WPR_Exit_Buy_Sig=0;
  WPR_Exit_Sell_Sig=0;
  FirstSignalBuy=0;
  FirstSignalSell=0;
  BuyHFT=0;
  SellHFT=0;
  TrendlineBuy=0;
  TrendlineSell=0;
  Count=0;
  //-----------------------------------------------------------------
  //Count open orders
  CountCurrentOrders();
  //-----------------------------------------------------------------
  //Time Offset
  if((IsTesting())||(IsOptimization()))
  {
  if(GMT_Offset==99) {Alert("Please set GMT Offset in the EA inputs"); Sleep(60000); return;}
  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=TimeGMTOffset()/3600;
  //---
  StarHour=Open_Hour+GMT_Offset+DSTextern;
  EndHour=Close_Hour+GMT_Offset+DSTextern;
  StarHour_Friday=Open_Hour_Friday+GMT_Offset+DSTextern;
  EndHour_Friday=Close_Hour_Friday+GMT_Offset+DSTextern;
  if(StarHour>=24) StarHour=StarHour-24;
  if(EndHour>=24) EndHour=EndHour-24;
  if(StarHour_Friday>=24) StarHour_Friday=StarHour_Friday-24;
  if(EndHour_Friday>=24) EndHour_Friday=EndHour_Friday-24;
  if(StarHour<0) StarHour=24+StarHour;
  if(EndHour<0) EndHour=24+EndHour;
  if(StarHour_Friday<0) StarHour_Friday=24+StarHour_Friday;
  if(EndHour_Friday<0) EndHour_Friday=24+EndHour_Friday;
  //-----------------------------------------------------------------
  //For box session
  if(DayOfWeek()!=5)
  {
  TimeToStart=StarHour;
  TimeToEnd=EndHour;
  }
  if(DayOfWeek()==5)
  {
  TimeToStart=StarHour_Friday;
  TimeToEnd=EndHour_Friday;
  }
  //-----------------------------------------------------------------
  if(((IsTesting())||(IsOptimization()))&&((PrvHoursCheck1==PrvHoursCheck2)||(PrvHoursCheck1>PrvHoursCheck2))) return;//optimization confirm
  //-----------------------------------------------------------------
  //confirm use indicators
  if(!Use_CCI  && !Use_WPR && !Use_Fractals && !Use_HFT) {Print("You must use at least one signal (CCI, WPR, HFT, or Fractals"); return;} 
  //-----------------------------------------------------------------
  //trade time
  if((TimeDayOfWeek(TimeCurrent()+GMT_Offset*3600)==5)&&(TradeOnFriday==true)&&(TradeInSession==false))//friday own hours
  {
  if((StarHour_Friday < EndHour_Friday)&&((TimeHour(TimeCurrent())=EndHour_Friday))) Trade = FALSE;
  if((StarHour_Friday > EndHour_Friday)&&(TimeHour(TimeCurrent())= EndHour_Friday)) Trade = FALSE;
  if((TimeHour(TimeCurrent())==StarHour_Friday)&&(TimeMinute(TimeCurrent())0)) Trade = FALSE;
  }
  //---
  if((TimeDayOfWeek(TimeCurrent()+GMT_Offset*3600)!=5)||((TradeOnFriday==true)&&(TradeInSession==true)))//all days and friday in session
  {
  if((StarHour=EndHour))) Trade = FALSE;
  if((StarHour>EndHour)&&(TimeHour(TimeCurrent())=EndHour)) Trade = FALSE;
  if((TimeHour(TimeCurrent())==StarHour)&&(TimeMinute(TimeCurrent())0)) Trade = FALSE;
  }
  //---filter months days
  if(Month()==12 && Day()>22)  Trade = FALSE; 
  if(Month()==1 && Day()<5)  Trade = FALSE; 
  if((TradeOnFriday==false)&&(TimeDayOfWeek(TimeCurrent()+GMT_Offset*3600)==5)) Trade = FALSE;//stop trade in friday
  //-----------------------------------------------------------------
  //Time trade box
  if((!IsTesting())&&(!IsOptimization())&&(TradeSessionBox==true))
  {
  PlusDays=0;
  datetime dt=TimeCurrent();
  if(TimeToEnd5) dt=decDateTradeDay(dt);
  }
  }
  //-----------------------------------------------------------------
  //check spread
  if((!IsTesting())&&(!IsOptimization()))
  {
  if(Trade && TotalOpenOrders==0 && !Allow_Second_Trade && Spread()Max_Spread_Open*PointsDec|| (Ask-Bid)>Max_Spread_Open*PointsDec)) Session="Trade Session Open .. Spread is High .. Trading Halted";
  if(Trade && TotalOpenOrders>0 && (Spread()>Max_Spread_Close*PointsDec|| (Ask-Bid)>Max_Spread_Close*PointsDec)) Session="Trade Session Open..Waiting to exit trades..Trading Halted"; 
  if(Trade && TotalOpenOrders>0 && (Spread()0) Session="Trade Session Closed .. Waiting to exit trades";
  if(Use_NewsFilter==true)
  {
  if((IsNewsTime()==true)&&(Trade==true)) Session="News en cours";
  }
  if((TradeOnFriday==false)&&(TimeDayOfWeek(TimeCurrent()-GMT_Offset*3600)==5)) Session="Not trade in Friday";
  //+-----------------------------------------------------------------------------------------------------+
  //Manage los trade
  if(PauseOnLoss==true)
  {
  CountHistoryOrders();//count history orders
  if((LastClosedPips>=PauseMinPips)&&(TimeCurrent()-LastClosedTime<=PauseMaxMinutes*60))
  {
  Trade_Pause_Buy_Sig=false;//stop trade buy
  Trade_Pause_Sell_Sig=false;//stop trade sell
  /*switch(LastClosedType)
  {
  case OP_BUY: Trade_Pause_Buy_Sig=false; break;//stop trade buy
  case OP_SELL: Trade_Pause_Sell_Sig=false; break;//stop trade sell
  }*/
  }
  }//end if(PauseOnLoss)
  //+-----------------------------------------------------------------------------------------------------+
  //News filter
  if(Use_NewsFilter==true)
  {
  if((Trade==true)&&(IsNewsTime()==true)) Trade=false;
  }
  //-----------------------------------------------------------------
  Display(); //Add Display
  if(Show_Fractal_Levels) iCustom(NULL,0,"Fractal_Levels",MidFractalDist,OppositFractalDist,0,0);
  if(!Hide_SL_TP && TotalOpenOrders>0) ModifyAll(1); // Double check to make sure all trades have TP and SL 
  }//end if(!IsTesting() && !IsOptimization()) 
  //-----------------------------------------------------------------
  //Reset volume
  //if(TimeHour(TimeCurrent())!=StarHour)
  if(Trade==false)
  {
  CheckLevelPriceBuy=0;
  CheckLevelPriceSell=0;
  }
  //-----------------------------------------------------------------
  //filter atr
  if(Trade || OrdersTotal()>0)
  {
  if(UseATRfilter==false) FilterATR=1;
  //---
  if((UseATRfilter==true)&&(TotalOpenOrders==0)&&(New_Trade))
  {
  switch(Type_ATR_level)
  {
  case 1: if(iATR(NULL, 0, Atr_Period, 0) > AtrLevel) FilterATR=1; break;
  case 2: if(iATR(NULL, 0, Atr_Period, 0) < AtrLevel) FilterATR=1; break;
  }
  }
  if(FilterATR==0) Trade=false;
  //-----------------------------------------------------------------
  //Custom indicators
  if(Use_Volatility==false) VolatilityEntry=1;
  //---
  if(Use_Volatility)
  {
  VolBuf0=iCustom(NULL,0,"Voladility v3.2",Vis_atr,Vis_std,Sed_atr,Sed_std,Threshold_level,lag_supressor,0,0);//grey
  VolBuf2=iCustom(NULL,0,"Voladility v3.2",Vis_atr,Vis_std,Sed_atr,Sed_std,Threshold_level,lag_supressor,2,0);//green
  //---get cross time
  if(MinutesWaitToOpen>0)
  {
  for(int cnt=0; cntVolBuf2)&&(VolBuf0_iVolBuf2_i))
  {
  CrossDn=Time[cnt];
  CrossUp=0;
  break;
  }
  }
  }
  //---filter open
  if((VolBuf0>VolBuf2)&&(((Time[0]-CrossUp>=MinutesWaitToOpen*60)&&(CrossUp!=0))||(MinutesWaitToOpen==0))) VolatilityEntry=1;
  }
  //---signals close
  if((TotalOpenOrders>0)&&(CloseInReverseSignal==true))
  {
  //-----------------------------------------------------------------
  if((VolBuf0=MinutesWaitToClose*60)&&(CrossDn!=0))||(MinutesWaitToClose==0)))
  {
  for(int cnt3=OrdersTotal()-1; cnt3>=0; cnt3--)
  {
  if(OrderSelect(cnt3,SELECT_BY_POS, MODE_TRADES)==false) continue;
  if(OrderSelect(cnt3,SELECT_BY_POS,MODE_TRADES))
  {
  if((OrderSymbol()!=Symbol())||(OrderMagicNumber()!=Magic)) continue;
  if((OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol())&&(OrderType()<=OP_SELL))
  {
  if((OrderType()==OP_BUY)&&(OrderOpenPrice()-Bid>=PipsLossToClose*PointsDec)) CloseAll(0,OP_BUY);
  if((OrderType()==OP_SELL)&&(Ask-OrderOpenPrice()>=PipsLossToClose*PointsDec)) CloseAll(0,OP_SELL);
  }
  }
  }
  }
  //-----------------------------------------------------------------
  }
  if(VolatilityEntry==0) Trade=false;
  //-----------------------------------------------------------------
  //indicators
  if(Use_CCI) CCI_1 = iCCI(NULL, 0, CCI_Period, PRICE_CLOSE, 0);  
  if(Use_WPR) WPR_1 = iWPR(NULL, 0, WPR_Period, 0);
  //---
  if((Trade==true)&&(TotalOpenOrders==0)&&(New_Trade==true))
  {
  if(Use_MA1) MA_1_1  = iMA(NULL,0,MA_Period1,0,MA_Mode1,PRICE_CLOSE,1);
  if(Use_MA1) MA_1_2  = iMA(NULL,0,MA_Period1,0,MA_Mode1,PRICE_CLOSE,2);    
  if(Use_MA2 || Use_MA2_Slope) MA_2_1  = iMA(NULL,0,MA_Period2,0,MA_Mode2,PRICE_CLOSE,1);
  if(Use_MA2 || Use_MA2_Slope) MA_2_2  = iMA(NULL,0,MA_Period2,0,MA_Mode2,PRICE_CLOSE,2);
  if(Use_MA_Dist) MA_3_1  = iMA(NULL,0,MA_Dist_Period,0,MA_Dist_Mode,PRICE_CLOSE,1);
  if(Use_BB) BB_L_1  = iBands(NULL,0,BB_Period,BB_Dev,0,PRICE_CLOSE,MODE_LOWER,0);
  if(Use_BB) BB_L_2  = iBands(NULL,0,BB_Period,BB_Dev,0,PRICE_CLOSE,MODE_LOWER,1); 
  if(Use_BB) BB_L_3  = iBands(NULL,0,BB_Period,BB_Dev,0,PRICE_CLOSE,MODE_LOWER,2);    
  if(Use_BB) BB_U_1  = iBands(NULL,0,BB_Period,BB_Dev,0,PRICE_CLOSE,MODE_UPPER,0);    
  if(Use_BB) BB_U_2  = iBands(NULL,0,BB_Period,BB_Dev,0,PRICE_CLOSE,MODE_UPPER,1);
  if(Use_BB) BB_U_3  = iBands(NULL,0,BB_Period,BB_Dev,0,PRICE_CLOSE,MODE_UPPER,2);    
  if(Use_RSI) RSI_1  = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 0);
  //-----------------------------------------------------------------
  //HFT source code
  if(Use_HFT==false) {BuyHFT=1; SellHFT=1;}
  if(Use_HFT==true)
  {
  FractalsUp2 = iFractals(NULL, 0, MODE_UPPER, 2);
  FractalsUp3 = iFractals(NULL, 0, MODE_UPPER, 3);
  FractalsLw2 = iFractals(NULL, 0, MODE_LOWER, 2);
  FractalsLw3 = iFractals(NULL, 0, MODE_LOWER, 3);
  if((FractalsLw2 > 0.0 && High[0] > High[1]) || (FractalsLw3 > 0.0 && High[0] > High[1] && High[1] <= High[2])) BuyHFT=1;
  if((FractalsUp2 > 0.0 && Low[0] < Low[1]) || (FractalsUp3 > 0.0 && Low[0] < Low[1] && Low[1] >= Low[2])) SellHFT=1;
  }
  //-----------------------------------------------------------------
  //Instant_Trendline_Filter
  if(Use_Trendline==false) {TrendlineBuy=1; TrendlineSell=1;}
  if(Use_Trendline==true)
  {
  TrendUp=iCustom(NULL,0,"Instant_Trendline_Filter",alpha,1,0);//red
  TrendDn=iCustom(NULL,0,"Instant_Trendline_Filter",alpha,0,0);//green
  if(TrendUp>TrendDn) TrendlineBuy=1;
  if(TrendUpCCI_Entry) CCI_Sell_Sig=1;  
  if(Use_WPR && WPR_1<-WPR_Entry) WPR_Buy_Sig=1;  
  if(Use_WPR && WPR_1>-(100-WPR_Entry)) WPR_Sell_Sig=1;  
  if(Use_MA1 && MA_1_1>MA_1_2)  MA_Buy_Sig1=1;  
  if(Use_MA1 && MA_1_1MA_2_2)  MA_Buy_Sig2=1;  
  if(Use_MA2 && MA_2_1=MA_Slope*PointsDec)  MA_Slope_Buy_Sig=1;  
  if(Use_MA2_Slope && (MA_2_2-MA_2_1)>=MA_Slope*PointsDec)  MA_Slope_Sell_Sig=1;    
  if(Use_BB && !Use_BB_Direction && AskBB_Range*PointsDec && BB_L_1-Ask>=BB_Penetration*PointsDec) BB_Buy_Sig=1;
  if(Use_BB && !Use_BB_Direction && Bid>BB_U_1 && (BB_U_1-BB_L_1)>BB_Range*PointsDec && Bid-BB_U_1>=BB_Penetration*PointsDec) BB_Sell_Sig=1;  
  if(Use_BB && Use_BB_Direction && AskBB_Range*PointsDec && BB_L_1-Ask>=BB_Penetration*PointsDec && BB_L_1>BB_L_2) BB_Buy_Sig=1;
  if(Use_BB && Use_BB_Direction && Bid>BB_U_1 && (BB_U_1-BB_L_1)>BB_Range*PointsDec && Bid-BB_U_1>=BB_Penetration*PointsDec && BB_U_1=Dist_to_MA*PointsDec)  MA_Dist_Sell_Sig=1;   
  if(Use_RSI && RSI_1>=RSI_Entry)  RSI_Buy_Sig=1;  
  if(Use_RSI && RSI_1<=(100-RSI_Entry))  RSI_Sell_Sig=1;
  //---------------------------------------------------------
  //Extern first signals
  if((Spread()(midFractal+MidFractalDist*PointsDec) && Bid>(LastDownFractal+OppositFractalDist*PointsDec)) Fractals_Sell_Sig=1;
  }//end if((Use_Fractals)..
  //---------------------------------------------------------
  //price levels
  if(UsePriceLevelFilter==true)
  {
  if(TimeHour(TimeCurrent())==StarHour)
  {
  if(iClose(NULL,PERIOD_H1,PrvHoursCheck2)>iClose(NULL,PERIOD_H1,PrvHoursCheck1)) CheckLevelPriceBuy=1;
  if(iClose(NULL,PERIOD_H1,PrvHoursCheck2)0)
  {
  if(UseTrailingSL==true) ModifyAll(0);
  //-----------------------------------------------------------------
  //Dynamic Trailing
  if((UseDynamicSLTP==true)&&(Hide_SL_TP==false))
  {
  if((Time[0]-TimeToTrailing>=MinutesStep*60)&&(Time[0]-TimeToOpen>=MinutesStep*60))
  {
  ModifyAll(2);
  TimeToTrailing=Time[0];
  }
  }
  //-----------------------------------------------------------------
  //exit signals
  if(Use_CCI && CCI_1>CCI_Exit) CCI_Exit_Buy_Sig=1;
  if(Use_CCI && CCI_1<-CCI_Exit) CCI_Exit_Sell_Sig=1;  
  if(Use_WPR && WPR_1>-WPR_Exit) WPR_Exit_Buy_Sig=1;  
  if(Use_WPR && WPR_1<(-100+WPR_Exit)) WPR_Exit_Sell_Sig=1; 
  //-----------------------------------------------------------------
  //close trades in time over
  if((UseTimeOver==true)&&(Time[0]-FirstOrderTimeOpen>=MaxMinutesOpen*60))
  {
  CloseAll(0,OP_BUY);
  CloseAll(0,OP_SELL);
  }
  //-----------------------------------------------------------------
  //close trades in exit signal
  if((CCI_Exit_Buy_Sig==1 || WPR_Exit_Buy_Sig==1) && Spread()0)
  //-----------------------------------------------------------------
  //open trades
  //---First buy trade
  if(TotalOpenOrders==0)
  {
  //---
  if((FirstSignalBuy==1)&&(CheckLevelPriceBuy==1)&&(Fractals_Buy_Sig==1)) 
  {
  Lot=CalculateLots(Risk);
  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) ModifyAll(1);
  if(SendMailInfo==true)  SendMail("Open Buy","No Of Ticket: "+Ticket_1+"  Order Open Price: "+Ask);
  }
  //---
  }
  //---First sell trade   
  if((FirstSignalSell==1)&&(CheckLevelPriceSell==1)&&(Fractals_Sell_Sig==1)) 
  {
  Lot=CalculateLots(Risk);
  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) ModifyAll(1);
  if(SendMailInfo==true)  SendMail("Open Sell","No Of Ticket: "+Ticket_1+"  Order Open Price: "+Bid);
  }
  //---
  }
  }  //end if(TotalOpenOrders==0)
  //---Second buy trade
  if(((TotalOpenOrders==1)&&(Allow_Second_Trade==true))&&((SecondInOpenSession==false)||(Trade==true)))
  {
  if(Time[0]!=Last_Time && Trade_Pause_Buy_Sig==true && LastOpenType==0 && Ask<=(LastBuyPrice-Distance*PointsDec))
  {
  Lot=CalculateLots(Risk*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) ModifyAll(1);
  if(SendMailInfo==true)  SendMail("Open Buy","No Of Ticket: "+Ticket_1+"  Order Open Price: "+Ask);
  }
  //---
  }
  //---Second sell trade   
  if(Time[0]!=Last_Time && Trade_Pause_Sell_Sig==true && LastOpenType==1 && Bid>=(LastSellPrice+Distance*PointsDec))
  {
  Lot=CalculateLots(Risk*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) ModifyAll(1);
  if(SendMailInfo==true)  SendMail("Open Sell","No Of Ticket: "+Ticket_1+"  Order Open Price: "+Bid);
  }
  //---
  }
  }//end (TotalOpenOrders==1)
  //---
  }//end (Trade || OrdersTotal()>0)  
  //return (0);
}
//=================================================================================================================================================//
void CountCurrentOrders()
{
  TotalOpenOrders=0;
  LastBuyPrice=0;
  LastSellPrice=0;
  LastOpenType=-1;
  SumProfit=0;
  NetProfit=0;
  TimeToOpen=0;
  FirstOrderTimeOpen=Time[0];
  //-----------------------------------------------------------------
  for(int cnt=0; cnt=TakeProfit*PointsDec)||(OrderOpenPrice()-Ask>=StopLoss*PointsDec))) CloseAll(OrderTicket(),OrderType());
  }
  if(OrderType()==OP_SELL)
  {
  TimeToOpen=OrderOpenTime();
  LastSellPrice=OrderOpenPrice();
  LastOpenType=OP_SELL;
  FirstOrderTimeOpen=MathMin(FirstOrderTimeOpen,TimeToOpen);
  if((Hide_SL_TP==true)&&((OrderOpenPrice()-Bid>=TakeProfit*PointsDec)||(Bid-OrderOpenPrice()>=StopLoss*PointsDec))) CloseAll(OrderTicket(),OrderType());
  }
  TotalOpenOrders++;
  SumProfit+=OrderProfit()+OrderCommission()+OrderSwap();
  NetProfit+=OrderProfit();
  }
  }
  }
  //-----------------------------------------------------------------
  //return(0);
}
//=================================================================================================================================================//
void CountHistoryOrders()
{
  HistoryProfits=0;
  LastClosedProfit=0;
  LastClosedPrice=0;
  LastClosedType=-1;
  LastClosedTime=0;
  LastClosedPips=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,Blue);
  if(WasOrderClosed>0) if(SendMailInfo==true)  SendMail("Close Buy","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,Red);
  if(WasOrderClosed>0) if(SendMailInfo==true)  SendMail("Close Sell","No Of Ticket: "+OrderTicket()+"  Order Close Price: "+Ask+"  Profit Order: "+OrderProfit());
  }
  //---
  } 
  }
  }
  //return(0); 
}
//=================================================================================================================================================//
double CalculateLots(double r) 
{
  double LotSize;
  //---
  if(lot>0.0) LotSize = lot;//manual
  else
  {
  if(CountRiskBaseSL==true) LotSize=(AccountEquity()*(r/100))/(StopLoss*((TickValue*PointsDec)/TickSize));//auto
  if(CountRiskBaseSL==false) LotSize=AccountBalance()/100000.0*r;//auto
  }
  //---
  return(MathMin(MathMax(MinLot,MathRound(LotSize/LotStep)*LotStep),MaxLot));
}
//=================================================================================================================================================//
void ModifyAll(int TypeModify) 
{ 
  double DynamicSL=0;
  double DynamicTP=0;
  //---
  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 ) 
  {
  //--put stop loss and take profit
  if((OrderStopLoss()==0 || OrderTakeProfit()==0)&&(TypeModify==1))
  {
  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);
  }
  //---trailing stop loss
  if(TypeModify==0)
  {
  //if((OrderType()==OP_BUY)&&(Bid-OrderStopLoss()>TrailingPips*PointsDec)&&(Bid-OrderOpenPrice()>=TrailingAfter*PointsDec))
  if((OrderType()==OP_BUY)&&(Bid-OrderStopLoss()>TrailingPips*PointsDec)&&(Bid-OrderOpenPrice()>=TrailingAfter*PointsDec))
  OrderModify(OrderTicket(),OrderOpenPrice(),ND(OrderOpenPrice()+((Bid-OrderOpenPrice())-TrailingPips*PointsDec)), OrderTakeProfit(),0,Blue); 
  //if((OrderType()==OP_SELL)&&(OrderStopLoss()-Ask>TrailingPips*PointsDec)&&(OrderOpenPrice()-Ask>=TrailingAfter*PointsDec))
  if((OrderType()==OP_SELL)&&(OrderStopLoss()-Ask>TrailingPips*PointsDec)&&(OrderOpenPrice()-Ask>=TrailingAfter*PointsDec))
  OrderModify(OrderTicket(),OrderOpenPrice(),ND(OrderOpenPrice()-((OrderOpenPrice()-Ask)-TrailingPips*PointsDec)), OrderTakeProfit(),0,Red);
  }
  //---dynamic trailing
  if(TypeModify==2)
  {
  if(OrderType()==OP_BUY)
  {
  //---sl
  if(OrderStopLoss()+PipsStep*PointsDec<=Bid-MathMax(StopLevel,MinimumPips)*PointsDec) DynamicSL=OrderStopLoss()+PipsStep*PointsDec;
  //else
  //if(Bid-StopLevel*PointsDec>OrderStopLoss()) DynamicSL=Bid-StopLevel*PointsDec;
  else
  DynamicSL=OrderStopLoss();
  //---tp
  if(OrderTakeProfit()-PipsStep*PointsDec>=Ask+MathMax(StopLevel,MinimumPips)*PointsDec) DynamicTP=OrderTakeProfit()-PipsStep*PointsDec;
  //else
  //if(Ask+StopLevel*PointsDec=Ask+MathMax(StopLevel,MinimumPips)*PointsDec) DynamicSL=OrderStopLoss()-PipsStep*PointsDec;
  //else
  //if(Ask+StopLevel*PointsDecOrderTakeProfit()) DynamicTP=Bid-StopLevel*PointsDec;
  else
  DynamicTP=OrderTakeProfit();
  //--break
  if((DynamicSL==OrderStopLoss())&&(DynamicTP==OrderTakeProfit())) break;
  //--modify
  OrderModify(OrderTicket(),OrderOpenPrice(),DynamicSL, DynamicTP,0,Red);
  }
  }
  //---
  } 
  }
  }
  //return(0);
}
//=================================================================================================================================================//
double Spread()
{
  double AvrgSpread=0;
  LoopCount=LastTicksCount-1;
  if(TickCountMeter9) DelayCom=DelaySessionMinutes;
  //---------------------------------------------------------
  if((TradeOnFriday==true)&&(TimeDayOfWeek(TimeCurrent()-GMT_Offset*3600)==5))
  {
  ShowStart=StarHour_Friday;
  ShowEnd=EndHour_Friday;
  }
  else
  {
  ShowStart=StarHour;
  ShowEnd=EndHour;
  }
  //---------------------------------------------------------
  Comment( 
          "\n*=====================*",   
          "\n    "+EA_Name,
          "\n*=====================*",
          "\n   "+Session,
          "\n*=====================*",
          "\n    Broker Time          = ",TimeToStr(TimeCurrent(), TIME_MINUTES),
          "\n    Start Hour             = "+DoubleToStr(ShowStart,0)+":"+DelayCom,          
          "\n    End Hour              = "+DoubleToStr(ShowEnd,0)+":00", 
          "\n*=====================*",                                                            
          "\n    Magic Number       = "+Magic,
          "\n    Max Spread Open  = "+DoubleToStr(Max_Spread_Open,1),
          "\n    Max Spread Close  = "+DoubleToStr(Max_Spread_Close,1),
          "\n    Average Spread    = "+DoubleToStr(Spread(),1),
          "\n    Lot size                 = "+DoubleToStr(CalculateLots(Risk),2),
          "\n    Stop Loss              = "+DoubleToStr(StopLoss,0), 
          "\n*=====================*",                                                            
          "\n    TotalProfit (Loss)    = "+DoubleToStr(SumProfit,2),
          "\n    NetProfit (Loss)      = "+DoubleToStr(NetProfit,2), 
          "\n*=====================*",                                                            
          "\n    History Profit (Loss) = "+DoubleToStr(HistoryProfits+SumProfit,2), 
          "\n*=====================*",
          "\n    B A L A N C E       = "+ DoubleToStr(AccountBalance(),2),
          "\n    E Q U I T Y          = "+ DoubleToStr(AccountEquity(),2),
          "\n    L E V E R A G E    = "+ DoubleToStr(AccountLeverage(),2),            
          "\n*=====================*"
         );
  //return(0);       
} 
//=================================================================================================================================================//
void DST()
{
  int GetMonth=Month();
  int GetDayOfMonth=Day();
  int GetDayOfWeek=DayOfWeek();
  if(GetDayOfWeek==0) GetDayOfWeek=7;
  //---
  if((GetMonth>3)&&(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);
} 
//=================================================================================================================================================//
bool IsNewsTime()
{
  static bool rval = false;
  static int lm = -1;
  bool is_news = false;
  // uncomment next line if not global variable
  int minutesSincePrevEvent = 10080, minutesUntilNextEvent = 10080; // 1 week
  if (lm != Minute()) { // update news status, run every minute
  lm = Minute();
  if(Use_NewsFilter==true)
  {
  // check high impact
  if(IncludeHigh) {
  minutesSincePrevEvent = iCustom(NULL,0,"FFCal",IncludeHigh,false,false,false,true,GMT_Offset,true,-1,-1,ReportAllForUSD,1,0);
  minutesUntilNextEvent = iCustom(NULL,0,"FFCal",IncludeHigh,false,false,false,true,GMT_Offset,true,-1,-1,ReportAllForUSD,1,1);
  if(minutesUntilNextEvent<=MinsBeforeHigh || minutesSincePrevEvent<=MinsAfterHigh)
  is_news = true;
  }
  // check medium impact
  if(IncludeMedium && !is_news) {
  minutesSincePrevEvent = iCustom(NULL,0,"FFCal",false,IncludeMedium,false,false,true,GMT_Offset,true,-1,-1,ReportAllForUSD,1,0);
  minutesUntilNextEvent = iCustom(NULL,0,"FFCal",false,IncludeMedium,false,false,true,GMT_Offset,true,-1,-1,ReportAllForUSD,1,1);
  if(minutesUntilNextEvent<=MinsBeforeMedium || minutesSincePrevEvent<=MinsAfterMedium)
  is_news = true;
  }
  // check low impact
  if(IncludeLow && !is_news) {
  minutesSincePrevEvent = iCustom(NULL,0,"FFCal",false,false,IncludeLow,false,true,GMT_Offset,true,-1,-1,ReportAllForUSD,1,0);
  minutesUntilNextEvent = iCustom(NULL,0,"FFCal",false,false,IncludeLow,false,true,GMT_Offset,true,-1,-1,ReportAllForUSD,1,1);
  if(minutesUntilNextEvent<=MinsBeforeLow || minutesSincePrevEvent<=MinsAfterLow)
  is_news = true;
  }
  // check speaks
  if(IncludeSpeaks && !is_news) {
  minutesSincePrevEvent = iCustom(NULL,0,"FFCal",false,false,false,IncludeSpeaks,true,GMT_Offset,true,-1,-1,ReportAllForUSD,1,0);
  minutesUntilNextEvent = iCustom(NULL,0,"FFCal",false,false,false,IncludeSpeaks,true,GMT_Offset,true,-1,-1,ReportAllForUSD,1,1);
  if(minutesUntilNextEvent<=MinsBeforeSpeaks || minutesSincePrevEvent<=MinsAfterSpeaks)
  is_news = true;
  }
  }
  if(is_news)
  rval = true;
  else
  rval = false;
  }
  return(rval);
}
//=================================================================================================================================================//
void DrawObjects(datetime TimeCurr, string no, string TimeStart, string TimeEnd, int NextDay)
{
  datetime t1,t2;
  double p1,p2;
  int b1,b2;
  t1=StrToTime(TimeToStr(TimeCurr,TIME_DATE)+" "+TimeStart);
  t2=StrToTime(TimeToStr(TimeCurr+NextDay,TIME_DATE)+" "+TimeEnd);
  b1=iBarShift(NULL,0,t1);
  b2=iBarShift(NULL,0,t2);
  p1=High[Highest(NULL,0,MODE_HIGH,b1-b2,b2)];
  p2=Low [Lowest(NULL,0,MODE_LOW,b1-b2,b2)];
  ObjectSet(no,OBJPROP_TIME1,t1);
  ObjectSet(no,OBJPROP_PRICE1,p1);
  ObjectSet(no,OBJPROP_TIME2,t2);
  ObjectSet(no,OBJPROP_PRICE2,p2);
}
//=================================================================================================================================================//
void CreateObjects(string no, color cl)
{
  ObjectCreate(no,OBJ_RECTANGLE,0,0,0,0,0);
  ObjectSet(no,OBJPROP_STYLE,STYLE_SOLID);
  ObjectSet(no,OBJPROP_COLOR,cl);
  ObjectSet(no,OBJPROP_BACK,True);
}
//=================================================================================================================================================//
datetime decDateTradeDay(datetime TimeCurr)
{
  int ty=TimeYear(TimeCurr);
  int tm=TimeMonth(TimeCurr);
  int td=TimeDay(TimeCurr);
  int th=TimeHour(TimeCurr);
  int ti=TimeMinute(TimeCurr);
  //---
  td--;
  if(td==0)
  {
  tm--;
  if(tm==0)
  {
  ty--;
  tm=12;
  }
  if(tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) td=31;
  if(tm==2) if (MathMod(ty, 4)==0) td=29; else td=28;
  if(tm==4 || tm==6 || tm==9 || tm==11) td=30;
  }
  return(StrToTime(ty+"."+tm+"."+td+" "+th+":"+ti));
}
//=================================================================================================================================================//

Recommend