Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Your title states "MQL5" code, but the file you attached is MQL4 code, so I have moved the topic to MQL4 section.
If you are the coder then you should learn to debug your own code. We are not going to debug it for you.
Add "Print" to monitor the issues. Look at the logs, Journal and Experts to track the progress.
- Harsha Rao: There are no Compilation errors, and hence I am not sure why the EA is not taking any trades.
Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
Code debugging - Developing programs - MetaEditor Help
Error Handling and Logging in MQL5 - MQL5 Articles (2015)
Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010) -
datetime currentTime = TimeCurrent(); datetime startTime = D'1970.01.01 ' + openHour + ':' + openMinute + ":00"; datetime endTime = D'1970.01.01 ' + closeHour + ':' + closeMinute + ":00"; if (currentTime >= startTime && currentTime <= endTime)
Your condition will never be true. Today is 2023 and that is after 1970.01.01 HH:MM
SECONDS currentTime = time(), startTime = openHour * 3600 + openMinute * 60, endTime = closeHour * 3600 + closeMinute * 60; if (currentTime >= startTime && currentTime <= endTime)
Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08)
MQL5 Programming Basics: Time - MQL5 Articles (2013.04.26)Remember to handle the case where start > end (e.g. 2100 … 0200)
Rather than on Start(), learn using OnInit(), OnTick(), etc
void TradeOnTime(int openHour, int openMinute, int closeHour, int closeMinute, string direction, double lotSize, int takeProfit, int stopLoss) { datetime currentTime = TimeCurrent(); MqlDateTime st1, // Start time struct et1, // end time struct temp; // Current time struct if(!TimeToStruct(currentTime, temp)) { Print("Error Generating time ", _LastError); return; } st1 = temp; et1 = temp; st1.min = openMinute; st1.hour = openHour; et1.min = closeMinute; et1.hour = closeHour; et1.sec = st1.sec = 0; datetime startTime = StructToTime(st1), endTime = StructToTime(et1); if(currentTime >= startTime && currentTime <= endTime) { // Trading conditions met, open a trade int ticket = 0; // Variable to store the order ticket number if(direction == "Buy") { ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, 0, 0, "Buy Order", 0, 0, Green); } else if(direction == "Sell") { ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, 0, 0, "Sell Order", 0, 0, Red); } // Check the return value of OrderSend if(ticket > 0) { // OrderSend was successful int takeProfitLevel = takeProfit; int stopLossLevel = stopLoss; if(OrderModify(ticket, OrderOpenPrice(), OrderOpenPrice() + takeProfitLevel * Point, OrderOpenPrice() - stopLossLevel * Point, 0, Blue)) { Print("OrderModify successful"); } else { Print("OrderModify failed with error code: ", GetLastError()); } } else { // OrderSend failed, handle the error (display a message, log it, etc.) Print("OrderSend failed with error code: ", GetLastError()); } } }
Set a limit on the maximum trades and open with a demo.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Basically, when I attach the EA to GBPUSD 1M chart, I want the EA to open a BUY trade at 0.01 lot size at 18:00:00 hours and close it at 18:59:00. It then waits and open a BUY trade at 20:00:00 hours and closes it at 20:59:59 hours.
There are no Compilation errors, and hence I am not sure why the EA is not taking any trades. Can someone please help? Have attached the MQL file here.
Regards,
Harsha Rao