MQL5: How to Develop a Multi-Currency Indicator?

Introduction to Multi-Currency Indicators in MQL5

What is a Multi-Currency Indicator?

A multi-currency indicator is a custom technical analysis tool designed to display information derived from multiple currency pairs on a single chart. Unlike standard indicators that operate solely on the chart’s current symbol, multi-currency indicators gather data from other instruments, enabling traders to identify correlations, divergences, and potential trading opportunities across different markets. These indicators essentially consolidate information, presenting a broader view of the market landscape. They are different from multi time frame indicators.

Why Develop a Multi-Currency Indicator?

Developing a multi-currency indicator offers several advantages:

  • Cross-Market Analysis: Identify relationships and patterns between different currency pairs.
  • Confirmation Signals: Strengthen trading signals by considering data from related markets.
  • Diversification Insights: Assess the overall market sentiment and potential risks across various currencies.
  • Automated Correlation Trading: Develop Expert Advisors (EAs) that automatically trade based on cross-currency correlations.

Prerequisites: MQL5 Fundamentals

Before diving into multi-currency indicator development, ensure you possess a solid understanding of the following MQL5 concepts:

  • MQL5 IDE and MetaEditor
  • MQL5 syntax and data types
  • Custom indicator creation basics (OnInit(), OnCalculate(), OnDeinit() functions)
  • Working with indicator buffers
  • Plotting indicator lines and drawing styles

Gathering Data from Other Currency Pairs

The core of a multi-currency indicator lies in its ability to retrieve data from symbols other than the one on the current chart. MQL5 provides several functions for this purpose.

Using iCustom() function to Access Data from Custom Indicators

The iCustom() function allows you to access data from custom indicators applied to different currency pairs. This is particularly useful when you want to incorporate signals or information generated by existing custom indicators into your multi-currency indicator.

double OtherSymbolMA(string symbol, int timeframe, int period, int shift)
{
   int handle = iCustom(symbol, timeframe, "Examples\\Moving Average", period, 0, shift);
   if(handle == INVALID_HANDLE)
   {
      Print("Error creating custom indicator handle for " + symbol);      
      return 0.0;   
   }
   double value;
   if(CopyBuffer(handle, 0, shift, 1, value) != 1)
   {
      Print("Error copying buffer for " + symbol);      
      IndicatorRelease(handle);
      return 0.0;   
   }
   IndicatorRelease(handle);
   return value;
}

Explanation: iCustom() function requires the symbol, timeframe, and the name of the custom indicator. Ensure the path is correct. The returned handle is then used with the CopyBuffer() function to retrieve data from indicator buffers. handle needs to be released using IndicatorRelease().

Accessing Standard Indicator Data from Other Symbols

MQL5 offers built-in functions to access standard indicator values (e.g., Moving Average, RSI, MACD) for other currency pairs. These functions typically start with i<IndicatorName>, for example, iMA() for Moving Average, iRSI() for Relative Strength Index.

double OtherSymbolMA(string symbol, int timeframe, int period, int shift)
{
   int handle = iMA(symbol, timeframe, period, 0, MODE_SMA, PRICE_CLOSE);
   if(handle == INVALID_HANDLE)
   {
      Print("Error creating MA indicator handle for " + symbol);      
      return 0.0;   
   }
   double value;
   if(CopyBuffer(handle, 0, shift, 1, value) != 1)
   {
      Print("Error copying MA buffer for " + symbol);      
      IndicatorRelease(handle);
      return 0.0;   
   }
   IndicatorRelease(handle);
   return value;
}

Explanation: iMA() function requires the symbol, timeframe, period, moving average method and applied price. The returned handle is then used with the CopyBuffer() function to retrieve data from indicator buffers. handle needs to be released using IndicatorRelease().

Working with SymbolInfoTick() and SymbolInfoDouble() functions

For accessing real-time price data (bid, ask, last, volume) from other symbols, use SymbolInfoTick() and SymbolInfoDouble() functions.

MqlTick tick;
string symbol = "EURUSD";

if(SymbolInfoTick(symbol, tick))
{
   double bid = tick.bid;
   double ask = tick.ask;
   Print("EURUSD Bid: ", bid, " Ask: ", ask);
}
else
{
   Print("Error getting tick data for EURUSD. Error code: ", GetLastError());
}

double closePrice = SymbolInfoDouble(symbol, SYMBOL_LAST);
Print("Last price of EURUSD: ", closePrice);

Explanation: SymbolInfoTick() retrieves the latest tick information, including bid, ask, and volume. SymbolInfoDouble() retrieves various symbol properties, such as last price, volume, and spread. Error handling is crucial when dealing with external data.

Developing the Multi-Currency Indicator

Defining Input Parameters (Currency Pairs, Indicator Settings)

Define input parameters to allow users to customize the indicator, such as the currency pairs to monitor and specific settings for each pair.

input string Symbol1 = "EURUSD";
input string Symbol2 = "GBPUSD";
input int MAPeriod = 20; 

Explanation: Using the input keyword defines variables that are accessible from the indicator’s properties window in MetaTrader.

Implementing the Indicator Logic (Calculation and Data Processing)

In the OnCalculate() function, retrieve data from the specified currency pairs using the functions described earlier. Perform the necessary calculations and data processing to generate the desired output.

double MA1 = OtherSymbolMA(Symbol1, PERIOD_CURRENT, MAPeriod, i);
double MA2 = OtherSymbolMA(Symbol2, PERIOD_CURRENT, MAPeriod, i);

// Calculate the difference between the two MAs
Buffer[i] = MA1 - MA2;

Explanation: This snippet calculates the difference between Moving Averages of two symbols. You can adapt this logic to calculate correlations, ratios, or any other custom metric.

Drawing the Indicator on the Chart

Use indicator buffers and plotting styles to visually represent the calculated data on the chart.

#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue

double         Buffer[];

int OnInit()
  {
   SetIndexBuffer(0, Buffer, INDICATOR_DATA);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2, clrBlue);
   IndicatorSetString(INDICATOR_SHORTNAME, "Multi-Currency Indicator");
   return(INIT_SUCCEEDED);
  }

Explanation: Defines one indicator buffer to store values to be plotted. Sets the style and color of the plotted line. SetIndexBuffer assigns the Buffer array to the indicator buffer for plotting.

Enhancements and Advanced Techniques

Handling Errors and Invalid Symbol Names

Implement robust error handling to gracefully manage cases where the specified symbol names are invalid or data retrieval fails. Display informative messages to the user.

if(handle == INVALID_HANDLE)
{
   Print("Error creating indicator handle for " + symbol + ". Error code: " + GetLastError());
   return 0.0;
}

Optimizing Indicator Performance

Retrieving data from multiple symbols can impact performance. Optimize your code by minimizing function calls, caching data where appropriate, and using efficient data structures.

Adding Alerts and Notifications

Incorporate alerts and notifications to inform traders when specific conditions are met based on the multi-currency indicator’s calculations. Use PlaySound() or Alert() functions for this purpose.

if(Buffer[i] > threshold)
{
   Alert("Multi-Currency Indicator crossed threshold!");
   PlaySound("alert.wav");
}

Multi Time Frame Analysis

Extend your indicator to incorporate multi-timeframe analysis by retrieving data from different timeframes for the specified currency pairs.

Conclusion

Summary of Key Concepts

This article covered the fundamental principles of developing multi-currency indicators in MQL5. You learned how to retrieve data from other currency pairs using iCustom(), iMA(), SymbolInfoTick(), and SymbolInfoDouble(), implement indicator logic, and draw the results on the chart. You also explored techniques for error handling, performance optimization, alerts, and multi-timeframe analysis. Multi-currency indicators enable traders to create more sophisticated analysis based on data from multiple markets and can provide valuable insights, patterns, and opportunities.

Further Learning Resources


Leave a Reply