diff --git a/hikyuu/strategy/strategy.py b/hikyuu/strategy/strategy.py index 77390324..d7d0d2aa 100644 --- a/hikyuu/strategy/strategy.py +++ b/hikyuu/strategy/strategy.py @@ -2,7 +2,7 @@ # -*- coding: utf8 -*- # cp936 -from hikyuu import StrategyBase, Query, Datetime, TimeDelta +from hikyuu import StrategyBase, Query, Datetime, TimeDelta, Seconds from hikyuu import StockManager @@ -15,12 +15,6 @@ class TestStrategy(StrategyBase): def init(self): print("strategy init") - def on_bar(self, ktype): - print("on bar {}".format(ktype)) - print("{}".format(len(StockManager.instance()))) - for s in self.sm: - print(s) - def my_func(): sm = StockManager.instance() @@ -31,5 +25,5 @@ def my_func(): if __name__ == '__main__': s = TestStrategy() - s.run_daily_at(my_func, TimeDelta(0, 17, 6)) + s.run_daily_at(my_func, Datetime.now() - Datetime.today() + Seconds(5)) s.start() diff --git a/hikyuu_cpp/hikyuu/strategy/StrategyBase.cpp b/hikyuu_cpp/hikyuu/strategy/StrategyBase.cpp index 1cc9f051..7d53e8a1 100644 --- a/hikyuu_cpp/hikyuu/strategy/StrategyBase.cpp +++ b/hikyuu_cpp/hikyuu/strategy/StrategyBase.cpp @@ -11,9 +11,17 @@ #include "hikyuu/utilities/ini_parser/IniParser.h" #include "hikyuu/global/schedule/scheduler.h" #include "hikyuu/global/GlobalTaskGroup.h" +#include "hikyuu/global/sysinfo.h" #include "hikyuu/hikyuu.h" #include "StrategyBase.h" +#define EVENT(func) \ + if (runningInPython()) { \ + event(func); \ + } else { \ + func(); \ + } + namespace hku { std::atomic_bool StrategyBase::ms_keep_running = true; @@ -83,9 +91,9 @@ void StrategyBase::run() { auto& agent = *getGlobalSpotAgent(); agent.addProcess( - [this](const SpotRecord& spot) { event([=]() { this->receivedSpot(spot); }); }); + [this](const SpotRecord& spot) { EVENT([=]() { this->receivedSpot(spot); }); }); agent.addPostProcess( - [this](Datetime revTime) { event([=]() { this->onReceivedSpot(revTime); }); }); + [this](Datetime revTime) { EVENT([=]() { this->onReceivedSpot(revTime); }); }); startSpotAgent(false); m_running = true; @@ -99,7 +107,7 @@ void StrategyBase::start() { void StrategyBase::receivedSpot(const SpotRecord& spot) { Stock stk = getStock(format("{}{}", spot.market, spot.code)); if (!stk.isNull()) { - event([=]() { this->onChange(stk, spot); }); + EVENT([=]() { this->onChange(stk, spot); }); } } @@ -125,7 +133,7 @@ void StrategyBase::runDaily(std::function&& func, const TimeDelta& delta Datetime close2 = today + market_info.closeTime2(); Datetime now = Datetime::now(); if ((now > open1 && now < close1) || (now > open2 && now < close2)) { - event(func); + EVENT(func); } }; @@ -194,7 +202,7 @@ void StrategyBase::runDailyAt(std::function&& func, const TimeDelta& del auto new_func = [=]() { if (!ignoreHoliday) { - event(func); + EVENT(func); return; } @@ -202,7 +210,7 @@ void StrategyBase::runDailyAt(std::function&& func, const TimeDelta& del auto today = Datetime::today(); int day = today.dayOfWeek(); if (day != 0 && day != 6 && !sm.isHoliday(today)) { - event(func); + EVENT(func); } };