hikyuu2/hikyuu_cpp/hikyuu/trade_manage/PositionRecord.h

113 lines
3.6 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* PositionRecord.h
*
* Created on: 2013-2-21
* Author: fasiondog
*/
#pragma once
2015-01-07 01:26:14 +08:00
#ifndef POSITIONRECORD_H_
#define POSITIONRECORD_H_
#include "../StockManager.h"
#include "../serialization/Stock_serialization.h"
#if HKU_SUPPORT_SERIALIZATION
#include <boost/serialization/split_member.hpp>
#endif
namespace hku {
/**
*
* @ingroup TradeManagerClass
*/
class HKU_API PositionRecord {
public:
PositionRecord();
2019-11-10 23:31:41 +08:00
PositionRecord(const Stock& stock, const Datetime& takeDatetime, const Datetime& cleanDatetime,
double number, price_t stoploss, price_t goalPrice, double totalNumber,
2019-11-10 23:31:41 +08:00
price_t buyMoney, price_t totalCost, price_t totalRisk, price_t sellMoney);
2015-01-07 01:26:14 +08:00
2016-04-03 00:08:31 +08:00
/** 仅用于python的__str__ */
string toString() const;
2019-11-10 23:31:41 +08:00
Stock stock; ///< 交易对象
2015-01-07 01:26:14 +08:00
Datetime takeDatetime; ///< 初次建仓日期
Datetime cleanDatetime; ///< 平仓日期当前持仓记录中为Null<Datetime>()
double number; ///< 当前持仓数量
2019-11-10 23:31:41 +08:00
price_t stoploss; ///< 当前止损价
price_t goalPrice; ///< 当前的目标价格
double totalNumber; ///< 累计持仓数量
2019-11-10 23:31:41 +08:00
price_t buyMoney; ///< 累计买入资金
price_t totalCost; ///< 累计交易总成本
price_t totalRisk; ///< 累计交易风险 = 各次 (买入价格-止损)*买入数量, 不包含交易成本
price_t sellMoney; ///< 累计卖出资金
2015-01-07 01:26:14 +08:00
//===================
2023-07-28 23:50:26 +08:00
// 序列化支持
2015-01-07 01:26:14 +08:00
//===================
#if HKU_SUPPORT_SERIALIZATION
private:
friend class boost::serialization::access;
2019-11-10 23:31:41 +08:00
template <class Archive>
void save(Archive& ar, const unsigned int version) const {
2015-01-07 01:26:14 +08:00
namespace bs = boost::serialization;
2019-11-10 23:31:41 +08:00
ar& BOOST_SERIALIZATION_NVP(stock);
uint64_t take = takeDatetime.number();
uint64_t clean = cleanDatetime.number();
2019-11-10 23:31:41 +08:00
ar& bs::make_nvp("takeDatetime", take);
ar& bs::make_nvp("cleanDatetime", clean);
ar& BOOST_SERIALIZATION_NVP(number);
ar& BOOST_SERIALIZATION_NVP(stoploss);
ar& BOOST_SERIALIZATION_NVP(goalPrice);
ar& BOOST_SERIALIZATION_NVP(totalNumber);
ar& BOOST_SERIALIZATION_NVP(buyMoney);
ar& BOOST_SERIALIZATION_NVP(totalCost);
ar& BOOST_SERIALIZATION_NVP(totalRisk);
ar& BOOST_SERIALIZATION_NVP(sellMoney);
2015-01-07 01:26:14 +08:00
}
2019-11-10 23:31:41 +08:00
template <class Archive>
void load(Archive& ar, const unsigned int version) {
2015-01-07 01:26:14 +08:00
namespace bs = boost::serialization;
2019-11-10 23:31:41 +08:00
ar& BOOST_SERIALIZATION_NVP(stock);
uint64_t take, clean;
2019-11-10 23:31:41 +08:00
ar& bs::make_nvp("takeDatetime", take);
ar& bs::make_nvp("cleanDatetime", clean);
2015-01-07 01:26:14 +08:00
takeDatetime = Datetime(take);
cleanDatetime = Datetime(clean);
2019-11-10 23:31:41 +08:00
ar& BOOST_SERIALIZATION_NVP(number);
ar& BOOST_SERIALIZATION_NVP(stoploss);
ar& BOOST_SERIALIZATION_NVP(goalPrice);
ar& BOOST_SERIALIZATION_NVP(totalNumber);
ar& BOOST_SERIALIZATION_NVP(buyMoney);
ar& BOOST_SERIALIZATION_NVP(totalCost);
ar& BOOST_SERIALIZATION_NVP(totalRisk);
ar& BOOST_SERIALIZATION_NVP(sellMoney);
2015-01-07 01:26:14 +08:00
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
#endif /* HKU_SUPPORT_SERIALIZATION */
};
/** @ingroup TradeManagerClass */
typedef vector<PositionRecord> PositionRecordList;
/**
*
* @ingroup TradeManagerClass
*/
2019-11-10 23:31:41 +08:00
HKU_API std::ostream& operator<<(std::ostream&, const PositionRecord&);
2015-01-07 01:26:14 +08:00
bool HKU_API operator==(const PositionRecord& d1, const PositionRecord& d2);
} /* namespace hku */
2023-07-28 23:50:26 +08:00
#if FMT_VERSION >= 90000
template <>
struct fmt::formatter<hku::PositionRecord> : ostream_formatter {};
#endif
2015-01-07 01:26:14 +08:00
#endif /* POSITIONRECORD_H_ */