【MQL5 Paradise Episode 5】Finally connect with the real market! Let's obtain the real-time “price”
Hello everyone! I’m Pineapple.
In the previous Episode 4, we learned about the “if statements (conditional branching)” that let a program make decisions. By writing rules like “If 〇〇, then do △△,” the program could judge things by itself, which was a big advancement.
However, last time we directly put the “current price” into a variable with a hard-coded number such as 150.50. This cannot respond to the real-time market that changes moment by moment. What automated trading programs truly need is“the real price that is moving right in front of us right now.”, right?
So this time, we finallylearn how to retrieve real-time prices (rates) from MT5The moment your program connects to the living market. Please relax and read on, perhaps with a cup of coffee.
The Basics and Most Important in FX! Let’s remember “Bid” and “Ask”
Before your program retrieves prices, let’s review one very important basic in FX trading:“Bid” and “Ask.”.
When you typically view MT5 charts or open order screens, you will surely see two prices lined up.
- Bid (selling price):The price at which you place a sell (short) order. The current price shown on the chart is usually this Bid price.
- Ask (buying price):The price at which you place a buy (long) order. It is slightly higher than the Bid.
And the difference between this “Ask” and “Bid” is“the spread”, which is the broker’s fee.
When building an automated trading program (EA), you need to clearly distinguish and use these two prices, for example: “Now I’ll place a buy order, so look at the Ask price” or “Now I want to sell, so look at the Bid price.”
Programs are very honest, so if asked vaguely, they get confused with questions like “What is the current price? Is it the selling price or the buying price?”
How to retrieve prices in MQL5
So how do we obtain the current Bid and Ask in MQL5? If you’ve written programs in the older MT4 (MQL4), you might think you could just typeBidandAskto retrieve them.
But with the more advanced and accurate MQL5, you need to make a slightly more polite request. That is the function (instruction set) namedSymbolInfoDouble()(Symbol Info Double).
The name is a bit long, but when broken down, it’s not scary at all.
- Symbol:The currency pair (e.g., USDJPY, EURUSD)
- Info:Information
- Double:A number with a decimal point (the box for numbers with decimals you learned in Chapter 3)
In other words, it’s an instruction to “tell me the decimal number information for the specified currency pair.” Here’s how you actually obtain the current Bid price.
SymbolInfoDouble(_Symbol, SYMBOL_BID);
Inside the parentheses, you separate two pieces of information with a comma (,)., The first_Symbolis a convenient keyword that automatically points to the currency pair of the chart that this program is currently running on.
The secondSYMBOL_BIDis the specification that we want the Bid information. If you want the Ask instead, justSYMBOL_ASK.
Getting real live prices
Now, using the variables and if statements you learned previously, let’s write a script that retrieves the current real-time price and evaluates the market environment.
Open MetaEditor, create a new script, and write the following code.
void OnStart()
{
// 1. Get the chart’s currency pair name and store it in a variable
string my_symbol = _Symbol;
// 2. Retrieve real-time Bid (selling) and Ask (buying) prices and store them
double current_bid = SymbolInfoDouble(my_symbol, SYMBOL_BID);
double current_ask = SymbolInfoDouble(my_symbol, SYMBOL_ASK);
Print("【Current real-time market retrieved】");
Print("Target currency pair:", my_symbol);
Print("Bid(selling price):", current_bid);
Print("Ask(buying price):", current_ask);
// 3. Set a target price (arbitrary 150.00 in this example)
double target_price = 150.00;
// 4. Use if statements to compare the current price with the target price
if (current_bid > target_price)
{
Print("Current Bid is above ", target_price, ". It might be a bull market!");
}
else if (current_bid < target_price)
{
Print("Current Bid is below ", target_price, ". It might be a bear market.");
}
else
{
Print("Current Bid is exactly equal to ", target_price, ".");
}
}
Explanation of the sample code
How about it—can you see how neatly the knowledge so far connects?
- First,
string my_symbol = _Symbol;put the current currency pair name (like "USDJPY") into a string box. - Next, use the magic spell
SymbolInfoDouble()to obtain real-time Bid and Ask, and assign them tocurrent_bid,current_askas decimals (type double). - Finally, as a recap, compare the price you’re watching (we’re using a neat 150.00 for this example) with the freshly retrieved “real Bid price” using an
ifstatement and print the results.
Compile and drag-and-drop this script onto a live chart to run. In MT5’s “Toolbox” → “Experts” tab, you should see the real-time price moving at the right edge of the chart, output together with your messages.
If it runs without errors on the first try, give yourself a pat on the back. You’ve just accessed real-time data from the global financial markets through a program!
Pineapple-style! Strengthening awareness of invisible costs
Now, I’d like to share a practical trading talk.
This time, we deliberately retrieved two prices, Bid and Ask. Some may think, “That’s troublesome; can’t I just look at the chart price (Bid) and still buy and sell?”
In fact, whether you are aware of the Bid-Ask difference (the spread) can drastically affect an EA’s performance.
I often say,“Avoid excessive optimization (curve-fitting) and trade with simple, robust rules.”One characteristic of overly optimized, poor EAs is that they ignore invisible costs like the spread.
During backtesting, if you unrealistically set the spread to a fixed 1 point, the program may seem to earn enormous profits by chasing tiny pips with ultra-short-term scalping.
But in real markets, the spread can widen drastically during economic announcements or illiquid early hours. You might think you could buy at 150.00, only to be forced to buy at 150.05 because of the widened spread—these scenarios are common.
Overly optimized, complex logic is very fragile against real-market noise (spread widening and slippage) and can fail once run on a real account.
On the other hand,“wait for a proper moving average cross to enter” and “aim for a wider, tens of pips price range”is a simple logic that hardly budges even with some spread widening. It has room to breathe.
When writing programs,SYMBOL_ASKandSYMBOL_BID—remember that you are facing a real cost barrier—the spread. Even considering real costs, a solid, simple rule that still yields profit is what makes a truly robust system.
Conclusion
This time, we learned how to use theSymbolInfoDouble()function to obtain live price data from MT5. Real-time data flows into variables, and you judge it with if statements. Mastering this flow is hardly an exaggeration of mastering the fundamentals of programming.
However, until now we were only creating “scripts.” A script performs its action and then ends, doing it only once when dropped onto a chart. That wouldn’t be sufficient for a continuously monitoring automated trading system.
Next time, we will graduate from scripts and begin building“Expert Advisors (EA)”and explore the new magical framework“OnTick”that repeats automatic calculations every time the price ticks.
The time has come for your program to evolve into a 24/7 automated trading robot. Please look forward to it!
Well then, see you in the next issue of “MQL5 Paradise.” May the goddess of the market smile upon your program. This was Pineapple!