MQL5 code errors

 

hi guys,

i wanted an EA that closes trades at a fixed time, so I asked AI to write me a mql5 code for that.

the problem is that when i put it into meta editor and hit compile, it returns with errors... errors i have no idea what they mean or how to fix them.

im hoping someone can look at the errors and fix it please!


this is the code written by AI

#property strict

// Define the exit time (hours and minutes)
int exitHour = 16;
int exitMinute = 0;

// Timer interval (in seconds) - Adjust as needed
int timerInterval = 60; // Check every minute

// Function to check if current time matches exit time
bool IsExitTime() {
  time_t currentTime = TimeCurrent();
  tm someTime;
  gmtime(&currentTime, &someTime); // Assuming you have gmtime defined
  return (someTime.tm_hour == exitHour && someTime.tm_min == exitMinute);
}

int OnTimer() {
  // Flag to prevent multiple executions within the timer interval
  static bool alreadyExecuted = false;

  // Check for first execution or exit time reached
  if (!alreadyExecuted && IsExitTime()) {
    alreadyExecuted = true;

    // Loop through all open positions
    int totalPositions = PositionsTotal();
    for (int i = 0; i < totalPositions; i++) {
      // Get position information
      position_info position = PositionGet(i);
      if (position.magic == EMPTY) continue; // Skip positions without magic number

      // Close the position
      double lots = position.lot;
      int result = OrderClose(position.ticket, lots, position.bid ? position.bid : position.ask, 0, "");
      if (result != RET_OK) {
        Print("Error closing position:", GetLastError());
      }
    }
  }

  return 0;
}
 

Error 1: Missing Definitions

  • The code references functions like  TimeCurrent() ,  gmtime() ,  PositionsTotal() ,  PositionGet() ,  OrderClose() ,  Print() ,  GetLastError() ,  EMPTY , and  RET_OK  without their definitions. These functions are part of the MQL5 standard library and need to be properly included.

Error 2: Incorrect Function Usage

  • The usage of  gmtime()  is incorrect. In MQL5, you should use  TimeToStruct()  to convert a  time_t  value to a  datetime  structure.

Error 3: Undefined Variables

  • Variables like  position_info ,  position , and  ticket  are not defined in the code. You need to define these structures or use the appropriate MQL5 functions to access position information.

Error 4: Incorrect Condition

  • The condition  position.bid ? position.bid : position.ask  is not valid. You should use  position.bid  or  position.ask  directly based on your logic.

Error 5: Incorrect Print Statement

  • The  Print()  function should have a proper format for error messages. Consider using  Print("Error closing position: ", GetLastError());  for correct output.
To fix these errors, you need to include the necessary MQL5 standard library files, correct the function usage, define required structures, and adjust the conditions and print statements accordingly. Once these modifications are made, the code should compile successfully in MetaEditor.

For more detailed guidance you can explore https://www.mql5.com/en/articles/100

Good luck with your coding experiments with AI under the hood!



Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
  • www.mql5.com
The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.
 
Rajendra Deonarine Ramphal: so I asked AI to write me

Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, “LP-MOBI”, Molanis, “Octa-FX Meta Editor”, Strategy Builder FX, “Strategy Quant”, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

Since you haven't learned MQL4/5, therefor 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 yours.

We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

ChatGPT
  1. Even it says do not use it for coding. * 
  2. Mixing MT4 and MT5 code together.
  3. Creating multiple OnCalculate/OnTick functions.
  4. OnCalculate returning a double.
  5. Filling buffers with zero in OnInit (they have no size yet). Setting buffer elements to zero but not setting Empty Value to correspond.
  6. Calling undefined functions.
  7. Calling MT4 functions in MT5 code.
  8. Sometimes, not using strict (MT4 code).
  9. Code that will not compile.
  10. Creating code outside of functions. * 
  11. Creating incomplete code. * 
  12. Initialization of Global variables with non-constants. * 
  13. Assigning a MT5 handle to a double or missing the buffer and bar indexes in a MT4 call. * 
  14. Useing MT4 Trade Functions without first selecting an order. * 
  15. Uses NULL in OrderSend. * 
bot builder Creating two OnInit() functions. * 
EA builder
  1. Counting up while closing multiple orders.
  2. Not useing time in new bar detection.
  3. Not adjusting for 4/5 digit brokers, TP/SL and slippage. * 
  4. Not checking return codes.
EATree Uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)
ForexEAdvisor
  1. Non-updateing global variables.
  2. Compilation errors.
  3. Not checking return codes.
  4. Not reporting errors.
FX EA Builder
  1. Not checking return codes.
  2. Loosing open tickets on terminal restart. No recovery (crash/power failure.)
  3. Not adjusting stops for the spread. * 
  4. Using OrdersTotal directly.
  5. Using the old event handlers.
 
Rajendra Deonarine Ramphal:

hi guys,

i wanted an EA that closes trades at a fixed time, so I asked AI to write me a mql5 code for that.

the problem is that when i put it into meta editor and hit compile, it returns with errors... errors i have no idea what they mean or how to fix them.

im hoping someone can look at the errors and fix it please!


this is the code written by AI

Never use generators or ChatGpt for coding, it will not be complete, Chat gpt does not understand the syntax, if you give us an Ai generated code we will not be able to assist you. We can see when its hand written, and when its Ai generated.