Hikyuu
StoplossBase.h
浏览该文件的文档.
1 /*
2  * StoplossBase.h
3  *
4  * Created on: 2013-3-3
5  * Author: fasiondog
6  */
7 
8 #ifndef STOPLOSSBASE_H_
9 #define STOPLOSSBASE_H_
10 
11 #include "../../KData.h"
12 //#include "../../utilities/Parameter.h"
13 #include "../../trade_manage/TradeManager.h"
14 
15 #if HKU_SUPPORT_SERIALIZATION
16 #include <boost/serialization/shared_ptr.hpp>
17 #include <boost/serialization/assume_abstract.hpp>
18 #include <boost/serialization/base_object.hpp>
19 #endif
20 
21 namespace hku {
22 
28 class HKU_API StoplossBase: public enable_shared_from_this<StoplossBase> {
30 
31 public:
32  StoplossBase();
33  StoplossBase(const string& name);
34  virtual ~StoplossBase();
35 
37  string name() const;
38 
40  void name(const string& name);
41 
43  void setTM(const TradeManagerPtr& tm);
44 
46  TradeManagerPtr getTM() const;
47 
49  void setTO(const KData& kdata);
50 
52  KData getTO() const;
53 
55  void reset();
56 
57  typedef shared_ptr<StoplossBase> StoplossPtr;
59  StoplossPtr clone();
60 
71  virtual price_t getPrice(const Datetime& datetime, price_t price) = 0;
72 
80  virtual price_t getShortPrice(const Datetime& datetime, price_t price) {
81  return getPrice(datetime, price);
82  }
83 
85  virtual void _reset() {}
86 
88  virtual StoplossPtr _clone() = 0;
89 
91  virtual void _calculate() = 0;
92 
93 
94 protected:
95  string m_name;
98 
99 //============================================
100 // 序列化支持
101 //============================================
102 #if HKU_SUPPORT_SERIALIZATION
103 private:
104  friend class boost::serialization::access;
105  template<class Archive>
106  void save(Archive & ar, const unsigned int version) const {
107  string name_str(GBToUTF8(m_name));
108  ar & boost::serialization::make_nvp("name", name_str);
109  ar & BOOST_SERIALIZATION_NVP(m_params);
110  // m_kdata都是系统运行时临时设置,不需要序列化
111  //ar & BOOST_SERIALIZATION_NVP(m_kdata);
112  }
113 
114  template<class Archive>
115  void load(Archive & ar, const unsigned int version) {
116  ar & boost::serialization::make_nvp("name", m_name);
117  ar & BOOST_SERIALIZATION_NVP(m_params);
118  // m_kdata都是系统运行时临时设置,不需要序列化
119  //ar & BOOST_SERIALIZATION_NVP(m_kdata);
120  }
121 
122  BOOST_SERIALIZATION_SPLIT_MEMBER()
123 #endif /* HKU_SUPPORT_SERIALIZATION */
124 };
125 
126 #if HKU_SUPPORT_SERIALIZATION
127 BOOST_SERIALIZATION_ASSUME_ABSTRACT(StoplossBase)
128 #endif
129 
130 #if HKU_SUPPORT_SERIALIZATION
131 
144 #define STOPLOSS_NO_PRIVATE_MEMBER_SERIALIZATION private:\
145  friend class boost::serialization::access; \
146  template<class Archive> \
147  void serialize(Archive & ar, const unsigned int version) { \
148  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(StoplossBase); \
149  }
150 #else
151 #define STOPLOSS_NO_PRIVATE_MEMBER_SERIALIZATION
152 #endif
153 
154 
155 #define STOPLOSS_IMP(classname, str_name) public:\
156  virtual StoplossPtr _clone() {\
157  return StoplossPtr(new classname());\
158  }\
159  virtual void _calculate();\
160  virtual price_t getPrice(const Datetime&, price_t);
161 
162 
167 typedef shared_ptr<StoplossBase> StoplossPtr;
168 typedef shared_ptr<StoplossBase> STPtr;
169 typedef shared_ptr<StoplossBase> TakeProfitPtr;
170 typedef shared_ptr<StoplossBase> TPPtr;
171 
172 HKU_API std::ostream& operator <<(std::ostream& os, const StoplossBase&);
173 HKU_API std::ostream& operator <<(std::ostream& os, const StoplossPtr&);
174 
175 inline string StoplossBase::name() const {
176  return m_name;
177 }
178 
179 inline void StoplossBase::name(const string& name) {
180  m_name = name;
181 }
182 
184  return m_tm;
185 }
186 
187 inline void StoplossBase::setTM(const TradeManagerPtr& tm) {
188  m_tm = tm;
189 }
190 
191 inline KData StoplossBase::getTO() const {
192  return m_kdata;
193 }
194 
195 inline void StoplossBase::reset() {
196  _reset();
197 }
198 
199 } /* namespace hku */
200 #endif /* STOPLOSSBASE_H_ */
string name() const
获取名称
Definition: StoplossBase.h:175
virtual void _reset()
子类复位接口
Definition: StoplossBase.h:85
K线数据
Definition: KData.h:19
virtual price_t getShortPrice(const Datetime &datetime, price_t price)
获取本次预期交易(卖空)时的计划止损价格,如果不存在止损价,则返回0。 用于系统在交易执行前向止损策略...
Definition: StoplossBase.h:80
shared_ptr< StoplossBase > StoplossPtr
Definition: StoplossBase.h:57
TradeManagerPtr m_tm
Definition: StoplossBase.h:96
日期类型
Definition: Datetime.h:33
#define GBToUTF8(s)
Definition: util.h:55
void load(Archive &ar, hku::Block &blk, unsigned int version)
Definition: Block_serialization.h:34
shared_ptr< StoplossBase > TPPtr
Definition: StoplossBase.h:170
string m_name
Definition: StoplossBase.h:95
shared_ptr< TradeManager > TradeManagerPtr
客户程序应使用此类型进行实际操作
Definition: TradeManager.h:635
#define HKU_API
Definition: DataType.h:12
void save(Archive &ar, const hku::Block &blk, unsigned int version)
Definition: Block_serialization.h:20
shared_ptr< StoplossBase > StoplossPtr
客户程序都应使用该指针类型,操作止损策略实例
Definition: StoplossBase.h:167
HKU_API std::ostream & operator<<(std::ostream &os, const Block &blk)
Definition: Block.cpp:13
double price_t
Definition: DataType.h:53
止损/止赢策略基类
Definition: StoplossBase.h:28
shared_ptr< StoplossBase > TakeProfitPtr
Definition: StoplossBase.h:169
void reset()
复位操作
Definition: StoplossBase.h:195
#define PARAMETER_SUPPORT
Definition: Parameter.h:233
void setTM(const TradeManagerPtr &tm)
设置交易管理实例
Definition: StoplossBase.h:187
TradeManagerPtr getTM() const
获取交易管理实例
Definition: StoplossBase.h:183
KData m_kdata
Definition: StoplossBase.h:97
Hikyuu核心命名空间,包含股票数据的管理、指标实现、交易系统框架等
Definition: Block.cpp:11
KData getTO() const
获取交易对象
Definition: StoplossBase.h:191
shared_ptr< StoplossBase > STPtr
Definition: StoplossBase.h:168