add BLOCKSETNUM indicator

This commit is contained in:
fasiondog 2024-05-21 06:00:14 +08:00
parent 992343dd68
commit e81b34f95b
5 changed files with 161 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#include "crt/BARSLAST.h"
#include "crt/BARSSINCE.h"
#include "crt/BETWEEN.h"
#include "crt/BLOCKSETNUM.h"
#include "crt/CEILING.h"
#include "crt/CORR.h"
#include "crt/COS.h"

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-05-21
* Author: fasiondog
*/
#pragma once
#include "../Indicator.h"
namespace hku {
/**
*
* @param blk
* @param query
* @return Indicator
*/
Indicator HKU_API BLOCKSETNUM(const Block& blk, const KQuery& query);
/**
*
* @param category
* @param name
* @param query
* @return Indicator
*/
Indicator HKU_API BLOCKSETNUM(const string& category, const string& name, const KQuery& query);
} // namespace hku

View File

@ -0,0 +1,84 @@
/*
* IBlockSetNum.cpp
*
* Copyright (c) 2019, hikyuu.org
*
* Created on: 2024-5-21
* Author: fasiondog
*/
#include "IBlockSetNum.h"
#include "../Indicator.h"
#include "../crt/KDATA.h"
#include "../crt/REF.h"
#include "../crt/ALIGN.h"
#include "../../StockManager.h"
#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::IBlockSetNum)
#endif
namespace hku {
IBlockSetNum::IBlockSetNum() : IndicatorImp("BLOCKSETNUM", 1) {
setParam<KQuery>("query", KQueryByIndex(-100));
setParam<Block>("block", Block());
setParam<bool>("ignore_context", false);
}
IBlockSetNum::~IBlockSetNum() {}
void IBlockSetNum::_calculate(const Indicator& ind) {
Block block = getParam<Block>("block");
bool ignore_context = getParam<bool>("ignore_context");
KData k = getContext();
KQuery q;
DatetimeList dates;
if (!ignore_context && !k.empty()) {
q = k.getQuery();
dates = k.getDatetimeList();
} else {
q = getParam<KQuery>("query");
dates = StockManager::instance().getTradingCalendar(q);
}
size_t total = dates.size();
m_discard = 0;
_readyBuffer(total, 1);
HKU_IF_RETURN(total == 0, void());
value_t zero = 0.0;
auto* dst = this->data();
for (size_t i = 0; i < total; i++) {
dst[i] = zero;
}
for (auto iter = block.begin(); iter != block.end(); ++iter) {
const Datetime& start_date = iter->startDatetime();
Datetime last_date = iter->lastDatetime().isNull() ? Datetime::max() : iter->lastDatetime();
for (size_t i = 0; i < total; i++) {
if (dates[i] >= start_date && dates[i] <= last_date) {
dst[i]++;
}
}
}
}
Indicator HKU_API BLOCKSETNUM(const Block& block, const KQuery& query) {
IndicatorImpPtr p = make_shared<IBlockSetNum>();
p->setParam<KQuery>("query", query);
p->setParam<Block>("block", block);
if (query == Null<KQuery>()) {
p->setParam<bool>("ignore_context", true);
} else {
p->calculate();
}
return Indicator(p);
}
Indicator HKU_API BLOCKSETNUM(const string& category, const string& name, const KQuery& query) {
Block block = StockManager::instance().getBlock(category, name);
return BLOCKSETNUM(block, query);
}
} /* namespace hku */

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2024 hikyuu.org
*
* Created on: 2024-05-21
* Author: fasiondog
*/
#pragma once
#ifndef INDICATOR_IMP_IBLOCKSETNUM_H_
#define INDICATOR_IMP_IBLOCKSETNUM_H_
#include "../Indicator.h"
namespace hku {
class IBlockSetNum : public IndicatorImp {
INDICATOR_IMP(IBlockSetNum)
INDICATOR_IMP_NO_PRIVATE_MEMBER_SERIALIZATION
public:
IBlockSetNum();
virtual ~IBlockSetNum();
};
} // namespace hku
#endif /* INDICATOR_IMP_IBLOCKSETNUM_H_ */

View File

@ -1848,4 +1848,22 @@ void export_Indicator_build_in(py::module& m) {
:param KData kdata: K线数据
:param int ix:
:param int name: )");
m.def("BLOCKSETNUM", py::overload_cast<const Block&, const KQuery&>(BLOCKSETNUM),
py::arg("block"), py::arg("query"), R"(BLOCKSETNUM(block, query)
:param Block block:
:param Query query: )");
m.def(
"BLOCKSETNUM", py::overload_cast<const string&, const string&, const KQuery&>(BLOCKSETNUM),
py::arg("category"), py::arg("name"), py::arg("query"), R"(BLOCKSETNUM(category, name, query)
:param str category:
:param str name:
:param query )");
}