Get the Current Market Price in MQL5 (the right way..)
Learn how to calculate the true current market price in MetaTrader 5 using MQL5, broken down line by line with simple functions.
Get the Current Market Price in MQL5 (the right way..)
Why this matters
If you’re building Expert Advisors or automated scripts in MetaTrader 5, you need a reliable way to access the current market price. You do not want the bid. You do not want the ask. You want the midpoint between both because that reflects the true trading price.
We are going to build a tiny function that gives you that value.
The goal
- Get the bid price
- Get the ask price
- Calculate the midpoint
- Format it correctly for the asset
- Print it to the terminal on every tick
Full Script
On-Tick
1
2
market_price();
Print(market_price());
Functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double ask_price()
{
return SymbolInfoDouble(_Symbol, SYMBOL_ASK);
}
double bid_price()
{
return SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
double market_price()
{
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
return NormalizeDouble((bid_price() + ask_price()) / 2, digits);
}
This post is licensed under
CC BY 4.0
by the author.
