-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathCtaStrategyBase.cpp
73 lines (61 loc) · 1.66 KB
/
CtaStrategyBase.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
#include "CtaStrategyBase.h"
#include "CtaEngine.h"
StrategyBase::StrategyBase(CtaEngine* ce, std::string name, std::string symbol)
: ce(ce), name(name), vtSymbol(symbol)
{
}
int StrategyBase::convert_time_str2int(const char* update_time)
{
std::string market_time_str = update_time;
market_time_str.erase(std::remove_if(market_time_str.begin(), market_time_str.end(), [](char c){ return c == ':'; }), market_time_str.end());
return atoi(market_time_str.c_str());
}
std::string StrategyBase::buy(double price, int volume, bool stop)
{
return sendOrder(CTAORDER_BUY, price, volume, stop);
}
std::string StrategyBase::sell(double price, int volume, bool stop)
{
return sendOrder(CTAORDER_SELL, price, volume, stop);
}
std::string StrategyBase::short_(double price, int volume, bool stop)
{
return sendOrder(CTAORDER_SHORT, price, volume, stop);
}
std::string StrategyBase::cover(double price, int volume, bool stop)
{
return sendOrder(CTAORDER_COVER, price, volume, stop);
}
std::string StrategyBase::sendOrder(char order_type, double price, int volume, bool stop)
{
std::string orderID;
if (trading)
{
// 如果stop为True,则意味着发本地停止单
if (stop)
{
orderID = ce->sendStopOrder(vtSymbol, order_type, price, volume, std::shared_ptr<StrategyBase>(this));
}
else
{
orderID = ce->sendOrder(vtSymbol, order_type, price, volume, std::shared_ptr<StrategyBase>(this));
}
}
return orderID;
}
void StrategyBase::cancelOrder(std::string orderID)
{
if (orderID.empty())
{
return;
}
if (std::string::npos != orderID.find_first_of(STOPORDERPREFIX))
{
// 取消停止单
ce->cancelStopOrder(orderID);
}
else
{
ce->cancelOrder(orderID);
}
}