diff --git a/hikyuu_cpp/hikyuu/GlobalInitializer.cpp b/hikyuu_cpp/hikyuu/GlobalInitializer.cpp index e30389910..2552143b7 100644 --- a/hikyuu_cpp/hikyuu/GlobalInitializer.cpp +++ b/hikyuu_cpp/hikyuu/GlobalInitializer.cpp @@ -74,15 +74,17 @@ void GlobalInitializer::init() { } void GlobalInitializer::clean() { - if (CanUpgrade()) { +#if HKU_ENABLE_SEND_FEEDBACK + if (runningInPython() && CanUpgrade()) { fmt::print( "\n====================================================================\n" "The new version of Hikyuu is {}, and you can run the upgrade command:\n" - "Hikyuu 的最新新版本是 {}, 您可以运行升级命令:\n" + "Hikyuu 的最新版本是 {}, 您可以运行升级命令:\n" "pip install hikyuu --upgrade\n" "========================================================\n\n", getLatestVersion(), getLatestVersion()); } +#endif releaseGlobalTaskGroup(); releaseScheduler(); diff --git a/hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp b/hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp index bc2212b26..0c11e489e 100644 --- a/hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp +++ b/hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp @@ -1709,6 +1709,26 @@ void TradeManager::tocsv(const string& path) { file.close(); } +bool TradeManager::addPosition(const PositionRecord& pr) { + HKU_ERROR_IF_RETURN(pr.stock.isNull(), false, "Invalid postion record! stock is null!"); + HKU_ERROR_IF_RETURN(pr.cleanDatetime != Null(), false, + "Position cleanDatetime({}) must be Null!", pr.cleanDatetime); + HKU_ERROR_IF_RETURN(pr.takeDatetime < initDatetime(), false, + "Poistion takeDatetime({}) > initDatetime({})", pr.takeDatetime, + initDatetime()); + HKU_ERROR_IF_RETURN(!m_trade_list.empty(), false, "Exist trade list!"); + + auto iter = m_position.find(pr.stock.id()); + HKU_ERROR_IF_RETURN(iter != m_position.end(), false, "The stock({}) has position!", + pr.stock.market_code()); + + m_position[pr.stock.id()] = pr; + if (pr.takeDatetime > m_init_datetime) { + m_init_datetime = pr.takeDatetime; + } + return true; +} + bool TradeManager::addTradeRecord(const TradeRecord& tr) { HKU_IF_RETURN(BUSINESS_INIT == tr.business, _add_init_tr(tr)); HKU_ERROR_IF_RETURN(tr.datetime < lastDatetime(), false, diff --git a/hikyuu_cpp/hikyuu/trade_manage/TradeManager.h b/hikyuu_cpp/hikyuu/trade_manage/TradeManager.h index 8700d69ac..2e4d5bd15 100644 --- a/hikyuu_cpp/hikyuu/trade_manage/TradeManager.h +++ b/hikyuu_cpp/hikyuu/trade_manage/TradeManager.h @@ -344,6 +344,15 @@ class HKU_API TradeManager : public TradeManagerBase { */ virtual bool addTradeRecord(const TradeRecord& tr) override; + /** + * 直接加入持仓记录 + * @note 特殊用途构建初始持仓,可能引起混乱 + * @param pr 持仓记录 + * @return true 成功 + * @return false 失败 + */ + virtual bool addPosition(const PositionRecord& pr) override; + /** 字符串输出 */ virtual string str() const override; diff --git a/hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h b/hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h index 16d255c3c..595f1de19 100644 --- a/hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h +++ b/hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h @@ -675,6 +675,17 @@ class HKU_API TradeManagerBase { return false; } + /** + * 直接加入持仓记录 + * @param pr 持仓记录 + * @return true 成功 + * @return false 失败 + */ + virtual bool addPosition(const PositionRecord& pr) { + HKU_WARN("The subclass does not implement this method"); + return false; + } + /** 字符串输出 */ virtual string str() const { HKU_WARN("The subclass does not implement this method"); diff --git a/hikyuu_pywrap/trade_manage/_TradeManager.cpp b/hikyuu_pywrap/trade_manage/_TradeManager.cpp index 9b65c428d..732cf355c 100644 --- a/hikyuu_pywrap/trade_manage/_TradeManager.cpp +++ b/hikyuu_pywrap/trade_manage/_TradeManager.cpp @@ -220,6 +220,10 @@ class PyTradeManagerBase : public TradeManagerBase { PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "add_trade_record", addTradeRecord, tr); } + bool addPosition(const PositionRecord& pr) override { + PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "add_position", addPosition, pr); + } + string str() const override { PYBIND11_OVERRIDE_NAME(string, TradeManagerBase, "__str__", str, ); } @@ -544,6 +548,13 @@ void export_TradeManager(py::module& m) { :return: True(成功) | False(失败) :rtype: bool)") + .def("add_position", &TradeManagerBase::addPosition, R"(add_postion(self, position) + + 建立初始账户后,直接加入持仓记录,仅用于构建初始有持仓的账户 + + :param PositionRecord position: 持仓记录 + return True | False)") + .def("tocsv", &TradeManagerBase::tocsv, R"(tocsv(self, path) 以csv格式输出交易记录、未平仓记录、已平仓记录、资产净值曲线