-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend_md_spi.cpp
97 lines (89 loc) · 3.75 KB
/
extend_md_spi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "include/common.h"
#include "datapage.h"
#define PRICE_MAX_LEN 20
using namespace std;
char* subID[]={"IF2012","IF2101","IF2103","IH2012","IH2101","IH2103","IC2012","IC2101","IC2103","IC2106"};
bool show_flag=true;
bool front_connected=false;
//,"zn2011","sn2011","ag2110","ag2111"
extend_md_spi::extend_md_spi(CThostFtdcMdApi* api){
setTapi(api);
}
//前端链接函数
void extend_md_spi::OnFrontConnected(){
log_info("Market front connectted,now logging in...");
system_status::is_market_connected=true;
}
//登录结果函数
void extend_md_spi::OnRspUserLogin(CThostFtdcRspUserLoginField *rsp_login_field, CThostFtdcRspInfoField *error_info, int nRequestID, bool bIsLast){
#ifdef DEBUG
log_info("Market login response:");
log_str("SessionID:");log_str(rsp_login_field->SessionID);
log_str("System Name:");log_str(rsp_login_field->SystemName);
log_str("Login Time:");log_str(rsp_login_field->LoginTime);
log_str("Trading Day:",rsp_login_field->TradingDay);
log_str("User ID:");log_str(rsp_login_field->UserID);
log_str("ErrorID:");log_str(error_info->ErrorID);
log_str("ErrorMsg:",error_info->ErrorMsg);
#endif
if(!error_info->ErrorID){
system_status::is_market_logined=true;
log_info("====Market Login Successs====");
}
}
//Once the contract is subscribed succesfully.it will touch a new table if it does not exist.
void extend_md_spi::OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast){
#ifdef DEBUG
log_info("Subscribtion responce:");
log_str("InstrumentID=");log_str(pSpecificInstrument->InstrumentID);
log_str("ErrorID:");log_str(pRspInfo->ErrorID);
log_str("Error Message:",pRspInfo->ErrorMsg);
#endif
if(!pRspInfo->ErrorID)
{
log_str("InstrumentID=",pSpecificInstrument->InstrumentID);
hfts_db::create_contract(pSpecificInstrument->InstrumentID);
log_str("Subscribtion success.");
system_status::is_subscribed=true;
}
else{
cout<<"Error ID"<<" "<<pRspInfo->ErrorID;
log_str("Error message:",pRspInfo->ErrorMsg);
}
}
void extend_md_spi::OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast){
if(!pRspInfo->ErrorID)
{
log_str("InstrumentID=",pSpecificInstrument->InstrumentID);
hfts_db::create_contract(pSpecificInstrument->InstrumentID);
log_str("UnSubscribtion success.");
system_status::is_unsubscribed=true;
}
else{
cout<<"Error ID"<<" "<<pRspInfo->ErrorID;
log_str("Error message:",pRspInfo->ErrorMsg);
}
}
//Depth data collection function.
//Once it receive a message,it will be called.
void extend_md_spi::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *data){
log_info("==market data recieved==");
//[syschronized:]insert the data into database
//[todo:]asyschronized
hfts_db::insert_depth_db(data);
SADateTime current_time=SADateTime::currentDateTimeWithFraction();
future_elemdata* e=new future_elemdata(current_time,data->LastPrice);
instrument_handler::insert_depth_instru(data->InstrumentID,e);
(*(datapage::item_set))[data->InstrumentID]->setprice(data->LastPrice);
log_str("Instrument ID:",data->InstrumentID,GREEN_STR,1);
log_str("Trading Day:",data->TradingDay,GREEN_STR,1);
log_str("Update time:",data->UpdateTime,GREEN_STR,1);
log_str("Update mills:",int2c(data->UpdateMillisec),GREEN_STR,1);
log_str("Last Price:",double2c(data->LastPrice),GREEN_STR,1);
log_str("Ask Price:",double2c(data->AskPrice1),GREEN_STR,1);
log_str("Ask Volume:",double2c(data->AskVolume1),GREEN_STR,1);
log_str("Bid Price:",double2c(data->BidPrice1),GREEN_STR,1);
log_str("Bid Volume:",double2c(data->BidVolume1),GREEN_STR,1);
log_str("OpenInterest:",double2c(data->OpenInterest),GREEN_STR,1);
log_str("Turn Over:",double2c(data->Turnover),GREEN_STR,1);
}