Get started with Metatrader4 & Excel

No Comments


We will introduce you to XLSGate Programming with Metatrader4. This instruction is basic and designed to get started rapidly.

Step 1 : Import DLL Functions

We have provided xlsgate.mqh file in Resources\Metatrader4\ folder. Copy this file into \MQL4\includes\ folder and import it with #import directive from MQL

#import <xlsgate.mqh>

Theoretically you could also import functions manually without including provided .mqh file, but this way is not very convenient. We keep updating xlsgate.mqh file during time. Make sure to copy it into your Includes folder whenever you update your XLSGate package.

Step 2 : Initialisation

To initialize we need to call ExcelInit and ExcelStart functions. Both must return TRUE. This is very important to capture. ExcelInit requires a name of client. This name should be unique. Initialisation should be places inside OnInit() function because these two functions needs to be called only once.

bool Excel=false;
int OnInit()
  if (ExcelInit("MT4"+Symbol()))
     if (ExcelStart(""))
         Print("Excel has been initialized properly");

Logics like adding new Sheet would also go inside OnInit after ExcelStart

Step 3 : Publish Data

Publishing data usually happens inside OnTick() function but also other scenarios are possible. To Publish data you would use one of ExcelSetValue, ExcelSetString, ExcelSetFromArray, ExcelSetFromArrayV, ExcelSetFromArrayH etc. Here is very basic code to export current Bid, Ask, Spread to Chart:

void OnTick()
   if (Excel)
      ExcelSetValue("A1",Ask); ExcelSetString("B1","Ask");
      ExcelSetValue("A2",Bid); ExcelSetString("B2","Bid");
      ExcelSetValue("A3",(Bid-Ask)/Point); ExcelSetString("B3","Spread");

Step 4 : DeInitialisation

You dont need to deinit anything in XLSApp