python 使用主线程循环,C++不用

This commit is contained in:
fasiondog 2024-08-12 17:25:36 +08:00
parent 8edb9d0a4b
commit 69730916cc
2 changed files with 16 additions and 14 deletions

View File

@ -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()

View File

@ -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<void()>&& 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<void()>&& func, const TimeDelta& del
auto new_func = [=]() {
if (!ignoreHoliday) {
event(func);
EVENT(func);
return;
}
@ -202,7 +210,7 @@ void StrategyBase::runDailyAt(std::function<void()>&& func, const TimeDelta& del
auto today = Datetime::today();
int day = today.dayOfWeek();
if (day != 0 && day != 6 && !sm.isHoliday(today)) {
event(func);
EVENT(func);
}
};