实现select; 增加setContext直接以KData为参数; kaufman画图小调整

This commit is contained in:
fasiondog 2019-03-28 23:24:15 +08:00
parent 87e028c1bd
commit b9168b197f
10 changed files with 85 additions and 12 deletions

View File

@ -173,6 +173,46 @@ zsbk_zz100 = sm.getBlock("指数板块", "沪深300")
use_draw_engine('matplotlib')
#==============================================================================
#
# 粗略的选股函数
#
#==============================================================================
def select(cond, start=Datetime(201801010000), end=Datetime.now(), print_out=True):
"""
#选出涨停股
C = CLOSE()
x = select(C / REF(C, 1) - 1 >= 0.0995))
:param Indicator cond:
:param Datetime start:
:param Datetime end:
:param bool print_out:
:rtype:
"""
q = QueryByDate(start, end)
d = sm.getTradingCalendar(q, 'SH')
if len(d) == 0:
return
result = []
for s in blocka:
if not s.valid:
continue
q = QueryByDate(start, end)
k = s.getKData(q)
cond.setContext(k)
if len(cond) > 0 and cond[-1] and len(k) > 0 and k[-1].datetime == d[-1]:
result.append(s)
if print_out:
print(d[-1], s)
return result
#==============================================================================
#
# 增加临时的实时数据更新函数 realtimeUpdate

View File

@ -174,22 +174,16 @@ def draw2(block, query = Query(-130),
c = CLOSE(kdata)
CVAL(c, 0.8).plot(axes=ax2,color='r',linestyle='--')
CVAL(c, 0.2).plot(axes=ax2,color='r',linestyle='--')
#ax2.hlines(0.8,0,len(kdata),color='r',linestyle='--')
#ax2.hlines(0.2,0,len(kdata),color='r',linestyle='--')
if ama1.name == "AMA":
cer = PRICELIST(cama, 1)
label = "ER(%s)" % cer[-1]
cer.plot(axes=ax3, color='b', marker='.', label=label,
legend_on=False, text_on=True)
CVAL(c, 0.8).plot(axes=ax2,color='r',linestyle='--')
CVAL(c, -0.6).plot(axes=ax2,color='r',linestyle='--')
CVAL(c, -0.8).plot(axes=ax2,color='r',linestyle='--')
CVAL(c, 0).plot(axes=ax2,color='k',linestyle='-')
#ax3.hlines(0.8,0,len(kdata),color='r',linestyle='--')
#ax3.hlines(-0.6,0,len(kdata),color='r',linestyle='--')
#ax3.hlines(-0.8,0,len(kdata),color='r',linestyle='--')
#ax3.hlines(0,0,len(kdata))
CVAL(c, 0.8).plot(axes=ax3,color='r',linestyle='--')
CVAL(c, -0.6).plot(axes=ax3,color='r',linestyle='--')
CVAL(c, -0.8).plot(axes=ax3,color='r',linestyle='--')
CVAL(c, 0).plot(axes=ax3,color='k',linestyle='-')
else:
ax_draw_macd(ax2, kdata)
#ax2.set_ylim(-1, 1)

View File

@ -20,6 +20,10 @@ void HKU_API setGlobalContext(const Stock& stock, const KQuery& query) {
}
}
void HKU_API setGlobalContext(const KData& k) {
g_hikyuu_context.set<KData>("kdata", k);
}
KData HKU_API getGlobalContextKData() {
return g_hikyuu_context.get<KData>("kdata");
}

View File

@ -16,6 +16,8 @@ namespace hku {
void HKU_API setGlobalContext(const Stock&, const KQuery&);
void HKU_API setGlobalContext(const KData&);
KData HKU_API getGlobalContextKData();
} /* namespace */

View File

@ -35,6 +35,10 @@ void Indicator::setContext(const Stock& stock, const KQuery& query) {
if (m_imp) m_imp->setContext(stock, query);
}
void Indicator::setContext(const KData& k) {
if (m_imp) m_imp->setContext(k);
}
Indicator& Indicator::operator=(const Indicator& indicator) {
if (this == &indicator)
return *this;

View File

@ -59,6 +59,7 @@ public:
Indicator clone() const;
void setContext(const Stock&, const KQuery&);
void setContext(const KData&);
KData getCurrentKData() const {
return m_imp ? m_imp->getCurrentKData() : KData();

View File

@ -75,6 +75,24 @@ void IndicatorImp::setContext(const Stock& stock, const KQuery& query) {
calculate();
}
void IndicatorImp::setContext(const KData& k) {
m_need_calculate = true;
//子节点设置上下文
if (m_left) m_left->setContext(k);
if (m_right) m_right->setContext(k);
if (m_three) m_three->setContext(k);
//如果该节点依赖上下文
if (isNeedContext()) {
//如果上下文有变化则重设上下文
setParam<KData>("kdata", k);
}
//启动重新计算
calculate();
}
KData IndicatorImp::getCurrentKData() {
KData kdata = getParam<KData>("kdata");
if (kdata.getStock().isNull()) {

View File

@ -137,6 +137,8 @@ public:
void setContext(const Stock&, const KQuery&);
void setContext(const KData&);
KData getCurrentKData();
void add(OPType, IndicatorImpPtr left, IndicatorImpPtr right);

View File

@ -11,8 +11,12 @@
using namespace boost::python;
using namespace hku;
void (*setGlobalContext_1)(const Stock&, const KQuery&) = setGlobalContext;
void (*setGlobalContext_2)(const KData&) = setGlobalContext;
void export_context() {
def("set_current_context", setGlobalContext);
def("set_current_context", setGlobalContext_1);
def("set_current_context", setGlobalContext_2);
def("get_current_context", getGlobalContextKData);
}

View File

@ -45,6 +45,9 @@ Indicator (*indicator_or2)(const Indicator&, price_t) = operator|;
string (Indicator::*ind_read_name)() const = &Indicator::name;
void (Indicator::*ind_write_name)(const string&) = &Indicator::name;
void (Indicator::*setContext_1)(const Stock&, const KQuery&) = &Indicator::setContext;
void (Indicator::*setContext_2)(const KData&) = &Indicator::setContext;
void export_Indicator() {
class_<Indicator>("Indicator", init<>())
@ -64,7 +67,8 @@ void export_Indicator() {
.def("get", &Indicator::get, get_overloads())
.def("getResult", &Indicator::getResult)
.def("getResultAsPriceList", &Indicator::getResultAsPriceList)
.def("setContext", &Indicator::setContext)
.def("setContext", setContext_1)
.def("setContext", setContext_2)
.def("getCurrentKData", &Indicator::getCurrentKData)
.def("getImp", &Indicator::getImp)
.def("__len__", &Indicator::size)