extern bool FilterByMagicNumber = FALSE;
extern bool CurrentPairOnly = FALSE;
extern int MagicNumber = 0;
extern string tbslh="----Time based StopLoss----";
bool UseTimeBasedStopLoss = TRUE;
extern string tsmh="1:days, 2:hours, 3:minuts, 4:bars";
extern int TimeBasedSLmode = 4;
extern double TimeBasedSLperiodProfit = 5;
extern double TimeBasedSLperiodLoss = 5;
extern string bth="true:Sends 2 orders everytime OrdersTotal is = 0";
extern bool Send2OrdersFrequently = FALSE;
string shmode;
int totalopenorders;//simple stats
int init() {
//simple userinfo
shmode="ERROR - check your settings!";
if (TimeBasedSLmode==1) shmode="days";
if (TimeBasedSLmode==2) shmode="hours";
if (TimeBasedSLmode==3) shmode="minutes";
if (TimeBasedSLmode==4) shmode="bars";
return(0);
}
int deinit() {
Comment("");
return(0);
}
int start() {
Comment("TBasedSL"+"\n"+
" current day is "+DayToStr(GetDayOfWeek(0))+", SETTINGS: Send2OrdersFrequently="+bool2txt(Send2OrdersFrequently)+"\n"+
" FilterByMagicNumber="+bool2txt(FilterByMagicNumber)+" (MagicNumber="+MagicNumber+"), CurrentPairOnly="+bool2txt(CurrentPairOnly)+" (pair: "+Symbol()+"), total open trades to manage: "+totalopenorders+"\n"+
" close trades in profit after "+DoubleToStr(TimeBasedSLperiodProfit,2)+" "+shmode+", close trades in loss after "+DoubleToStr(TimeBasedSLperiodLoss,2)+" "+shmode);
if (Send2OrdersFrequently && OrdersTotal()==0) {
OrderSend(Symbol(),OP_BUY,MarketInfo(Symbol(),MODE_MINLOT),MarketInfo(Symbol(),MODE_ASK),5000,0,0,"TESTORDER BUY", MagicNumber,0,Blue);
OrderSend(Symbol(),OP_SELL,MarketInfo(Symbol(),MODE_MINLOT),MarketInfo(Symbol(),MODE_BID),0500,0,0,"TESTORDER SELL", MagicNumber,0,Red);
//OrderSend(Symbol(),OP_BUYSTOP,MarketInfo(Symbol(),MODE_MINLOT),MarketInfo(Symbol(),MODE_ASK)+350*Point,5000,0,0,"TESTORDER BUYSTOP", MagicNumber,0,Blue);
//OrderSend(Symbol(),OP_SELLSTOP,MarketInfo(Symbol(),MODE_MINLOT),MarketInfo(Symbol(),MODE_BID)-350*Point,5000,0,0,"TESTORDER SELLSTOP", MagicNumber,0,Red);
}//if (IsTesting() && SendOrdersAtStartAtBacktesting) {
if (UseTimeBasedStopLoss) TimeBasedSL(Symbol(),MagicNumber,TimeBasedSLperiodProfit,TimeBasedSLperiodLoss,TimeBasedSLmode,-1);
/* if you would like to delete pending orders only, the call should be (e.g. for BUYSTOP) */
/*
if (UseTimeBasedStopLoss) TimeBasedSL(Symbol(),MagicNumber,TimeBasedSLperiodProfit,TimeBasedSLperiodLoss,TimeBasedSLmode,OP_BUYSTOP);
*/
}
//mode - 1:days, 2:hours, 3:minuts, 4:bars
//type - if not -1 only the specified ordertypes will be closed, for example if type=OP_BUY it will only close buy-orders, or if type=OP_BUYSTOP it will delete pending buystop orders (nice if you broker does not support pending order expiration times)
void TimeBasedSL(string symbol,int magicnumber,double TimeBasedSLperiodProfit,double TimeBasedSLperiodLoss,int mode=4,int type=-1) {
bool result;
if (TimeBasedSLmode>4 || TimeBasedSLmode<=0) { Print("TimeBasedSL(): *** ERROR - mode must be 1,2,3 or 4 ..."); return; }//simple errorchecking
double TimeBasedSL=0.0;
string smode="unknown";
if (mode==1) smode="days";
if (mode==2) smode="hours";
if (mode==3) smode="minutes";
if (mode==4) smode="bars";
string sTimeBasedSLperiod;
totalopenorders=0;
for (int cnt=OrdersTotal()-1; cnt>=0; cnt--) {
if (!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) continue;
if (CurrentPairOnly && OrderSymbol()!=symbol) continue;
if (FilterByMagicNumber && OrderMagicNumber()!=magicnumber) continue;
if (type!=-1 && OrderType()!=type) continue;
{
totalopenorders++;
double profit = (OrderProfit()+OrderCommission()+OrderSwap());
//Order is in profit
if (profit>0.0) {
if (mode==1) TimeBasedSL = TimeBasedSLperiodProfit * 86400;//days
if (mode==2) TimeBasedSL = TimeBasedSLperiodProfit * 3600;//hours
if (mode==3) TimeBasedSL = TimeBasedSLperiodProfit * 60;//minutes
if (mode==4) TimeBasedSL = TimeBasedSLperiodProfit * Period() * 60;//bars
sTimeBasedSLperiod = "closed (timebased SL="+DoubleToStr(TimeBasedSLperiodProfit,2)+" "+smode+") at PROFIT";
}
//Order is in loss
if (profit<0.0) {
if (mode==1) TimeBasedSL = TimeBasedSLperiodLoss * 86400;//days
if (mode==2) TimeBasedSL = TimeBasedSLperiodLoss * 3600;//hours
if (mode==3) TimeBasedSL = TimeBasedSLperiodLoss * 60;//minutes
if (mode==4) TimeBasedSL = TimeBasedSLperiodLoss * Period() * 60;//bars
sTimeBasedSLperiod = "closed (timebased SL="+DoubleToStr(TimeBasedSLperiodLoss,2)+" "+smode+") at LOSS";
}
if ((TimeCurrent() - OrderOpenTime()) >= TimeBasedSL) {
while(IsTradeContextBusy()) Sleep(100);
RefreshRates();
if (OrderType()==OP_BUY) {//buy
result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),9999,CLR_NONE);
if (result) Print("TimeBasedSL(): **** BUY order ticket "+OrderTicket()+" (opentime: "+TimeToStr(OrderOpenTime())+") "+sTimeBasedSLperiod+" of "+DoubleToStr(profit,2)+" "+AccountCurrency());
}
if (OrderType()==OP_SELL) {//sell
result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),9999,CLR_NONE);
if (result) Print("TimeBasedSL(): **** SELL order ticket "+OrderTicket()+" (opentime: "+TimeToStr(OrderOpenTime())+") "+sTimeBasedSLperiod+" of "+DoubleToStr(profit,2)+" "+AccountCurrency());
}
if (OrderType()>1 && OrderType()<6) {//pending orders
result=OrderDelete(OrderTicket());
if (result) Print("TimeBasedSL(): **** Pending Order ticket "+OrderTicket()+" (opentime: "+TimeToStr(OrderOpenTime())+") deleted (timebased SL="+sTimeBasedSLperiod+" "+smode+")");
}
}//if (TimeCurrent() - ...
}
}//for (int cnt=OrdersTotal()-1; cnt>=0; cnt--) {
}
int GetDayOfWeek(int bar) {
return(TimeDayOfWeek(iTime(NULL,0,bar)));
}
string DayToStr(int day) {
switch (day) {
case 0: return("sunday");
case 1: return("monday");
case 2: return("tuesday");
case 3: return("wednesday");
case 4: return("thursday");
case 5: return("friday");
case 6: return("saturday");
default: return("unknown");
}
}
string bool2txt(int varbool) {
if (varbool==true) return("yes");
if (varbool==false) return("no");
}