If you want something coded for you, then consider hiring someone in the Freelance section to do it for you. If not, then search for it in the Market or in the CodeBase.
- 2022.06.03
- www.mql5.com
You have only four choices:
-
Search for it (CodeBase or Market). Do you expect us to do your research for you?
- Beg at:
-
MT5: Begin learning to code it.
If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.
-
Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
Hiring to write script - General - MQL5 programming forum (2019)
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
No free help (2017)
Greeting someone and helping is against the rules moderator ?
You need to CAREFULLY read: The user is asking about MQL5. Please remember: this is the MQL5 forum, and there is one special section for the old terminal.
I apologize i did not see that .
//+------------------------------------------------------------------+ //| OpenTradeOnTimeAndCloseOnTime.mq5 | //| yright 2022,Institute Of Bald Helmets | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ input double lots=0.01; enum time_type{ time_local=0,//local time_broker=1//broker }; input time_type time_to_use=time_broker;//time to use input string open_time="04:20";//open time input string close_time="11:20";//close time input double close_profit=1.0;//profit input int magic=274;//magic number+system id enum direction{ dir_buy=0,//Buy dir_sell=1//Sell }; input direction which_direction=dir_buy;//direction input int attempts=10;//attempts input uint delay=300;//delay b2in attempts input int slippage=100;//slippage bool waiting=false; long ticket=0; int day=0; int OnInit() { start_minutes=time_string_to_minutes(open_time,":"); end_minutes=time_string_to_minutes(close_time,":"); full_day_minutes=1440; waiting=!traded_today(ticket,day); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- //--- //waiting if(waiting){ //and time to trade if(time_to_trade(start_minutes,end_minutes)){ if(IsAssetInTradingSession(_Symbol,(ENUM_DAY_OF_WEEK)(TimeDayOfWeek(TimeTradeServer())))){ bool traded=trade(which_direction,lots,attempts,delay,slippage,magic,ticket); if(traded){waiting=false;day=TimeDay(TimeCurrent());}} } } else{//not waiting //in trade if(ticket!=0){ double my_profit=order_profit(ticket); if(my_profit>=close_profit||time_to_close(start_minutes,end_minutes)){ if(close(ticket,attempts,delay,slippage)){ticket=0;} }} //not in trade else{ // if(TimeDay(TimeCurrent())!=day){waiting=true;} } } } int start_minutes=0,end_minutes=0,full_day_minutes=0; int time_string_to_minutes(string _time,string separator){ ushort usep=StringGetCharacter(separator,0); string parts[]; int k=StringSplit(_time,usep,parts); if(k==1){ return(((int)StringToInteger(parts[0])*60)); }else{ return(((int)StringToInteger(parts[0])*60+(int)StringToInteger(parts[1]))); } } bool traded_today(ulong &_ticket,int &_day){ _ticket=-1; _day=-1; if(HistorySelect(Flatten(TimeCurrent()),TimeCurrent())){ for(int i=0;i<HistoryDealsTotal();i++){ ulong historyticket=HistoryDealGetTicket(i); if(HistoryDealGetInteger(historyticket,DEAL_MAGIC)==magic){_day=TimeDay(TimeCurrent());return(true);} }} for(int i=0;i<PositionsTotal();i++){ ulong position_ticket=PositionGetTicket(i); if(PositionGetInteger(POSITION_MAGIC)==magic){_day=TimeDay(TimeCurrent());_ticket=position_ticket;return(true);} }return(false); } bool time_to_trade(int start_mins,int end_mins){ datetime now=TimeCurrent(); if(time_to_use==time_local){now=TimeLocal();} int mins_now=TimeHour(now)*60+TimeMinute(now); if(start_mins<end_mins&&mins_now>=start_mins){return(true);} else{if(mins_now>=start_mins&&mins_now<=full_day_minutes){return(true);}} return(false); } bool time_to_close(int start_mins,int end_mins){ datetime now=TimeCurrent(); if(time_to_use==time_local){now=TimeLocal();} int mins_now=TimeHour(now)*60+TimeMinute(now); if(start_mins<end_mins&&mins_now>=end_mins){return(true);} else{if(mins_now>=end_mins&&mins_now<start_mins){return(true);}} return(false); } bool trade(direction _dir,double _lots,int _attempts,uint _delay,int _slippage,int _magic,ulong &ticket_result){ int atts=0,digits=0,stoplimit=0; ticket_result=0; MqlTick tick; MqlTradeRequest Treq={}; MqlTradeResult Tres; Treq.action=TRADE_ACTION_DEAL; Treq.magic=magic; Treq.deviation=slippage; Treq.volume=_lots; Treq.symbol=_Symbol; Treq.type_filling=GetFillingA(_Symbol,0); bool opened=false; while(!opened&&atts<attempts) { atts++; //basics bool GetTick=SymbolInfoTick(_Symbol,tick); if(!GetTick&&tick.ask!=0&&tick.bid!=0) continue; if(_dir==dir_buy) { Treq.type=ORDER_TYPE_BUY; Treq.price=tick.ask; } else if(_dir==dir_sell) { Treq.type=ORDER_TYPE_SELL; Treq.price=tick.bid; } //Sell Ends Here bool first_opened=OrderSend(Treq,Tres); //check if order opened - the boolean is not indicative of succesful open according to metaquotes if(first_opened) { if(Tres.retcode==10008||Tres.retcode==10009){opened=true;ticket_result=Tres.deal;} } //if it fails delay if(!opened&&atts<=attempts){Print("Retcode ["+IntegerToString(Tres.retcode)+"]");Sleep(delay);} } return(opened); } bool close(ulong _ticket,int _attempts,uint _delay,int _slippage){ MqlTradeRequest Treq={}; MqlTradeResult Tres; MqlTick tick; if(PositionSelectByTicket(ticket)) { //request int atts=0; bool pclose=false,sub_close=false; while(!pclose&&atts<attempts) { atts++; //basics bool GetTick=SymbolInfoTick(PositionGetString(POSITION_SYMBOL),tick); if(!GetTick&&tick.ask!=0&&tick.bid!=0) continue; //Buy if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { Treq.symbol=PositionGetString(POSITION_SYMBOL); Treq.action=TRADE_ACTION_DEAL; Treq.type_filling=GetFillingA(Treq.symbol,0); Treq.position=ticket; Treq.volume=PositionGetDouble(POSITION_VOLUME); Treq.deviation=slippage; Treq.price=tick.bid; Treq.type=ORDER_TYPE_SELL; } //Buy ends here //Sell if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) { Treq.symbol=PositionGetString(POSITION_SYMBOL); Treq.action=TRADE_ACTION_DEAL; Treq.type_filling=GetFillingA(Treq.symbol,0); Treq.position=ticket; Treq.volume=PositionGetDouble(POSITION_VOLUME); Treq.deviation=slippage; Treq.price=tick.ask; Treq.type=ORDER_TYPE_BUY; } //Sell ends here sub_close=OrderSend(Treq,Tres); if(!sub_close) pclose=false; if(sub_close) { pclose=false; if(Tres.retcode==10008||Tres.retcode==10009||Tres.retcode==10004) pclose=true; } if(!pclose&&atts<=attempts){Print("Retcode ["+IntegerToString(Tres.retcode)+"]");} } return(pclose); } return(false); } double AccountFreeMarginCheck(string symbol,ENUM_ORDER_TYPE type,double volume){ double mronelot=0.0; double margin=(double)SymbolInfoDouble(symbol,SYMBOL_MARGIN_INITIAL); double price=(double)SymbolInfoDouble(symbol,SYMBOL_ASK); if(type==ORDER_TYPE_SELL){price=(double)SymbolInfoDouble(symbol,SYMBOL_BID);} bool c=OrderCalcMargin(type,symbol,volume,price,mronelot); if(c){margin=mronelot;} return(margin); } double order_profit(long deal_ticket){ //select in open bool select=PositionSelectByTicket(deal_ticket); if(select){return((double)PositionGetDouble(POSITION_PROFIT));} if(!select){select=HistorySelectByPosition(deal_ticket); if(select) { int oip=HistoryDealsTotal(); double sum=0; for(int o=0;o<oip;o++) { ulong historyticket=HistoryDealGetTicket(o); sum+=HistoryDealGetDouble(historyticket,DEAL_PROFIT); } } } return(0); } bool IsOrderOpen(ulong deal_ticket){ return(PositionSelectByTicket(deal_ticket)); } int TimeMinute(datetime time) {MqlDateTime mt;bool turn=TimeToStruct(time,mt);return(mt.min);} int TimeHour(datetime time) {MqlDateTime mt;bool turn=TimeToStruct(time,mt);return(mt.hour);} int TimeDay(datetime time){MqlDateTime mt;bool turn=TimeToStruct(time,mt);return(mt.day);} int TimeDayOfWeek(datetime time){MqlDateTime mt;bool turn=TimeToStruct(time,mt);return(mt.day_of_week);} datetime Flatten(datetime time){MqlDateTime mt;bool turn=TimeToStruct(time,mt);mt.hour=0;mt.min=0;return(StructToTime(mt));} ENUM_ORDER_TYPE_FILLING GetFillingA( const string Symb, const uint Type = ORDER_FILLING_FOK ) { const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE); const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE); return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ? (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ? ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) : (ENUM_ORDER_TYPE_FILLING)Type); } bool IsAssetInTradingSession(string asset,ENUM_DAY_OF_WEEK day_of_week){ bool result=false; datetime session_from=0,session_to=0; //find total sessions int sessions_total=0; for(int se=0;se<10;se++) { bool sessexist=SymbolInfoSessionTrade(asset,day_of_week,se,session_from,session_to); //if session exists if(sessexist){sessions_total=se+1;} else{break;} } //loop into all sessions and if we are not in any of them , return false for(int se=0;se<sessions_total;se++) { SymbolInfoSessionTrade(asset,day_of_week,se,session_from,session_to); //result is "For the day requested ,i.e , today , which server hours can you trade at //turn starting time in seconds , i think it is already //so , get the server time datetime servertime=TimeTradeServer(); //turn MqlDateTime mqdt_server; bool turn=TimeToStruct(servertime,mqdt_server); if(turn){ //zero minutes , hours and seconds mqdt_server.hour=0;mqdt_server.min=0;mqdt_server.sec=0; //back to datetime servertime=StructToTime(mqdt_server); //project start server time and end server time by adding the seconds from step one respectively session_from=(datetime)((int)(servertime)+(int)(session_from)); session_to=(datetime)((int)(servertime)+(int)(session_to)); //Print(asset+" server session from "+TimeToString(session_from,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); //Print(asset+" server session to "+TimeToString(session_to,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); //if we are within that time window then cool if(TimeTradeServer()>=session_from&&TimeTradeServer()<=session_to){return(true);} } }//loop into sessions ends here return(false); }
Vladimir Karputov is correct in stating that this is an MQL5 section, but I suspect the OP posted in the wrong section and actually wants MQL4 code. I am guessing this because he only has MT4 signals available and no MT5 ones.
This is also why I suggested he use Freelance, because he is a signal provider with subscribers, so requesting for free coding is in my personal view, not very ethical.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use