Skip to content

Commit

Permalink
fixed CycleSignale
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed May 7, 2024
1 parent 2169f3f commit 9abafb4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions hikyuu_cpp/hikyuu/trade_sys/portfolio/Portfolio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ void Portfolio::_runMoment(const Datetime& date, const Datetime& nextCycle, bool
// 强制卖出失败的情况下,如果当前仍有持仓,则需要下一交易日继续进行处理
PositionRecord position = sys.sys->getTM()->getPosition(date, sys.sys->getStock());
if (position.number > 0.0) {
HKU_INFO_IF("[{}] failed to force sell, delay to next day", name());
tmp_continue_adjust_sys_list.emplace_back(sys);
}
}
Expand Down Expand Up @@ -357,17 +358,18 @@ void Portfolio::_runMoment(const Datetime& date, const Datetime& nextCycle, bool
if (trace) {
auto funds = m_tm->getFunds(date, m_query.kType());
HKU_INFO("[PF] [after adjust] - total funds: {}, cash: {}, market_value: {}",
funds.cash + funds.market_value, funds.cash, funds.market_value);
funds.total_assets(), funds.cash, funds.market_value);
}

//----------------------------------------------------------------------------
// 执行所有运行中的系统,无论是延迟还是非延迟,当天运行中的系统都需要被执行一次
//----------------------------------------------------------------------------
for (auto& sub_sys : m_running_sys_set) {
HKU_TRACE_IF(trace, "run: {}", sub_sys->name());
HKU_INFO_IF(trace, "[PF] run: {}", sub_sys->name());
if (adjust) {
auto sg = sub_sys->getSG();
sg->startCycle(date, nextCycle);
HKU_INFO_IF(trace, "[PF] sg should buy: {}", sg->shouldBuy(date));
}

auto tr = sub_sys->runMoment(date);
Expand Down
5 changes: 5 additions & 0 deletions hikyuu_cpp/hikyuu/trade_sys/signal/imp/CycleSignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ namespace hku {

CycleSignal::CycleSignal() : SignalBase("SG_AllwaysBuy") {
setParam<bool>("cycle", true);
setParam<bool>("alternate", false);
}

void CycleSignal::_checkParam(const string& name) const {
if ("cycle" == name) {
bool cycle = getParam<bool>(name);
HKU_CHECK(cycle, "param cycle must be true!");
} else if ("alternate" == name) {
bool alternate = getParam<bool>("alternate");
HKU_CHECK(!alternate, "param alternate must be false!");
}
}

void CycleSignal::_calculate(const KData& kdata) {
HKU_INFO("cycle start: {}", getCycleStart());
_addBuySignal(getCycleStart());
}

Expand Down
10 changes: 10 additions & 0 deletions hikyuu_cpp/hikyuu/trade_sys/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ TradeRecord System::_runMoment(const KRecord& today, const KRecord& src_today) {

// 如果当前环境无效
if (!current_ev_valid) {
HKU_INFO_IF(trace, "[{}] current EV is invalid", name());
TradeRecord tr;
// 如果持有多头仓位,则立即清仓卖出
if (m_tm->have(m_stock)) {
Expand All @@ -465,6 +466,8 @@ TradeRecord System::_runMoment(const KRecord& today, const KRecord& src_today) {

// 环境是从无效变为有效时
if (!m_pre_ev_valid) {
HKU_INFO_IF(trace, "[{}] EV status from invalid to valid", name());

// 如果使用环境判定策略进行初始建仓
if (getParam<bool>("ev_open_position")) {
HKU_INFO_IF(trace, "[{}] EV to buy", name());
Expand All @@ -484,6 +487,7 @@ TradeRecord System::_runMoment(const KRecord& today, const KRecord& src_today) {

// 如果系统当前无效
if (!current_cn_valid) {
HKU_INFO_IF(trace, "[{}] current CN is invalid", name());
TradeRecord tr;
// 如果持有多头仓位,则立即清仓卖出
if (m_tm->have(m_stock)) {
Expand All @@ -497,6 +501,8 @@ TradeRecord System::_runMoment(const KRecord& today, const KRecord& src_today) {

// 如果系统从无效变为有效
if (!m_pre_cn_valid) {
HKU_INFO_IF(trace, "[{}] CN status from invalid to valid", name());

// 如果使用环境判定策略进行初始建仓
if (getParam<bool>("cn_open_position")) {
HKU_INFO_IF(trace, "[{}] CN to buy", name());
Expand Down Expand Up @@ -550,6 +556,7 @@ TradeRecord System::_runMoment(const KRecord& today, const KRecord& src_today) {
price_t src_current_price = src_today.closePrice; // 未复权的原始价格

PositionRecord position = m_tm->getPosition(today.datetime, m_stock);
HKU_INFO_IF(trace, "[{}] current postion: {}", name(), position.number);
if (position.number != 0) {
TradeRecord tr;
if (src_current_price <= position.stoploss) {
Expand Down Expand Up @@ -716,6 +723,9 @@ void System::_submitBuyRequest(const KRecord& today, const KRecord& src_today, P
}

TradeRecord System::_sellForce(const Datetime& date, double num, Part from, bool on_open) {
bool trace = getParam<bool>("trace");
HKU_INFO_IF(trace, "[{}] force sell {} by {}", name(), num, getSystemPartName(from));

TradeRecord record;
size_t pos = m_kdata.getPos(date);
HKU_TRACE_IF_RETURN(pos == Null<size_t>(), record,
Expand Down

0 comments on commit 9abafb4

Please sign in to comment.