-
Notifications
You must be signed in to change notification settings - Fork 1
/
DtAlgorithm.cpp
executable file
·311 lines (230 loc) · 6.81 KB
/
DtAlgorithm.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// DtAlgorithm.cpp: implementation of the DtAlgorithm class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "tradesystem.h"
#include "DtAlgorithm.h"
#include <math.h>
#include "DbConn.h"
#include "matlab\\libMethod_4.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern DbAccessorPool dbAccessPool;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DtAlgorithm::DtAlgorithm(string instrument_id):
m_InstrumentID(instrument_id), m_Amount(0), m_log(".\\dt_algo_data.log", ios::app),
m_state_log(".\\dt_algo_state.log", ios::app)
{
lastVol=0;
totalAmount=0;
totalMoney=0;
m_BidPrice=0;
m_AskPrice=0;
isIni=0;
ininow=0;
}
DtAlgorithm::~DtAlgorithm()
{
m_log.close();
m_state_log.close();
}
int DtAlgorithm::OnMinuteData(const CMinuteData& data)
{
double val = 0;
mwArray newopen(1,1, mxDOUBLE_CLASS);
mwArray newm(1,1, mxDOUBLE_CLASS);
mwArray newe(1,1, mxDOUBLE_CLASS);
mwArray newrm(1,1, mxDOUBLE_CLASS);
mwArray newre(1,1, mxDOUBLE_CLASS);
mwArray bidprice(1,1, mxDOUBLE_CLASS);
mwArray to(1,1, mxDOUBLE_CLASS);
mwArray th(1,1, mxDOUBLE_CLASS);
mwArray tl(1,1, mxDOUBLE_CLASS);
mwArray tc(1,1, mxDOUBLE_CLASS);
mwArray tv(1,1, mxDOUBLE_CLASS);
mwArray trm(1,1, mxDOUBLE_CLASS);
mwArray tre(1,1, mxDOUBLE_CLASS);
mwArray time(1,100,mxCHAR_CLASS);
val = data.m_OpenPrice;
to.SetData(&val,1);
val = data.m_HighPrice;
th.SetData(&val,1);
val = data.m_LowPrice;
tl.SetData(&val,1);
val = data.m_ClosePrice;
tc.SetData(&val,1);
val = data.m_Volume;
tv.SetData(&val,1);
MinKsymbProcess4(2, newopen, bidprice, to, th, tl, tc);
OrderInfoShort res;
bidprice.GetData(&(res.price), 1);
newopen.GetData(&(res.amount), 1);
if ( totalAmount >=2 && res.amount > 0)
res.amount =0;
else if ( totalAmount <=-2 && res.amount < 0 )
res.amount =0;
if (data.m_Time > "15:10")
{
//isIni=0;
res.amount = -totalAmount;
}
if(res.amount>0)
{
res.price=m_AskPrice;
}
if(res.amount<0)
{
res.price=m_BidPrice;
}
res.day= data.m_Day;
res.time = data.m_Time;
res.milliSec =0;
res.m_instrumentID = data.m_InstrumentID;
totalAmount += res.amount;
SendStrategy(res);
mwArray s_m(1,1, mxDOUBLE_CLASS);
mwArray s_e(1,1, mxDOUBLE_CLASS);
mwArray s_ud(1,1, mxDOUBLE_CLASS);
mwArray s_pacounter(1,1, mxDOUBLE_CLASS);
mwArray s_nacounter(1,1, mxDOUBLE_CLASS);
mwArray s_pisstart(1,1, mxDOUBLE_CLASS);
mwArray s_nisstart(1,1, mxDOUBLE_CLASS);
GetInnerState4(7, s_m, s_e, s_ud, s_pacounter, s_nacounter, s_pisstart, s_nisstart);
double im=0;
s_m.GetData(&im,1);
double ie=0;
s_e.GetData(&ie,1);
double iud=0;
s_ud.GetData(&iud,1);
double ipa=0;
s_pacounter.GetData(&ipa,1);
double ina=0;
s_nacounter.GetData(&ina,1);
double ips=0;
s_pisstart.GetData(&ips,1);
double ins=0;
s_nisstart.GetData(&ins,1);
m_state_log<<res.m_instrumentID+", "+res.day+" "+res.time<<", price:"<<res.price<<", m:"<<im<<", e:"
<<ie<<", ud:"<<iud<<", P_action_con:"<<ipa<<", N_action_con:"<<ina<<", P_is_start:"<<ips<<", N_is_start:"<<ins<<endl;
return res.amount;
}
int DtAlgorithm::SendStrategy( OrderInfoShort & res)
{
//第一行就是真实的发送指令,第二行是本地模拟写log
if( res.amount != 0 )
{
//Algorithm::SendStrategy(res);
m_log<<res.m_instrumentID+", "+res.day+" "+res.time<<", Amount, "<< res.amount <<", Price, "<<res.price<<endl;
}
return 0;
}
int DtAlgorithm::OnTickData(const CThostFtdcDepthMarketDataField& data)
{
m_AskPrice = data.AskPrice1;
m_BidPrice = data.BidPrice1;
return 0;
}
void DtAlgorithm::OnTradeData(const CThostFtdcTradeField& data)
{
}
void DtAlgorithm::OnAccountData(const CThostFtdcTradingAccountField& data)
{
}
void DtAlgorithm::OnPositionData(const CThostFtdcInvestorPositionField& data)
{
}
BOOL DtAlgorithm::InitInstance()
{
this->RegisterInstrument(m_InstrumentID);
try{
DbConn conn(dbAccessPool);
//conn.m_db->getData(m_InstrumentID, startDayBuffer, startTimeBuffer,
// currDayBuffer, currTimeBuffer, m_historyData);
conn.m_db->getData(m_InstrumentID, 100, m_historyData);
}
catch(CDBException* pe)
{
// The error code is in pe->m_nRetCode
pe->ReportError();
pe->Delete();
}
int size = m_historyData.size();
//size=1;/////////////////////////////////////////////////////
double val =10;
mwArray mwErrorCode(1,1, mxDOUBLE_CLASS); // 1,1表示矩阵的大小(所有maltab只有一种变量,就是矩阵,为了和Cpp变量接轨,设置成1*1的矩阵,mxDOUBLE_CLASS表示变量的精度)
mwArray mwOpen( size, 1,mxDOUBLE_CLASS);
mwArray mwHigh( size, 1,mxDOUBLE_CLASS);
mwArray mwLow(size, 1, mxDOUBLE_CLASS);
mwArray mwClose( size,1, mxDOUBLE_CLASS);
mwArray mwVolume( size,1, mxDOUBLE_CLASS);
mwArray intime(size,1, mxDOUBLE_CLASS);
mwArray inn1(1,1, mxDOUBLE_CLASS);
mwArray inkb(1,1, mxDOUBLE_CLASS);
mwArray inkm(1,1, mxDOUBLE_CLASS);
mwArray inw(1,1, mxDOUBLE_CLASS);
mwArray inwl(1,1, mxDOUBLE_CLASS);
mwArray inks(1,1, mxDOUBLE_CLASS);
double* openPrice = new double[size];
double* highPrice = new double[size];
double* lowPrice = new double[size];
double* closePrice = new double[size];
double* vol = new double[size];
int * intimeA=new int[size];
if ( size >0)
intimeA[size-1]=0;
int iSameDay=1;
val=0;
// set data,不用我解释了吧,很简单的,调用类里面的SetData函数给类赋值
for( int i=size-1; i>=0; i-- )
{
openPrice[i] = m_historyData[i].m_OpenPrice;
highPrice[i] = m_historyData[i].m_HighPrice;
lowPrice[i] = m_historyData[i].m_LowPrice;
closePrice[i] = m_historyData[i].m_ClosePrice;
vol[i] = m_historyData[i].m_Volume;
if(i!=size-1)
{
if(m_historyData[i].m_Day!=m_historyData[i+1].m_Day)
{
intimeA[i] = intimeA[i+1]-1;
}
else
{
intimeA[i] = intimeA[i+1];
}
}
}
mwOpen.SetData(openPrice, size);
mwHigh.SetData(highPrice, size);
mwLow.SetData(lowPrice, size);
mwClose.SetData(closePrice, size);
intime.SetData(intimeA, size);
mwVolume.SetData(vol,size);
val = 5;
inn1.SetData(&val,1);
val = 2;
inkb.SetData(&val,1);
val = 5;
inkm.SetData(&val,1);
val = 40;
inks.SetData(&val,1);
val = 30;
inwl.SetData(&val,1);
val = 15;
inw.SetData(&val,1);
IniMethod4(1, mwErrorCode,
mwOpen, mwHigh, mwLow, mwClose, inn1, inkb, inkm, inw, inwl, inks);
mwErrorCode.GetData(&val,1);
delete []openPrice;
delete []highPrice;
delete []lowPrice;
delete []closePrice;
delete []vol;
delete []intimeA;
return TRUE;
}