#include "ThostFtdcMdApi.h"
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <sstream>
char dateParam[30]; // global param of date
// just going to input the general details and not the port numbers
struct connection_details
{
char *server;
char *user;
char *password;
char *database;
};
MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
// first of all create a mysql instance and initialize the variables within
MYSQL *connection = mysql_init(NULL);
// connect to the database with the details attached.
if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
printf("Conection error : %s\n", mysql_error(connection));
exit(1);
}
return connection;
}
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
// send the query to the database
if (mysql_query(connection, sql_query))
{
printf("MySQL query error : %s\n", mysql_error(connection));
exit(1);
}
return mysql_use_result(connection);
}
class CMduserHandler : public CThostFtdcMdSpi
{
private:
CThostFtdcMdApi *m_mdApi;
MYSQL *conn; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row; // the results row (line by line)
struct connection_details mysqlD;
public:
CMduserHandler()
{
struct connection_details mysqlD;
mysqlD.server = "localhost"; // where the mysql database is
mysqlD.user = "root"; // the root user of mysql
mysqlD.password = "Piercing77#"; // the password of the root user in mysql
mysqlD.database = "shfuture"; // the databse to pick
// connect to the mysql database
conn = mysql_connection_setup(mysqlD);
}
~CMduserHandler()
{
mysql_free_result(res);
/* clean up the database link */
mysql_close(conn);
}
void connect()
{
//创建并初始化API
m_mdApi = CThostFtdcMdApi::CreateFtdcMdApi("", true, true);
m_mdApi->RegisterSpi(this);
m_mdApi->RegisterFront("tcp://218.28.130.102:41413"); //real
//m_mdApi->RegisterFront("tcp://210.5.151.249:51205"); // Pan Hou
m_mdApi->Init();
}
//登陆
void login()
{
CThostFtdcReqUserLoginField t = {0};
while (m_mdApi->ReqUserLogin(&t, 1)!=0) sleep(1);
}
// 订阅行情
void subscribe()
{
char **ppInstrument=new char * [50];
ppInstrument[0] = "rb1910";
//ppInstrument[1] = "jm1901";
// ppInstrument[1] = "IF1812";
ppInstrument[1] = "IF1909";
ppInstrument[2] = "IC1909";
ppInstrument[3] = "ni1909";
while (m_mdApi->SubscribeMarketData(ppInstrument, 4)!=0) sleep(1);
}
//接收行情
void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData)
{
//printf("%d,%d,%d",(int)pDepthMarketData->LastPrice,(int)pDepthMarketData->BidPrice1,(int)pDepthMarketData->BidVolume1);
// printf("\n");
//printf("%d,%d",(int)pDepthMarketData->AskPrice1,(int)pDepthMarketData->AskVolume1);
// printf("\n");
//printf("%s %s",(char*)pDepthMarketData->TradingDay,(char*)pDepthMarketData->UpdateTime);
// MYSQL *conn; // the connection
// MYSQL_RES *res; // the results
// MYSQL_ROW row; // the results row (line by line)
//struct connection_details mysqlD;
//mysqlD.server = "localhost"; // where the mysql database is
//mysqlD.user = "root"; // the root user of mysql
//mysqlD.password = "Piercing77#"; // the password of the root user in mysql
//mysqlD.database = "shfuture"; // the databse to pick
// connect to the mysql database
//conn = mysql_connection_setup(mysqlD);
if((pDepthMarketData->BidVolume1>1000000)||(pDepthMarketData->BidVolume1<-1000000))
return;
if((pDepthMarketData->BidPrice1>1000000)||(pDepthMarketData->BidPrice1<-1000000))
return;
std::string str((char*)pDepthMarketData->ActionDay);
std::string strtime((char*)pDepthMarketData->UpdateTime);
std::string strDayTime = str.substr(0,4) + '-' + str.substr(4,2) + '-' + str.substr(6,2) + ' ' + strtime ;
//combine update char* example : "INSERT INTO rb1 VALUES('1950-11-12 00:00:00', 100,200,300,400)"
char updatestring[256];
memset(updatestring, '\0', sizeof(updatestring));
if(strcmp (pDepthMarketData->InstrumentID,"rb1910")==0)
{
strcpy(updatestring,"INSERT INTO rb1910_");
strncat(updatestring,dateParam, sizeof(updatestring));
strncat(updatestring, " VALUES('", sizeof(updatestring));
}
else if(strcmp (pDepthMarketData->InstrumentID,"IF1909")==0)
{
strcpy(updatestring,"INSERT INTO if1909_");
strncat(updatestring,dateParam, sizeof(updatestring));
strncat(updatestring, " VALUES('", sizeof(updatestring));
}
else if(strcmp (pDepthMarketData->InstrumentID,"IC1909")==0)
{
strcpy(updatestring,"INSERT INTO ic1909_");
strncat(updatestring,dateParam, sizeof(updatestring));
strncat(updatestring, " VALUES('", sizeof(updatestring));
}
else if(strcmp (pDepthMarketData->InstrumentID,"ni1909")==0)
{
strcpy(updatestring,"INSERT INTO ni1909_");
strncat(updatestring,dateParam, sizeof(updatestring));
strncat(updatestring, " VALUES('", sizeof(updatestring));
}
else
{
//strcpy(updatestring,"INSERT INTO if20181221 VALUES('");
}
strncat(updatestring, strDayTime.c_str(), sizeof(updatestring));
strncat(updatestring, "',", sizeof(updatestring));
std::ostringstream ostr;
ostr << pDepthMarketData->LastPrice;
std::string sLastPrice = ostr.str();
strncat(updatestring, sLastPrice.c_str(), sizeof(updatestring));
strncat(updatestring, ",", sizeof(updatestring));
std::ostringstream ostr1;
ostr1 << pDepthMarketData->BidPrice1;
std::string sBidPrice1 = ostr1.str();
strncat(updatestring, sBidPrice1.c_str(), sizeof(updatestring));
strncat(updatestring, ",", sizeof(updatestring));
std::ostringstream ostr2;
ostr2 << (int)pDepthMarketData->BidVolume1;
std::string sBidVolume1 = ostr2.str();
strncat(updatestring, sBidVolume1.c_str(), sizeof(updatestring));
strncat(updatestring, ",", sizeof(updatestring));
std::ostringstream ostr3;
ostr3 << pDepthMarketData->AskPrice1;
std::string sAskPrice1 = ostr3.str();
strncat(updatestring, sAskPrice1.c_str(), sizeof(updatestring));
strncat(updatestring, ",", sizeof(updatestring));
std::ostringstream ostr4;
ostr4 << (int)pDepthMarketData->AskVolume1;
std::string sAskVolume1 = ostr4.str();
strncat(updatestring, sAskVolume1.c_str(), sizeof(updatestring));
strncat(updatestring, ")", sizeof(updatestring));
std::cout<<updatestring<<std::endl;
res = mysql_perform_query(conn,updatestring);
mysql_free_result(res);
/* clean up the database link */
//mysql_close(conn);
return;
}
};
int main(int argc, char* argv[])
{
MYSQL *conn; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row; // the results row (line by line)
struct connection_details mysqlD;
mysqlD.server = "localhost"; // where the mysql database is
mysqlD.user = "root"; // the root user of mysql
mysqlD.password = "Piercing77#"; // the password of the root user in mysql
mysqlD.database = "shfuture"; // the databse to pick
int i;
printf("%d\n",argc);
for(i=0;i<=argc-1;i++)
{
printf("%s\n",argv[i]);
}
memset(dateParam, '\0', sizeof(dateParam));
strcpy(dateParam,argv[1]);
// connect to the mysql database
conn = mysql_connection_setup(mysqlD);
// assign the results return to the MYSQL_RES pointer
// res = mysql_perform_query(conn, "show tables");
// res = mysql_perform_query(conn, "create table rb1910_20190620 (happentime DATETIME,lastprice double,b1 double,biVol int ,s1 double, s1Vol int);");
char CreateTableString[256];
memset(CreateTableString, '\0', sizeof(CreateTableString));
strcpy(CreateTableString,"create table if not exists rb1910_");
strncat(CreateTableString, argv[1], sizeof(CreateTableString));
strncat(CreateTableString," (happentime DATETIME,lastprice double,b1 double,biVol int ,s1 double, s1Vol int);",sizeof(CreateTableString));
printf("%s\n",CreateTableString);
res = mysql_perform_query(conn, CreateTableString);
//
memset(CreateTableString, '\0', sizeof(CreateTableString));
strcpy(CreateTableString,"create table if not exists if1909_");
strncat(CreateTableString, argv[1], sizeof(CreateTableString));
strncat(CreateTableString," (happentime DATETIME,lastprice double,b1 double,biVol int ,s1 double, s1Vol int);",sizeof(CreateTableString));
printf("%s\n",CreateTableString);
res = mysql_perform_query(conn, CreateTableString);
memset(CreateTableString, '\0', sizeof(CreateTableString));
strcpy(CreateTableString,"create table if not exists ni1909_");
strncat(CreateTableString, argv[1], sizeof(CreateTableString));
strncat(CreateTableString," (happentime DATETIME,lastprice double,b1 double,biVol int ,s1 double, s1Vol int);",sizeof(CreateTableString));
printf("%s\n",CreateTableString);
res = mysql_perform_query(conn, CreateTableString);
//
memset(CreateTableString, '\0', sizeof(CreateTableString));
strcpy(CreateTableString,"create table if not exists ic1909_");
strncat(CreateTableString, argv[1], sizeof(CreateTableString));
strncat(CreateTableString," (happentime DATETIME,lastprice double,b1 double,biVol int ,s1 double, s1Vol int);",sizeof(CreateTableString));
printf("%s\n",CreateTableString);
res = mysql_perform_query(conn, CreateTableString);
// printf("MySQL Tables in mysql database:\n");
// while ((row = mysql_fetch_row(res)) !=NULL)
//printf("%s\n", row[0]);
/* clean up the database result set */
mysql_free_result(res);
/* clean up the database link */
mysql_close(conn);
CMduserHandler *mduser = new CMduserHandler;
mduser->connect();
mduser->login();
mduser->subscribe();
// while(1)
//{}
sleep(1000);
return 0;
}