TicksInMySQL
Download scriptThis script serves as a bridge between your MetaTrader 4 trading platform and an external database management system. Its primary purpose is to automatically record real-time snapshots of your account status into a permanent storage location for later review or analysis.
Core Functionality
The tool functions by capturing specific financial data points from your trading account at every "tick" (the smallest price movement in the market). Instead of keeping this information only within the trading platform, it pushes this data out to a MySQL database.
How the Process Works
1. Establishing a Connection When you first launch the script, it performs a "handshake" with your database server. It looks for a specific location (host), username, and password provided in its settings to open a communication line. If successful, it confirms the connection; if it fails, it alerts you through the platform's log.
2. Data Gathering Once the connection is active, the script begins monitoring your account. Every time the market moves, it gathers seven specific pieces of information:
- Margin & Free Margin: The amount of capital currently locked in trades and what is available for new positions.
- Timestamp: The exact date and second the data was recorded.
- Ask & Bid Prices: The current buying and selling prices for your active currency or asset.
- Symbol: The specific name of the instrument you are trading (e.g., EURUSD).
- Equity: The total value of your account at that exact moment.
3. Automated Recording The script takes all these gathered values and formats them into a standardized command. It then sends this command to the database to "insert" a new row of information. This happens automatically and continuously, ensuring that you have a second-by-second history of your account's performance.
4. Clean Shutdown When you stop the script or close the platform, it is designed to properly "hang up" the connection with the database. This ensures that the communication line is closed cleanly, preventing any data corruption or lingering connections.
Summary for the User
Essentially, this tool acts like an automated journalist for your trading account. It constantly watches your balance and market prices, writing down every single update into a permanent ledger (the database) so you can track your trading history with high precision.
//+------------------------------------------------------------------+
//| TicksInMySQL.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#import "libmysql.dll"
int mysql_init(int db);
int mysql_errno(int TMYSQL);
int mysql_real_connect(int TMYSQL, string host, string user, string password,
string DB,int port,int socket,int clientflag);
int mysql_real_query(int TMSQL, string query, int length);
void mysql_close(int TMSQL);
#import
int mysql;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
mysql = mysql_init(mysql);
if(mysql != 0)
Print("allocated");
string host = "localhost";
string user = "user";
string password = "pwd";
string DB = "mt4";
int clientflag = 0;
int port = 3306;
string socket = "";
int res = mysql_real_connect(mysql,host,user,password,DB,port,socket,clientflag);
int err = GetLastError();
if(res == mysql)
Print("connected");
else
Print("error=", mysql, " ", mysql_errno(mysql), " ");
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
mysql_close(mysql);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
string query = "";
int length = 0;
query = StringConcatenate("insert into ticks(margin,freemargin,date,ask,bid,symbol,equity) values(",
AccountMargin(), ",", AccountFreeMargin(), ",\"",
TimeToStr(CurTime(), TIME_DATE|TIME_SECONDS), "\",",
NormalizeDouble(Ask, 4), ",", NormalizeDouble(Bid, 4),
",\"", Symbol(), "\",", AccountEquity(), ");");
length = StringLen(query);
mysql_real_query(mysql, query, length);
int myerr = mysql_errno(mysql);
if(myerr > 0)
Print("error=",myerr);
}
//+------------------------------------------------------------------+
Comments