MultiSymbol

Author: Copyright 2015, MetaQuotes Software Corp.
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
MultiSymbol
//+------------------------------------------------------------------+
//|                                                  MultiSymbol.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright  "Copyright 2015, MetaQuotes Software Corp."
#property link       "https://www.mql5.com"
#property description"MultiSymbol by pipPod"
#property version    "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 20            //Inc/dec by factor 5
#property indicator_plots   4             //can't call indicator_plots 
#define indicator_symbols   4             //indicator_symbols=indicator_plots
//---
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrRoyalBlue,clrLimeGreen
//---
#property indicator_type2   DRAW_COLOR_CANDLES
#property indicator_color2  clrSilver,clrLimeGreen
//---
#property indicator_type3   DRAW_COLOR_CANDLES
#property indicator_color3  clrLimeGreen,clrFireBrick
//---
#property indicator_type4   DRAW_COLOR_CANDLES
#property indicator_color4  clrLimeGreen,clrYellow
//---
//+------------------------------------------------------------------+
//| Class for indicator and index buffers                            |
//+------------------------------------------------------------------+
class CBuffers
  {
public:
   //---       constructor
               CBuffers(void):indicatorHandle(INVALID_HANDLE)  
                 {}
   //---       destructor
              ~CBuffers(void) 
                 {}
   //---       indicator handlers
   int         indicatorHandle;
   //---       indicator buffers
   double      SymbolBuffer[1];
   //---       copy symbol/indicator data
   int         CopyBuffer(const int &buf_num,const datetime &from,const int &count)
                 {return(CopyBuffer(indicatorHandle,buf_num,from,count,SymbolBuffer));}
   //---       previously calculated bars
   int         BarsCalculated(void)
                 {return(BarsCalculated(indicatorHandle));}
   //---       release indicator handles
   bool        IndicatorRelease(void)
                 {return(IndicatorRelease(indicatorHandle));}
   //---       index buffers
   double      IndexBuffer[];
   //---       assign index buffers
   bool        SetIndexBuffer(int index,ENUM_INDEXBUFFER_TYPE buffer_mode=INDICATOR_DATA)
                 {return(SetIndexBuffer(index,IndexBuffer,buffer_mode));}
   //---       index buffer series flag
   bool        ArraySetAsSeries(bool flag)
                 {return(ArraySetAsSeries(IndexBuffer,flag));}
   //---       initialize index buffers 
   bool        ArrayInitialize(double value)
                 {return(ArrayInitialize(IndexBuffer,value));}
   //---       detach index buffers
   void        ArrayFree(void)
                 {ArrayFree(IndexBuffer);return;}
  };
//+------------------------------------------------------------------+
//| Symbols to display                                               |
//+------------------------------------------------------------------+
input string Symbol1="EURUSD";   //Symbol 1         
input string Symbol2="GBPUSD";   //Symbol 2      
input string Symbol3="USDCHF";   //Symbol 3         
input string Symbol4="USDJPY";   //Symbol 4      
//--- indicator and index objects                            
CBuffers symbol[indicator_symbols][5];
CBuffers buffer[indicator_symbols][5];
//--- chart properties
long chartID=ChartID();
short window;
//--- symbols for indicators
string Symbol[indicator_symbols];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorSetInteger(0,INDICATOR_DIGITS,4);
   //--- set indicator symbols
   Symbol[0] = Symbol1;
   Symbol[1] = Symbol2;
   Symbol[2] = Symbol3;
   Symbol[3] = Symbol4;
   for(int i=0;i<indicator_symbols;i++)
     { 
      if(!SymbolSelect(Symbol[i],true))
        {
         Alert(Symbol[i]," not available in Market Watch.\nInitialization Failed.");
         return(INIT_FAILED);
        }
      PlotIndexSetString(i,PLOT_LABEL,Symbol[i]+" Open;"+Symbol[i]+" High;"+Symbol[i]+" Low;"+Symbol[i]+" Close;");
     }
   //--- set indicator handlers
   for(int a=0;a<indicator_symbols;a++)
      for(int b=0;b<5;b++)
        { 
         symbol[a][b].indicatorHandle = INVALID_HANDLE;
         symbol[a][b].indicatorHandle = iCustom(Symbol[a],0,"PercentChange");
         if(symbol[a][b].indicatorHandle==INVALID_HANDLE)
            return(INIT_FAILED);
        }
   //--- set name & create labels
   string shortName = "MultiSymbol";
   IndicatorSetString(INDICATOR_SHORTNAME,shortName);
   window = (short)ChartWindowFind(chartID,shortName);
   CreateLabels();
   //--- indicator buffers mapping and series flag
   int buf_num = 0;
   for(int a=0;a<indicator_symbols;a++)
     { 
      for(int b=0;b<4;b++,buf_num++)
        {
         buffer[a][b].SetIndexBuffer(buf_num,INDICATOR_DATA);
         buffer[a][b].ArraySetAsSeries(true);
        }
      for(int c=4;c<5;c++,buf_num++)
        { 
         buffer[a][c].SetIndexBuffer(buf_num,INDICATOR_COLOR_INDEX);
         buffer[a][c].ArraySetAsSeries(true);
        }
     }  
   //--- index buffers show data flag
   for(int i=0;i<indicator_buffers;i++)
      PlotIndexSetInteger(i,PLOT_SHOW_DATA,false);
//---
   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[])
  {
//---
   ArraySetAsSeries(time,true);
   int limit = rates_total-prev_calculated;
   //--- set initial bars to count and get bars/indicator calculated
   if(prev_calculated<=0 || prev_calculated>rates_total)
     {
      int barsWindow = (int)ChartGetInteger(chartID,CHART_VISIBLE_BARS)+50;
      if(rates_total<barsWindow)
         return(0);
      for(int a=0;a<indicator_symbols;a++)
         for(int b=0;b<5;b++)
            if(symbol[a][b].BarsCalculated()<barsWindow)
               return(0);
            //buffer[a][b].ArrayInitialize(0.0);
      int begin = rates_total-barsWindow;
      for(int i=0;i<indicator_buffers;i++)
         PlotIndexSetInteger(i,PLOT_DRAW_BEGIN,begin);
      limit = barsWindow;
      Sleep(50);
     }
   int toCopy = 1;
   int Copied[indicator_symbols][5];
   //--- main loop for indicator calculation
   for(int i=limit;i>=0 && !IsStopped();i--)
     {
      //--- fill indicator buffers and assign to index buffers
      for(int a=0;a<indicator_symbols;a++)
         for(int b=0;b<5;b++)
           { 
            Copied[a][b] = symbol[a][b].CopyBuffer(b,time[i],toCopy);
            if(Copied[a][b]<=0)
               return(0);
            buffer[a][b].IndexBuffer[i] = symbol[a][b].SymbolBuffer[0]; 
           }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   //--- delete labels
   ObjectsDeleteAll(chartID,window,OBJ_LABEL);
   //--- release indicator handlers
   for(int a=0;a<indicator_symbols;a++)
      for(int b=0;b<5;b++)
         symbol[a][b].IndicatorRelease();
         //buffer[a][b].ArrayFree();
   return;
  }
//+-------------------------------------------------------------------+
//| Create coloured symbol labels                                     |
//+-------------------------------------------------------------------+
void CreateLabels()
  {
   //---names to show
   string Label[indicator_symbols*2];
   color  Color[indicator_symbols*2] = {clrNONE};
   for(int i=0,j=0,k=1;i<indicator_symbols;i++,j+=2,k+=2)
     { 
      Label[j] = StringSubstr(Symbol[i],0,3);  //Base currency name
      Color[j] = (color)PlotIndexGetInteger(i,PLOT_LINE_COLOR,0);
      Label[k] = StringSubstr(Symbol[i],3,3);  //Quote currency name
      Color[k] = (color)PlotIndexGetInteger(i,PLOT_LINE_COLOR,1);
     }
   //--- x coordinates
   int xStart = 4,xStart2 = 26;
   int xIncrement = 0;
   //--- y coordinates
   int yStart = 16;
   int yIncrement = 16;
   //--- create all labels
   for(int i=0,k=1;k<indicator_symbols*2;i+=2,k+=2)
     {
      string name1=string(window+i);
      string name2=string(window+k);
      ObjectCreate(name1,Label[i],xStart ,yStart,Color[i]);
      ObjectCreate(name2,Label[k],xStart2,yStart,Color[k]);
      xStart += xIncrement;
      yStart += yIncrement;
     }
  }
//+------------------------------------------------------------------+
//| Create label objects at coordinates                              |
//+------------------------------------------------------------------+
void ObjectCreate(string name,string label,int x,int y,int clr)
  {
   ObjectCreate(chartID,name,OBJ_LABEL,window,0,0);
   ObjectSetString(chartID,name,OBJPROP_TEXT,label);
   ObjectSetString(chartID,name,OBJPROP_FONT,"Arial Bold");
   ObjectSetInteger(chartID,name,OBJPROP_FONTSIZE,8);
   ObjectSetInteger(chartID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chartID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chartID,name,OBJPROP_YDISTANCE,y);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---