Skip to content

Commit

Permalink
Merge pull request fasiondog#276 from fasiondog/feature/strategey
Browse files Browse the repository at this point in the history
Feature/strategey TradeManager 增加 addPosition 用于构建存在初始持仓的账户
  • Loading branch information
fasiondog authored Aug 16, 2024
2 parents 8189538 + ba66be0 commit c010caf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
6 changes: 4 additions & 2 deletions hikyuu_cpp/hikyuu/GlobalInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions hikyuu_cpp/hikyuu/trade_manage/TradeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Datetime>(), 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,
Expand Down
9 changes: 9 additions & 0 deletions hikyuu_cpp/hikyuu/trade_manage/TradeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions hikyuu_cpp/hikyuu/trade_manage/TradeManagerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
11 changes: 11 additions & 0 deletions hikyuu_pywrap/trade_manage/_TradeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, );
}
Expand Down Expand Up @@ -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格式输出交易记录、未平仓记录、已平仓记录、资产净值曲线
Expand Down

0 comments on commit c010caf

Please sign in to comment.