MySQL





//+------------------------------------------------------------------+
//|                                                        MySQL.mq4 |
//|                            Copyright © 2007, GwadaTradeBoy Corp. |
//|                                            racooni_1975@yahoo.fr |
//+------------------------------------------------------------------+
//|                                                          Kalenzo |
//|                                      bartlomiej.gorski@gmail.com |
//+------------------------------------------------------------------+
//|                                              AKA : Administrator |
//|                                      creditbanc@worldnet.att.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, GwadaTradeBoy Corp."
#property link      "racooni_1975@yahoo.fr"
#property copyright "Kalenzo"
#property link      "bartlomiej.gorski@gmail.com"
#include <mysql.mqh>
/*
//+------------------------------------------------------------------+
//|                          **** REQUIREMENTS ****                  |
//|                                                                  |
//| 1. Make the inputs for the database "extern".  - DONE            |
//| 2. Add error checking and reconnect if the Indicator loses       |
//| connection with the database.                  - WAITING         |
//| 3. Time shift, I need to shift the DateTime sent to the database |
//| by +/- x hours to adjust to GMT time.          - DONE            |
//|    BrokerTZ  - Timezone of your Broker (in hours from GMT)       |
//|    LocalTz   - Your timezone in hours from GMT                   |
//+------------------------------------------------------------------+
*/
#property indicator_chart_window

//---- Variables Externes - Inputs Variables
extern string  host = "localhost";
extern string  user = "root";
extern string  password = "";
extern string  DB = "MetaTrader";
extern string  table = "mqdata";
extern int     port = 3306;
extern string  socket = "";
extern bool    UseGMT = False;
extern double  BrokerTZ = 2;
extern int     Waiting = 10;  // Temps en seconde
extern int     MaxWait = 60;  // Temps en minute
extern bool    Alarm = False;

//---- Variables
string IndicName, MyErrMsg;
int mysql, MyErr, res, err;
int clientflag=0;
int length=0;
string query="";
datetime BrokerTime, GMT;
int Years, Months, Days, Hours, Minutes, Secondes;
bool connect = False;
int iteration = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
//---- indicators
      IndicName = "MetaTrader -> MySQL";
      mysql = mysql_init(mysql);
      res=mysql_real_connect(mysql,host,user,password,DB,port,socket,clientflag);
      err=GetLastError();
      if (mysql!=0)
         {
            Print(IndicName,"Allocated");
            Comment(IndicName,"Allocated");
         }
      if (res==mysql) 
         {
            Print(IndicName,"Connected");
            Comment(IndicName,"Connected");
         }
      else 
         {
            Print(IndicName,"Error=",mysql," ",mysql_errno(mysql)," ");
            Comment(IndicName,"Error=",mysql," ",mysql_errno(mysql)," ");
         }
//----
      return(0);
   }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
   {
//----
      mysql_close(mysql);
//----
      return(0);
   }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
      int    counted_bars=IndicatorCounted();
//---- GMT Calculation
      BrokerTime = TimeCurrent();
      GMT = BrokerTime - (BrokerTZ)*3600;
//---- Test UseGMT
      if (UseGMT)
         {
            Years = TimeYear(GMT);
            Months = TimeMonth(GMT);
            Days = TimeDay(GMT);
            Hours = TimeHour(GMT);
            Minutes = TimeMinute(GMT);
            Secondes = TimeSeconds(GMT);
         }
      else
         {
            Years = Year();
            Months = Month();
            Days = Day();
            Hours = Hour();
            Minutes = Minute();
            Secondes = Seconds();
         }
//---- Test 
//---- SQL Query
      query=StringConcatenate("insert into ",table," set symbol=\'",Symbol(),"\',open="+Open[0]+",high="+High[0]+",low="+Low[0]+",close="+Close[0]+",ask="+Ask+",bid="+Bid+",ts=\'",Years,"-",Months,"-",Days," ",Hours,":",Minutes,":",Secondes,"\'");
      length=StringLen(query);
      mysql_real_query(mysql,query,length);
      MyErr=mysql_errno(mysql);
      //if (MyErr>0)Print("error=",MyErr);
//---- Test MySQL Connection
//If the indicator is disconnected from the database then attempt to reconnect every x number of seconds.
      if (
      (MyErr == 2003)         // MySQL Error : Can't connect to MySQL server on '%s'
      || (MyErr == 2006)      // MySQL Error : MySQL server has gone away
      || (MyErr == 2013)      // MySQL Error : Lost connection to MySQL server during query
      )
         {
            connect = False;
            if (MyErr == 2003)
               MyErrMsg = "Can\'t connect to MySQL server : "+host;
            else
               if (MyErr == 2006)
                  MyErrMsg = "MySQL server has gone away";
               else
                  if (MyErr == 2013)
                     MyErrMsg = "Lost connection to MySQL server during query";
            Print(IndicName,"Connection Lost - ", MyErrMsg);
            Comment(IndicName,"Connection Lost - ", MyErrMsg);
            if (Alarm)
               {
                  Alert(IndicName," - Connection Lost - ", MyErrMsg);
               }
            while((!connect) || (iteration * Waiting > MaxWait))  // 
               {
                  Sleep(Waiting * 1000);                          // Intervalle en milliseconde
                  Print("Connection not restored ", iteration * Waiting, " seconds passed");
                  mysql = mysql_init(mysql);
                  res=mysql_real_connect(mysql,host,user,password,DB,port,socket,clientflag);
                  err=GetLastError();
                  if (res==mysql) 
                     {
                        Print(IndicName,"Connection restored");
                        Comment (IndicName,"Connection restored");
                        if (Alarm)
                           {
                              Alert(IndicName," - Connection restored");
                           }
                        connect = True;
                     }
                  if(connect)
                     {
                        mysql_real_query(mysql,query,length);
                     }
                  iteration++;
               }
            Print(IndicName,"Error=",mysql," ",mysql_errno(mysql)," ");
            Comment (IndicName,"Error=",mysql," ",mysql_errno(mysql)," ");
            if (Alarm)
               {
                  Alert(IndicName," - Error=",mysql," ",mysql_errno(mysql)," ");
               }

/*      
      if (MyErr>0)      // MySQL Error code for not connected
         {
            mysql = mysql_init(mysql);
            res=mysql_real_connect(mysql,host,user,password,DB,port,socket,clientflag);
            err=GetLastError();
            if (mysql!=0) 
               Print("allocated");
            if (res==mysql) 
               {
                  Print("connected");
                  mysql_real_query(mysql,query,length);
               }
            else 
               Print("error=",mysql," ",mysql_errno(mysql)," ");
*/
         }     
//----
      return(0);
   }
//+------------------------------------------------------------------+



Sample



image not available


Analysis



Market Information Used:

Series array that contains close prices for each bar
Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar
Series array that contains open prices of each bar


Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:

Other Features:

It issuies visual alerts to the screen