Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化 INDEXC/INDEXO 等为对应大盘指数 #320

Merged
merged 7 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hikyuu/data/em_block_to_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def em_import_block_to_mysql(connect, code_market_dict, categorys=('行业板块
hku_info("更新数据库")
cur = connect.cursor()
if len(blks) == 1:
sql = f"delete from hku_base.block where category in ({blks[0]})"
sql = f"delete from hku_base.block where category in ('{blks[0]}')"
else:
sql = f"delete from hku_base.block where category in {tuple(blks)}"
cur.execute(sql)
Expand All @@ -88,6 +88,7 @@ def em_import_block_to_mysql(connect, code_market_dict, categorys=('行业板块

if insert_records:
sql = "insert into hku_base.block (category, name, market_code) values (%s,%s,%s)"
hku_info(f"insert block records: {len(insert_records)}")
cur.executemany(sql, insert_records)

connect.commit()
Expand Down
8 changes: 5 additions & 3 deletions hikyuu/data/em_block_to_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def em_import_block_to_sqlite(connect, code_market_dict, categorys=('行业板
hku_info("更新数据库")
cur = connect.cursor()
if len(blks) == 1:
sql = f"delete from block where category in ({blks[0]})"
sql = f"delete from block where category in ('{blks[0]}')"
else:
sql = f"delete from block where category in {tuple(blks)}"
hku_info(sql)
Expand All @@ -90,6 +90,7 @@ def em_import_block_to_sqlite(connect, code_market_dict, categorys=('行业板

if insert_records:
sql = "insert into block (category, name, market_code) values (?,?,?)"
hku_info(f"insert block records: {len(insert_records)}")
cur.executemany(sql, insert_records)

connect.commit()
Expand All @@ -101,7 +102,8 @@ def em_import_block_to_sqlite(connect, code_market_dict, categorys=('行业板
from hikyuu.data.common_sqlite3 import create_database

# dest_dir = "/home/fasiondog/stock"
dest_dir = "d:\\stock"
dest_dir = "/Users/fasiondog/stock"
# dest_dir = "d:\\stock"

connect = sqlite3.connect(dest_dir + "/stock.db")
create_database(connect)
Expand All @@ -112,6 +114,6 @@ def em_import_block_to_sqlite(connect, code_market_dict, categorys=('行业板
code_market_dict[v["code"]] = MARKET.SH
# print(code_market_dict)

em_import_block_to_sqlite(connect, code_market_dict)
em_import_block_to_sqlite(connect, code_market_dict, categorys=('行业板块', '指数板块',))

connect.close()
1 change: 1 addition & 0 deletions hikyuu_cpp/hikyuu/indicator/build_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "crt/HSL.h"
#include "crt/IC.h"
#include "crt/ICIR.h"
#include "crt/INBLOCK.h"
#include "crt/INDEX.h"
#include "crt/INSUM.h"
#include "crt/IR.h"
Expand Down
31 changes: 31 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/crt/INBLOCK.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2025 hikyuu.org
*
* Created on: 2025-01-26
* Author: fasiondog
*/

#pragma once

#include "../Indicator.h"

namespace hku {

/**
* @brief 返回品种是否属于某板块
* @param category 指定板块分类
* @param name 板块名称
* @return Indicator
*/
Indicator HKU_API INBLOCK(const string& category, const string& name);

/**
* @brief 返回品种是否属于某板块
* @param kdata K线数据
* @param category 指定板块分类
* @param name 板块名称
* @return Indicator
*/
Indicator HKU_API INBLOCK(const KData& kdata, const string& category, const string& name);

} // namespace hku
62 changes: 1 addition & 61 deletions hikyuu_cpp/hikyuu/indicator/crt/INDEX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,6 @@

namespace hku {

Indicator HKU_API INDEXO() {
KData k = getKData("SH000001", KQueryByIndex(-1));
return CONTEXT(k.open());
}

Indicator HKU_API INDEXO(const KQuery& query) {
KData k = getKData("SH000001", query);
return CONTEXT(k.open());
}

Indicator HKU_API INDEXH() {
KData k = getKData("SH000001", KQueryByIndex(-1));
return CONTEXT(k.high());
}

Indicator HKU_API INDEXH(const KQuery& query) {
KData k = getKData("SH000001", query);
return CONTEXT(k.high());
}

Indicator HKU_API INDEXL() {
KData k = getKData("SH000001", KQueryByIndex(-1));
return CONTEXT(k.low());
}

Indicator HKU_API INDEXL(const KQuery& query) {
KData k = getKData("SH000001", query);
return CONTEXT(k.low());
}

Indicator HKU_API INDEXC() {
KData k = getKData("SH000001", KQueryByIndex(-1));
return CONTEXT(k.close());
}

Indicator HKU_API INDEXC(const KQuery& query) {
KData k = getKData("SH000001", query);
return CONTEXT(k.close());
}

Indicator HKU_API INDEXA() {
KData k = getKData("SH000001", KQueryByIndex(-1));
return CONTEXT(k.amo());
}

Indicator HKU_API INDEXA(const KQuery& query) {
KData k = getKData("SH000001", query);
return CONTEXT(k.amo());
}

Indicator HKU_API INDEXV() {
KData k = getKData("SH000001", KQueryByIndex(-1));
return CONTEXT(k.vol());
}

Indicator HKU_API INDEXV(const KQuery& query) {
KData k = getKData("SH000001", query);
return CONTEXT(k.vol());
}

Indicator HKU_API INDEXADV() {
KData k = getKData("SH880005", KQueryByIndex(-1));
return CONTEXT(k.close());
Expand All @@ -90,4 +30,4 @@ Indicator HKU_API INDEXDEC(const KQuery& query) {
return CONTEXT(k.open());
}

}
} // namespace hku
58 changes: 35 additions & 23 deletions hikyuu_cpp/hikyuu/indicator/crt/INDEX.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,41 @@ namespace hku {

// 这里的大盘都指 sh000001

/** 大盘开盘价 */
Indicator HKU_API INDEXO();
Indicator HKU_API INDEXO(const KQuery& query);

/** 大盘最高价盘价 */
Indicator HKU_API INDEXH();
Indicator HKU_API INDEXH(const KQuery& query);

/** 大盘最低价 */
Indicator HKU_API INDEXL();
Indicator HKU_API INDEXL(const KQuery& query);

/** 大盘收盘价 */
Indicator HKU_API INDEXC();
Indicator HKU_API INDEXC(const KQuery& query);

/** 大盘成交额 */
Indicator HKU_API INDEXA();
Indicator HKU_API INDEXA(const KQuery& query);

/** 大盘成交量 */
Indicator HKU_API INDEXV();
Indicator HKU_API INDEXV(const KQuery& query);
/** 对应的大盘开盘价,分别是上证指数,深证成指,科创50,创业板指 */
Indicator HKU_API INDEXO(bool fill_null = true);
inline Indicator INDEXO(const KData& k, bool fill_null = true) {
return INDEXO(fill_null)(k);
}

/** 对应的大盘最高价,分别是上证指数,深证成指,科创50,创业板指 */
Indicator HKU_API INDEXH(bool fill_null = true);
inline Indicator INDEXH(const KData& k, bool fill_null = true) {
return INDEXH(fill_null)(k);
}

/** 对应的大盘最低价,分别是上证指数,深证成指,科创50,创业板指 */
Indicator HKU_API INDEXL(bool fill_null = true);
inline Indicator INDEXL(const KData& k, bool fill_null = true) {
return INDEXL(fill_null)(k);
}

/** 对应的大盘收盘价,分别是上证指数,深证成指,科创50,创业板指 */
Indicator HKU_API INDEXC(bool fill_null = true);
inline Indicator INDEXC(const KData& k, bool fill_null = true) {
return INDEXC(fill_null)(k);
}

/** 对应的大盘成交金额,分别是上证指数,深证成指,科创50,创业板指 */
Indicator HKU_API INDEXA(bool fill_null = true);
inline Indicator INDEXA(const KData& k, bool fill_null = true) {
return INDEXA(fill_null)(k);
}

/** 对应的大盘成交量,分别是上证指数,深证成指,科创50,创业板指 */
Indicator HKU_API INDEXV(bool fill_null = true);
inline Indicator INDEXV(const KData& k, bool fill_null = true) {
return INDEXV(fill_null)(k);
}

/** 大盘上涨家数, 使用通达信 SH880005,可能无法用于实盘 */
Indicator HKU_API INDEXADV();
Expand Down
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu/indicator/imp/IDma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void IDma::_calculate(const Indicator& ind) {
size_t total = ind.size();
HKU_IF_RETURN(total == 0, void());

_readyBuffer(total, 2);
_readyBuffer(total, 1);

auto k = getContext();
m_ref_ind.setContext(k);
Expand Down
59 changes: 59 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/imp/IInBlock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2025 hikyuu.org
*
* Created on: 2025-01-26
* Author: fasiondog
*/

#include "IInBlock.h"

#if HKU_SUPPORT_SERIALIZATION
BOOST_CLASS_EXPORT(hku::IInBlock)
#endif

namespace hku {

IInBlock::IInBlock() : IndicatorImp("INBLOCK", 1) {
setParam<string>("category", "");
setParam<string>("name", "");
}

IInBlock::~IInBlock() {}

IInBlock::IInBlock(const KData& kdata, const string& category, const string& name)
: IndicatorImp("INBLOCK", 1) {
setParam<string>("category", category);
setParam<string>("name", name);
setParam<KData>("kdata", kdata);
IInBlock::_calculate(Indicator());
}

void IInBlock::_calculate(const Indicator& data) {
HKU_IF_RETURN(!isLeaf() && !data.empty(), void());

KData k = getContext();
size_t total = k.size();
HKU_IF_RETURN(total == 0, void());

_readyBuffer(total, 1);

Block block = getBlock(getParam<string>("category"), getParam<string>("name"));
value_t in = block.have(k.getStock()) ? 1.0 : 0.0;
auto* dst = this->data();
for (size_t i = 0; i < total; ++i) {
dst[i] = in;
}
}

Indicator HKU_API INBLOCK(const string& category, const string& name) {
auto p = make_shared<IInBlock>();
p->setParam<string>("category", category);
p->setParam<string>("name", name);
return Indicator(p);
}

Indicator HKU_API INBLOCK(const KData& k, const string& category, const string& name) {
return Indicator(make_shared<IInBlock>(k, category, name));
}

} /* namespace hku */
30 changes: 30 additions & 0 deletions hikyuu_cpp/hikyuu/indicator/imp/IInBlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2025 hikyuu.org
*
* Created on: 2025-01-26
* Author: fasiondog
*/

#pragma once
#ifndef INDICATOR_IMP_IINBLOCK_H_
#define INDICATOR_IMP_IINBLOCK_H_

#include "../Indicator.h"

namespace hku {

/* 已指标形式返回是否在指定板块中 */
class IInBlock : public IndicatorImp {
INDICATOR_IMP(IInBlock)
INDICATOR_NEED_CONTEXT
INDICATOR_IMP_NO_PRIVATE_MEMBER_SERIALIZATION

public:
IInBlock();
explicit IInBlock(const KData& kdata, const string& category, const string& name);
virtual ~IInBlock();
};

} /* namespace hku */

#endif /* INDICATOR_IMP_IINBLOCK_H_ */
Loading
Loading