hikyuu2/hikyuu_pywrap/_Stock.cpp
2024-04-26 18:46:09 +08:00

245 lines
9.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* _Stock.cpp
*
* Created on: 2011-12-4
* Author: fasiondog
*/
#include <hikyuu/serialization/Stock_serialization.h>
#include <hikyuu/KData.h>
#include "pybind_utils.h"
using namespace hku;
namespace py = pybind11;
// BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getIndex_overloads, getIndex, 1, 2)
KRecord (Stock::*getKRecord1)(size_t pos, const KQuery::KType& kType) const = &Stock::getKRecord;
KRecord (Stock::*getKRecord2)(const Datetime&,
const KQuery::KType& kType) const = &Stock::getKRecord;
void export_Stock(py::module& m) {
py::class_<Stock>(m, "Stock", "证券对象")
.def(py::init<>())
.def(py::init<const string&, const string&, const string&>(), py::arg("market"),
py::arg("code"), py::arg("name"))
.def(py::init<const Stock&>())
.def("__str__", &Stock::toString)
.def("__repr__", &Stock::toString)
.def_property_readonly("id", &Stock::id, "内部id")
.def_property("market", py::overload_cast<>(&Stock::market, py::const_),
py::overload_cast<const string&>(&Stock::market), py::return_value_policy::copy,
"所属市场简称,市场简称是市场的唯一标识")
.def_property("code", py::overload_cast<>(&Stock::code, py::const_),
py::overload_cast<const string&>(&Stock::code), py::return_value_policy::copy,
"证券代码")
.def_property_readonly("market_code", py::overload_cast<>(&Stock::market_code, py::const_),
"市场简称+证券代码,如: sh000001")
.def_property("name", py::overload_cast<>(&Stock::name, py::const_),
py::overload_cast<const string&>(&Stock::name), py::return_value_policy::copy,
"证券名称")
.def_property("type", py::overload_cast<>(&Stock::type, py::const_),
py::overload_cast<uint32_t>(&Stock::type), "证券类型参见constant")
.def_property("valid", py::overload_cast<>(&Stock::valid, py::const_),
py::overload_cast<bool>(&Stock::valid), "该证券当前是否有效")
.def_property("start_datetime", py::overload_cast<>(&Stock::startDatetime, py::const_),
py::overload_cast<const Datetime&>(&Stock::startDatetime),
py::return_value_policy::copy, "证券起始日期")
.def_property("last_datetime", py::overload_cast<>(&Stock::lastDatetime, py::const_),
py::overload_cast<const Datetime&>(&Stock::lastDatetime),
py::return_value_policy::copy, "证券最后日期")
.def_property("tick", py::overload_cast<>(&Stock::tick, py::const_),
py::overload_cast<price_t>(&Stock::tick), "最小跳动量")
.def_property("tick_value", py::overload_cast<>(&Stock::tickValue, py::const_),
py::overload_cast<price_t>(&Stock::tickValue), "最小跳动量价值")
.def_property_readonly("unit", &Stock::unit, "每单位价值 = tickValue / tick")
.def_property("precision", py::overload_cast<>(&Stock::precision, py::const_),
py::overload_cast<int>(&Stock::precision), "价格精度")
.def_property("atom", py::overload_cast<>(&Stock::atom, py::const_),
py::overload_cast<double>(&Stock::atom), "最小交易数量同min_tradeNumber")
.def_property("min_trade_number", py::overload_cast<>(&Stock::minTradeNumber, py::const_),
py::overload_cast<double>(&Stock::minTradeNumber), "最小交易数量")
.def_property("max_trade_number", py::overload_cast<>(&Stock::maxTradeNumber, py::const_),
py::overload_cast<double>(&Stock::maxTradeNumber), "最大交易数量")
.def("is_null", &Stock::isNull, R"(is_null(self)
Null
:rtype: bool)")
.def("is_buffer", &Stock::isBuffer, R"(指定类型的K线数据是否被缓存)")
.def("get_kdata", &Stock::getKData, R"(get_kdata(self, query)
K线数据
:param Query query:
:return: K线数据
:rtype: KData)")
.def("get_timeline_list", &Stock::getTimeLineList, R"(get_timeline_list(self, query)
线
:param Query query: K线类型
:rtype: TimeLineList)")
.def("get_trans_list", &Stock::getTransList, R"(get_trans_list(self, query)
:param Query query: K线类型
:rtype: TransList)")
.def("get_count", &Stock::getCount, py::arg("ktype") = KQuery::DAY,
R"(get_count(self, [ktype=Query.DAY])
K线数据量
:param Query.KType ktype: K线数据类别
:return: K线记录数
:rtype: int)")
.def("get_market_value", &Stock::getMarketValue, R"(get_market_value(self, date, ktype)
:param Datetime date:
:param Query.KType ktype: K线数据类别
:return:
:rtype: float)")
.def("get_krecord", getKRecord1, py::arg("pos"), py::arg("ktype") = KQuery::DAY,
R"(get_krecord(self, pos[, ktype=Query.DAY])
K线数据记录
:param int pos:
:param Query.KType ktype: K线数据类别
:return: K线记录
:rtype: KRecord)")
.def("get_krecord", getKRecord2, py::arg("date"), py::arg("ktype") = KQuery::DAY,
R"(get_krecord(self, datetime[, ktype=Query.DAY])
线/线KRecord
:param Datetime datetime:
:param Query.KType ktype: K线数据类别
:return: K线记录
:rtype: KRecord)")
.def("get_krecord_list", &Stock::getKRecordList, R"(get_krecord_list(self, start, end,
ktype)
K线记录 [start, end)使.
:param int start:
:param int end:
:param Query.KType ktype: K线类别
:return: K线记录列表
:rtype: KRecordList)")
.def("get_datetime_list", &Stock::getDatetimeList, R"(get_datetime_list(self, query)
:param Query query:
:rtype: DatetimeList)")
.def("get_finance_info", &Stock::getFinanceInfo, R"(get_finance_info(self)
:rtype: Parameter)")
.def("realtime_update", &Stock::realtimeUpdate, py::arg("krecord"),
py::arg("ktype") = KQuery::DAY,
R"(realtime_update(self, krecord)
线
:param KRecord krecord: K线记录
:param KQuery.KType ktype: K 线)")
.def("get_weight", &Stock::getWeight, py::arg("start") = Datetime::min(),
py::arg("end") = Datetime(),
R"(get_weight(self, [start, end])
[start,end)
:param Datetime start:
: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) {
auto finances = stk.getHistoryFinance();
py::list ret;
for (const auto& f : finances) {
ret.append(py::make_tuple(f.fileDate, f.reportDate, f.values));
}
return ret;
},
R"(get_history_finance(self)
)")
.def("load_kdata_to_buffer", &Stock::loadKDataToBuffer, R"(load_kdata_to_buffer(self,
ktype)
K线数据加载至内存缓存
:param Query.KType ktype: K线类型)")
.def("release_kdata_buffer", &Stock::releaseKDataBuffer, R"(release_kdata_buffer(self,
ktype)
K线数据
:param Query.KType ktype: K线类型)")
.def(
"set_krecord_list",
[](Stock& self, const py::object& obj) {
if (py::isinstance<KRecordList>(obj)) {
const auto& ks = obj.cast<const KRecordList&>();
self.setKRecordList(ks);
} else if (py::isinstance<py::sequence>(obj)) {
auto seq = obj.cast<py::sequence>();
auto ks = python_list_to_vector<KRecord>(seq);
self.setKRecordList(ks);
} else {
HKU_THROW("Unusable input data type");
}
},
R"(set_krecord_list(self, krecord_list)
"谨慎调用!!!直接设置当前内存 KRecordList, 仅供需临时增加的外部 Stock 设置 K 线数据)")
.def(py::self == py::self)
.def(py::self != py::self)
DEF_PICKLE(Stock);
}