mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-03 04:17:58 +08:00
PF测试及修改(继续)
This commit is contained in:
parent
88f083aaf0
commit
bdecc6a0bc
@ -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; }
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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,19 +95,24 @@ 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()) {
|
||||
KData k = sys->getStock().getKData(query);
|
||||
sys->setTO(k);
|
||||
}
|
||||
if (sys->getParam<bool>("delay")) {
|
||||
delay_sys_list.push_back(sys);
|
||||
}
|
||||
|
||||
SystemList delay_sys_list;
|
||||
for (sys_iter = m_sys_list.begin(); sys_iter != m_sys_list.end(); ++sys_iter) {
|
||||
if ((*sys_iter)->getParam<bool>("delay")) {
|
||||
delay_sys_list.push_back(*sys_iter);
|
||||
KData k = sys->getStock().getKData(query);
|
||||
sys->setTO(k);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<Datetime>());
|
||||
TradeRecordList tr_list = sys->getTM()->getTradeList(*date_iter, Null<Datetime>());
|
||||
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<Datetime>());
|
||||
TradeRecordList tr_list = sys->getTM()->getTradeList(*date_iter, Null<Datetime>());
|
||||
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 */
|
||||
|
@ -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; //影子账户,用于内部协调分配资金
|
||||
|
||||
//============================================
|
||||
// 序列化支持
|
||||
|
@ -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());
|
||||
|
||||
|
@ -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<Portfolio>(tm, sys, st, af);
|
||||
return make_shared<Portfolio>(tm, st, af);
|
||||
}
|
||||
|
||||
} /* namespace hku */
|
||||
|
@ -100,7 +100,8 @@ void System::initParam() {
|
||||
//连续延迟交易请求的限制次数,需大于等于0,0表示只允许延迟1次
|
||||
setParam<int>("max_delay_count", 3);
|
||||
|
||||
setParam<bool>("delay", true); //延迟操作,取当前Bar的收盘价操作;非延迟取开盘价
|
||||
//是否延迟到下一个bar开盘时进行交易
|
||||
setParam<bool>("delay", true); //非延迟操作取当前Bar的收盘价操作;延迟取下一BAR开盘价
|
||||
|
||||
//延迟操作的情况下,是使用当前的价格计算新的止损价/止赢价/目标价还是使用上次计算的结果
|
||||
setParam<bool>("delay_use_current_price", true);
|
||||
|
@ -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)
|
||||
|
@ -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,7 +23,6 @@ void export_Portfolio() {
|
||||
class_<Portfolio>("Portfolio", init<>())
|
||||
.def(init<const string&>())
|
||||
.def(init<const TradeManagerPtr&,
|
||||
const SystemPtr&,
|
||||
const SelectorPtr&,
|
||||
const AFPtr&>())
|
||||
.def(self_ns::str(self))
|
||||
|
@ -14,7 +14,7 @@
|
||||
#endif
|
||||
|
||||
#include <hikyuu/StockManager.h>
|
||||
#include <hikyuu/trade_sys/portfolio/Portfolio.h>
|
||||
#include <hikyuu/trade_sys/portfolio/crt/PF_Simple.h>
|
||||
|
||||
using namespace hku;
|
||||
|
||||
@ -24,16 +24,15 @@ using namespace hku;
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @par 检测点 */
|
||||
BOOST_AUTO_TEST_CASE( test_Portfolio) {
|
||||
PortfolioPtr pf = make_shared<Portfolio>();
|
||||
|
||||
/** @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());
|
||||
}
|
||||
|
||||
/** @} */
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* test_SYS_Simple.cpp
|
||||
*
|
||||
* Created on: 2013-4-20
|
||||
* Author: fasiondog
|
||||
*/
|
||||
|
||||
|
||||
#ifdef TEST_ALL_IN_ONE
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#else
|
||||
#define BOOST_TEST_MODULE test_hikyuu_trade_sys_suite
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#endif
|
||||
|
||||
#include <hikyuu/StockManager.h>
|
||||
#include <hikyuu/trade_manage/crt/crtTM.h>
|
||||
#include <hikyuu/trade_sys/portfolio/crt/PF_Simple.h>
|
||||
#include <hikyuu/trade_sys/selector/crt/SE_Fixed.h>
|
||||
#include <hikyuu/trade_sys/allocatefunds/crt/AF_EqualWeight.h>
|
||||
|
||||
#include <hikyuu/trade_sys/system/crt/SYS_Simple.h>
|
||||
#include <hikyuu/trade_sys/signal/crt/SG_CrossGold.h>
|
||||
#include <hikyuu/trade_sys/moneymanager/crt/MM_FixedCount.h>
|
||||
#include <hikyuu/indicator/crt/EMA.h>
|
||||
|
||||
|
||||
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<Datetime>(),
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user