How can I make this more efficient?

 

I have developed a correlation matrix however I know it can be made more efficient. Some 'cells' do not load, and i would like it to update every tick. What methods could improve the efficiency of it?

#include <Math\Stat\Math.mqh>

enum correlation_method
  {
   Kendall,
   Pearson,
   Spearman
  };
  
input correlation_method method = Pearson;
input int period = 7;

//+------------------------------------------------------------------+
void OnTick()
  {
   create_table();
   ChartSetInteger(0,CHART_COLOR_BACKGROUND,clrWhite);
   ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,clrWhite);
   ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,clrWhite);
   ChartSetInteger(0,CHART_COLOR_CHART_DOWN,clrWhite);
   ChartSetInteger(0,CHART_COLOR_CHART_LINE,clrWhite);
   ChartSetInteger(0,CHART_COLOR_CHART_UP,clrWhite);
  }

//+------------------------------------------------------------------
color text_color(double value)
  {
   color col = value > 75 ? clrRed : value < -75 ? clrRed : value > -75 && value < -25 ? clrOrange : value < 75 && value > 25 ? clrOrange : clrGreen;
   return col;
  }
  
//+------------------------------------------------------------------+
void create_table()
  {
   create_label("Instrument",0,0,"Inst.",clrBlack,0);
   create_label("Col1",1,0,"AUDCAD",clrBlack,0);
   create_label("Col2",2,0,"AUDCHF",clrBlack,0);
   create_label("Col3",3,0,"AUDJPY",clrBlack,0);
   create_label("Col4",4,0,"AUDNZD",clrBlack,0);
   create_label("Col5",5,0,"AUDUSD",clrBlack,0);
   create_label("Col6",6,0,"CADCHF",clrBlack,0);
   create_label("Col7",7,0,"CADJPY",clrBlack,0);
   create_label("Col8",8,0,"CHFJPY",clrBlack,0);
   create_label("Col9",9,0,"EURAUD",clrBlack,0);
   create_label("Col10",10,0,"EURCAD",clrBlack,0);
   create_label("Col11",11,0,"EURCHF",clrBlack,0);
   create_label("Col12",12,0,"EURGBP",clrBlack,0);
   create_label("Col13",13,0,"EURJPY",clrBlack,0);
   create_label("Col14",14,0,"EURNZD",clrBlack,0);
   create_label("Col15",15,0,"EURUSD",clrBlack,0);
   create_label("Col16",16,0,"GBPAUD",clrBlack,0);
   create_label("Col17",17,0,"GBPCAD",clrBlack,0);
   create_label("Col18",18,0,"GBPCHF",clrBlack,0);
   create_label("Col19",19,0,"GBPJPY",clrBlack,0);
   create_label("Col20",20,0,"GBPNZD",clrBlack,0);
   create_label("Col21",21,0,"GBPUSD",clrBlack,0);
   create_label("Col22",22,0,"NZDCAD",clrBlack,0);
   create_label("Col23",23,0,"NZDCHF",clrBlack,0);
   create_label("Col24",24,0,"NZDJPY",clrBlack,0);
   create_label("Col25",25,0,"NZDUSD",clrBlack,0);
   create_label("Col26",26,0,"USDCAD",clrBlack,0);
   create_label("Col27",27,0,"USDCHF",clrBlack,0);
   create_label("Col28",28,0,"USDJPY",clrBlack,0);
   
   create_label("Row1",0,1,"AUDCAD",clrBlack,0);
   create_label("Row2",0,2,"AUDCHF",clrBlack,0);
   create_label("Row3",0,3,"AUDJPY",clrBlack,0);
   create_label("Row4",0,4,"AUDNZD",clrBlack,0);
   create_label("Row5",0,5,"AUDUSD",clrBlack,0);
   create_label("Row6",0,6,"CADCHF",clrBlack,0);
   create_label("Row7",0,7,"CADJPY",clrBlack,0);
   create_label("Row8",0,8,"CHFJPY",clrBlack,0);
   create_label("Row9",0,9,"EURAUD",clrBlack,0);
   create_label("Row10",0,10,"EURCAD",clrBlack,0);
   create_label("Row11",0,11,"EURCHF",clrBlack,0);
   create_label("Row12",0,12,"EURGBP",clrBlack,0);
   create_label("Row13",0,13,"EURJPY",clrBlack,0);
   create_label("Row14",0,14,"EURNZD",clrBlack,0);
   create_label("Row15",0,15,"EURUSD",clrBlack,0);
   create_label("Row16",0,16,"GBPAUD",clrBlack,0);
   create_label("Row17",0,17,"GBPCAD",clrBlack,0);
   create_label("Row18",0,18,"GBPCHF",clrBlack,0);
   create_label("Row19",0,19,"GBPJPY",clrBlack,0);
   create_label("Row20",0,20,"GBPNZD",clrBlack,0);
   create_label("Row21",0,21,"GBPUSD",clrBlack,0);
   create_label("Row22",0,22,"NZDCAD",clrBlack,0);
   create_label("Row23",0,23,"NZDCHF",clrBlack,0);
   create_label("Row24",0,24,"NZDJPY",clrBlack,0);
   create_label("Row25",0,25,"NZDUSD",clrBlack,0);
   create_label("Row26",0,26,"USDCAD",clrBlack,0);
   create_label("Row27",0,27,"USDCHF",clrBlack,0);
   create_label("Row28",0,28,"USDJPY",clrBlack,0);
   
   create_column(1,"AUDCAD");
   create_column(2,"AUDCHF");
   create_column(3,"AUDJPY");
   create_column(4,"AUDNZD");
   create_column(5,"AUDUSD");
   create_column(6,"CADCHF");
   create_column(7,"CADJPY");
   create_column(8,"CHFJPY");
   create_column(9,"EURAUD");
   create_column(10,"EURCAD");
   create_column(11,"EURCHF");
   create_column(12,"EURGBP");
   create_column(13,"EURJPY");
   create_column(14,"EURNZD");
   create_column(15,"EURUSD");
   create_column(16,"GBPAUD");
   create_column(17,"GBPCAD");
   create_column(18,"GBPCHF");
   create_column(19,"GBPJPY");
   create_column(20,"GBPNZD");
   create_column(21,"GBPUSD");
   create_column(22,"NZDCAD");
   create_column(23,"NZDCHF");
   create_column(24,"NZDJPY");
   create_column(25,"NZDUSD");
   create_column(26,"USDCAD");
   create_column(27,"USDCHF");
   create_column(28,"USDJPY");
  }
  
//+------------------------------------------------------------------+
void create_column(int column_no, string base)
  {
   create_label(IntegerToString(column_no)+"1",column_no,1,DoubleToString(correlation(base,"AUDCAD"),2)+"%",text_color(correlation(base,"AUDCAD")),0);
   create_label(IntegerToString(column_no)+"2",column_no,2,DoubleToString(correlation(base,"AUDCHF"),2)+"%",text_color(correlation(base,"AUDCHF")),0);
   create_label(IntegerToString(column_no)+"3",column_no,3,DoubleToString(correlation(base,"AUDJPY"),2)+"%",text_color(correlation(base,"AUDJPY")),0);
   create_label(IntegerToString(column_no)+"4",column_no,4,DoubleToString(correlation(base,"AUDNZD"),2)+"%",text_color(correlation(base,"AUDNZD")),0);
   create_label(IntegerToString(column_no)+"5",column_no,5,DoubleToString(correlation(base,"AUDUSD"),2)+"%",text_color(correlation(base,"AUDUSD")),0);
   create_label(IntegerToString(column_no)+"6",column_no,6,DoubleToString(correlation(base,"CADCHF"),2)+"%",text_color(correlation(base,"CADCHF")),0);
   create_label(IntegerToString(column_no)+"7",column_no,7,DoubleToString(correlation(base,"CADJPY"),2)+"%",text_color(correlation(base,"CADJPY")),0);
   create_label(IntegerToString(column_no)+"8",column_no,8,DoubleToString(correlation(base,"CHFJPY"),2)+"%",text_color(correlation(base,"CHFJPY")),0);
   create_label(IntegerToString(column_no)+"9",column_no,9,DoubleToString(correlation(base,"EURAUD"),2)+"%",text_color(correlation(base,"EURAUD")),0);
   create_label(IntegerToString(column_no)+"10",column_no,10,DoubleToString(correlation(base,"EURCAD"),2)+"%",text_color(correlation(base,"EURCAD")),0);
   create_label(IntegerToString(column_no)+"11",column_no,11,DoubleToString(correlation(base,"EURCHF"),2)+"%",text_color(correlation(base,"EURCHF")),0);
   create_label(IntegerToString(column_no)+"12",column_no,12,DoubleToString(correlation(base,"EURGBP"),2)+"%",text_color(correlation(base,"EURGBP")),0);
   create_label(IntegerToString(column_no)+"13",column_no,13,DoubleToString(correlation(base,"EURJPY"),2)+"%",text_color(correlation(base,"EURJPY")),0);
   create_label(IntegerToString(column_no)+"14",column_no,14,DoubleToString(correlation(base,"EURNZD"),2)+"%",text_color(correlation(base,"EURNZD")),0);
   create_label(IntegerToString(column_no)+"15",column_no,15,DoubleToString(correlation(base,"EURUSD"),2)+"%",text_color(correlation(base,"EURUSD")),0);
   create_label(IntegerToString(column_no)+"16",column_no,16,DoubleToString(correlation(base,"GBPAUD"),2)+"%",text_color(correlation(base,"GBPAUD")),0);
   create_label(IntegerToString(column_no)+"17",column_no,17,DoubleToString(correlation(base,"GBPCAD"),2)+"%",text_color(correlation(base,"GBPCAD")),0);
   create_label(IntegerToString(column_no)+"18",column_no,18,DoubleToString(correlation(base,"GBPCHF"),2)+"%",text_color(correlation(base,"GBPCHF")),0);
   create_label(IntegerToString(column_no)+"19",column_no,19,DoubleToString(correlation(base,"GBPJPY"),2)+"%",text_color(correlation(base,"GBPJPY")),0);
   create_label(IntegerToString(column_no)+"20",column_no,20,DoubleToString(correlation(base,"GBPNZD"),2)+"%",text_color(correlation(base,"GBPNZD")),0);
   create_label(IntegerToString(column_no)+"21",column_no,21,DoubleToString(correlation(base,"GBPUSD"),2)+"%",text_color(correlation(base,"GBPUSD")),0);
   create_label(IntegerToString(column_no)+"22",column_no,22,DoubleToString(correlation(base,"NZDCAD"),2)+"%",text_color(correlation(base,"NZDCAD")),0);
   create_label(IntegerToString(column_no)+"23",column_no,23,DoubleToString(correlation(base,"NZDCHF"),2)+"%",text_color(correlation(base,"NZDCHF")),0);
   create_label(IntegerToString(column_no)+"24",column_no,24,DoubleToString(correlation(base,"NZDJPY"),2)+"%",text_color(correlation(base,"NZDJPY")),0);
   create_label(IntegerToString(column_no)+"25",column_no,25,DoubleToString(correlation(base,"NZDUSD"),2)+"%",text_color(correlation(base,"NZDUSD")),0);
   create_label(IntegerToString(column_no)+"26",column_no,26,DoubleToString(correlation(base,"USDCAD"),2)+"%",text_color(correlation(base,"USDCAD")),0);
   create_label(IntegerToString(column_no)+"27",column_no,27,DoubleToString(correlation(base,"USDCHF"),2)+"%",text_color(correlation(base,"USDCHF")),0);
   create_label(IntegerToString(column_no)+"28",column_no,28,DoubleToString(correlation(base,"USDJPY"),2)+"%",text_color(correlation(base,"USDJPY")),0);
  }
  
//+------------------------------------------------------------------+
bool create_label(const string            name="Label",
                  const int               column=0,
                  const int               row=0,
                  const string            text="Label",
                  const int               col=clrBlack,
                  const double            angle = 45)
  {
   ResetLastError();

   if(!ObjectCreate(0,name,OBJ_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create text label! Error code = ",GetLastError());
      return(false);
     }
   int x = (column*50) + 40;
   int y = (row*15) + 40;
   
   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetString(0,name,OBJPROP_TEXT,text);
   ObjectSetString(0,name,OBJPROP_FONT,"Tahoma");
   ObjectSetInteger(0,name,OBJPROP_FONTSIZE,7);
   ObjectSetDouble(0,name,OBJPROP_ANGLE,angle);
   ObjectSetInteger(0,name,OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
   ObjectSetInteger(0,name,OBJPROP_COLOR,col);
   ObjectSetInteger(0,name,OBJPROP_BACK,false);
   ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,name,OBJPROP_SELECTED,false);
   ObjectSetInteger(0,name,OBJPROP_HIDDEN,false);
   ObjectSetInteger(0,name,OBJPROP_ZORDER,1);
   return(true);
   ChartRedraw();
  }
  
//+------------------------------------------------------------------+
bool create_rectangle(const string           name="RectLabel",         // label name
                      const int              x=0,                      // X coordinate
                      const int              y=0,                      // Y coordinate
                      const int              width=50,                 // width
                      const int              height=18)                // priority for mouse click
  {
   ResetLastError();
   
   if(!ObjectCreate(0,name,OBJ_RECTANGLE_LABEL,0,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create a rectangle label! Error code = ",GetLastError());
      return(false);
     }

   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(0,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(0,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clrGreen);
   ObjectSetInteger(0,name,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
   ObjectSetInteger(0,name,OBJPROP_COLOR,clrWhite);
   ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_SOLID);
   ObjectSetInteger(0,name,OBJPROP_WIDTH,1);
   ObjectSetInteger(0,name,OBJPROP_BACK,false);
   ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,name,OBJPROP_SELECTED,false);
   ObjectSetInteger(0,name,OBJPROP_HIDDEN,false);
   ObjectSetInteger(0,name,OBJPROP_ZORDER,0);
   return(true);
  }
  
//+------------------------------------------------------------------+  
double correlation(string symbol1, string symbol2)
  {
   double buf1[],buf2[];
   ArrayInitialize(buf1,0);
   ArrayInitialize(buf2,0);
   int copied1=CopyClose(symbol1,Period(),1,period,buf1);
   int copied2=CopyClose(symbol2,Period(),1,period,buf2);
      
   double correlation;
   
   double output = 0;
   
   if(method == Pearson && MathCorrelationPearson(buf1,buf2,correlation))
      output = correlation;
   else
   if(method == Spearman && MathCorrelationSpearman(buf1,buf2,correlation))
      output = correlation;
   else
   if(method == Kendall && MathCorrelationKendall(buf1,buf2,correlation))
      output = correlation;
      
   return output*100;
  }
 
Benjamin David Hardman:  have developed a correlation matrix however I know it can be made more efficient. Some 'cells' do not load, and i would like it to update every tick. What methods could improve the efficiency of it?
create_label(IntegerToString(column_no)+"1",column_no,1,DoubleToString(correlation(base,"AUDCAD"),2)+"%",text_color(correlation(base,"AUDCAD")),0);

  1. Call your functions once, save the result in a variable, use the variable multiple times.

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

 
William Roeder #:
  1. Call your functions once, save the result in a variable, use the variable multiple times.

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019)

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

Thankyou very much!