Publish Data with Arrays

No Comments

Current Situation

Assume you need to export huge amount of data fastest way, you would have hard time if you had used ExcelSetValue, ExcelSetString because both functions are working data by data. This means to send 1000 data, functions will be used 1000 times. Even in fastest environments this will require processor time and resources.


We recommend you to use ExcelSetFromArray, ExcelSetFromArrayV, ExcelSetFromArrayH functions. These Array functions can publish data within 1 function call. ExcelSetFromArray is able to send double/integer values to excel with single call. ExcelSetFromArray required 2D array to work with. ArraySetFromArrayV is designed to work with 1D arrays and data will be deployed vertically, ArraySetFromArrayH does same horizontally.

The Problematic

Some of Clients such as Tradestation,Multicharts does not support 2D Arrays, nor 1D Arrays with DLLs. This is not XLSgate restriction, this is Trading-Application-Side restriction.


We have implemented alternative functions for Tradestation,Multichart clients. Pleae use ExcelSetFromParsedV and ExcelSetFromParsedH functions, they are well documented and can speed up publishing numbers by x1000 easly.


With ExcelSetFromArrayV/H you need to pass the amount of data as last parameter. This is very important, this is required due to DLL communication rules. If you have an array with 300 items and you pass different count to these functions, this may lead to crashes etc. Please use these functions carefully.

Metatrader4 Usage

// Lets export close data to 1D array
// we will export last 300 close prices
double M30[300]; int c=0;
  c++; if (c>=ArraySize(M30)) break;
// Single call to export 300 Data to Excel beginnning by D5, Vertically 300 columns
// Single call to export 300 Data to Excel beginnning by D5, Horizontally 300 rows

Multicharts.NET Usage

Here is very basic Multicharts.NET Example to export Close price with single call.

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;
using System.Runtime.InteropServices;

namespace PowerLanguage.Strategy {
	public class FastExporter : SignalObject {
		public FastExporter(object _ctx):base(_ctx){}				
		private int idx=0;
		private bool Excel = false;
		private double[] ToExport = new Double[300];
		protected override void Create() {		} 
		protected override void StartCalc() {
			// initialize XLSGate
			if (ExcelInit("From MC.NET") && ExcelStart("")) {
				ExcelSetString("A1","Running XLSgate v"+ExcelGateVersion());
		protected override void CalcBar(){
			if (Excel)
				if (idx<ToExport.Length)
				if (Bars.LastBarOnChart) 
		[DllImport("xlsgate-a64.dll")]  public static extern bool ExcelStart(string Filename);
		[DllImport("xlsgate-a64.dll")]  public static extern bool ExcelSetString(string Address,string Value);
        [DllImport("xlsgate-a64.dll")]  public static extern bool ExcelInit(string ClientName);
		[DllImport("xlsgate-a64.dll")]  public static extern int ExcelGateVersion();
        [DllImport("xlsgate-a64.dll")]  public static extern bool ExcelSetFromArrayH( string Address, [MarshalAs(UnmanagedType.LPArray)] double[] Data, int Count);
        [DllImport("xlsgate-a64.dll")]  public static extern bool ExcelSetFromArrayV( string Address, [MarshalAs(UnmanagedType.LPArray)] double[] Data, int Count);		

Array Matrix

Function ExcelSetFromArray is able to publish data from 2D Array. Advantage is that you could publish 100 columns x 100 rows with single call! Unfortunately it is a bit tricky to use this function

#property strict
#include <xlsgate.mqh>
double OHLC[,4];
int OnInit()
  if (ExcelInit("MT4"+Symbol()))
     if (ExcelStart(""))
         // Generate OHLC Data from Last 100 Bars
         for(int i=0;i<100;i++)
            double H = High[i];
            double L = Low[i];
            double O = Open[i];
            double C = Close[i];
         // Publish data 4 columns x 100 rows
void OnDeinit(const int reason) {   }
void OnTick()  {   }


Once the concept has been understood it is very easy to use Array Data Export functions of XLSgate. Ninjatrader should also support Array functions, you can review MC.NET example to adapt it to Ninjatrader.