Back to MetaTrader 5 MQL5 for CFD Traders
Part 1

Data Types, Variables, and Strict Typing

MQL5 will not silently convert types the way Pine does. Know your ints from your doubles.

Beginner8 min readBeginner → AdvancedLesson 03 / 17
Try it in MetaEditor
Types, conversions, and the float-equality trap

Save as MQL5/Scripts/Lesson3Types.mq5 and run it. Note the last two lines: naive double comparison fails, tolerance-based comparison works.

//+------------------------------------------------------------------+
//| Lesson 3 - Types, conversions, float comparison                  |
//+------------------------------------------------------------------+
#property copyright "Strategist Academy"
#property version   "1.00"

void OnStart()
{
   string   sym      = _Symbol;
   int      bars     = Bars(sym, _Period);
   double   point    = _Point;
   datetime now      = TimeCurrent();
   long     tradeMode= SymbolInfoInteger(sym, SYMBOL_TRADE_MODE);
   bool     tradable = (tradeMode != SYMBOL_TRADE_MODE_DISABLED &&
                        tradeMode != SYMBOL_TRADE_MODE_CLOSEONLY);

   Print("Symbol:        ", sym);
   Print("Bars on chart: ", bars);
   Print("Point:         ", DoubleToString(point, _Digits));
   Print("Server time:   ", TimeToString(now, TIME_DATE | TIME_SECONDS));
   Print("Trading open:  ", tradable ? "yes" : "no");

   // Build strings explicitly - no implicit double-to-string here.
   string label = StringFormat("%s | bars=%d | point=%.5f", sym, bars, point);
   Print(label);

   // Parsing back from string
   string volText = "0.10";
   double vol     = StringToDouble(volText);
   Print("Parsed volume: ", DoubleToString(vol, 2));

   // --- The float trap ------------------------------------------------
   double a = 0.1 + 0.2;
   Print("0.1 + 0.2 == 0.3 exactly? ", (a == 0.3) ? "true" : "false  <-- the trap");
   Print("Same check with tolerance: ",
         (MathAbs(a - 0.3) < 0.000001) ? "true" : "false");

   // In trading terms: compare prices using points, not raw equality.
   double bid  = SymbolInfoDouble(sym, SYMBOL_BID);
   double level= NormalizeDouble(bid, _Digits);
   Print("Bid equals its own normalised copy (within half a point)? ",
         (MathAbs(bid - level) <= _Point / 2.0) ? "yes" : "no");
}
To run it
  1. 1. Open MetaEditor from MT5 (press F4), and pick the folder this program belongs in — Experts, Indicators, or Scripts.
  2. 2. Create a new file, paste this over it, then press F7 to compile. Fix anything the Errors tab reports.
  3. 3. Back in MT5, drag it onto a chart — or open the Strategy Tester if it is an Expert Advisor.

MQL5 is strictly typed

This is the single biggest adjustment coming from PineScript. In Pine, a value is a series and the runtime mostly forgives you. In MQL5 you declare a type and the compiler holds you to it. Mismatches are compile errors, not silent wrong numbers — which is frustrating at first and genuinely safer later.

The types you will actually use

  • int — counts, shifts, error codes, magic numbers (as long/ulong).
  • double — every price, volume, and money value.
  • bool — flags from &&, ||, !.
  • string — symbol names, comments, messages.
  • datetime — seconds since 1970. Format with TimeToString.
  • colorclrDodgerBlue, or C'255,0,0'.
  • ENUM_* — named constants: ENUM_TIMEFRAMES, ENUM_ORDER_TYPE, ENUM_MA_METHOD, and dozens more. Prefer these over magic numbers.

Conversion is explicit

Use DoubleToString(), IntegerToString(), StringToDouble(), StringToTime(), and StringFormat(). Do not rely on implicit coercion — and never concatenate a double into a string expecting it to look right.

Never compare doubles for equality

Floating point arithmetic means 0.1 + 0.2 is not exactly 0.3. Anywhere you compare two prices, use a tolerance measured in points. This bites hardest in "did the price hit my level" checks, where an exact comparison silently never fires.

Scope and static

Variables declared inside a function die when it returns. Declared at file scope, they live for the program's lifetime. Mark a local static to keep its value between calls — a cheap way to count ticks or remember the last bar without a global.

What you just did

Lesson 03 of 17 in MetaTrader 5 MQL5 for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.