one to one coaching

How to Add A Timefilter

Here is a code for the news filter.

EASY METHOD:

Here is an easier way to do

After the extern parameters, add :

Code:

extern bool AvoidNews=true;
extern int MinimumImpact=1;
extern int MinsBeforeNews=30;
extern int MinsAfterNews=30;

At the beginning of the start body, add :

Code:

bool ContinueTrading=true;
if(AvoidNews)
{
static int PrevMinute=-1;

int MinSinceNews=iCustom(NULL,0,"FFCal",true,true,false,true,true,1,0);
int MinToNews=iCustom(NULL,0,"FFCal",true,true,false,true,true,1,1);

int ImpactSinceNews=iCustom(NULL,0,"FFCal",true,true,false,true,true,2,0);
int ImpactToNews=iCustom(NULL,0,"FFCal",true,true,false,true,true,2,1);

if(Minute()!=PrevMinute)
{
PrevMinute=Minute();
if((MinToNews<=MinsBeforeNews && ImpactToNews>=MinimumImpact) || (MinSinceNews<=MinsAfterNews && ImpactSinceNews>=MinimumImpact))ContinueTrading=false;
}
}

In each entry conditions, add :

Code:

&& ContinueTrading

Finally copy the two attached files in the indicators folder.


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

METHOD 2 BELOW

Extern parameters :

Code:

extern string S8="---------------- News Filter";

extern bool UseNewsFilter=false;//|---------------use news filter
extern int StartNewsFilter=0;//|------------------start news filter
extern int EndNewsFilter=24;//|-------------------end news filter
extern int MinsUntilNews=30;//|-------------------minutes until news
extern int MinsAfterNews=30;//|-------------------minutes after news
extern string News1="3=High; 2=Medium; 1=Low";//|-news impact information
extern int NewsImpact=3;//|-----------------------news impact
extern bool CloseProfitableTrades=true;//|--------close profitable trades before the news
extern bool CloseAllTrades=true;//|---------------close all the trades before the news
extern bool ShowAlerts=true;//|-------------------show alerts

Conditions :

Code:

&&(Hour()>StartNewsFilter&&Hour()<EndNewsFilter&&UseNewsFilter==false||(UseNewsFilter&&NewsFilterCheck()))

Functions :

Code:

//|---------close profitable trades before news

void CloseProfitableTradesBeforeNews()
{
for(int cc=0;cc<OrdersTotal();cc++)
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderProfit()>=0)bool ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,CLR_NONE);
return;
}
}
}

//|---------close existing trades

bool CloseExistingTrades(int type)
{
if(OrdersTotal()==0)return(true);
for(int cc=0;cc<OrdersTotal();cc++)
{
OrderSelect(cc,SELECT_BY_POS);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==type)
{
bool result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,CLR_NONE);
if(!result)
{
int err=GetLastError();
if(ShowAlerts)Alert(OrderTicket()," ",Symbol()," OrderClose failed with error(",err,"). ");
Print(OrderTicket()," ",Symbol()," OrderClose failed with error(",err,"). ");
return(false);
}
else
{
if(ShowAlerts)Alert(OrderTicket()," ",Symbol()," OrderClose");
Print(OrderTicket()," ",Symbol(), "OrderClose");
return(true);
}
}
}
return(true);
}

//|---------news impact

string Num2Impact(int impact)
{
if(impact==3)return("HIGH IMPACT");
if(impact==2)return("MED IMPACT");
if(impact==1)return("LOW IMPACT");
else return("");
}

//|---------news filter check

bool NewsFilterCheck()
{
if(!IsTesting())
{
int minutesUntilNextEvent=iCustom(NULL,0,"FFCal",true,true,false,true,true,1,1);
int minutesSincePrecEvent=iCustom(NULL,0,"FFCal",true,true,false,true,true,1,0);
int impactOfPrecEvent=iCustom(NULL,0,"FFCal",true,true,false,true,true,2,0);
datetime dTime=minutesUntilNextEvent*60;
string sTime=TimeToStr(dTime,TIME_MINUTES);
string sText;
bool TradeAllowed=true;

if(minutesUntilNextEvent>MinsUntilNews&&(impactOfPrecEvent<NewsImpact||(impactOfPrecEvent>=NewsImpact&&minutesSincePrecEvent>MinsAfterNews)))
{
sText=StringConcatenate("No News within the next ",sTime);
}
else
{
int impactOfNextEvent=iCustom(NULL,0,"FFCal",true,true,false,true,true,2,1);
string sImpact=Num2Impact(impactOfNextEvent);
if(StringLen(sImpact)>0)sImpact="["+sImpact+"] ";
sText=sImpact+"News in "+sTime;
if(impactOfNextEvent>=NewsImpact)
{
TradeAllowed=false;
if(CloseProfitableTrades && OrdersTotal()>0)CloseProfitableTradesBeforeNews();
if(CloseAllTrades && OrdersTotal()>0){CloseExistingTrades(OP_BUY);CloseExistingTrades(OP_SELL);}
}
}
return(TradeAllowed);
}
}

Put FFCal.mq4 in the indicators folder.
Put WebGet.dll in the libraries folder.

If you have a better code, don't hesitate to post.
Attached Files
File Type: zip FFCal.zip (56.5 KB, 53 views)

EASIER METHOD:
 
one to one coaching
Top