Another Symbol Synchronized

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Another Symbol Synchronized
ÿþ//+------------------------------------------------------------------+

//|                                  Another Symbol Synchronized.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43161 |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43161"

#property version   "1.000"

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   1

//---

#include <Trade\SymbolInfo.mqh>

//---

CSymbolInfo    m_symbol;                     // object of CSymbolInfo class

//--- plot Label1

#property indicator_label1  "Label1"

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1  clrLimeGreen,clrLavender

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input string   InpName  = "USDJPY";    // Name of another symbol

//--- indicator buffers

double   OpenBuffer[];

double   HighBuffer[];

double   LowBuffer[];

double   CloseBuffer[];

double   ColorColors[];

//---

bool     m_init_error               = false;    // error on InInit

long     m_counter=0;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- forced initialization of variables

   m_init_error               = false;    // error on InInit

   m_counter                  = 0;

//--- indicator buffers mapping

   SetIndexBuffer(0,OpenBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,HighBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,LowBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,CloseBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,ColorColors,INDICATOR_COLOR_INDEX);

//--- an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//--- the name of the symbol, for which the bars are drawn

   ResetLastError();

   if(!m_symbol.Name(InpName)) // sets symbol name

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,m_symbol.Digits());

//--- Set the display of the symbol

   PlotIndexSetString(0,PLOT_LABEL,InpName+" Open;"+InpName+" High;"+InpName+" Low;"+InpName+" Close");

   IndicatorSetString(INDICATOR_SHORTNAME,"Another Symbol Synchronized("+InpName+")");

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Indicator deinitialization function                              |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   Comment("");

  }

//+------------------------------------------------------------------+

//| 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=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

//--- main loop

   for(int i=limit; i<rates_total; i++)

     {

      ResetLastError();

      int i_bar_shift=iBarShift(InpName,PERIOD_CURRENT,time[i],true);

      if(i_bar_shift<0)

        {

         OpenBuffer[i]=0.0;

         HighBuffer[i]=0.0;

         LowBuffer[i]=0.0;

         CloseBuffer[i]=0.0;

         ColorColors[i]=0.0;

         Comment("");

        }

      else

        {

         MqlRates rates[];

         int copy_rates=CopyRates(InpName,PERIOD_CURRENT,i_bar_shift,1,rates);

         if(copy_rates<1)

           {

            OpenBuffer[i]=0.0;

            HighBuffer[i]=0.0;

            LowBuffer[i]=0.0;

            CloseBuffer[i]=0.0;

            ColorColors[i]=0.0;

            Comment("");

            m_counter++;

            Print(time[i],", ",m_counter);

            return(0);

           }

         else

           {

            Comment(TimeCurrent(),", ",InpName,": ",rates[0].time);

            OpenBuffer[i]=rates[0].open;

            HighBuffer[i]=rates[0].high;

            LowBuffer[i]=rates[0].low;

            CloseBuffer[i]=rates[0].close;

            ColorColors[i]=(OpenBuffer[i]<CloseBuffer[i])?1.0:0.0;

           }

        }

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments