2016-04-03 00:08:31 +08:00
|
|
|
/*
|
|
|
|
* _Block.cpp
|
|
|
|
*
|
|
|
|
* Created on: 2015年2月10日
|
2016-04-14 01:36:53 +08:00
|
|
|
* Author: fasiondog
|
2016-04-03 00:08:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <hikyuu/serialization/Block_serialization.h>
|
2023-12-27 02:16:10 +08:00
|
|
|
#include "pybind_utils.h"
|
2016-04-03 00:08:31 +08:00
|
|
|
|
|
|
|
using namespace hku;
|
2023-12-27 02:16:10 +08:00
|
|
|
namespace py = pybind11;
|
2016-04-03 00:08:31 +08:00
|
|
|
|
2022-02-21 22:23:52 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(disable : 4267)
|
|
|
|
#endif
|
|
|
|
|
2016-04-03 00:08:31 +08:00
|
|
|
string (Block::*getCategory)() const = &Block::category;
|
|
|
|
void (Block::*setCategory)(const string&) = &Block::category;
|
|
|
|
string (Block::*getName)() const = &Block::name;
|
|
|
|
void (Block::*setName)(const string&) = &Block::name;
|
|
|
|
|
|
|
|
bool (Block::*add_1)(const Stock&) = &Block::add;
|
|
|
|
bool (Block::*add_2)(const string&) = &Block::add;
|
|
|
|
bool (Block::*remove_1)(const Stock&) = &Block::remove;
|
|
|
|
bool (Block::*remove_2)(const string&) = &Block::remove;
|
|
|
|
|
2023-12-27 02:16:10 +08:00
|
|
|
void export_Block(py::module& m) {
|
|
|
|
py::class_<Block>(m, "Block", "板块类,可视为证券的容器")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def(py::init<const string&, const string&>())
|
|
|
|
.def(py::init<const Block&>())
|
2017-09-29 01:37:12 +08:00
|
|
|
|
2023-12-27 02:16:10 +08:00
|
|
|
.def("__str__", to_py_str<Block>)
|
|
|
|
.def("__repr__", to_py_str<Block>)
|
2020-07-06 00:41:32 +08:00
|
|
|
|
2023-12-27 02:16:10 +08:00
|
|
|
.def_property("category", setCategory, getCategory, "板块所属分类")
|
|
|
|
.def_property("name", getName, setName, "板块名称")
|
2020-07-06 00:41:32 +08:00
|
|
|
|
|
|
|
.def("empty", &Block::empty, R"(empty(self)
|
|
|
|
|
|
|
|
是否为空)")
|
|
|
|
|
|
|
|
.def("add", add_1, R"(add(self, stock)
|
|
|
|
|
|
|
|
加入指定的证券
|
|
|
|
|
|
|
|
:param Stock stock: 待加入的证券
|
|
|
|
:return: 是否成功加入
|
2023-12-27 23:18:18 +08:00
|
|
|
:rtype: bool)",
|
|
|
|
py::keep_alive<1, 2>())
|
2020-07-06 00:41:32 +08:00
|
|
|
|
|
|
|
.def("add", add_2, R"(add(self, market_code)
|
|
|
|
|
|
|
|
根据"市场简称证券代码"加入指定的证券
|
|
|
|
|
|
|
|
:param str market_code: 市场简称证券代码
|
|
|
|
:return: 是否成功加入
|
2023-12-27 23:18:18 +08:00
|
|
|
:rtype: bool)",
|
|
|
|
py::keep_alive<1, 2>())
|
2020-07-06 00:41:32 +08:00
|
|
|
|
|
|
|
.def("remove", remove_1, R"(remove(self, stock)
|
|
|
|
|
|
|
|
移除指定证券
|
|
|
|
|
|
|
|
:param Stock stock: 指定的证券
|
|
|
|
:return: 是否成功
|
|
|
|
:rtype: bool)")
|
|
|
|
|
|
|
|
.def("remove", remove_2, R"(remove(market_code)
|
|
|
|
|
|
|
|
移除指定证券
|
|
|
|
|
|
|
|
:param str market_code: 市场简称证券代码
|
2020-07-07 00:34:25 +08:00
|
|
|
:return: True 成功 | False 失败
|
2020-07-06 00:41:32 +08:00
|
|
|
:rtype: bool)")
|
|
|
|
|
|
|
|
.def("clear", &Block::clear, "移除包含的所有证券")
|
|
|
|
|
2020-07-07 00:34:25 +08:00
|
|
|
.def("__len__", &Block::size, "包含的证券数量")
|
|
|
|
|
|
|
|
.def("__getitem__", &Block::get, R"(__getitem__(self, market_code)
|
|
|
|
|
|
|
|
:param str market_code: 证券代码
|
|
|
|
:return: Stock 实例)")
|
|
|
|
|
2023-12-27 02:16:10 +08:00
|
|
|
.def(
|
|
|
|
"__iter__",
|
|
|
|
[](const Block& blk) {
|
|
|
|
return py::make_iterator<py::return_value_policy::reference_internal, StockMapIterator,
|
|
|
|
StockMapIterator, const Stock&>(blk.begin(), blk.end());
|
|
|
|
},
|
|
|
|
py::keep_alive<0, 1>())
|
2016-04-03 00:08:31 +08:00
|
|
|
|
2024-03-29 17:57:06 +08:00
|
|
|
.def(
|
|
|
|
"get_stock_list",
|
|
|
|
[](const Block& self, py::object filter) {
|
|
|
|
StockList ret;
|
|
|
|
if (filter.is_none()) {
|
|
|
|
ret = self.getStockList();
|
|
|
|
} else {
|
|
|
|
HKU_CHECK(py::hasattr(filter, "__call__"), "filter not callable!");
|
|
|
|
py::object filter_func = filter.attr("__call__");
|
|
|
|
ret = self.getStockList(
|
|
|
|
[&](const Stock& stk) { return filter_func(stk).cast<bool>(); });
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
py::arg("filter") = py::none(), R"(get_stock_list(self[, filter=None])
|
|
|
|
|
|
|
|
获取证券列表
|
|
|
|
|
|
|
|
:param func filter: 输入参数为 stock, 返回 True | False 的过滤函数)")
|
|
|
|
|
2023-12-27 02:16:10 +08:00
|
|
|
DEF_PICKLE(Block);
|
2016-04-03 00:08:31 +08:00
|
|
|
}
|