Mastering MQL5: More Built-In Constants - Day 5

Discover and utilize key built-in constants in MetaTrader 5 for optimized Expert Advisor coding. Explore trade constants, mathematical gems, market insights, price illumination, and color creativity.

Mastering MQL5: More Built-In Constants - Day 5
2 min read·
No comment yet!

It's a while from our Day 4, it's not because I am lazy, it's because life is hard. You know, learning new thing is far harder than just sitting and watching Netflix.

Anyway, I try to make Day 5 as a sample that we can draw a Moving Average line to our chart but, no, our knowledge just not enough yet. Let's continue to learning basic thing.

Trade Constants and MarketInfo constants

These constants hold the secrets to trading operations, order types, and trade-related parameters.

Example 1 - Exploring Trade Constants :

void OnStart()
   // Caculate SL and TP
   double currentSellPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   double currentBuyPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double lotSize = 0.1;
   double stopLoss = currentBuyPrice - 500 * _Point;
   double takeProfit = currentSellPrice + 1000 * _Point;
   Print("price ", currentSellPrice);
   Print("_Symbol ", _Symbol);
   Print("stopLoss ", MathRound(stopLoss*10000)/10000);
   Print("takeProfit ", MathRound(takeProfit*10000)/10000);

Explain the code:

  1. First we get the current BID price and current BUY price of the current chart using SymbolInfoDouble
  2. 3 built-in constants here: _Symbol, SYMBOL_BID, SYMBOL_ASK
  3. Then we calculate the lotSize, basically, this lotSize should match our account balance, but we set it = 0.1 here
  4. Calculate stopLoss and takeProfit. We use built-in _Point here.
  5. Finally print out all the result.

Timeframe Constants

These constants provide the keys to different timeframes used in technical analysis and strategy development.

Example 2 - Navigating Timeframe Constants:

void OnStart()
   ENUM_TIMEFRAMES selectedTimeframe = Period(); // Get the current chart timeframe
   if (PERIOD_H1 == selectedTimeframe)
      Print("Current chart timeframe is H1.");

   MqlDateTime STime;
   datetime time_current=TimeCurrent();
   Print("Time Current ",TimeToString(time_current,TIME_DATE|TIME_SECONDS));

This is quite straight forward, check if the timeframe is a specific value. You can find more in the document of mql5 (which quite a pain in the ***). Then get the current time of the market (not your local time).

Color Constants and Objects Constants

Constants representing colors for graphical objects. It's help a lot when we start interacting with the chart.

I will give you some time to self-research about "how to create an object and display it to chart". Hint is to use "ObjectCreate" function and "Objects constants".

The Day 6 will answer this question.


In this blog, you might encouter some constants, and it's hundreds other constants out there.

We will learn them all (or at least all that we need). Be chill.