-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmdSpi.h
More file actions
151 lines (123 loc) · 3.79 KB
/
Copy pathmdSpi.h
File metadata and controls
151 lines (123 loc) · 3.79 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once
#include <list>
#include <string>
#include <mutex>
#include <condition_variable>
#include "ThostFtdcMdApi.h"
class TraderSpi;
class MdSpi : public CThostFtdcMdSpi
{
public:
MdSpi(CThostFtdcMdApi *);
virtual ~MdSpi();
virtual void OnFrontConnected();
virtual void OnFrontDisconnected(int nReason);
virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData);
//virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
//virtual void OnHeartBeatWarning(int nTimeLapse);
//---------------------------------------------------------------------------------------------------
enum MODE
{
MARKET,
TRADER,
};
enum STATE
{
NEGTIVE = -1,
EMPTY,
POSTIVE,
SWITCHING,
};
enum TREND
{
DOWN = -1,
BOTH,
UP,
};
enum ORDER_ACTION
{
OPEN_LONG,
OPEN_SHORT,
CLOSE_LONG,
CLOSE_SHORT,
};
struct FUTURE
{
std::string _name;
//----------------------------
double _open_price;
double _close_price;
int _open_vol;
int _wait_vol;
int _slippage_num;
int _wait_timer;
//----------------------------
std::list<double> _datas;
double _buy;
int _vol_buy;
double _sell;
int _vol_sell;
int _hour;
int _min;
int _sec;
int _minisec;
};
// 参数配置
void setLoginInfo(const std::string& brokerID, const std::string& userID) { m_brokerID = brokerID; m_userID = userID; };
void setTrader(TraderSpi* trader) { m_trader = trader; };
void setFutures(const std::string& main, const std::string& submain) { m_main._name = main; m_submain._name = submain; };
void setPriorPara(double u1, double e2, double u2) { m_u1_squ = u1; m_e2 = e2; m_u2_squ = u2; };
void setGapLen(int len) { m_gapLen = len; };
void setLimitRatio(double ratio) { m_limitRatio = ratio; };
void setMaxOrderNum(int num) { m_max_order_num = num; };
void setMode(MODE md) { m_mode = md; };
void setGap(std::list<double>& gaps) { m_gap = gaps; };
// 下单函数
bool order(const std::string& id, double price, int vol, TThostFtdcDirectionType dir, TThostFtdcOffsetFlagType kpp);
private:
//void handleData();
//
double computeExpectDiff();
// 下单方式
bool firstSecondOrder(STATE to, bool normalslilppage, int vol,
TThostFtdcDirectionType firDir, TThostFtdcOffsetFlagType firKpp, const std::string& firName, double firPrice,
TThostFtdcDirectionType secDir, TThostFtdcOffsetFlagType secKpp, const std::string& secName, double secPrice);
// 涨跌停限制时不进行操作
bool zeroCheck(STATE s, bool b);
TREND trend();
bool processingCheck(bool stage_to);
void logCurrentTime();
private:
CThostFtdcMdApi* m_api;
TraderSpi* m_trader;
std::string m_userID;
std::string m_brokerID;
double m_u1_squ;
double m_e2;
double m_u2_squ;
double m_limitRatio;
int m_gapLen;
int m_max_order_num; // 最大下单数
double m_future_tick; // 合约最小单位
int m_slippage_allowed; // 允许的滑点数
FUTURE m_main;
FUTURE m_submain;
FUTURE* m_fir; // 交易下单时的首单
FUTURE* m_sec; // 交易下单时的次单
MODE m_mode;
STATE m_state;
TThostFtdcOffsetFlagType m_kpp;
std::list<double> m_gap;
std::string m_log;
///< trader模块会用到
public:
TThostFtdcOrderStatusType m_orderStatus;
int m_vol_rtn;
double m_price_rtn;
std::mutex m_mutex;
std::condition_variable m_cond;
};