This commit is contained in:
fasiondog 2024-08-13 15:11:08 +08:00
parent 761b233204
commit f2a979a92e
3 changed files with 20 additions and 3 deletions

View File

@ -15,6 +15,9 @@ class TestStrategy(StrategyBase):
def init(self):
print("strategy init")
def on_received_spot(self, rev_time):
print("rev_time:", rev_time)
def my_func():
sm = StockManager.instance()
@ -26,5 +29,6 @@ def my_func():
if __name__ == '__main__':
s = TestStrategy()
# s.run_daily_at(my_func, Datetime.now() - Datetime.today() + Seconds(5))
# s.on_received_spot(Datetime.now())
s.run_daily(my_func, Minutes(1))
s.start()
# s.start()

View File

@ -96,7 +96,7 @@ void StrategyBase::run() {
[this](const SpotRecord& spot) { EVENT([=]() { this->receivedSpot(spot); }); });
agent.addPostProcess(
[this](Datetime revTime) { EVENT([=]() { this->onReceivedSpot(revTime); }); });
startSpotAgent(false);
startSpotAgent(true);
m_running = true;
}
@ -135,7 +135,7 @@ void StrategyBase::runDaily(std::function<void()>&& func, const TimeDelta& delta
Datetime open2 = today + market_info.openTime2();
Datetime close2 = today + market_info.closeTime2();
Datetime now = Datetime::now();
if ((now > open1 && now < close1) || (now > open2 && now < close2)) {
if ((now >= open1 && now <= close1) || (now >= open2 && now <= close2)) {
EVENT(func);
}
};
@ -146,6 +146,7 @@ void StrategyBase::runDaily(std::function<void()>&& func, const TimeDelta& delta
auto now = Datetime::now();
TimeDelta now_time = now - today;
if (now_time >= market_info.closeTime2()) {
HKU_INFO("time: {}", today.nextDay() + market_info.openTime1());
scheduler->addFuncAtTime(today.nextDay() + market_info.openTime1(), [=]() {
new_func();
auto* sched = getScheduler();
@ -168,6 +169,7 @@ void StrategyBase::runDaily(std::function<void()>&& func, const TimeDelta& delta
} else if (now_time == market_info.closeTime1()) {
scheduler->addFuncAtTime(today + market_info.openTime2(), [=]() {
new_func();
auto* sched = getScheduler();
sched->addDurationFunc(std::numeric_limits<int>::max(), delta, new_func);
});
@ -189,6 +191,7 @@ void StrategyBase::runDaily(std::function<void()>&& func, const TimeDelta& delta
} else if (now_time < market_info.openTime1()) {
scheduler->addFuncAtTime(today + market_info.openTime1(), [=]() {
new_func();
auto* sched = getScheduler();
sched->addDurationFunc(std::numeric_limits<int>::max(), delta, new_func);
});

View File

@ -19,6 +19,14 @@ public:
void init() override {
PYBIND11_OVERLOAD(void, StrategyBase, init);
}
void onChange(const Stock& stk, const SpotRecord& spot) override {
PYBIND11_OVERLOAD(void, StrategyBase, onChange, stk, spot);
}
virtual void onReceivedSpot(Datetime revTime) override {
PYBIND11_OVERLOAD(void, StrategyBase, onReceivedSpot, revTime);
}
};
void export_Strategy(py::module& m) {
@ -41,6 +49,8 @@ void export_Strategy(py::module& m) {
py::return_value_policy::copy, "需要的K线类型")
.def("init", &StrategyBase::init)
.def("on_change", &StrategyBase::onChange)
.def("on_received_spot", &StrategyBase::onReceivedSpot)
.def("start", &PyStrategyBase::start)
.def("run_daily",
[](StrategyBase& self, py::object func, const TimeDelta& time) {