update for strategy trade

This commit is contained in:
fasiondog 2021-03-26 01:23:02 +08:00
parent 816557a3d6
commit 96a33080a0
7 changed files with 483 additions and 52 deletions

View File

@ -5,6 +5,6 @@
* Author: fasiondog * Author: fasiondog
*/ */
#include "StrategyTradeManager.h" #include "AccountTradeManager.h"
namespace hku {} // namespace hku namespace hku {} // namespace hku

View File

@ -0,0 +1,467 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-03-23
* Author: fasiondog
*/
#pragma once
#include "../trade_manage/TradeManagerBase.h"
namespace hku {
class HKU_API AccountTradeManager : public TradeManagerBase {
public:
AccountTradeManager() = default;
AccountTradeManager(const string& name) : TradeManagerBase(name, TC_Zero()) {}
virtual ~AccountTradeManager() = default;
virtual void _reset() override {}
virtual shared_ptr<TradeManagerBase> _clone() override {
return std::make_shared<AccountTradeManager>();
}
/**
*
* @param datetime
* @param stock
*/
virtual double getMarginRate(const Datetime& datetime, const Stock& stock) override {
HKU_WARN("The subclass does not implement a getMarginRate method");
return 0.0;
}
/** 初始资金 */
virtual price_t initCash() const override {
HKU_WARN("The subclass does not implement this method");
return 0.0;
}
/** 账户建立日期 */
virtual Datetime initDatetime() const override {
HKU_WARN("The subclass does not implement this method");
return Datetime();
}
/** 第一笔买入交易发生日期如未发生交易返回Null<Datetime>() */
virtual Datetime firstDatetime() const override {
HKU_WARN("The subclass does not implement this method");
return Datetime();
}
/** 最后一笔交易日期,注意和交易类型无关,如未发生交易返回账户建立日期 */
virtual Datetime lastDatetime() const override {
HKU_WARN("The subclass does not implement this method");
return Datetime();
}
/**
*
* @note
*/
virtual price_t currentCash() const override {
HKU_WARN("The subclass does not implement this method");
return 0.0;
}
/**
*
* @note
*/
virtual price_t cash(const Datetime& datetime, KQuery::KType ktype = KQuery::DAY) override {
HKU_WARN("The subclass does not implement this method");
return 0.0;
}
/**
*
* @note 使
* @param stock
* @return true | false
*/
virtual bool have(const Stock& stock) const override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @note 使
* @param stock
* @return true | false
*/
virtual bool haveShort(const Stock& stock) const override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/** 当前持有的证券种类数量 */
virtual size_t getStockNumber() const override {
HKU_WARN("The subclass does not implement this method");
return 0;
}
/** 当前空头持有的证券种类数量 */
virtual size_t getShortStockNumber() const override {
HKU_WARN("The subclass does not implement this method");
return 0;
}
/** 获取指定时刻的某证券持有数量 */
virtual double getHoldNumber(const Datetime& datetime, const Stock& stock) override {
HKU_WARN("The subclass does not implement this method");
return 0.0;
}
/** 获取指定时刻的空头某证券持有数量 */
virtual double getShortHoldNumber(const Datetime& datetime, const Stock& stock) override {
HKU_WARN("The subclass does not implement this method");
return 0.0;
}
/** 获取指定时刻已借入的股票数量 */
virtual double getDebtNumber(const Datetime& datetime, const Stock& stock) override {
HKU_WARN("The subclass does not implement this method");
return 0.0;
}
/** 获取指定时刻已借入的现金额 */
virtual price_t getDebtCash(const Datetime& datetime) override {
HKU_WARN("The subclass does not implement this method");
return 0.0;
}
/** 获取全部交易记录 */
virtual TradeRecordList getTradeList() const override {
HKU_WARN("The subclass does not implement this method");
return TradeRecordList();
}
/**
* [start, end)
* @param start
* @param end
* @return
*/
virtual TradeRecordList getTradeList(const Datetime& start,
const Datetime& end) const override {
HKU_WARN("The subclass does not implement this method");
return TradeRecordList();
}
/** 获取当前全部持仓记录 */
virtual PositionRecordList getPositionList() const override {
HKU_WARN("The subclass does not implement this method");
return PositionRecordList();
}
/** 获取全部历史持仓记录,即已平仓记录 */
virtual PositionRecordList getHistoryPositionList() const override {
HKU_WARN("The subclass does not implement this method");
return PositionRecordList();
}
/** 获取当前全部空头仓位记录 */
virtual PositionRecordList getShortPositionList() const override {
HKU_WARN("The subclass does not implement this method");
return PositionRecordList();
}
/** 获取全部空头历史仓位记录 */
virtual PositionRecordList getShortHistoryPositionList() const override {
HKU_WARN("The subclass does not implement this method");
return PositionRecordList();
}
/** 获取指定证券的当前持仓记录如当前未持有该票返回Null<PositionRecord>() */
virtual PositionRecord getPosition(const Stock&) const override {
HKU_WARN("The subclass does not implement this method");
return PositionRecord();
}
/** 获取指定证券的当前空头仓位持仓记录如当前未持有该票返回Null<PositionRecord>() */
virtual PositionRecord getShortPosition(const Stock&) const override {
HKU_WARN("The subclass does not implement this method");
return PositionRecord();
}
/** 获取当前借入的股票列表 */
virtual BorrowRecordList getBorrowStockList() const override {
HKU_WARN("The subclass does not implement this method");
return BorrowRecordList();
}
/**
*
* @param datetime
* @param cash
* @return true | false
*/
virtual bool checkin(const Datetime& datetime, price_t cash) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @param datetime
* @param cash
* @return true | false
*/
virtual bool checkout(const Datetime& datetime, price_t cash) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @param datetime
* @param stock
* @param price
* @param number
* @return true | false
*/
virtual bool checkinStock(const Datetime& datetime, const Stock& stock, price_t price,
double number) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @param datetime
* @param stock
* @param price
* @param number
* @return true | false
* @note
*/
virtual bool checkoutStock(const Datetime& datetime, const Stock& stock, price_t price,
double number) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @param datetime
* @param stock
* @param realPrice
* @param number
* @param stoploss
* @param goalPrice
* @param planPrice
* @param from
* @return business等于BUSINESS_INVALID
*/
virtual TradeRecord buy(const Datetime& datetime, const Stock& stock, price_t realPrice,
double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
price_t planPrice = 0.0, SystemPart from = PART_INVALID) override {
HKU_WARN("The subclass does not implement this method");
return TradeRecord();
}
/**
*
* @param datetime
* @param stock
* @param realPrice
* @param number MAX_DOUBLE,
* @param stoploss
* @param goalPrice
* @param planPrice
* @param from
* @return business等于BUSINESS_INVALID
*/
virtual TradeRecord sell(const Datetime& datetime, const Stock& stock, price_t realPrice,
double number = MAX_DOUBLE, price_t stoploss = 0.0,
price_t goalPrice = 0.0, price_t planPrice = 0.0,
SystemPart from = PART_INVALID) override {
HKU_WARN("The subclass does not implement this method");
return TradeRecord();
}
/**
*
* @param datetime
* @param stock
* @param realPrice
* @param number
* @param stoploss
* @param goalPrice
* @param planPrice
* @param from
* @return business等于BUSINESS_INVALID
*/
virtual TradeRecord sellShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
double number, price_t stoploss = 0.0, price_t goalPrice = 0.0,
price_t planPrice = 0.0,
SystemPart from = PART_INVALID) override {
HKU_WARN("The subclass does not implement this method");
return TradeRecord();
}
/**
*
* @param datetime
* @param stock
* @param realPrice
* @param number MAX_DOUBLE,
* @param stoploss
* @param goalPrice
* @param planPrice
* @param from
* @return business等于BUSINESS_INVALID
*/
virtual TradeRecord buyShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
double number = MAX_DOUBLE, price_t stoploss = 0.0,
price_t goalPrice = 0.0, price_t planPrice = 0.0,
SystemPart from = PART_INVALID) override {
HKU_WARN("The subclass does not implement this method");
return TradeRecord();
}
/**
*
* @param datetime
* @param cash
* @return true | false
*/
virtual bool borrowCash(const Datetime& datetime, price_t cash) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @param datetime
* @param cash
* @return true | false
*/
virtual bool returnCash(const Datetime& datetime, price_t cash) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @param datetime
* @param stock stock
* @param price
* @param number
* @return true | false
*/
virtual bool borrowStock(const Datetime& datetime, const Stock& stock, price_t price,
double number) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @param datetime
* @param stock stock
* @param price
* @param number
* @return true | false
*/
virtual bool returnStock(const Datetime& datetime, const Stock& stock, price_t price,
double number) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/**
*
* @param ktype
* @return
*/
virtual FundsRecord getFunds(KQuery::KType ktype = KQuery::DAY) const override {
HKU_WARN("The subclass does not implement this method");
return FundsRecord();
}
/**
*
* @param datetime Null<Datetime>()
* @param ktype
* @return
* @note datetime等于Null<Datetime>()getFunds(KType)
*/
virtual FundsRecord getFunds(const Datetime& datetime,
KQuery::KType ktype = KQuery::DAY) override {
HKU_WARN("The subclass does not implement this method");
return FundsRecord();
}
/**
* 线
* @param dates 线
* @param ktype K线类型KQuery::DAY
* @return
*/
virtual PriceList getFundsCurve(const DatetimeList& dates,
KQuery::KType ktype = KQuery::DAY) override {
HKU_WARN("The subclass does not implement this method");
return PriceList();
}
/**
* 线
* @return
*/
virtual PriceList getFundsCurve() override {
HKU_WARN("The subclass does not implement this method");
return PriceList();
}
/**
* 线线
* @param dates 线
* @param ktype K线类型KQuery::DAY
* @return 线
*/
virtual PriceList getProfitCurve(const DatetimeList& dates,
KQuery::KType ktype = KQuery::DAY) override {
HKU_WARN("The subclass does not implement this method");
return PriceList();
}
/**
* 线线
* @return 线
*/
virtual PriceList getProfitCurve() override {
HKU_WARN("The subclass does not implement this method");
return PriceList();
}
/**
*
* @note
* @param tr
* @return bool true | false
*/
virtual bool addTradeRecord(const TradeRecord& tr) override {
HKU_WARN("The subclass does not implement this method");
return false;
}
/** 字符串输出 */
virtual string str() const override {
HKU_WARN("The subclass does not implement this method");
return string();
}
/**
* csv格式输出交易记录线
* @param path
*/
virtual void tocsv(const string& path) override {
HKU_WARN("The subclass does not implement this method");
}
};
} // namespace hku

View File

@ -1,20 +0,0 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-03-23
* Author: fasiondog
*/
#pragma once
#include "../trade_manage/TradeManagerBase.h"
namespace hku {
class HKU_API StrategyTradeManager : public TradeManagerBase {
public:
StrategyTradeManager() = default;
virtual ~StrategyTradeManager() = default;
};
} // namespace hku

View File

@ -152,14 +152,6 @@ TradeManagerPtr TradeManager::_clone() {
return TradeManagerPtr(p); return TradeManagerPtr(p);
} }
void TradeManager::regBroker(const OrderBrokerPtr& broker) {
m_broker_list.push_back(broker);
}
void TradeManager::clearBroker() {
m_broker_list.clear();
}
double TradeManager::getMarginRate(const Datetime& datetime, const Stock& stock) { double TradeManager::getMarginRate(const Datetime& datetime, const Stock& stock) {
// TODO 获取保证金比率默认固定取60% // TODO 获取保证金比率默认固定取60%
return 0.6; return 0.6;

View File

@ -51,17 +51,6 @@ public:
/** 复位,清空交易、持仓记录 */ /** 复位,清空交易、持仓记录 */
virtual void _reset() override; virtual void _reset() override;
/**
*
* @param broker
*/
virtual void regBroker(const OrderBrokerPtr& broker) override;
/**
*
*/
virtual void clearBroker() override;
virtual shared_ptr<TradeManagerBase> _clone() override; virtual shared_ptr<TradeManagerBase> _clone() override;
/** /**
@ -437,7 +426,7 @@ private:
position_map_type m_short_position; //空头仓位记录 position_map_type m_short_position; //空头仓位记录
PositionRecordList m_short_position_history; //空头仓位历史记录 PositionRecordList m_short_position_history; //空头仓位历史记录
list<OrderBrokerPtr> m_broker_list; //订单代理列表 // list<OrderBrokerPtr> m_broker_list; //订单代理列表
// Datetime m_broker_last_datetime; //订单代理最近一次执行操作的时刻 // Datetime m_broker_last_datetime; //订单代理最近一次执行操作的时刻
list<string> m_actions; //记录交易动作,便于修改或校准实盘时的交易 list<string> m_actions; //记录交易动作,便于修改或校准实盘时的交易

View File

@ -40,7 +40,7 @@ public:
TradeManagerBase() : TradeManagerBase("", TC_Zero()) {} TradeManagerBase() : TradeManagerBase("", TC_Zero()) {}
TradeManagerBase(const string& name, const TradeCostPtr& costFunc) TradeManagerBase(const string& name, const TradeCostPtr& costFunc)
: m_name(name), m_broker_last_datetime(Datetime::now()), m_costfunc(costFunc) { : m_name(name), m_costfunc(costFunc), m_broker_last_datetime(Datetime::now()) {
setParam<int>("precision", 2); //计算精度 setParam<int>("precision", 2); //计算精度
} }
@ -186,15 +186,15 @@ public:
* *
* @param broker * @param broker
*/ */
virtual void regBroker(const OrderBrokerPtr& broker) { void regBroker(const OrderBrokerPtr& broker) {
HKU_WARN("The subclass does not implement a regBroker method"); m_broker_list.push_back(broker);
} }
/** /**
* *
*/ */
virtual void clearBroker() { void clearBroker() {
HKU_WARN("The subclass does not implement a clearBroker method"); m_broker_list.clear();
} }
/** /**
@ -633,9 +633,11 @@ public:
} }
protected: protected:
string m_name; //账户名称 string m_name; // 账户名称
Datetime m_broker_last_datetime; // 当前启动运行时间 TradeCostPtr m_costfunc; // 成本算法
TradeCostPtr m_costfunc; //成本算法
Datetime m_broker_last_datetime; // 订单代理最近一次执行操作的时刻,当前启动运行时间
list<OrderBrokerPtr> m_broker_list; // 订单代理列表
//============================================ //============================================
// 序列化支持 // 序列化支持
@ -647,16 +649,18 @@ private:
void save(Archive& ar, const unsigned int version) const { void save(Archive& ar, const unsigned int version) const {
ar& BOOST_SERIALIZATION_NVP(m_params); ar& BOOST_SERIALIZATION_NVP(m_params);
ar& BOOST_SERIALIZATION_NVP(m_name); ar& BOOST_SERIALIZATION_NVP(m_name);
ar& BOOST_SERIALIZATION_NVP(m_broker_last_datetime);
ar& BOOST_SERIALIZATION_NVP(m_costfunc); ar& BOOST_SERIALIZATION_NVP(m_costfunc);
ar& BOOST_SERIALIZATION_NVP(m_broker_last_datetime);
ar& BOOST_SERIALIZATION_NVP(m_broker_list);
} }
template <class Archive> template <class Archive>
void load(Archive& ar, const unsigned int version) { void load(Archive& ar, const unsigned int version) {
ar& BOOST_SERIALIZATION_NVP(m_params); ar& BOOST_SERIALIZATION_NVP(m_params);
ar& BOOST_SERIALIZATION_NVP(m_name); ar& BOOST_SERIALIZATION_NVP(m_name);
ar& BOOST_SERIALIZATION_NVP(m_broker_last_datetime);
ar& BOOST_SERIALIZATION_NVP(m_costfunc); ar& BOOST_SERIALIZATION_NVP(m_costfunc);
ar& BOOST_SERIALIZATION_NVP(m_broker_last_datetime);
ar& BOOST_SERIALIZATION_NVP(m_broker_list);
} }
BOOST_SERIALIZATION_SPLIT_MEMBER() BOOST_SERIALIZATION_SPLIT_MEMBER()

View File

@ -40,7 +40,6 @@ add_requires("fmt", {system=false, configs = {header_only = true, vs_runtime = "
add_requires("spdlog", {system=false, configs = {header_only = true, fmt_external=true, vs_runtime = "MD"}}) add_requires("spdlog", {system=false, configs = {header_only = true, fmt_external=true, vs_runtime = "MD"}})
add_requires("flatbuffers", {system=false, configs = {vs_runtime="MD"}}) add_requires("flatbuffers", {system=false, configs = {vs_runtime="MD"}})
add_requires("nng", {system=false, configs = {vs_runtime="MD"}}) add_requires("nng", {system=false, configs = {vs_runtime="MD"}})
add_requires("yyjson", {system=false, configs = {vs_runtime="MD"}})
add_defines("SPDLOG_DISABLE_DEFAULT_LOGGER") -- 禁用 spdlog 默认 logger add_defines("SPDLOG_DISABLE_DEFAULT_LOGGER") -- 禁用 spdlog 默认 logger