How to Use Constants, Enums, and Structures in MQL4

Watch on YouTube: MQL4 Code with Constants, Enums, and Structures

MQL4 provides predefined constants, enumerations, and structures to handle trading tasks, colors, timeframes, and other market data more efficiently and consistently. These elements simplify code readability and reduce errors by organizing data into intuitive, standardized groups.

1. Constants and Enumerations

  • Standard Constants: Fixed values that represent trading-related data, like order types (OP_BUY, OP_SELL), price modes (MODE_ASK, MODE_BID), and color codes (clrRed, clrBlue).
  • Enumerations (enum): Grouped values representing defined options, such as timeframes (PERIOD_M1, PERIOD_H1) and market info modes (MODE_SPREAD, MODE_POINT). Enums enforce consistency by allowing only defined values for variables.

Example:

void OnStart() {
   ENUM_TIMEFRAMES selectedTimeFrame = PERIOD_M15; // Predefined 15-minute timeframe
   Print("Selected Timeframe: ", selectedTimeFrame);
   ENUM_ORDER_TYPE orderType = ORDER_TYPE_BUY_STOP; // Buy-stop order constant 
   Print("Order Type: ", orderType);
}

2. Structures

Structures, defined with the struct keyword, are custom data types that bundle related variables. They’re useful for organizing complex data, such as storing order details or candlestick properties in a single object.

Example Structure for Trade Details:

// Define a structure to hold trade details
struct TradeDetails {
   int orderID;           // Order ID of the trade
   double price;          // Price at which the trade is executed
   datetime orderTime;    // Time when the order was placed
};
void OnStart() {
   TradeDetails trade;  // Create an instance of the TradeDetails structure
   // Assign values to the structure fields
   trade.orderID = 12345;  // Set the order ID
   trade.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);  // Get the current ask price of the symbol
   trade.orderTime = TimeCurrent();  // Record the current server time as the order time
   // Print the trade details, including Order ID, Price, and Order Time
   Print("Order ID: ", trade.orderID, ", Price: ", trade.price, ", Order Time: ", TimeToString(trade.orderTime));
}

In MQL4, several predefined structures simplify working with key trading data. For instance, MqlDateTime is used to handle date and time values efficiently. The MqlRates structure provides access to historical price data, including price, volume, and spread, essential for backtesting and historical analysis. The MqlTick structure is optimized for quickly retrieving the latest tick data, giving access to real-time bid and ask prices. These structures make it easy for developers to organize and access crucial trading information in their MQL4 applications.

MqlRates Structure:

void OnStart() {
   // Declare an array of MqlRates to store price data
   MqlRates rate[];
   // Copy the last 10 one-minute (M1) historical price bars for the chart symbol (_Symbol)
   int copied = CopyRates(_Symbol, PERIOD_M1, 0, 10, rate);
   // Reverse the array to treat the last copied bar as the first element
   ArraySetAsSeries(rate, true);
   // Check if the data retrieval was successful
   if (copied > 0) {
      // Print the open and close price of the most recent period (now at index 0)
      Print("Last period open: ", rate[0].open, ", Close: ", rate[0].close);
   }
}

Summary

MQL4’s predefined constants and enumerations simplify and standardize common operations, while structures allow for organizing complex data within custom data types. Using these tools makes code more readable, consistent, and less error-prone, especially for trading and market data management.

Next: Code Compilation

Previous: Functions