历史财务信息加载(continue)

This commit is contained in:
fasiondog 2024-04-13 17:16:18 +08:00
parent 9b1e2d9911
commit 6a4df74f95
10 changed files with 182 additions and 0 deletions

View File

@ -245,6 +245,9 @@ struct HKU_API Stock::Data {
StockWeightList m_weightList; // 权息信息列表
std::mutex m_weight_mutex;
vector<vector<float>> m_history_finance; // 历史财务信息 [财务报告日期, 字段1, 字段2, ...]
std::mutex m_history_finance_mutex;
price_t m_tick;
price_t m_tickValue;
price_t m_unit;

View File

@ -125,6 +125,7 @@ void StockManager::init(const Parameter& baseInfoParam, const Parameter& blockPa
loadAllStocks();
loadAllStockWeights();
loadAllZhBond10();
loadHistoryFinanceField();
// 获取板块驱动
m_blockDriver = DataDriverFactory::getBlockDriver(blockParam);
@ -273,6 +274,7 @@ void StockManager::reload() {
loadAllStocks();
loadAllStockWeights();
loadAllZhBond10();
loadHistoryFinanceField();
m_blockDriver->load();
@ -589,4 +591,12 @@ void StockManager::loadAllZhBond10() {
m_zh_bond10 = m_baseInfoDriver->getAllZhBond10();
}
void StockManager::loadHistoryFinanceField() {
auto fields = m_baseInfoDriver->getHistoryFinanceField();
for (const auto& field : fields) {
m_field_ix_to_name[field.first] = field.second;
m_field_name_to_ix[field.second] = field.first;
}
}
} // namespace hku

View File

@ -161,6 +161,11 @@ public:
*/
bool isHoliday(const Datetime& d) const;
const string& getHistoryFinanceFieldName(size_t ix) const;
size_t getHistoryFinanceFieldIndex(const string& name) const;
vector<vector<float>> getHistoryFinance(const Stock& stk, Datetime start, Datetime end);
/**
* StockStock使用
* @param stock
@ -233,6 +238,9 @@ private:
/** 加载10年期中国国债收益率数据 */
void loadAllZhBond10();
/** 加载历史财经字段索引 */
void loadHistoryFinanceField();
private:
StockManager();
@ -261,6 +269,9 @@ private:
ZhBond10List m_zh_bond10; // 10年期中国国债收益率数据
unordered_map<string, size_t> m_field_name_to_ix; // 财经字段名称到字段索引映射
unordered_map<size_t, string> m_field_ix_to_name; // 财经字段索引到字段名称映射
Parameter m_baseInfoDriverParam;
Parameter m_blockDriverParam;
Parameter m_kdataDriverParam;
@ -309,6 +320,19 @@ inline BaseInfoDriverPtr StockManager::getBaseInfoDriver() const {
return m_baseInfoDriver;
}
inline const string& StockManager::getHistoryFinanceFieldName(size_t ix) const {
return m_field_ix_to_name.at(ix);
}
inline size_t StockManager::getHistoryFinanceFieldIndex(const string& name) const {
return m_field_name_to_ix.at(name);
}
inline vector<vector<float>> StockManager::getHistoryFinance(const Stock& stk, Datetime start,
Datetime end) {
return m_baseInfoDriver->getHistoryFinance(stk.market(), stk.code(), start, end);
}
} // namespace hku
#endif /* STOCKMANAGER_H_ */

View File

@ -117,6 +117,26 @@ public:
return ret;
}
/**
*
* @param market
* @param code
* @param start
* @return vector<float> [[(ymd), 1, 2, ...], ...]
*/
virtual vector<vector<float>> getHistoryFinance(const string& market, const string& code,
Datetime start, Datetime end) {
return vector<vector<float>>();
}
/**
*
* @return vector<std::pair<size_t, string>>
*/
virtual vector<std::pair<size_t, string>> getHistoryFinanceField() {
return vector<std::pair<size_t, string>>();
}
/**
*
* @param market

View File

@ -17,6 +17,8 @@
#include "../table/StockTable.h"
#include "../table/HolidayTable.h"
#include "../table/ZhBond10Table.h"
#include "../table/HistoryFinanceTable.h"
#include "../table/HistoryFinanceFieldTable.h"
namespace hku {
@ -368,4 +370,22 @@ ZhBond10List MySQLBaseInfoDriver::getAllZhBond10() {
return result;
}
vector<std::pair<size_t, string>> MySQLBaseInfoDriver::getHistoryFinanceField() {
vector<std::pair<size_t, string>> result;
auto con = m_pool->getConnect();
try {
vector<HistoryFinanceFieldTable> fields;
con->batchLoad(fields);
size_t total = fields.size();
result.resize(total);
for (size_t i = 0; i < total; i++) {
result[i].first = size_t(fields[i].id());
result[i].second = std::move(fields[i].name);
}
} catch (...) {
}
return result;
}
} /* namespace hku */

View File

@ -41,6 +41,8 @@ public:
virtual std::unordered_set<Datetime> getAllHolidays() override;
virtual ZhBond10List getAllZhBond10() override;
virtual vector<std::pair<size_t, string>> getHistoryFinanceField() override;
private:
ConnectPool<MySQLConnect>* m_pool;
};

View File

@ -16,6 +16,8 @@
#include "../table/StockTable.h"
#include "../table/HolidayTable.h"
#include "../table/ZhBond10Table.h"
#include "../table/HistoryFinanceTable.h"
#include "../table/HistoryFinanceFieldTable.h"
namespace hku {
@ -358,4 +360,59 @@ ZhBond10List SQLiteBaseInfoDriver::getAllZhBond10() {
return result;
}
vector<std::pair<size_t, string>> SQLiteBaseInfoDriver::getHistoryFinanceField() {
vector<std::pair<size_t, string>> result;
auto con = m_pool->getConnect();
try {
vector<HistoryFinanceFieldTable> fields;
con->batchLoad(fields);
size_t total = fields.size();
result.resize(total);
for (size_t i = 0; i < total; i++) {
result[i].first = size_t(fields[i].id());
result[i].second = std::move(fields[i].name);
}
} catch (...) {
}
return result;
}
vector<vector<float>> SQLiteBaseInfoDriver::getHistoryFinance(const string& market,
const string& code, Datetime start,
Datetime end) {
vector<vector<float>> result;
Datetime new_start = start.isNull() ? Datetime::min() : start;
Datetime new_end = end.isNull() ? Datetime::max() : end;
HKU_IF_RETURN(start >= end, result);
auto con = m_pool->getConnect();
try {
string market_code(fmt::format("{}{}", market, code));
to_upper(market_code);
vector<HistoryFinanceTable> finances;
con->batchLoad(finances, ((Field("market_code") == market_code) &
(Field("report_date") >= start.ymd())) +
ASC("report_date"));
size_t total = finances.size();
result.resize(total);
for (size_t i = 0; i < total; i++) {
const auto& finance = finances[i];
auto& cur = result[i];
size_t len = finance.values.size() / sizeof(float);
cur.resize(len + 1);
auto* data = cur.data();
data[0] = static_cast<float>(finance.report_date);
memcpy(data + 1, finance.values.data(), len * sizeof(float));
}
} catch (const std::exception& e) {
HKU_ERROR(e.what());
} catch (...) {
}
return result;
}
} // namespace hku

View File

@ -37,6 +37,10 @@ public:
virtual std::unordered_set<Datetime> getAllHolidays() override;
virtual ZhBond10List getAllZhBond10() override;
virtual vector<std::pair<size_t, string>> getHistoryFinanceField() override;
virtual vector<vector<float>> getHistoryFinance(const string& market, const string& code,
Datetime start, Datetime end) override;
private:
// 股票基本信息数据库实例
ConnectPool<SQLiteConnect>* m_pool;

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-04-13
* Author: fasiondog
*/
#pragma once
#include "hikyuu/utilities/db_connect/TableMacro.h"
namespace hku {
struct HistoryFinanceFieldTable {
TABLE_BIND1(HistoryFinanceFieldTable, HistoryFinanceField, name)
std::string name;
};
}

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-04-13
* Author: fasiondog
*/
#pragma once
#include "hikyuu/utilities/db_connect/TableMacro.h"
namespace hku {
struct HistoryFinanceTable {
TABLE_BIND4(HistoryFinanceTable, HistoryFinanceField, file_date, market_code, report_date,
values)
uint64_t file_date;
uint64_t report_date;
std::string market_code;
std::string values;
};
} // namespace hku