完善组合指标测试

This commit is contained in:
fasiondog 2023-10-19 18:35:40 +08:00
parent 7b56c30a34
commit 8322f9ee53
4 changed files with 62 additions and 31 deletions

View File

@ -63,6 +63,16 @@ Stock Block::get(const string& market_code) const {
return result;
}
vector<Stock> Block::getAllStocks() const {
vector<Stock> ret;
ret.reserve(size());
auto iter = m_data->m_stockDict.begin();
for (; iter != m_data->m_stockDict.end(); ++iter) {
ret.emplace_back(iter->second);
}
return ret;
}
bool Block::add(const Stock& stock) {
HKU_IF_RETURN(stock.isNull() || have(stock), false);
if (!m_data)

View File

@ -86,6 +86,9 @@ public:
return get(market_code);
}
/** 获取板块下所有证券 */
vector<Stock> getAllStocks() const;
/** 加入指定证券 */
bool add(const Stock& stock);

View File

@ -5,9 +5,10 @@
* Author: fasiondog
*/
#include "hikyuu/utilities/thread/ThreadPool.h"
#include "hikyuu/utilities/thread/MQStealThreadPool.h"
#include "hikyuu/indicator/crt/EXIST.h"
#include "hikyuu/trade_sys/signal/crt/SG_Bool.h"
#include "hikyuu/global/GlobalTaskGroup.h"
#include "combinate.h"
namespace hku {
@ -59,8 +60,6 @@ vector<CombinateAnalysisOutput> HKU_API combinateIndicatorAnalysisWithBlock(
const Block& blk, const KQuery& query, TradeManagerPtr tm, SystemPtr sys,
const std::vector<Indicator>& buy_inds, const std::vector<Indicator>& sell_inds, int n) {
SPEND_TIME(combinateIndicatorAnalysisWithBlock);
HKU_INFO("combinateIndicatorAnalysisWithBlock");
auto inds = combinateIndicator(buy_inds, n);
std::vector<SignalPtr> sgs;
for (const auto& buy_ind : inds) {
@ -72,16 +71,34 @@ vector<CombinateAnalysisOutput> HKU_API combinateIndicatorAnalysisWithBlock(
}
vector<CombinateAnalysisOutput> result;
HKU_INFO("combinateIndicatorAnalysisWithBlock create tasks");
ThreadPool tg;
size_t work_num = 5;
MQStealThreadPool tg(work_num);
vector<std::future<vector<CombinateAnalysisOutput>>> tasks;
int count = 0;
for (auto iter = blk.begin(); iter != blk.end(); ++iter) {
tasks.emplace_back(tg.submit([&sgs, id = count, n_query = query, n_stk = *iter,
auto stocks = blk.getAllStocks();
size_t total = stocks.size();
HKU_IF_RETURN(total == 0, result);
size_t per_num = total > work_num ? total / (work_num * 10) : 1;
size_t count = total % per_num == 0 ? total / per_num : total / per_num + 1;
vector<Stock> buf;
for (size_t i = 0; i < count; i++) {
buf.clear();
for (size_t n = i * per_num, end = (i + 1) * per_num; n < end; n++) {
if (n >= stocks.size()) {
break;
}
buf.emplace_back(stocks[n]);
}
tasks.emplace_back(
tg.submit([sgs, stks = std::move(buf), n_query = query, start = i * per_num,
n_tm = tm->clone(), n_sys = sys->clone()]() {
vector<CombinateAnalysisOutput> ret;
try {
Performance per;
for (size_t i = 0, len = stks.size(); i < len; i++) {
const Stock& n_stk = stks[i];
for (const auto& sg : sgs) {
auto n_sg = sg->clone();
n_sys->setSG(n_sg);
@ -96,12 +113,12 @@ vector<CombinateAnalysisOutput> HKU_API combinateIndicatorAnalysisWithBlock(
ret.emplace_back(out);
// HKU_INFO("id: {}", id);
}
printf(" | id: %zd, stock: %s", start + i, n_stk.code().c_str());
}
} catch (...) {
}
printf(" | id: %d, stock: %s", id, n_stk.code().c_str());
return ret;
}));
count++;
}
for (auto& task : tasks) {

View File

@ -102,6 +102,7 @@ static py::dict combinate_ind_analysis_with_block(const Block& blk, const KQuery
for (size_t i = 0, total = names.size(); i < total; i++) {
result[names[i]] = tmp[i];
}
return result;
}