continue for strategy

This commit is contained in:
fasiondog 2021-02-24 00:43:40 +08:00
parent b8cec2cf67
commit 8410e99d6a
3 changed files with 47 additions and 8 deletions

View File

@ -776,6 +776,7 @@ class MyMainWindow(QMainWindow, Ui_MainWindow):
if self.collect_spot_thread is not None and self.collect_spot_thread.isRunning():
self.collect_spot_thread.terminate()
self.collect_spot_thread.wait()
self.collect_spot_thread = None
self._is_collect_running = False
self.logger.info("停止采集")
self.collect_status_label.setText("已停止")

View File

@ -38,11 +38,11 @@ StrategyBase::StrategyBase(const string& name, const string& config_file)
: m_name(name), m_config_file(config_file) {}
StrategyBase::~StrategyBase() {
HKU_INFO("Quit Strategy {}", m_name);
HKU_INFO("[Strategy {}] Quit Strategy!", m_name);
}
void StrategyBase::run() {
HKU_INFO("Strategy {} is running! You can press Ctrl-C to terminte ...", m_name);
HKU_INFO("[Strategy {}] strategy is running! You can press Ctrl-C to terminte ...", m_name);
// 注册 ctrl-c 终止信号
std::signal(SIGINT, sig_handler);
@ -56,12 +56,14 @@ void StrategyBase::run() {
config.read(m_config_file);
} catch (std::exception& e) {
HKU_FATAL("Failed read configure file (\"{}\")! {}", m_config_file, e.what());
HKU_INFO("Exit Strategy {}", m_name);
HKU_FATAL("[Strategy {}] Failed read configure file (\"{}\")! {}", m_name, m_config_file,
e.what());
HKU_INFO("[Strategy {}] Exit Strategy", m_name);
exit(1);
} catch (...) {
HKU_FATAL("Failed read configure file (\"{}\")! Unknow error!", m_config_file);
HKU_INFO("Exit Strategy {}", m_name);
HKU_FATAL("[Strategy {}] Failed read configure file (\"{}\")! Unknow error!", m_name,
m_config_file);
HKU_INFO("[Strategy {}] Exit Strategy", m_name);
exit(1);
}
@ -113,11 +115,36 @@ void StrategyBase::run() {
StockManager& sm = StockManager::instance();
sm.init(baseParam, blockParam, kdataParam, preloadParam, hkuParam, m_context);
const auto& stk_code_list = getStockCodeList();
m_stock_list.reserve(stk_code_list.size());
for (const auto& code : stk_code_list) {
Stock stk = getStock(code);
if (!stk.isNull()) {
m_stock_list.push_back(stk);
} else {
HKU_WARN("[Strategy {}] Invalid code: {}, can't find the stock!", m_name, code);
}
}
HKU_WARN_IF(m_stock_list.empty(), "[Strategy {}] stock list is empty!", m_name);
if (m_stock_list.size() > 0) {
Stock& ref_stk = m_stock_list[0];
for (auto& ktype : ktype_list) {
size_t count = ref_stk.getCount(ktype);
if (count > 0) {
KRecord k = ref_stk.getKRecord(count - 1, ktype);
m_ref_last_time[ktype] = k.datetime;
} else {
m_ref_last_time[ktype] = Null<Datetime>();
}
}
}
// 启动行情接收代理
auto& agent = *getGlobalSpotAgent();
agent.addProcess([this](const SpotRecord& spot) { this->receivedSpot(spot); });
agent.addPostProcess([this](Datetime revTime) { this->finishReceivedSpot(revTime); });
startSpotAgent(true);
startSpotAgent(false);
_startEventLoop();
}
@ -130,11 +157,20 @@ void StrategyBase::receivedSpot(const SpotRecord& spot) {
}
void StrategyBase::finishReceivedSpot(Datetime revTime) {
HKU_IF_RETURN(m_stock_list.empty(), void());
event([this]() { this->onTick(); });
Stock& ref_stk = m_stock_list[0];
const auto& ktype_list = getKTypeList();
for (const auto& ktype : ktype_list) {
event([this, ktype]() { this->onBar(ktype); });
size_t count = ref_stk.getCount(ktype);
if (count > 0) {
KRecord k = ref_stk.getKRecord(count - 1, ktype);
if (k.datetime != m_ref_last_time[ktype]) {
m_ref_last_time[ktype] = k.datetime;
event([this, ktype]() { this->onBar(ktype); });
}
}
}
}

View File

@ -95,6 +95,8 @@ private:
StrategyContext m_context;
TMPtr m_tm;
StockList m_stock_list;
std::unordered_map<KQuery::KType, Datetime> m_ref_last_time;
std::unordered_map<Stock, SpotRecord> m_spot_map;
private: