Problem Description
Solution
To get the most recent tick's price, volume, and timestamp in MQL4, the most robust method is using the MqlTick structure combined with the SymbolInfoTick function. This ensures that the Bid, Ask, and Time values are synchronized from the exact same data packet.
Key Concepts
MqlTickStructure: A built-in structure that stores the latest price and time data.SymbolInfoTick: A function that fills theMqlTickstructure with the latest data for a specific symbol.- Volume Distinction:
- Real Volume (
last_tick.volume): Represents the size of the last deal. This is often0in Forex (OTC markets) but populated for Exchange instruments (Futures/Stocks). - Tick Volume (
Volume[0]): Represents the number of ticks received for the current bar. This is the standard "Volume" used in Forex technical analysis.
- Real Volume (
MQL4 Implementation
Here is a complete script that retrieves and prints the latest tick data.
//+------------------------------------------------------------------+
//| GetLatestTickData.mq4 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// 1. Declare the MqlTick structure variable
MqlTick last_tick;
// 2. Reset error cache
ResetLastError();
// 3. Call SymbolInfoTick to populate the structure
// We use _Symbol to get data for the current chart's symbol
if(SymbolInfoTick(_Symbol, last_tick))
{
// --- Extracting Data ---
// Prices
double bidPrice = last_tick.bid;
double askPrice = last_tick.ask;
// Timestamp (Server time of the tick)
datetime tickTime = last_tick.time;
// Milliseconds are also available if needed
// long timeMsc = last_tick.time_msc;
// Volume Types
// 'volume' in MqlTick is Real Volume (Exchange size).
// Usually 0 for Forex brokers.
ulong realVolume = last_tick.volume;
// To get the standard MT4 Tick Volume (count of ticks in current bar):
long tickVolume = Volume[0];
// --- Output to Journal ---
Print("--------------------------------------------------");
Print("Symbol: ", _Symbol);
Print("Time: ", TimeToString(tickTime, TIME_DATE|TIME_SECONDS));
Print("Bid: ", DoubleToString(bidPrice, _Digits));
Print("Ask: ", DoubleToString(askPrice, _Digits));
Print("Real Volume (Last Deal Size): ", realVolume);
Print("Current Bar Tick Volume: ", tickVolume);
Print("--------------------------------------------------");
}
else
{
Print("Failed to get tick data. Error Code: ", GetLastError());
}
}
//+------------------------------------------------------------------+
Explanation of the Code
MqlTick last_tick;: This initializes the structure that will hold the data. It contains fields like.time,.bid,.ask,.last, and.volume.SymbolInfoTick(_Symbol, last_tick): This function attempts to filllast_tickwith the latest market data. It returnstrueif successful.Volume[0]vslast_tick.volume:- The code explicitly retrieves
Volume[0](a standard array in MQL4) to get the Tick Volume (the count of price updates in the current candle), which is what most Forex traders refer to as "Volume". last_tick.volumeis printed to show Real Volume, which will likely be 0 unless your broker provides Level 2 data for exchange-traded instruments.
- The code explicitly retrieves
Q&A
Q: Why use SymbolInfoTick instead of the Bid and Ask variables?
A: Bid and Ask are global variables that update automatically, but accessing them separately alongside TimeCurrent() can theoretically result in a race condition where the price updates between the moment you read the Bid and the moment you read the Time. SymbolInfoTick provides an atomic snapshot where all data points belong to the exact same event.
Q: Why is last_tick.volume returning 0?
A: In the MetaTrader 4 environment, specifically for Forex (OTC), there is no centralized exchange to report the "size" of a trade (Real Volume). Therefore, this field is usually empty. Traders typically use Volume[0] (Tick Volume) as a proxy for volatility.
Q: Can I get milliseconds from the timestamp?
A: Yes. The MqlTick structure in modern MQL4 includes a time_msc member (type long), which holds the time in milliseconds since 1970. You can access it via last_tick.time_msc.