//+------------------------------------------------------------------+ //| Opening_EA.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #include #include CTrade trade; input string EAComment = "Opening_EA";// EA Comment input string Trade_Parameters = "=======================";//Trade Parameters ___________________________________________ input double Lots = 0.1; // Lot size input double TakeProfit = 0; // Take Profit in point input double StopLoss = 0; // Stop Loss point input int Magic = 1; // Magic Number input string TimeSettings = "====< Time Settings >====";//   input string TimeStart = "08:00"; // Start Time input string TimeEnd = "22:00"; // Stop Time input string TimeClose = "22:00"; // Close Time //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// datetime buy, sell,t1,Lasttime;bool Buy=0, Sell=0;double Sloss=0,Tprof=0;int b1,zz=0,xx=0,CloseDay=0; //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- initialization end return(INIT_SUCCEEDED); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { return; } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //+------------------------------------------------------------------+ if(CloseTime(TimeClose) && Positions() > 0){CloseDay=DayOfYearMQL4();} if(DayOfYearMQL4()==CloseDay){ClosePosition(Symbol());return;} //+------------------------------------------------------------------+ t1=StringToTime(TimeToString(TimeCurrent(), TIME_DATE)+" "+TimeStart); b1=iBarShift(NULL,0,t1); //+------------------------------------------------------------------+ Buy = (iHigh(NULL,0,b1) < iClose(NULL,0,1)); Sell= (iLow(NULL,0,b1) > iClose(NULL,0,1)); //+------------------------------------------------------------------+ if(TimeDayOfYearMQL4(Lasttime) != DayOfYearMQL4()){zz=0;xx=0;} //+------------------------------------------------------------------+ //---- Getting buy signals if(Buy && TimeCurrent() > StringToTime(TimeEnd)+60*Period() && zz==0) { if(Positions() < 1) MarketOrder(_Symbol, POSITION_TYPE_BUY, Lots, 0, 0, 0, Magic, 10, EAComment);zz=1;Lasttime=TimeCurrent(); } //+------------------------------------------------------------------+ //---- Getting sell signals if(Sell && TimeCurrent() > StringToTime(TimeEnd)+60*Period() && xx==0) { if(Positions() < 1) MarketOrder(_Symbol, POSITION_TYPE_SELL, Lots, 0, 0, 0, Magic, 10, EAComment);xx=1;Lasttime=TimeCurrent(); } //+------------------------------------------------------------------+ //--- if(Positions() > 0) { Tprof=TakeProfit; Sloss=StopLoss; if(Sloss > 0 || Tprof > 0) InitialSLTP(_Symbol, Sloss, Tprof); } //+------------------------------------------------------------------+ //--- } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// int DayOfYearMQL4() { MqlDateTime tm; TimeCurrent(tm); return(tm.day_of_year); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// int TimeDayOfYearMQL4(datetime date) { MqlDateTime tm; TimeToStruct(date,tm); return(tm.day_of_year); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ bool MarketOrder(const string sSymbol, const ENUM_POSITION_TYPE eType, const double fLot, const double prices, const int nSL = 0, const int nTP = 0, const ulong nMagic = 0, const uint nSlippage = 1000, const string nComment = "") { bool bRetVal = false; MqlTradeRequest oRequest = {0}; MqlTradeResult oResult = {0}; double fPoint = SymbolInfoDouble(sSymbol, SYMBOL_POINT); int nDigits = (int) SymbolInfoInteger(sSymbol, SYMBOL_DIGITS); if(prices == 0) { oRequest.action = TRADE_ACTION_DEAL; } if(prices > 0) { oRequest.action = TRADE_ACTION_PENDING; } oRequest.symbol = sSymbol; oRequest.volume = fLot; oRequest.stoplimit = 0; oRequest.deviation = nSlippage; oRequest.comment = nComment; if(eType == POSITION_TYPE_BUY && prices == 0) { oRequest.type = ORDER_TYPE_BUY; oRequest.price = NormalizeDouble(SymbolInfoDouble(sSymbol, SYMBOL_ASK), nDigits); oRequest.sl = NormalizeDouble(oRequest.price - nSL * fPoint, nDigits) * (nSL > 0); oRequest.tp = NormalizeDouble(oRequest.price + nTP * fPoint, nDigits) * (nTP > 0); } if(eType == POSITION_TYPE_SELL && prices == 0) { oRequest.type = ORDER_TYPE_SELL; oRequest.price = NormalizeDouble(SymbolInfoDouble(sSymbol, SYMBOL_BID), nDigits); oRequest.sl = NormalizeDouble(oRequest.price + nSL * fPoint, nDigits) * (nSL > 0); oRequest.tp = NormalizeDouble(oRequest.price - nTP * fPoint, nDigits) * (nTP > 0); } if(eType == POSITION_TYPE_BUY && prices > 0) { oRequest.type = ORDER_TYPE_BUY_LIMIT; oRequest.price = NormalizeDouble(prices, nDigits); oRequest.sl = NormalizeDouble(oRequest.price - nSL * fPoint, nDigits) * (nSL > 0); oRequest.tp = NormalizeDouble(oRequest.price + nTP * fPoint, nDigits) * (nTP > 0); } if(eType == POSITION_TYPE_SELL && prices > 0) { oRequest.type = ORDER_TYPE_SELL_LIMIT; oRequest.price = NormalizeDouble(prices, nDigits); oRequest.sl = NormalizeDouble(oRequest.price + nSL * fPoint, nDigits) * (nSL > 0); oRequest.tp = NormalizeDouble(oRequest.price - nTP * fPoint, nDigits) * (nTP > 0); } if((int) SymbolInfoInteger(sSymbol, SYMBOL_FILLING_MODE)==SYMBOL_FILLING_FOK) { oRequest.type_filling = ORDER_FILLING_FOK; } if((int) SymbolInfoInteger(sSymbol, SYMBOL_FILLING_MODE)==SYMBOL_FILLING_IOC) { oRequest.type_filling = ORDER_FILLING_IOC; } if((int) SymbolInfoInteger(sSymbol, SYMBOL_FILLING_MODE)==0) { oRequest.type_filling = ORDER_FILLING_RETURN; } //--- check filling if((int) SymbolInfoInteger(sSymbol, SYMBOL_FILLING_MODE)>2) { if(!FillingCheck(sSymbol)) return(false); } oRequest.magic = nMagic; MqlTradeCheckResult oCheckResult= {0}; bool bCheck = OrderCheck(oRequest, oCheckResult); Print("Order Check MarketOrder:", " OrderCheck = ", bCheck, ", retcode = ", oCheckResult.retcode, ", balance = ", NormalizeDouble(oCheckResult.balance, 2), ", equity = ", NormalizeDouble(oCheckResult.equity, 2), ", margin = ", NormalizeDouble(oCheckResult.margin, 2), ", margin_free = ", NormalizeDouble(oCheckResult.margin_free, 2), ", margin_level = ", NormalizeDouble(oCheckResult.margin_level, 2), ", comment = ", oCheckResult.comment); if(bCheck == true && oCheckResult.retcode == 0) { bool bResult = false; for(int k = 0; k < 5; k++) { bResult = OrderSend(oRequest, oResult); if(bResult == true && (oResult.retcode == TRADE_RETCODE_PLACED || oResult.retcode == TRADE_RETCODE_DONE)) break; if(k == 4) break; Sleep(100); } Print("Order Send MarketOrder:", " OrderSend = ", bResult, ", retcode = ", oResult.retcode, ", deal = ", oResult.deal, ", order = ", oResult.order, ", volume = ", NormalizeDouble(oResult.volume, 2), ", price = ", NormalizeDouble(oResult.price, _Digits), ", bid = ", NormalizeDouble(oResult.bid, _Digits), ", ask = ", NormalizeDouble(oResult.ask, _Digits), ", comment = ", oResult.comment, ", request_id = ",oResult.request_id); if(oResult.retcode == TRADE_RETCODE_DONE) bRetVal = true; } else if(oResult.retcode == TRADE_RETCODE_NO_MONEY) { Print("???????????? ????? ??? ???????? ???????. ?????? ???????? ??????????."); ExpertRemove(); } return(bRetVal); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ bool InitialSLTP(const string sSymbol, const double nSL = 0, const double nTP = 0) { bool bRetVal = false; //--- declare and initialize the trade request and result of trade request MqlTradeRequest request; MqlTradeResult result; int total=PositionsTotal(); // number of open positions //--- iterate over all open positions for(int i=0; i2) { if(!FillingCheck(sSymbol)) return(false); } if(type==POSITION_TYPE_BUY) { if(nSL > 0) sl=NormalizeDouble(bid-nSL*point(),digits); if(nTP > 0) tp=NormalizeDouble(ask+nTP*point(),digits); } else { if(nSL > 0) sl=NormalizeDouble(ask+nSL*point(),digits); if(nTP > 0) tp=NormalizeDouble(bid-nTP*point(),digits); } //--- zeroing the request and result values ZeroMemory(request); ZeroMemory(result); //--- setting the operation parameters request.action =TRADE_ACTION_SLTP; // type of trade operation request.position=position_ticket; // ticket of the position request.symbol=position_symbol; // symbol request.sl =sl; // Stop Loss of the position request.tp =tp; // Take Profit of the position request.magic=Magic; // MagicNumber of the position //--- send the request if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code else bRetVal=true; } } return (bRetVal); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ bool ClosePosition(const string sSymbol, double fLot = 0) { bool bRetVal = false; //--- declare and initialize the trade request and result of trade request MqlTradeRequest request; MqlTradeResult result; int total=PositionsTotal(); // number of open positions //--- iterate over all open positions for(int i=total-1; i>=0; i--) { //--- parameters of the order ulong position_ticket=PositionGetTicket(i); // ticket of the position string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position double volume=PositionGetDouble(POSITION_VOLUME); // volume of the position ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // type of the position //--- if the MagicNumber matches if(magic==Magic && position_symbol==sSymbol) { //--- zeroing the request and result values ZeroMemory(request); ZeroMemory(result); if((int) SymbolInfoInteger(sSymbol, SYMBOL_FILLING_MODE)==SYMBOL_FILLING_FOK) { request.type_filling = ORDER_FILLING_FOK; } if((int) SymbolInfoInteger(sSymbol, SYMBOL_FILLING_MODE)==SYMBOL_FILLING_IOC) { request.type_filling = ORDER_FILLING_IOC; } if((int) SymbolInfoInteger(sSymbol, SYMBOL_FILLING_MODE)==0) { request.type_filling = ORDER_FILLING_RETURN; } //--- check filling if((int) SymbolInfoInteger(sSymbol, SYMBOL_FILLING_MODE)>2) { if(!FillingCheck(sSymbol)) return(false); } //--- setting the operation parameters request.action =TRADE_ACTION_DEAL; // type of trade operation request.position =position_ticket; // ticket of the position request.symbol =position_symbol; // symbol request.volume =volume; // volume of the position request.deviation=5; // allowed deviation from the price request.magic =Magic; // MagicNumber of the position //--- set the price and order type depending on the position type if(type==POSITION_TYPE_BUY) { request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID); request.type =ORDER_TYPE_SELL; } else { request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK); request.type =ORDER_TYPE_BUY; } //--- send the request if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code //--- } } return(bRetVal); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ //| Checks and corrects type of filling policy | //+------------------------------------------------------------------+ bool FillingCheck(const string symbol) { MqlTradeRequest m_request= {0}; // request data MqlTradeResult m_result= {0}; // result data ENUM_ORDER_TYPE_FILLING m_type_filling=0; //--- get execution mode of orders by symbol ENUM_SYMBOL_TRADE_EXECUTION exec=(ENUM_SYMBOL_TRADE_EXECUTION)SymbolInfoInteger(symbol,SYMBOL_TRADE_EXEMODE); //--- check execution mode if(exec==SYMBOL_TRADE_EXECUTION_REQUEST || exec==SYMBOL_TRADE_EXECUTION_INSTANT) { //--- neccessary filling type will be placed automatically return(true); } //--- get possible filling policy types by symbol uint filling=(uint)SymbolInfoInteger(symbol,SYMBOL_FILLING_MODE); //--- check execution mode again if(exec==SYMBOL_TRADE_EXECUTION_MARKET) { //--- for the MARKET execution mode //--- analyze order if(m_request.action!=TRADE_ACTION_PENDING) { //--- in case of instant execution order //--- if the required filling policy is supported, add it to the request if(m_type_filling==ORDER_FILLING_FOK && (filling & SYMBOL_FILLING_FOK)!=0) { m_request.type_filling=m_type_filling; return(true); } if(m_type_filling==ORDER_FILLING_IOC && (filling & SYMBOL_FILLING_IOC)!=0) { m_request.type_filling=m_type_filling; return(true); } //--- wrong filling policy, set error code m_result.retcode=TRADE_RETCODE_INVALID_FILL; return(false); } return(true); } //--- EXCHANGE execution mode switch(m_type_filling) { case ORDER_FILLING_FOK: //--- analyze order if(m_request.action==TRADE_ACTION_PENDING) { //--- in case of pending order //--- add the expiration mode to the request if(!ExpirationCheck(symbol)) m_request.type_time=ORDER_TIME_DAY; //--- stop order? if(m_request.type==ORDER_TYPE_BUY_STOP || m_request.type==ORDER_TYPE_SELL_STOP) { //--- in case of stop order //--- add the corresponding filling policy to the request m_request.type_filling=ORDER_FILLING_RETURN; return(true); } } //--- in case of limit order or instant execution order //--- if the required filling policy is supported, add it to the request if((filling & SYMBOL_FILLING_FOK)!=0) { m_request.type_filling=m_type_filling; return(true); } //--- wrong filling policy, set error code m_result.retcode=TRADE_RETCODE_INVALID_FILL; return(false); case ORDER_FILLING_IOC: //--- analyze order if(m_request.action==TRADE_ACTION_PENDING) { //--- in case of pending order //--- add the expiration mode to the request if(!ExpirationCheck(symbol)) m_request.type_time=ORDER_TIME_DAY; //--- stop order? if(m_request.type==ORDER_TYPE_BUY_STOP || m_request.type==ORDER_TYPE_SELL_STOP) { //--- in case of stop order //--- add the corresponding filling policy to the request m_request.type_filling=ORDER_FILLING_RETURN; return(true); } } //--- in case of limit order or instant execution order //--- if the required filling policy is supported, add it to the request if((filling & SYMBOL_FILLING_IOC)!=0) { m_request.type_filling=m_type_filling; return(true); } //--- wrong filling policy, set error code m_result.retcode=TRADE_RETCODE_INVALID_FILL; return(false); case ORDER_FILLING_RETURN: //--- add filling policy to the request m_request.type_filling=m_type_filling; return(true); } //--- unknown execution mode, set error code m_result.retcode=TRADE_RETCODE_ERROR; return(false); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ //| Check expiration type of pending order | //+------------------------------------------------------------------+ bool ExpirationCheck(const string symbol) { CSymbolInfo sym; MqlTradeRequest m_request= {0}; // request data MqlTradeResult m_result= {0}; // result data //--- check symbol if(!sym.Name((symbol==NULL)?Symbol():symbol)) return(false); //--- get flags int flags=sym.TradeTimeFlags(); //--- check type switch(m_request.type_time) { case ORDER_TIME_GTC: if((flags&SYMBOL_EXPIRATION_GTC)!=0) return(true); break; case ORDER_TIME_DAY: if((flags&SYMBOL_EXPIRATION_DAY)!=0) return(true); break; case ORDER_TIME_SPECIFIED: if((flags&SYMBOL_EXPIRATION_SPECIFIED)!=0) return(true); break; case ORDER_TIME_SPECIFIED_DAY: if((flags&SYMBOL_EXPIRATION_SPECIFIED_DAY)!=0) return(true); break; default: Print(__FUNCTION__+": Unknown expiration type"); break; } //--- failed return(false); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ //| Check Symbol Points | //+------------------------------------------------------------------+ double point(string symbol=NULL) { string sym=symbol; if(symbol==NULL) sym=_Symbol; return(SymbolInfoDouble(sym,SYMBOL_POINT)); } //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+ //| Check for open positions | //+------------------------------------------------------------------+ int Positions(int ty=-1) { int result = 0, total=PositionsTotal(); // number of open positions //--- iterate over all open positions for(int i=0; i= ms) || TimeHour(TimeCurrent()) > hs) return(true); return(false); } //+------------------------------------------------------------------+ int TimeHour(datetime date) { MqlDateTime tm; TimeToStruct(date,tm); return(tm.hour);} //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int TimeMinute(datetime date) { MqlDateTime tm; TimeToStruct(date,tm); return(tm.min);} //IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// //+------------------------------------------------------------------+