From bdecc6a0bc6a624903ec98ca0870182f181c36f7 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Mon, 19 Feb 2018 12:01:27 +0800 Subject: [PATCH] =?UTF-8?q?PF=E6=B5=8B=E8=AF=95=E5=8F=8A=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=88=E7=BB=A7=E7=BB=AD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/hikyuu/trade_manage/TradeManager.h | 2 +- .../allocatefunds/AllocateFundsBase.cpp | 3 +- .../moneymanager/MoneyManagerBase.cpp | 1 + libs/hikyuu/trade_sys/portfolio/Portfolio.cpp | 85 ++++++++++--------- libs/hikyuu/trade_sys/portfolio/Portfolio.h | 7 +- .../trade_sys/portfolio/crt/PF_Simple.h | 2 - .../trade_sys/portfolio/imp/PF_Simple.cpp | 3 +- libs/hikyuu/trade_sys/system/System.cpp | 3 +- .../trade_manage/_TradeManager.cpp | 5 +- libs/hikyuu_python/trade_sys/_Portfolio.cpp | 7 +- ...est_Portfolio.cpp => test_PF_for_base.cpp} | 17 ++-- .../test_PF_for_delay_and_no_adjust.cpp | 78 +++++++++++++++++ 12 files changed, 148 insertions(+), 65 deletions(-) rename test/libs/hikyuu/trade_sys/portfolio/{test_Portfolio.cpp => test_PF_for_base.cpp} (60%) create mode 100644 test/libs/hikyuu/trade_sys/portfolio/test_PF_for_delay_and_no_adjust.cpp diff --git a/libs/hikyuu/trade_manage/TradeManager.h b/libs/hikyuu/trade_manage/TradeManager.h index fcf19af0..332056fe 100644 --- a/libs/hikyuu/trade_manage/TradeManager.h +++ b/libs/hikyuu/trade_manage/TradeManager.h @@ -84,7 +84,7 @@ public: string name() const { return m_name; } /** 设置账户名称 */ - void setName(const string& name) { m_name = name; } + void name(const string& name) { m_name = name; } /** 初始资金 */ price_t initCash() const { return m_init_cash; } diff --git a/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp b/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp index da594c0c..ef54c5df 100644 --- a/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp +++ b/libs/hikyuu/trade_sys/allocatefunds/AllocateFundsBase.cpp @@ -122,7 +122,8 @@ void AllocateFundsBase::_getAllocatedSystemList_not_adjust_hold( //从当前选中的系统列表中将持仓的系统排除 SystemList pure_se_list; for (auto iter = se_list.begin(); iter != se_list.end(); ++iter) { - if ((*iter)->getTM()->getStockNumber() == 0) { + //if ((*iter)->getTM()->getStockNumber() == 0) { + if (!(*iter)->getTM()->have((*iter)->getStock())) { pure_se_list.push_back(*iter); } } diff --git a/libs/hikyuu/trade_sys/moneymanager/MoneyManagerBase.cpp b/libs/hikyuu/trade_sys/moneymanager/MoneyManagerBase.cpp index c0c73f3f..2627918a 100644 --- a/libs/hikyuu/trade_sys/moneymanager/MoneyManagerBase.cpp +++ b/libs/hikyuu/trade_sys/moneymanager/MoneyManagerBase.cpp @@ -129,6 +129,7 @@ size_t MoneyManagerBase HKU_ERROR("risk is zero! Datetime(" << datetime << ") Stock(" << stock.market_code() << ") price(" << price << ") risk(" << risk + << ") Part(" << getSystemPartName(from) << ") [MoneyManagerBase::getBuyNumber]"); return 0; } diff --git a/libs/hikyuu/trade_sys/portfolio/Portfolio.cpp b/libs/hikyuu/trade_sys/portfolio/Portfolio.cpp index c800e7d6..b5021509 100644 --- a/libs/hikyuu/trade_sys/portfolio/Portfolio.cpp +++ b/libs/hikyuu/trade_sys/portfolio/Portfolio.cpp @@ -36,7 +36,6 @@ Portfolio::Portfolio(const string& name) : m_name(name) { } Portfolio::Portfolio(const TradeManagerPtr& tm, - const SystemPtr& sys, const SelectorPtr& se, const AFPtr& af) : m_name("Portfolio"), m_se(se), m_tm(tm), m_af(af) { @@ -52,7 +51,6 @@ void Portfolio::reset() { if (m_se) m_se->reset(); if (m_af) m_af->reset(); if (m_tm_shadow) m_tm_shadow->reset(); - m_sys_list.clear(); } PortfolioPtr Portfolio::clone() { @@ -63,7 +61,6 @@ PortfolioPtr Portfolio::clone() { if (m_af) p->m_af = m_af->clone(); if (m_tm) p->m_tm = m_tm->clone(); if (m_tm_shadow) p->m_tm_shadow = m_tm_shadow->clone(); - p->m_sys_list = m_sys_list; return p; } @@ -86,20 +83,10 @@ bool Portfolio::readyForRun() { reset(); - m_tm_shadow = m_tm->clone(); - //将影子账户指定给资产分配器 + m_tm_shadow = m_tm->clone(); m_af->setTM(m_tm_shadow); - //为每一个圈定的系统实例设置一个初始为0的子账户,并缓存所有的系统实例 - TMPtr pro_tm = crtTM(m_tm->initDatetime(), 0.0, m_tm->costFunc(), "SUB"); - SystemList all_sys_list = m_se->getAllSystemList(); - auto sysIter = all_sys_list.begin(); - for (; sysIter != all_sys_list.end(); ++sysIter) { - (*sysIter)->setTM(pro_tm->clone()); - m_sys_list.push_back(*sysIter); - } - return true; } @@ -108,22 +95,27 @@ void Portfolio::run(const KQuery& query) { return; } - auto sys_iter = m_sys_list.begin(); - for (; sys_iter != m_sys_list.end(); ++sys_iter) { - SystemPtr sys = *sys_iter; + SystemList delay_sys_list; //存储延迟模式的系统列表 + + SystemList all_sys_list = m_se->getAllSystemList(); + TMPtr pro_tm = crtTM(m_tm->initDatetime(), 0.0, m_tm->costFunc(), "SUB"); + auto sys_iter = all_sys_list.begin(); + for (; sys_iter != all_sys_list.end(); ++sys_iter) { + + //为每一个系统实例分配子账户 + SystemPtr& sys = *sys_iter; + sys->setTM(pro_tm->clone()); + if (sys->readyForRun()) { + if (sys->getParam("delay")) { + delay_sys_list.push_back(sys); + } + KData k = sys->getStock().getKData(query); sys->setTO(k); } } - SystemList delay_sys_list; - for (sys_iter = m_sys_list.begin(); sys_iter != m_sys_list.end(); ++sys_iter) { - if ((*sys_iter)->getParam("delay")) { - delay_sys_list.push_back(*sys_iter); - } - } - std::set cur_hold_sys_sets; DatetimeList datelist = StockManager::instance().getTradingCalendar(query); @@ -137,17 +129,28 @@ void Portfolio::run(const KQuery& query) { //处理延迟操作请求 sys_iter = delay_sys_list.begin(); for (; sys_iter != delay_sys_list.end(); ++sys_iter) { - (*sys_iter)->_processRequest(*date_iter); + SYSPtr& sys = *sys_iter; + if (!sys->haveDelayRequest()) { + continue; + } - //if ((*sys_iter)->getTM()->have((*sys_iter)->getStock())) { - if ((*sys_iter)->getTM()->getStockNumber() != 0) { - cur_hold_sys_sets.insert(*sys_iter); + Stock stk = sys->getStock(); + if (stk.isNull()) { + continue; + } + + KRecord kr = stk.getKRecordByDate(*date_iter); + //sys->_processRequest(kr); + + if (sys->getTM()->have(stk)) { + //if ((*sys_iter)->getTM()->getStockNumber() != 0) { + cur_hold_sys_sets.insert(sys); } else { - cur_hold_sys_sets.erase(*sys_iter); + cur_hold_sys_sets.erase(sys); } //同步交易记录 - TradeRecordList tr_list = (*sys_iter)->getTM()->getTradeList(*date_iter, Null()); + TradeRecordList tr_list = sys->getTM()->getTradeList(*date_iter, Null()); auto tr_iter = tr_list.begin(); for (; tr_iter != tr_list.end(); ++tr_iter) { m_tm_shadow->addTradeRecord(*tr_iter); @@ -165,11 +168,22 @@ void Portfolio::run(const KQuery& query) { SystemList ac_list = m_af->getAllocatedSystemList(*date_iter, selected_list, cur_hold_sys_list); + + std::cout << "=====================" << std::endl; for (sys_iter = ac_list.begin(); sys_iter != ac_list.end(); ++sys_iter) { - (*sys_iter)->runMoment(*date_iter); + SYSPtr& sys = *sys_iter; + sys->runMoment(*date_iter); + + std::cout << *date_iter << ": " << sys->getStock() << std::endl; + + if (sys->getTM()->have(sys->getStock())) { + cur_hold_sys_sets.insert(sys); + } else { + cur_hold_sys_sets.erase(sys); + } //同步交易记录 - TradeRecordList tr_list = (*sys_iter)->getTM()->getTradeList(*date_iter, Null()); + TradeRecordList tr_list = sys->getTM()->getTradeList(*date_iter, Null()); auto tr_iter = tr_list.begin(); for (; tr_iter != tr_list.end(); ++tr_iter) { m_tm_shadow->addTradeRecord(*tr_iter); @@ -196,12 +210,5 @@ void Portfolio::run(const KQuery& query) { } -void Portfolio::runOneMoment(Datetime date) { - if (date < m_tm->initDatetime()) { - return; - } - -} - } /* namespace hku */ diff --git a/libs/hikyuu/trade_sys/portfolio/Portfolio.h b/libs/hikyuu/trade_sys/portfolio/Portfolio.h index 5f2b76d5..e4e0a253 100644 --- a/libs/hikyuu/trade_sys/portfolio/Portfolio.h +++ b/libs/hikyuu/trade_sys/portfolio/Portfolio.h @@ -30,7 +30,6 @@ public: Portfolio(); Portfolio(const string& name); Portfolio(const TradeManagerPtr& tm, - const SystemPtr& sys, const SelectorPtr& st, const AFPtr& af); virtual ~Portfolio(); @@ -58,16 +57,14 @@ private: void rebuildOnlyTotalTM(); - void runOneMoment(Datetime); - protected: string m_name; SEPtr m_se; AFPtr m_af; TMPtr m_tm; - TMPtr m_tm_shadow; //临时变量,影子账户,用于内部协调分配资金 - SystemList m_sys_list; //临时变量,缓存从SE获取的全部系统实例,仅在调用readyForRun后有效 + //以下为临时变量 + TMPtr m_tm_shadow; //影子账户,用于内部协调分配资金 //============================================ // 序列化支持 diff --git a/libs/hikyuu/trade_sys/portfolio/crt/PF_Simple.h b/libs/hikyuu/trade_sys/portfolio/crt/PF_Simple.h index 19d6a336..4e094915 100644 --- a/libs/hikyuu/trade_sys/portfolio/crt/PF_Simple.h +++ b/libs/hikyuu/trade_sys/portfolio/crt/PF_Simple.h @@ -9,7 +9,6 @@ #define TRADE_SYS_PORTFOLIO_IMP_PF_SIMPLE_H_ #include "../Portfolio.h" -#include "../../system/crt/SYS_Simple.h" #include "../../selector/crt/SE_Fixed.h" #include "../../allocatefunds/crt/AF_EqualWeight.h" @@ -17,7 +16,6 @@ namespace hku { PortfolioPtr HKU_API PF_Simple( const TMPtr& tm = TradeManagerPtr(), - const SYSPtr& sys = SYS_Simple(), const SEPtr& st = SE_Fixed(), const AFPtr& af = AF_EqualWeight()); diff --git a/libs/hikyuu/trade_sys/portfolio/imp/PF_Simple.cpp b/libs/hikyuu/trade_sys/portfolio/imp/PF_Simple.cpp index 71867c6e..1bdf8ef4 100644 --- a/libs/hikyuu/trade_sys/portfolio/imp/PF_Simple.cpp +++ b/libs/hikyuu/trade_sys/portfolio/imp/PF_Simple.cpp @@ -11,10 +11,9 @@ namespace hku { PortfolioPtr HKU_API PF_Simple( const TMPtr& tm, - const SYSPtr& sys, const SEPtr& st, const AFPtr& af) { - return make_shared(tm, sys, st, af); + return make_shared(tm, st, af); } } /* namespace hku */ diff --git a/libs/hikyuu/trade_sys/system/System.cpp b/libs/hikyuu/trade_sys/system/System.cpp index 2d56b070..b369ea88 100644 --- a/libs/hikyuu/trade_sys/system/System.cpp +++ b/libs/hikyuu/trade_sys/system/System.cpp @@ -100,7 +100,8 @@ void System::initParam() { //连续延迟交易请求的限制次数,需大于等于0,0表示只允许延迟1次 setParam("max_delay_count", 3); - setParam("delay", true); //延迟操作,取当前Bar的收盘价操作;非延迟取开盘价 + //是否延迟到下一个bar开盘时进行交易 + setParam("delay", true); //非延迟操作取当前Bar的收盘价操作;延迟取下一BAR开盘价 //延迟操作的情况下,是使用当前的价格计算新的止损价/止赢价/目标价还是使用上次计算的结果 setParam("delay_use_current_price", true); diff --git a/libs/hikyuu_python/trade_manage/_TradeManager.cpp b/libs/hikyuu_python/trade_manage/_TradeManager.cpp index d4093a15..1c42f9a8 100644 --- a/libs/hikyuu_python/trade_manage/_TradeManager.cpp +++ b/libs/hikyuu_python/trade_manage/_TradeManager.cpp @@ -59,6 +59,9 @@ BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(sellShort_overload, sellShort, 4, 8); TradeCostPtr (TradeManager::*get_costFunc)() const = &TradeManager::costFunc; void (TradeManager::*set_costFunc)(const TradeCostPtr&) = &TradeManager::costFunc; +string (TradeManager::*tm_get_name)() const = &TradeManager::name; +void (TradeManager::*tm_set_name)(const string&) = &TradeManager::name; + const TradeRecordList& (TradeManager::*_getTradeList_1)() const = &TradeManager::getTradeList; TradeRecordList (TradeManager::*_getTradeList_2)(const Datetime&, const Datetime&) const = &TradeManager::getTradeList; @@ -70,7 +73,7 @@ void export_TradeManager() { const TradeCostPtr&, const string&>()) //.def(self_ns::str(self)) .def("__str__", &TradeManager::toString) - .add_property("name", &TradeManager::name, &TradeManager::setName) + .add_property("name", tm_get_name, tm_set_name) .add_property("initCash", &TradeManager::initCash) .add_property("currentCash", &TradeManager::currentCash) .add_property("initDatetime", &TradeManager::initDatetime) diff --git a/libs/hikyuu_python/trade_sys/_Portfolio.cpp b/libs/hikyuu_python/trade_sys/_Portfolio.cpp index 42746c69..1b1b91d3 100644 --- a/libs/hikyuu_python/trade_sys/_Portfolio.cpp +++ b/libs/hikyuu_python/trade_sys/_Portfolio.cpp @@ -13,7 +13,7 @@ using namespace boost::python; using namespace hku; -BOOST_PYTHON_FUNCTION_OVERLOADS(PF_Simple_overload, PF_Simple, 0, 4); +BOOST_PYTHON_FUNCTION_OVERLOADS(PF_Simple_overload, PF_Simple, 0, 3); void (Portfolio::*pf_set_name)(const string&) = &Portfolio::name; string (Portfolio::*pf_get_name)() const= &Portfolio::name; @@ -23,9 +23,8 @@ void export_Portfolio() { class_("Portfolio", init<>()) .def(init()) .def(init()) + const SelectorPtr&, + const AFPtr&>()) .def(self_ns::str(self)) .def("getParam", &Portfolio::getParam) .def("setParam", &Portfolio::setParam) diff --git a/test/libs/hikyuu/trade_sys/portfolio/test_Portfolio.cpp b/test/libs/hikyuu/trade_sys/portfolio/test_PF_for_base.cpp similarity index 60% rename from test/libs/hikyuu/trade_sys/portfolio/test_Portfolio.cpp rename to test/libs/hikyuu/trade_sys/portfolio/test_PF_for_base.cpp index 899d7ae1..1ce5fd90 100644 --- a/test/libs/hikyuu/trade_sys/portfolio/test_Portfolio.cpp +++ b/test/libs/hikyuu/trade_sys/portfolio/test_PF_for_base.cpp @@ -14,7 +14,7 @@ #endif #include -#include +#include using namespace hku; @@ -24,16 +24,15 @@ using namespace hku; * @{ */ -/** @par 检测点 */ -BOOST_AUTO_TEST_CASE( test_Portfolio) { - PortfolioPtr pf = make_shared(); - +/** @par 检测点 Portfolio基础操作 */ +BOOST_AUTO_TEST_CASE( test_PF_for_base) { + PortfolioPtr pf = PF_Simple(); BOOST_CHECK(pf->name() == "Portfolio"); - StockManager& sm = StockManager::instance(); - - //pf->addStock(sm["sh000001"]); - //std::cout << sys << std::endl; + /** @arg 克隆操作 */ + PFPtr pf2 = pf->clone(); + BOOST_CHECK(pf2.get() != pf.get()); + BOOST_CHECK(pf2->name() == pf->name()); } /** @} */ diff --git a/test/libs/hikyuu/trade_sys/portfolio/test_PF_for_delay_and_no_adjust.cpp b/test/libs/hikyuu/trade_sys/portfolio/test_PF_for_delay_and_no_adjust.cpp new file mode 100644 index 00000000..176d00eb --- /dev/null +++ b/test/libs/hikyuu/trade_sys/portfolio/test_PF_for_delay_and_no_adjust.cpp @@ -0,0 +1,78 @@ +/* + * test_SYS_Simple.cpp + * + * Created on: 2013-4-20 + * Author: fasiondog + */ + + +#ifdef TEST_ALL_IN_ONE + #include +#else + #define BOOST_TEST_MODULE test_hikyuu_trade_sys_suite + #include +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + + +using namespace hku; + +/** + * @defgroup test_Portfolio test_Portfolio + * @ingroup test_hikyuu_trade_sys_suite + * @{ + */ + +/** @par 检测点 全部为delay模式系统实例,不调整持仓*/ +BOOST_AUTO_TEST_CASE( test_PF_for_delay_and_no_adjust) { + StockManager& sm = StockManager::instance(); + + + SYSPtr sys = SYS_Simple(); + sys->setSG(SG_CrossGold(OP(EMA(12)), OP(EMA(26)))); + sys->setMM(MM_FixedCount(100)); + SYSPtr pro_sys = sys->clone(false, false); + + TMPtr tm = crtTM(Datetime(199001010000L), 500000); + SEPtr se = SE_Fixed(); + AFPtr af = AF_EqualWeight(); + PFPtr pf = PF_Simple(tm, se, af); + + KQuery query = KQueryByDate(Datetime(201101010000L), Null(), + KQuery::DAY); + + + + /** @arg */ + sys->setTM(tm->clone()); + sys->run(sm["sz000001"], query); + sys->getTM()->tocsv(sm.tmpdir()); + + se->addStock(sm["sz000001"], pro_sys); + pf->run(query); + tm->name("PF"); + tm->tocsv(sm.tmpdir()); + + TradeRecordList tr1 = sys->getTM()->getTradeList(); + TradeRecordList tr2 = tm->getTradeList(); + BOOST_CHECK(tr1.size() == tr2.size()); + + std::cout << tr1.size() << std::endl; + std::cout << tr2.size() << std::endl; + + +} + +/** @} */ + +