Excel based Indicator in Terminal

No Comments

Objective

We will try to show how to use an Excel based indicator in our Trading Terminal and visualize it. For this purphose we will use Metatrader4 but you could use any other Terminal too.

Comments

It is very easy to read from Excel using XLSgate fuctions. We have a demonstration code for you ready which can import a chart from excel and display on MT4 Chart. Same demonstration can be done in any of other Trading Platforms such as Multicharts or Tradestation etc.. Dont be shy to adapt same algoritm in other platforms.

Source Code

#include <xlsgate.mqh>
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot mainline
#property indicator_label1  "mainline"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDeepSkyBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot average
#property indicator_label2  "average"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDarkOrange
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- indicator buffers
double         mainlineBuffer[];
double         averageBuffer[];

//--- externals

extern   string   StartCell="G6";
extern   string   ExcelFile="c:\\temp\\my.xlsx";


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,mainlineBuffer);
   SetIndexBuffer(1,averageBuffer);   
   ArrayInitialize(mainlineBuffer, 0);
   ArrayInitialize(averageBuffer, 4);
   
//---
   if (!ExcelInit(WindowExpertName()) || !ExcelStart(ExcelFile))
      return(INIT_FAILED);
      

   EventSetMillisecondTimer(500);
   ExcelButtonAdd("Refresh","Refresh",10,10,100,30);          
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
bool done=false;
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[])
  {   
  if (rates_total-prev_calculated>0) ReadFromExcel();  
  return(rates_total);
  }
//+------------------------------------------------------------------+
void ReadFromExcel()
{
string cell=StartCell;      
int i=0;
while(true)
      {
         double cellValue1 = ExcelGetValue(cell);
         double cellValue2 = ExcelGetValue(ExcelColAdd(cell,+1));
         if (cellValue1==-1) break;
         mainlineBuffer[i]=cellValue1;
         averageBuffer[i]=cellValue2;               
         cell=ExcelRowAdd(cell,+1); i++;
      }
}
//+------------------------------------------------------------------+
void OnTimer() {   
   if (ExcelButtonIsClicked("Refresh")) ReadFromExcel();
}