Skip to content

Commit

Permalink
Merge pull request #241 from fasiondog/feature/belong
Browse files Browse the repository at this point in the history
Stock 添加获取所属板块列表方法
  • Loading branch information
fasiondog committed Apr 26, 2024
2 parents 3504345 + 02a09de commit a4bf23f
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 2 deletions.
14 changes: 13 additions & 1 deletion hikyuu_cpp/hikyuu/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,29 @@ Block::Block(const string& category, const string& name, const string& indexCode
}

Block::Block(const Block& block) noexcept {
if (m_data == block.m_data)
if (!block.m_data)
return;
m_data = block.m_data;
}

Block::Block(Block&& block) noexcept {
if (!block.m_data)
return;
m_data = std::move(block.m_data);
}

Block& Block::operator=(const Block& block) noexcept {
HKU_IF_RETURN(this == &block || m_data == block.m_data, *this);
m_data = block.m_data;
return *this;
}

Block& Block::operator=(Block&& block) noexcept {
HKU_IF_RETURN(this == &block || m_data == block.m_data, *this);
m_data = std::move(block.m_data);
return *this;
}

bool Block::have(const string& market_code) const {
HKU_IF_RETURN(!m_data, false);
string query_str = market_code;
Expand Down
2 changes: 2 additions & 0 deletions hikyuu_cpp/hikyuu/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class HKU_API Block {
Block(const string& category, const string& name);
Block(const string& category, const string& name, const string& indexCode);
Block(const Block&) noexcept;
Block(Block&&) noexcept;
Block& operator=(const Block&) noexcept;
Block& operator=(Block&&) noexcept;
virtual ~Block();

typedef StockMapIterator const_iterator;
Expand Down
4 changes: 4 additions & 0 deletions hikyuu_cpp/hikyuu/Stock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,10 @@ Parameter Stock::getFinanceInfo() const {
return result;
}

vector<Block> Stock::getBelongToBlockList(const string& category) const {
return StockManager::instance().getStockBelongs(*this, category);
}

// 判断是否在交易时间段内(不判断日期)
bool Stock::isTransactionTime(Datetime time) {
MarketInfo market_info = StockManager::instance().getMarketInfo(market());
Expand Down
8 changes: 8 additions & 0 deletions hikyuu_cpp/hikyuu/Stock.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef DriverConnectPool<KDataDriverConnect> KDataDriverConnectPool;
typedef shared_ptr<KDataDriverConnectPool> KDataDriverConnectPoolPtr;
class HKU_API KData;
class HKU_API Parameter;
class HKU_API Block;

/**
* Stock基类,Application中一般使用StockPtr进行操作
Expand Down Expand Up @@ -191,6 +192,13 @@ class HKU_API Stock {
*/
Parameter getFinanceInfo() const;

/**
* 获取所属板块列表
* @param category 指定的板块分类,如果为空,则返回所有板块分类的所属板块
* @return BlockList
*/
vector<Block> getBelongToBlockList(const string& category) const;

/**
* 获取历史财务信息
*/
Expand Down
4 changes: 4 additions & 0 deletions hikyuu_cpp/hikyuu/StockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ BlockList StockManager::getBlockList() {
return m_blockDriver ? m_blockDriver->getBlockList() : BlockList();
}

BlockList StockManager::getStockBelongs(const Stock& stk, const string& category) {
return m_blockDriver ? m_blockDriver->getStockBelongs(stk, category) : BlockList();
}

DatetimeList StockManager::getTradingCalendar(const KQuery& query, const string& market) {
auto marketinfo = getMarketInfo(market);
return getStock(fmt::format("{}{}", marketinfo.market(), marketinfo.code()))
Expand Down
9 changes: 8 additions & 1 deletion hikyuu_cpp/hikyuu/StockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@ class HKU_API StockManager {
*/
BlockList getBlockList();

// 目前支持"SH"
/**
* 获取指定证券所属的板块列表
* @param stk 指定证券
* @param category 板块分类,如果为空字符串,返回所有板块分类下的所属板块
* @return BlockList
*/
BlockList getStockBelongs(const Stock& stk, const string& category);

/**
* 获取交易日历,目前支持"SH"
* @param query
Expand Down
11 changes: 11 additions & 0 deletions hikyuu_cpp/hikyuu/data_driver/BlockInfoDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ bool BlockInfoDriver::init(const Parameter& params) {
return _init();
}

BlockList BlockInfoDriver::getStockBelongs(const Stock& stk, const string& category) {
BlockList ret;
auto category_blks = category.empty() ? getBlockList() : getBlockList(category);
for (auto&& blk : category_blks) {
if (blk.have(stk)) {
ret.emplace_back(std::move(blk));
}
}
return ret;
}

} /* namespace hku */
8 changes: 8 additions & 0 deletions hikyuu_cpp/hikyuu/data_driver/BlockInfoDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class HKU_API BlockInfoDriver {
*/
bool init(const Parameter& params);

/**
* 获取指定证券所属的板块列表
* @param stk 指定证券
* @param category 板块分类,如果为空字符串,返回所有板块分类下的所属板块
* @return BlockList
*/
BlockList getStockBelongs(const Stock& stk, const string& category);

/**
* 子类如果需要缓存,可实现该方法将数据加载至自身的缓存
*/
Expand Down
16 changes: 16 additions & 0 deletions hikyuu_pywrap/_Stock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ void export_Stock(py::module& m) {
:param Datetime end: 结束时刻
:rtype: StockWeightList)")

.def(
"get_belong_to_block_list",
[](Stock& stk, const py::object& category) {
string c_category;
if (!category.is_none()) {
c_category = category.cast<string>();
}
return stk.getBelongToBlockList(c_category);
},
py::arg("category") = py::none(), R"(get_belong_to_block_list(self[, category=None])
获取所属板块列表
:param str category: 指定的板块分类,为 None 时,返回所有板块分类下的所属板块
:rtype: list)")

.def(
"get_history_finance",
[](const Stock& stk) {
Expand Down

0 comments on commit a4bf23f

Please sign in to comment.