How to use a Symbol as an indicator?

 

Hello,

Basically I would like to use a symbol as an indicator in another chart. 

For example, in my EURUSD chart, i'd like to have an indicator below that rapresent GBPUSD price. Simple as that.

I couldn't fine the code in the codebase section... Can you help me with this? 

Thanks..

 
dario1133:

Hello,

Basically I would like to use a symbol as an indicator in another chart. 

For example, in my EURUSD chart, i'd like to have an indicator below that rapresent GBPUSD price. Simple as that.

I couldn't fine the code in the codebase section... Can you help me with this? 

Thanks..

Usually you show your attempts first, and people on the forum can try to help you depending on your coding level.

I have written a small sketch, just serve as an idea of how it can be done, adapt it according to your specific needs.

//+------------------------------------------------------------------+
//|                                      GBPUSD in EURUSD.mq5        |
//|                            Copyright 2023, MetaQuotes Ltd.       |
//|                                       https://www.mql5.com       |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1

#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_color1 clrGreenYellow
#property indicator_label1 "Price"

//--- indicator buffers
double Buffer1[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    //--- indicator buffers mapping
    SetIndexBuffer(0, Buffer1);
    PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
    //---
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    int limit = rates_total - prev_calculated;
    ArraySetAsSeries(Buffer1, false);

    // Get historical data of GBP/USD using MqlRates structure
    MqlRates rates[];
    int copied = CopyRates("GBPUSD", PERIOD_CURRENT, 0, rates_total, rates);

    if (copied <= 0)
    {
        Print("Failed to copy price data ", GetLastError());
        return 0;
    }

    // Fill the indicator buffer with historical close prices of GBP/USD
    for(int i = 0; i < rates_total; i++)
    {
        Buffer1[i] = rates[i].close;
    }

    return(rates_total);
}
//+------------------------------------------------------------------+
 
Enrique Enguix #:

Usually you show your attempts first, and people on the forum can try to help you depending on your coding level.

I have written a small sketch, just serve as an idea of how it can be done, adapt it according to your specific needs.

I thought there was already a code in the codebase section but I couldn't find it. I'm going to trying to code it for myself. Thanks for your help.

 
dario1133 #: I thought there was already a code in the codebase section but I couldn't find it. I'm going to trying to code it for myself. Thanks for your help.

Yes, there are, and here one such example ...

Code Base

Chart on Chart

prostotrader, 2017.01.26 09:25

The Chart on Chart indicator displays a chart of other symbol on the current chart.
 

However, both examples, from the CodeBase and from the post above, suffers from one major flaw — they are not "aligned" or "synchronised".

In other words, should there be missing bars in one symbol and not the other, then their timestamps will misaligned on the chart.

Consider adding that sort of verification and correction to your code to prevent the possible misalignment.

 
Fernando Carreiro #:

Yes, there are, and here one such example ...

Thank you very much... that was really helpful.

Reason: