extern bool TradeOffFriday = FALSE; if (TradeOffFriday == TRUE){ if (Day()!=5 ) Comment("No Trade For Friday!"); }
do u think my code works?
please give me your comment & suggestions....thank you
regards,
MANSTIR
badguy wrote >>
This code is form EA4.mq4 I hope its a help, the DayOfWeek was add by another.
if(DayOfWeek()==CloseDay && OrdersTotal()>0){
// Call your close routine or this CloseAllOrder().
}
if(Hour()==EndHour && Minute()==1 && OrdersTotal()>0){
CloseAllOrder();
}
void CloseAllOrder()
{
int total = OrdersTotal();
for (int cnt = OrdersTotal()-1 ; cnt < total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == MagicNumber) {
if(OrderType()==OP_BUY){
OrderClose(OrderTicket(),OrderLots(),Bid,2,Violet);}
if(OrderType()==OP_SELL) {
OrderClose(OrderTicket(),OrderLots(),Ask,2,Violet);}
if(OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) {
OrderDelete(OrderTicket());}
}
}
}
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello Coders
please somebody can help me.
I try to create an EA with the Options to Close all open trades at specified time and / or day.
The function for open trades at specifed time and / or day seems it's working.
But the closing function by time, i don't found the solutions.
I have try to code and integrate several scripts > no success.
I marked the relevant parts of my script ( blue and bold )
//+------------------------------------------------------------------+
//| #1EA_Test_Template v1.mq4 |
//| by badguy 09.07.2008 |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
extern string MAGIC_NUMBER = "Magic Number";
extern int MAGICMA = 13042008;
extern int MAGICMA_Trailing = 14042008;
extern string P11 = "MACD1 Settings";
extern double MACD_TF1 = 60;
extern double S_MA1 = 2;
extern double F_MA1 = 5;
extern double Sig_MA1 = 3;
extern double BarsBack10 = 0;
extern double BarsBack11 = 1;
extern double Trig_Buy_1 = 0.0007;
extern double Trig_Sell_1 = -0.0007;
extern string TradingHours = "TRADING HOURS";
extern bool UseHourTrade = false;
extern int FromHourTrade = 2;
extern int ToHourTrade = 7;
extern string TradingDays = "TRADING Days";
extern bool UseDayTrade = false;
extern int FromDayTrade = 1; // Tuesday
extern int ToDayTrade = 5; // Friday
extern string ClosingHours = "CLOSING HOURS";
extern bool UseHourClose = false;
extern int CloseHour = 23;
extern int CloseMinute = 55;
extern string ClosingDays = "CLOSING Days";
extern bool UseDayClose = false;
extern int CloseDay = 5; // Friday
extern string P0 = "LotSize Trailing Position";
double Order_Anzahl_T = 1;
extern double Order_Lotsize_T = 0.5;
extern int StopLoss_T = 100;
extern int TakeProfit_T = 100;
extern int TrailingStop = 50;
extern string P1 = "LotSize Order_1";
double Order_Anzahl_1 = 1;
extern double Order_Lotsize_1 = 0.5;
extern int StopLoss_1 = 100;
extern int TakeProfit_1 = 30;
int slippage=3;
double D_open;
int BarShift=1;
//+-----------------------------------------------------------------------+
//| Calculate open positions |
//+-----------------------------------------------------------------------+
int CalculateNumberOfOrders(string symbol)
{
int long=0,short=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if
(
OrderSymbol()==Symbol()
&&
OrderMagicNumber()==MAGICMA
)
{
if(OrderType()==OP_BUY)
{
long++;
}
if(OrderType()==OP_SELL)
{
short++;
}
}
}
if(long>0) return(long);
else return(short);
}
//+-----------------------------------------------------------------------+
//| Calculate open positions |
//+-----------------------------------------------------------------------+
int CalculateNumberOfOrdersTrailing(string symbol)
{
int long=0,short=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if
(
OrderSymbol()==Symbol()
&&
OrderMagicNumber()==MAGICMA_Trailing
)
{
if(OrderType()==OP_BUY)
{
long++;
}
if(OrderType()==OP_SELL)
{
short++;
}
}
}
if(long>0) return(long);
else return(short);
}
//+-----------------------------------------------------------------------+
//| Check for open order conditions |
//+-----------------------------------------------------------------------+
int GetSignal()
{
double diMACD10 =iMACD(NULL,MACD_TF1,S_MA1,F_MA1,Sig_MA1,PRICE_CLOSE,MODE_SIGNAL,BarsBack10);
double diMACD11 =iMACD(NULL,MACD_TF1,S_MA1,F_MA1,Sig_MA1,PRICE_CLOSE,MODE_SIGNAL,BarsBack11);
if
(
//Daily Buy
(diMACD10 > Trig_Buy_1)
)
return(1);
if
(
// Daily Sell
(diMACD10 < Trig_Sell_1)
)
return(2);
if
(!(Hour()>=CloseHour && Minute()>=CloseMinute && DayOfWeek()>=CloseDay)
)
return(3);
return(0);
}
//+-----------------------------------------------------------------------+
//| Check for open order conditions |
//+-----------------------------------------------------------------------+
void CheckForOpenConditions()
{
int ticket,i;
if(GetSignal()==2)
{
if(Order_Anzahl_1>0)
{
for( i=1;i<=Order_Anzahl_1;i++)
{
ticket=OrderSend(Symbol(),OP_BUY,Order_Lotsize_1,Ask,3,
Bid-StopLoss_1*Point,
Ask+TakeProfit_1*Point,
"Buy 1 is active and LONG",MAGICMA,0,Green);
}
}
if(Order_Lotsize_T>0)
{
ticket=OrderSend(Symbol(),OP_BUY,Order_Lotsize_T,Ask,3,
Bid-StopLoss_T*Point,
Ask+TakeProfit_T*Point,
"Buy Trail is active and LONG",MAGICMA_Trailing,0,Green);
}
return(0);
}
if(GetSignal()==1)
{
if(Order_Anzahl_T>0)
{
for( i=1;i<=Order_Anzahl_T;i++)
{
ticket=OrderSend(Symbol(),OP_SELL,Order_Lotsize_T,Bid,3,
Ask+StopLoss_T*Point,
Bid-TakeProfit_T*Point,
"Sell 1 is active and SHORT",MAGICMA,0,Red);
}
}
if(Order_Lotsize_T>0)
{
ticket=OrderSend(Symbol(),OP_SELL,Order_Lotsize_T,Bid,3,
Ask+StopLoss_T*Point,
Bid-TakeProfit_T*Point,
"Sell Trail is active and SHORT",MAGICMA_Trailing,0,Red);
}
return(0);
}
}
//+-----------------------------------------------------------------------+
//| Check for close order conditions |
//+-----------------------------------------------------------------------+
void CheckForCloseConditions()
{
int ticket;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
if(OrderType()==OP_BUY)
{
if (GetSignal()==1)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
return(0);
}
/* if (GetSignal()==3)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
return(0);
}
*/
}
if(OrderType()==OP_SELL)
{
if (GetSignal()==2)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
return(0);
}
/* if (GetSignal()==3)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
return(0);
}
*/
}
}
return(0);
}
//+-----------------------------------------------------------------------+
//| Check for close order conditions by time and day |
//+-----------------------------------------------------------------------+
void CheckForCloseConditionsTime()
{
int ticket;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
if(OrderType()==OP_BUY)
{
if (GetSignal()==1)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
return(0);
}
if (GetSignal()==3)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
return(0);
}
}
if(OrderType()==OP_SELL)
{
if (GetSignal()==2)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
return(0);
}
if (GetSignal()==3)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
return(0);
}
}
}
return(0);
}
//+-----------------------------------------------------------------------+
//| Check for close order conditions trailing |
//+-----------------------------------------------------------------------+
void CheckForCloseConditionsTrailing()
{
int ticket;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA_Trailing || OrderSymbol()!=Symbol()) continue;
if(OrderType()==OP_BUY)
{
if (GetSignal()==1)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
return(0);
}
/* if (GetSignal()==3)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
return(0);
}
*/
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
if(OrderType()==OP_SELL)
{
if (GetSignal()==2)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
return(0);
}
/* if (GetSignal()==3)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
return(0);
}
*/
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Magenta);
return(0);
}
}
}
}
}
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Magenta);
return(0);
}
}
}
return(0);
}
//+-----------------------------------------------------------------------+
//| Start function |
//+-----------------------------------------------------------------------+
int start()
{
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//Select Trading Hours
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if (UseHourTrade){
if (!(Hour()>=FromHourTrade && Hour()<=ToHourTrade)) {
Comment("Time for trade has not come else!");
return(0);
} else Comment("");
}else Comment("");
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//Select Trading Days
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if (UseDayTrade){
if (!(DayOfWeek()>=FromDayTrade && DayOfWeek()<=ToDayTrade)) {
Comment("Time for trade has not come else!");
return(0);
} else Comment("");
}else Comment("");
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//Select Closing Hours
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if (UseHourClose){
if (!(Hour()>=UseHourClose && Minute()<=CloseMinute)) {
Comment("Time for close has not come else!");
return(0);
} else Comment("");
}else Comment("");
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//Select Closing Days
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if (UseDayClose){
if (!(DayOfWeek()>=CloseDay /* && DayOfWeek()<=ToDayTrade */)) {
Comment("Time for close has not come else!");
return(0);
} else Comment("");
}else Comment("");
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if(CalculateNumberOfOrders(Symbol())<1&&CalculateNumberOfOrdersTrailing(Symbol())<1)
CheckForOpenConditions();
else
{
CheckForCloseConditions();
CheckForCloseConditionsTrailing();
CheckForCloseConditionsTime();
}
return(0);
}