ExcelExportLib
Download scriptThis tool serves as a bridge between your MetaTrader trading platform and Microsoft Excel, allowing for the automated transfer of data between the two applications. By utilizing a standard Windows communication protocol, it enables your trading software to "talk" to Excel in real-time without requiring complex manual exports.
The logic of this script is organized into three primary phases:
1. Establishing the Connection
Before any data can be moved, the script initiates a handshake with the Microsoft Excel application. It prepares the environment by identifying the specific software service it needs to interact with and targets a designated sheet within your workbook. If the connection is successful, it holds an open communication line; if it fails to find Excel or the specified sheet, it safely shuts down to prevent errors.
2. Data Transfer Execution
The core functionality lies in the ability to write specific pieces of information into precise locations. You can instruct the script to take a specific value (such as a price, profit figure, or trade volume) and "push" it into a exact cell address (e.g., "A1" or "B10"). The script handles the conversion of your trading data into a text format that Excel can instantly recognize and display.
3. Resource Management
To ensure your computer remains stable and the software runs smoothly, the script includes a cleanup routine. Once the data transfer is complete or the tool is closed, it automatically severs the connection and releases the system resources it borrowed from Windows. This prevents "ghost" processes from running in the background and ensures that Excel remains responsive for your other tasks.
Key Benefits for the User:
- Automation: Eliminates the need to manually copy and paste data from your trading charts into spreadsheets.
- Precision: Allows for specific data placement, making it easy to build organized automated logs ordashboards.
- Stability: Uses a standard initialization and deinitialization process to ensure the connection is opened and closed cleanly every time.
//+------------------------------------------------------------------+
//| ExcelExportLib.mq4 |
//| Copyright 2012, Stephen Ambatoding |
//| http://www.forexfactory.com/sangmane |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, Stephen Ambatoding"
#property link "http://www.forexfactory.com/sangmane"
#property library
#define APPCLASS_STANDARD 0x00000000
#define APPCMD_CLIENTONLY 0x00000010
#define DMLERR_NO_ERROR 0
#define XTYP_POKE 0x4090
#define CF_TEXT 1
#import "user32.dll"
int DdeInitializeA(int pidInst[], int pfnCallback, int afCmd, int ulRes);
int DdeUninitialize(int idInst);
int DdeCreateStringHandleA(int idInst, string psz, int iCodePage=0);
int DdeFreeStringHandle(int idInst, int iStrHandle);
int DdeConnect(int idInst, int iService, int iSheet, int pCC=0);
int DdeDisconnect(int iConv);
int DdeClientTransaction(string szData, int cbData, int iConv, int iItem, int wFmt, int wType, int dwTimeout, int pdwResult=0);
int idInst=0, iConv=0;
int ExcelInit(string Sheet)
{
int ptr[1] = {0};
int iRet = DdeInitializeA(ptr, 0, APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0);
if (iRet != DMLERR_NO_ERROR)
{
return(0);
}
idInst = ptr[0];
int iService = DdeCreateStringHandleA(idInst, "EXCEL", 0);
int iSheet = DdeCreateStringHandleA(idInst, Sheet, 0);
iConv = DdeConnect(idInst, iService, iSheet, 0);
DdeFreeStringHandle(idInst, iService);
DdeFreeStringHandle(idInst, iSheet);
if (iConv == 0)
{
DdeUninitialize(idInst);
return(0);
}
return(1);
}
void ExcelDeinit()
{
if (iConv > 0)
DdeDisconnect(iConv);
if (idInst > 0)
DdeUninitialize(idInst);
}
int ExcelWrite(string CellAddress, string CellValue)
{
int iItem = DdeCreateStringHandleA(idInst, CellAddress, 0);
int iRet = DdeClientTransaction(CellValue, StringLen(CellValue)+1, iConv, iItem, CF_TEXT, XTYP_POKE, 1000, 0);
DdeFreeStringHandle(idInst, iItem);
return (iRet);
}
//+------------------------------------------------------------------+
Comments