forked from pegasusTrader/PandoraTrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwStrategyDemo.cpp
194 lines (169 loc) · 4.42 KB
/
cwStrategyDemo.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
#include "cwStrategyDemo.h"
cwStrategyDemo::cwStrategyDemo()
{
}
cwStrategyDemo::~cwStrategyDemo()
{
}
void cwStrategyDemo::PriceUpdate(cwMarketDataPtr pPriceData)
{
if (pPriceData.get() == NULL)
{
return;
}
m_strCurrentUpdateTime = pPriceData->UpdateTime;
//定义map,用于保存持仓信息
std::map<std::string, cwPositionPtr> CurrentPosMap;
//定义map,用于保存挂单信息
std::map<cwActiveOrderKey, cwOrderPtr> WaitOrderList;
//获取挂单信 当前持仓信息
GetPositionsAndActiveOrders(CurrentPosMap, WaitOrderList);
//找出当前合约的持仓
std::map<std::string, cwPositionPtr>::iterator PosIt;
PosIt = CurrentPosMap.find(pPriceData->InstrumentID);
if (PosIt != CurrentPosMap.end())
{
int iPos = PosIt->second->GetLongTotalPosition();
if (iPos > 0)
{
//有多仓
bool bHasWaitOrder = false;
//检查所有挂单
for (auto WaitOrderIt = WaitOrderList.begin();
WaitOrderIt != WaitOrderList.end(); WaitOrderIt++)
{
//确定这个挂单是这个合约的
if ((std::string)pPriceData->InstrumentID == (std::string)WaitOrderIt->second->InstrumentID)
{
//多单撤去
if (WaitOrderIt->second->Direction == CW_FTDC_D_Buy)
{
CancelOrder(WaitOrderIt->second);
}
else
{
//标识有挂单
bHasWaitOrder = true;
//挂单价格大于最新价+2,撤单
if (WaitOrderIt->second->LimitPrice > pPriceData->LastPrice + 2)
{
CancelOrder(WaitOrderIt->second);
}
}
}
}
//没有挂单,就挂单平仓
if (!bHasWaitOrder)
{
//挂委托限价单,
EasyInputOrder(pPriceData->InstrumentID, -1 * iPos, pPriceData->BidPrice1);
}
}
iPos = PosIt->second->GetShortTotalPosition();
if (iPos > 0)
{
//有空仓
//定义map,用于保存挂单信息
std::map<cwActiveOrderKey, cwOrderPtr> WaitOrderList;
//获取挂单信息
GetActiveOrders(WaitOrderList);
bool bHasWaitOrder = false;
for (auto WaitOrderIt = WaitOrderList.begin();
WaitOrderIt != WaitOrderList.end(); WaitOrderIt++)
{
if ((std::string)pPriceData->InstrumentID == (std::string)WaitOrderIt->second->InstrumentID)
{
if (WaitOrderIt->second->Direction == CW_FTDC_D_Sell)
{
CancelOrder(WaitOrderIt->second);
}
else
{
//有挂单
bHasWaitOrder = true;
//挂单价格小于最新价-2,撤单
if (WaitOrderIt->second->LimitPrice < pPriceData->LastPrice - 2)
{
CancelOrder(WaitOrderIt->second);
}
}
}
}
//没有挂单,就挂单平仓
if (!bHasWaitOrder)
{
EasyInputOrder(pPriceData->InstrumentID, iPos, pPriceData->AskPrice1);
}
}
if (PosIt->second->GetLongTotalPosition() + PosIt->second->GetShortTotalPosition() == 0)
{
//定义map,用于保存挂单信息
std::map<cwActiveOrderKey, cwOrderPtr> WaitOrderList;
//获取挂单信息
GetActiveOrders(WaitOrderList);
//看看有没有挂单
bool bHasWaitOrder = false;
for (auto WaitOrderIt = WaitOrderList.begin();
WaitOrderIt != WaitOrderList.end(); WaitOrderIt++)
{
if ((std::string)pPriceData->InstrumentID == (std::string)WaitOrderIt->second->InstrumentID)
{
//有挂单
bHasWaitOrder = true;
//挂单价格小于最新价-2,撤单
if (WaitOrderIt->second->LimitPrice < pPriceData->LastPrice - 2)
{
CancelOrder(WaitOrderIt->second);
}
}
}
//没有挂单就报委托单,方向多,价格买一价
if (!bHasWaitOrder)
{
EasyInputOrder(pPriceData->InstrumentID, 1, pPriceData->BidPrice1);
}
}
}
else
{
//没找到持仓信息
//定义map,用于保存挂单信息
std::map<cwActiveOrderKey, cwOrderPtr> WaitOrderList;
//获取挂单信息
GetActiveOrders(WaitOrderList);
//看看有没有挂单
bool bHasWaitOrder = false;
for (auto WaitOrderIt = WaitOrderList.begin();
WaitOrderIt != WaitOrderList.end(); WaitOrderIt++)
{
if ((std::string)pPriceData->InstrumentID == (std::string)WaitOrderIt->second->InstrumentID
&& (WaitOrderIt->second->Direction == CW_FTDC_D_Buy))
{
bHasWaitOrder = true;
//挂单价格小于最新价-2,撤单
if (WaitOrderIt->second->LimitPrice < pPriceData->LastPrice - 2)
{
CancelOrder(WaitOrderIt->second);
}
}
}
//没有挂单就报委托单,方向多,价格买一价
if (!bHasWaitOrder)
{
EasyInputOrder(pPriceData->InstrumentID, 1, pPriceData->BidPrice1);
}
}
}
void cwStrategyDemo::OnRtnTrade(cwTradePtr pTrade)
{
}
void cwStrategyDemo::OnRtnOrder(cwOrderPtr pOrder, cwOrderPtr pOriginOrder)
{
}
void cwStrategyDemo::OnOrderCanceled(cwOrderPtr pOrder)
{
}
void cwStrategyDemo::OnReady()
{
SubScribePrice("ag2312");
}