Skip to content

Commit

Permalink
System “delay" 参数拆分为 "buy_delay" 和 "sell_delay" 分别控制
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Sep 17, 2022
1 parent 4745dde commit 58bca1b
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 34 deletions.
3 changes: 2 additions & 1 deletion docs/source/trade_sys/system.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

公共参数:

* **delay=True** *(bool)* : 是否延迟到下一个bar开盘时进行交易
* **buy_delay=True** *(bool)* : 买入操作是否延迟到下一个bar开盘时进行交易
* **sell_delay=True** *(bool)* : 卖出操作是否延迟到下一个bar开盘时进行交易
* **delay_use_current_price=True** *(bool)* : 延迟操作的情况下,是使用当前交易时bar的价格计算新的止损价/止赢价/目标价还是使用上次计算的结果
* **max_delay_count=3** *(int)* : 连续延迟交易请求的限制次数,应大于等于0,0表示只允许延迟1次
* **tp_monotonic=True** *(bool)* : 止赢单调递增
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,9 @@ void AllocateFundsBase::_adjust_with_running(const Datetime& date, const SystemL
KRecord k = kdata.getKRecord(pos);
KRecord srcK = stock.getKRecord(kdata.startPos() + pos);
TradeRecord tr;
double need_sell_num = sys->getParam<bool>("delay") ? need_cash / k.closePrice
: need_cash / k.openPrice;
double need_sell_num = sys->getParam<bool>("sell_delay")
? need_cash / k.closePrice
: need_cash / k.openPrice;
if (position.number <= need_sell_num) {
// 如果当前持仓数小于等于需要卖出的数量,则全部卖出
tr = sys->sellForce(k, srcK, position.number, PART_ALLOCATEFUNDS);
Expand Down
4 changes: 2 additions & 2 deletions hikyuu_cpp/hikyuu/trade_sys/portfolio/Portfolio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void Portfolio::_runMomentOnOpen(const Datetime& date) {

// 在开盘时执行所有运行中的非延迟交易系统系统
for (auto& sub_sys : m_running_sys_list) {
if (!sub_sys->getParam<bool>("delay")) {
if (!sub_sys->getParam<bool>("buy_delay")) {
auto tr = sub_sys->runMoment(date);
if (!tr.isNull()) {
m_tm->addTradeRecord(tr);
Expand Down Expand Up @@ -263,7 +263,7 @@ void Portfolio::_runMomentOnClose(const Datetime& date) {

// 执行所有非延迟运行中系统
for (auto& sub_sys : m_running_sys_list) {
if (sub_sys->getParam<bool>("delay")) {
if (sub_sys->getParam<bool>("buy_delay")) {
auto tr = sub_sys->runMoment(date);
if (!tr.isNull()) {
m_tm->addTradeRecord(tr);
Expand Down
4 changes: 2 additions & 2 deletions hikyuu_cpp/hikyuu/trade_sys/selector/imp/FixedSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bool FixedSelector::isMatchAF(const AFPtr& af) {
SystemList FixedSelector::getSelectedOnOpen(Datetime date) {
SystemList result;
for (auto& sys : m_real_sys_list) {
if (!sys->getParam<bool>("delay")) {
if (!sys->getParam<bool>("buy_delay")) {
result.emplace_back(sys);
}
}
Expand All @@ -30,7 +30,7 @@ SystemList FixedSelector::getSelectedOnOpen(Datetime date) {
SystemList FixedSelector::getSelectedOnClose(Datetime date) {
SystemList result;
for (auto& sys : m_real_sys_list) {
if (sys->getParam<bool>("delay")) {
if (sys->getParam<bool>("buy_delay")) {
result.emplace_back(sys);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void SignalSelector::_calculate() {
auto sg = sys->getSG();
auto dates = sg->getBuySignal();
unordered_map<Datetime, SystemList>* date_dict;
date_dict = sys->getParam<bool>("delay") ? &m_sys_dict_on_close : &m_sys_dict_on_open;
date_dict = sys->getParam<bool>("buy_delay") ? &m_sys_dict_on_close : &m_sys_dict_on_open;
for (auto& date : dates) {
auto iter = date_dict->find(date);
if (iter != date_dict->end()) {
Expand Down
13 changes: 7 additions & 6 deletions hikyuu_cpp/hikyuu/trade_sys/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ void System::initParam() {
setParam<int>("max_delay_count", 3);

//是否延迟到下一个bar开盘时进行交易
setParam<bool>("delay", true); //非延迟操作取当前Bar的收盘价操作;延迟取下一BAR开盘价
setParam<bool>("buy_delay", true); //非延迟操作取当前Bar的收盘价操作;延迟取下一BAR开盘价
setParam<bool>("sell_delay", true);

//延迟操作的情况下,是使用当前的价格计算新的止损价/止赢价/目标价还是使用上次计算的结果
setParam<bool>("delay_use_current_price", true);
Expand Down Expand Up @@ -467,7 +468,7 @@ TradeRecord System::_runMoment(const KRecord& today, const KRecord& src_today) {

TradeRecord System::_buy(const KRecord& today, const KRecord& src_today, Part from) {
TradeRecord result;
if (getParam<bool>("delay")) {
if (getParam<bool>("buy_delay")) {
_submitBuyRequest(today, src_today, from);
return result;
} else {
Expand Down Expand Up @@ -599,7 +600,7 @@ TradeRecord System::sellForce(const KRecord& today, const KRecord& src_today, do
HKU_ASSERT_M(from == PART_ALLOCATEFUNDS || from == PART_PORTFOLIO,
"Only Allocator or Portfolis can perform this operation!");
TradeRecord result;
if (getParam<bool>("delay")) {
if (getParam<bool>("sell_delay")) {
if (m_sellRequest.valid) {
if (m_sellRequest.count > getParam<int>("max_delay_count")) {
//超出最大延迟次数,清除买入请求
Expand Down Expand Up @@ -635,7 +636,7 @@ TradeRecord System::sellForce(const KRecord& today, const KRecord& src_today, do

TradeRecord System::_sell(const KRecord& today, const KRecord& src_today, Part from) {
TradeRecord result;
if (getParam<bool>("delay")) {
if (getParam<bool>("sell_delay")) {
_submitSellRequest(today, src_today, from);
return result;
} else {
Expand Down Expand Up @@ -771,7 +772,7 @@ TradeRecord System::_buyShort(const KRecord& today, const KRecord& src_today, Pa
if (getParam<bool>("support_borrow_stock") == false)
return result;

if (getParam<bool>("delay")) {
if (getParam<bool>("buy_delay")) {
_submitBuyShortRequest(today, src_today, from);
return result;
} else {
Expand Down Expand Up @@ -915,7 +916,7 @@ TradeRecord System::_sellShort(const KRecord& today, const KRecord& src_today, P
if (getParam<bool>("support_borrow_stock") == false)
return result;

if (getParam<bool>("delay")) {
if (getParam<bool>("sell_delay")) {
_submitSellShortRequest(today, src_today, from);
return result;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ TEST_CASE("test_SYS_Simple_for_base") {

/** @arg 指定了TM、SG、MM,但未指定其他策略组件,非延迟操作 */
sys = SYS_Simple();
sys->setParam("delay", false);
sys->setParam("buy_delay", false);
sys->setParam("sell_delay", false);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
sys->setMM(mm->clone());
Expand Down Expand Up @@ -128,7 +129,8 @@ TEST_CASE("test_SYS_Simple_for_base") {

/** @arg 指定了TM、SG、MM,但未指定其他策略组件,延迟操作 */
sys = SYS_Simple();
sys->setParam("delay", true);
sys->setParam("buy_delay", true);
sys->setParam("sell_delay", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
sys->setMM(mm->clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ TEST_CASE("test_SYS_Simple_for_cn") {

/** @arg 指定了TM、SG、MM、ST、TP、EV、CN,CN有效日期和EV重合 */
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down Expand Up @@ -122,7 +123,8 @@ TEST_CASE("test_SYS_Simple_for_cn") {

/** @arg 指定了TM、SG、MM、ST、TP、EV、CN,CN的有效日期范围完全覆盖并大于EV的范围 */
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down Expand Up @@ -180,7 +182,8 @@ TEST_CASE("test_SYS_Simple_for_cn") {

/** @arg 指定了TM、SG、MM、EV、CN(不触发建仓),CN的有效日期范围在EV的范围之内 */
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", true);
sys->setParam<bool>("cn_open_position", false);
sys->setTM(tm->clone());
Expand Down Expand Up @@ -237,7 +240,8 @@ TEST_CASE("test_SYS_Simple_for_cn") {

/** @arg 指定了TM、SG、MM、EV、CN(触发建仓),CN的有效日期范围在EV的范围之内 */
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", true);
sys->setParam<bool>("cn_open_position", true);
sys->setTM(tm->clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ TEST_CASE("test_SYS_Simple_for_ev") {

/** @arg 指定了TM、SG、MM、ST、TP、EV(不触发建仓),但未指定其他策略组件,非延迟操作 */
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", false);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down Expand Up @@ -104,7 +105,8 @@ TEST_CASE("test_SYS_Simple_for_ev") {

/** @arg 指定了TM、SG、MM、ST、TP、EV(触发建仓),但未指定其他策略组件,非延迟操作 */
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down Expand Up @@ -161,7 +163,8 @@ TEST_CASE("test_SYS_Simple_for_ev") {

/** @arg 指定了TM、SG、MM、ST、TP、EV(不触发建仓),但未指定其他策略组件,延迟操作 */
sys = SYS_Simple();
sys->setParam<bool>("delay", true);
sys->setParam<bool>("buy_delay", true);
sys->setParam<bool>("sell_delay", true);
sys->setParam<bool>("ev_open_position", false);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down Expand Up @@ -205,7 +208,8 @@ TEST_CASE("test_SYS_Simple_for_ev") {

/** @arg 指定了TM、SG、MM、ST、TP、EV(触发建仓),但未指定其他策略组件,延迟操作 */
sys = SYS_Simple();
sys->setParam<bool>("delay", true);
sys->setParam<bool>("buy_delay", true);
sys->setParam<bool>("sell_delay", true);
sys->setParam<bool>("ev_open_position", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down Expand Up @@ -260,9 +264,12 @@ TEST_CASE("test_SYS_Simple_for_ev") {
CHECK_LT(std::fabs(tr_list[3].cash - current_cash), 0.00001);
CHECK_EQ(tr_list[3].from, PART_ENVIRONMENT);

/** @arg 指定了TM、SG、MM、EV(刚好覆盖一对买入/卖出信号、不触发建仓),但未指定其他策略组件,非延迟操作 */
/** @arg
* 指定了TM、SG、MM、EV(刚好覆盖一对买入/卖出信号、不触发建仓),但未指定其他策略组件,非延迟操作
*/
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", false);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down Expand Up @@ -302,9 +309,12 @@ TEST_CASE("test_SYS_Simple_for_ev") {
CHECK_LT(std::fabs(tr_list[2].cash - current_cash), 0.00001);
CHECK_EQ(tr_list[2].from, PART_SIGNAL);

/** @arg 指定了TM、SG、MM、EV(刚好覆盖一对买入/卖出信号、触发建仓),但未指定其他策略组件,非延迟操作 */
/** @arg
* 指定了TM、SG、MM、EV(刚好覆盖一对买入/卖出信号、触发建仓),但未指定其他策略组件,非延迟操作
*/
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down Expand Up @@ -346,7 +356,8 @@ TEST_CASE("test_SYS_Simple_for_ev") {

/** @arg 指定了TM、SG、MM、EV(触发建仓),EV的有效起始日期刚好是买入信号日期 */
sys = SYS_Simple();
sys->setParam<bool>("delay", false);
sys->setParam<bool>("buy_delay", false);
sys->setParam<bool>("sell_delay", false);
sys->setParam<bool>("ev_open_position", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ TEST_CASE("test_SYS_Simple_for_pg") {

/** @arg 指定了TM、SG、MM、ST、TP、PG,但未指定其他策略组件,非延迟操作 */
sys = SYS_Simple();
sys->setParam("delay", false);
sys->setParam("buy_delay", false);
sys->setParam("sell_delay", false);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
sys->setMM(mm->clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ TEST_CASE("test_SYS_Simple_for_st") {

/** @arg 指定了TM、SG、MM、ST,但未指定其他策略组件,非延迟操作 */
sys = SYS_Simple();
sys->setParam("delay", false);
sys->setParam("buy_delay", false);
sys->setParam("sell_delay", false);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
sys->setMM(mm->clone());
Expand Down Expand Up @@ -103,7 +104,8 @@ TEST_CASE("test_SYS_Simple_for_st") {

/** @arg 指定了TM、SG、MM、ST,但未指定其他策略组件,延迟操作 */
sys = SYS_Simple();
sys->setParam("delay", true);
sys->setParam("buy_delay", true);
sys->setParam("sell_delay", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
sys->setMM(mm->clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ TEST_CASE("test_SYS_Simple_for_tp") {

/** @arg 指定了TM、SG、MM、ST、TP,但未指定其他策略组件,非延迟操作 */
sys = SYS_Simple();
sys->setParam("delay", false);
sys->setParam("buy_delay", false);
sys->setParam("sell_delay", false);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
sys->setMM(mm->clone());
Expand Down Expand Up @@ -118,7 +119,8 @@ TEST_CASE("test_SYS_Simple_for_tp") {

/** @arg 指定了TM、SG、MM、ST、TP,但未指定其他策略组件,延迟操作 */
sys = SYS_Simple();
sys->setParam("delay", true);
sys->setParam("buy_delay", true);
sys->setParam("sell_delay", true);
sys->setTM(tm->clone());
sys->setSG(sg->clone());
sys->setMM(mm->clone());
Expand Down

0 comments on commit 58bca1b

Please sign in to comment.