Problem Description
Solution
To programmatically retrieve contract details in MQL4, you can use two primary sets of functions: the newer SymbolInfo... functions (recommended for compatibility and detail) or the legacy MarketInfo() function.
Below is a script that demonstrates how to retrieve and print the specific details you requested (Swap, Tick Size, Lot Size) along with other essential contract specifications.
MQL4 Script: Get Contract Details
This script allows you to input a symbol name (or use the current chart's symbol) and prints the details to the "Experts" tab in the Terminal.
//+------------------------------------------------------------------+
//| GetContractDetails.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
//--- Input Parameters
input string InpSymbol = ""; // Symbol (leave empty for current chart)
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// 1. Determine the symbol to check
string symbol = InpSymbol;
if(symbol == "") symbol = _Symbol; // Use current chart symbol if input is empty
// 2. Ensure the symbol is selected in Market Watch to get fresh data
if(!SymbolSelect(symbol, true))
{
Print("Error: Symbol ", symbol, " not found or could not be selected.");
return;
}
// 3. Retrieve Integer Properties
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
int spread = (int)SymbolInfoInteger(symbol, SYMBOL_SPREAD);
int stopLevel = (int)SymbolInfoInteger(symbol, SYMBOL_TRADE_STOPS_LEVEL);
int swapMode = (int)SymbolInfoInteger(symbol, SYMBOL_SWAP_MODE);
// 4. Retrieve Double Properties (The specific details you asked for)
double tickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
double tickValue = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
double minLot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);
double swapLong = SymbolInfoDouble(symbol, SYMBOL_SWAP_LONG);
double swapShort = SymbolInfoDouble(symbol, SYMBOL_SWAP_SHORT);
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
double contract = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE);
// 5. Helper to describe Swap Mode
string swapModeDesc;
switch(swapMode)
{
case 0: swapModeDesc = "Points"; break;
case 1: swapModeDesc = "Base Currency"; break;
case 2: swapModeDesc = "Interest"; break;
case 3: swapModeDesc = "Margin Currency"; break;
default: swapModeDesc = "Unknown";
}
// 6. Print the Report
Print("========== Contract Details: ", symbol, " ==========");
Print("--- Pricing ---");
PrintFormat("Digits: %d", digits);
PrintFormat("Point: %.5f", point);
PrintFormat("Tick Size: %.5f", tickSize);
PrintFormat("Tick Value: %.2f (in deposit currency)", tickValue);
PrintFormat("Spread: %d points", spread);
PrintFormat("Stop Level: %d points", stopLevel);
Print("--- Volume (Lots) ---");
PrintFormat("Min Lot: %.2f", minLot);
PrintFormat("Max Lot: %.2f", maxLot);
PrintFormat("Lot Step: %.2f", lotStep);
PrintFormat("Contract Size: %.2f", contract);
Print("--- Swaps ---");
PrintFormat("Swap Mode: %s", swapModeDesc);
PrintFormat("Swap Long: %.2f", swapLong);
PrintFormat("Swap Short: %.2f", swapShort);
Print("================================================");
}
Key Functions Used
-
SymbolInfoDouble(symbol, PROPERTY_ID):SYMBOL_TRADE_TICK_SIZE: The minimum price change.SYMBOL_VOLUME_MIN: The minimum lot size allowed.SYMBOL_SWAP_LONG/SYMBOL_SWAP_SHORT: The swap rates.SYMBOL_TRADE_TICK_VALUE: The value of a single tick in the account currency (crucial for risk management).
-
SymbolInfoInteger(symbol, PROPERTY_ID):SYMBOL_SWAP_MODE: Tells you how the swap is calculated (e.g., in points, currency, or percentage).SYMBOL_DIGITS: The number of decimal places.
-
SymbolSelect(symbol, true):- Ensures the symbol exists and is available in the "Market Watch" window. If a symbol is not selected,
SymbolInfofunctions might return outdated or zero values.
- Ensures the symbol exists and is available in the "Market Watch" window. If a symbol is not selected,
Alternative: Using MarketInfo (Legacy)
If you are maintaining older code, you might see MarketInfo. It performs the same task but uses different constants.
double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);
double minLot = MarketInfo(Symbol(), MODE_MINLOT);
double swapLong = MarketInfo(Symbol(), MODE_SWAPLONG);
Q&A
Q: Why is SymbolInfoDouble preferred over MarketInfo?
A: SymbolInfoDouble is the modern standard that aligns with MQL5 syntax, making it easier to port code between MT4 and MT5. It also supports a wider range of properties than the older MarketInfo.
Q: Why do I get 0 for Tick Value?
A: This usually happens if the symbol is not selected in the Market Watch window. The script above uses SymbolSelect to prevent this.
Q: How do I calculate the cost of a swap in money?
A: It depends on the SYMBOL_SWAP_MODE.
- If mode is Points (0):
SwapCost = SwapRate * PointValue * Lots. - If mode is Money (1 or 3):
SwapCost = SwapRate * Lots. - If mode is Interest (2): The calculation involves the current price and annual interest logic.