Skip to content

Commit

Permalink
Merge pull request #236 from fasiondog/feature/finance
Browse files Browse the repository at this point in the history
增强 FINANCE, 增加 only_year_report 和 dynamic 参数;indicator 绘制时设置 x 轴为日期
  • Loading branch information
fasiondog committed Apr 20, 2024
2 parents 03a7798 + 3efefcb commit 36b2408
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 7 deletions.
10 changes: 9 additions & 1 deletion hikyuu/draw/drawplot/matplotlib_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __call__(self, x, pos=None): # IGNORE:W0613

def getDayLocatorAndFormatter(dates):
"""获取显示日线时使用的Major Locator和Major Formatter"""
sep = len(dates) / 8
sep = len(dates) / 10
loc = [
(i, str(d) if (i != (len(dates) - 1)) and (i % sep != 0) else "{}-{}-{}".format(d.year, d.month, d.day))
for i, d in enumerate(dates)
Expand Down Expand Up @@ -394,6 +394,10 @@ def iplot(
axes.set_xlim(-1, len(indicator) + 1)
if kref:
ax_set_locator_formatter(axes, kref.get_datetime_list(), kref.get_query().ktype)
else:
k = indicator.get_context()
if len(k) > 0:
ax_set_locator_formatter(axes, k.get_datetime_list(), k.get_query().ktype)
# draw()


Expand Down Expand Up @@ -474,6 +478,10 @@ def ibar(
axes.set_xlim(-1, len(indicator) + 1)
if kref:
ax_set_locator_formatter(axes, kref.get_datetime_list(), kref.get_query().ktype)
else:
k = indicator.get_context()
if len(k) > 0:
ax_set_locator_formatter(axes, k.get_datetime_list(), k.get_query().ktype)
# draw()


Expand Down
2 changes: 2 additions & 0 deletions hikyuu_cpp/hikyuu/HistoryFinanceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ namespace hku {

HistoryFinanceInfo& HistoryFinanceInfo::operator=(const HistoryFinanceInfo& other) {
HKU_IF_RETURN(this == &other, *this);
fileDate = other.fileDate;
reportDate = other.reportDate;
values = other.values;
return *this;
}

HistoryFinanceInfo& HistoryFinanceInfo::operator=(HistoryFinanceInfo&& other) {
HKU_IF_RETURN(this == &other, *this);
fileDate = std::move(other.fileDate);
reportDate = std::move(other.reportDate);
values = std::move(other.values);
return *this;
Expand Down
5 changes: 4 additions & 1 deletion hikyuu_cpp/hikyuu/HistoryFinanceInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ namespace hku {
* @ingroup StockManage
*/
struct HKU_API HistoryFinanceInfo {
Datetime fileDate; ///< 用于区分一季报、半年报、三季报、年报
Datetime reportDate; ///< 财务报告日期
vector<float>
values; ///< 详细财务信息,字段索引可使用 StockManager.getHistoryFinanceAllFields 查询

HistoryFinanceInfo() = default;
HistoryFinanceInfo(const HistoryFinanceInfo&) = default;
HistoryFinanceInfo(HistoryFinanceInfo&& rv)
: reportDate(std::move(rv.reportDate)), values(std::move(rv.values)) {}
: fileDate(std::move(rv.fileDate)),
reportDate(std::move(rv.reportDate)),
values(std::move(rv.values)) {}

HistoryFinanceInfo& operator=(const HistoryFinanceInfo&);
HistoryFinanceInfo& operator=(HistoryFinanceInfo&&);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ vector<HistoryFinanceInfo> MySQLBaseInfoDriver::getHistoryFinance(const string &
for (size_t i = 0; i < total; i++) {
auto &finance = finances[i];
auto &cur = result[i];
cur.fileDate = Datetime(finance.file_date);
cur.reportDate = Datetime(finance.report_date);
// cur.values = std::move(finance.values);
size_t count = finance.values.size() / sizeof(float);
cur.values.resize(count);
memcpy(cur.values.data(), finance.values.data(), count * sizeof(float));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ vector<HistoryFinanceInfo> SQLiteBaseInfoDriver::getHistoryFinance(const string&
for (size_t i = 0; i < total; i++) {
auto& finance = finances[i];
auto& cur = result[i];
cur.fileDate = Datetime(finance.file_date);
cur.reportDate = Datetime(finance.report_date);
// cur.values = std::move(finance.values);
size_t count = finance.values.size() / sizeof(float);
cur.values.resize(count);
memcpy(cur.values.data(), finance.values.data(), count * sizeof(float));
Expand Down
58 changes: 56 additions & 2 deletions hikyuu_cpp/hikyuu/indicator/imp/IFinance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ namespace hku {
IFinance::IFinance() : IndicatorImp("FINANCE", 1) {
setParam<int>("field_ix", 0);
setParam<string>("field_name", "");

// 某些信息如每股收益,只使用年报计算
setParam<bool>("only_year_report", false);

// 某些信息如每股收益,需要动态计算,如全年收益在只有一季报时,使用一季报*4进行预估
setParam<bool>("dynamic", false);
}

IFinance::IFinance(const KData& k) : IndicatorImp("FINANCE", 1) {
setParam<int>("field_ix", 0);
setParam<string>("field_name", "");
setParam<KData>("kdata", k);
setParam<bool>("only_year_report", false);
setParam<bool>("dynamic", false);
IFinance::_calculate(Indicator());
}

Expand All @@ -40,6 +48,17 @@ void IFinance::_calculate(const Indicator& data) {

Stock stock = kdata.getStock();
auto finances = stock.getHistoryFinance();

if (getParam<bool>("only_year_report")) {
vector<HistoryFinanceInfo> tmp_finances;
for (auto&& finance : finances) {
if (finance.fileDate.month() == 12L) {
tmp_finances.emplace_back(std::move(finance));
}
}
finances = std::move(tmp_finances);
}

if (finances.empty()) {
m_discard = total;
return;
Expand All @@ -52,6 +71,7 @@ void IFinance::_calculate(const Indicator& data) {
StockManager::instance().getHistoryFinanceFieldIndex(getParam<string>("field_name")));
}

bool dynamic = getParam<bool>("dynamic");
auto* dst = this->data();
auto dates = kdata.getDatetimeList();
auto* k = kdata.data();
Expand All @@ -63,13 +83,47 @@ void IFinance::_calculate(const Indicator& data) {
auto value = finances[pos].values.at(field_ix);
if (pos + 1 == finances_total) {
while (cur_kix < total && finances[pos].reportDate <= k[cur_kix].datetime) {
dst[cur_kix] = value;
if (dynamic) {
long month = finances[pos].fileDate.month();
if (3L == month) {
// 一季报
dst[cur_kix] = value * 4;
} else if (6L == month) {
// 半年报
dst[cur_kix] = value * 2;
} else if (9L == month) {
// 三季报
dst[cur_kix] = value / 3.0 * 4.0;
} else {
// 年报
dst[cur_kix] = value;
}
} else {
dst[cur_kix] = value;
}
cur_kix++;
}
} else {
while (cur_kix < total && finances[pos].reportDate <= k[cur_kix].datetime &&
finances[pos + 1].reportDate > k[cur_kix].datetime) {
dst[cur_kix] = value;
if (dynamic) {
long month = finances[pos].fileDate.month();
if (3L == month) {
// 一季报
dst[cur_kix] = value * 4;
} else if (6L == month) {
// 半年报
dst[cur_kix] = value * 2;
} else if (9L == month) {
// 三季报
dst[cur_kix] = value / 3.0 * 4.0;
} else {
// 年报
dst[cur_kix] = value;
}
} else {
dst[cur_kix] = value;
}
cur_kix++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion hikyuu_pywrap/_Stock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void export_Stock(py::module& m) {
auto finances = stk.getHistoryFinance();
py::list ret;
for (const auto& f : finances) {
ret.append(py::make_tuple(f.reportDate, f.values));
ret.append(py::make_tuple(f.fileDate, f.reportDate, f.values));
}
return ret;
},
Expand Down

0 comments on commit 36b2408

Please sign in to comment.