In [1]:
%matplotlib inline
%time from hikyuu.interactive import *

std::cout are redirected to python::stdout
std::cerr are redirected to python::stderr
2020-08-05 23:39:09.017 [HKU-I] - 5.18s Loaded Data. [init]
CPU times: user 5.55 s, sys: 377 ms, total: 5.93 s
Wall time: 5.93 s


# 1 绘制指标¶

In [2]:
s = sm['sz000001']
k = s.get_kdata(Query(-200))

#抽取K线收盘价指标，一般指标计算参数只能是指标类型，所以必须先将K线数据生成指标类型
c = CLOSE(k)

#计算收盘价的EMA指标
a = EMA(c)

#绘制指标
c.plot(legend_on=True)
a.plot(new=False, legend_on=True)

#绘制柱状图
a.bar()

#修正下柱状图，使其更美观
PRICELIST([x-9 for x in a]).bar()


# 2 指标（Indicator）¶

## 2.1 特殊的Indicator¶

In [3]:
print("k is a instance of KData:\n", k)
print("--------------------------\n")

kind = KDATA(k)
print("kind is a instance of Indicator:\n", kind)

k is a instance of KData:
KData{
size : 200
stock: Stock(SZ, 000001, 平安银行, A股, 1, 1991-01-02 00:00:00, +infinity),
query: KQuery(-200, 9223372036854775807, INDEX, DAY, NO_RECOVER)
}
--------------------------

kind is a instance of Indicator:
Indicator{
name: KDATA
size: 200
result sets: 6
params: params[kdata(KData): SZ000001, kpart(string): KDATA, ]
formula: KDATA
}

In [4]:
#获取 ind 的结果集数量，如MACD通常返回3个结果集
r = kind.get_result_num()
print("result_num: ", r)

#获取第一个结果集
x = kind.get_result(0)
print(x)

result_num:  6
Indicator{
name: IndicatorImp
size: 200
result sets: 1
params: params[kdata(KData): Null, ]
formula: IndicatorImp
}

In [5]:
#以下效果相同
c1 = CLOSE(k)
c2 = KDATA_PART(k, 'close')


In [6]:
x = PRICELIST([i for i in range(100)])
print(len(x), x)

100 Indicator{
name: PRICELIST
size: 100
result sets: 1
params: params[data(PriceList): 100, discard(int): 0, kdata(KData): Null, result_index(int): 0, ]
formula: PRICELIST
}


## 2.2 Indicator的特性与参数¶

In [7]:
e1 = EMA(CLOSE(k), n=5)
e2 = e1(CLOSE(k))
e3 = e2
print(e1 == e2)

Indicator{
name: IndicatorImp
size: 200
result sets: 1
params: params[kdata(KData): Null, ]
formula: EMA(CLOSE) == EMA(CLOSE)
}


In [8]:
e = EMA(c)
print(e)
print(e.get_param('n'))
e.plot(legend_on=True)

e.set_param('n', 30)
e = e(c)
e.plot(new=False, legend_on=True)

Indicator{
name: EMA
size: 200
result sets: 1
params: params[kdata(KData): SZ000001, n(int): 22, ]
formula: EMA(CLOSE)
}
22


• i : int
• s : str
• b : bool
• d : float
In [9]:
#EMA指标有参数 "n"，类型"i"代表整数
print(EMA())

Indicator{
name: EMA
size: 0
result sets: 1
params: params[kdata(KData): Null, n(int): 22, ]
formula: EMA
}


## 2.3 TA-Lib包装指标¶

In [10]:
x = TA_SMA(CLOSE(k))
print(x)
x.plot()


Indicator{
name: TA_SMA
size: 200
result sets: 1
params: params[kdata(KData): SZ000001, timeperiod(int): 30, ]
formula: TA_SMA(CLOSE)
}
29


In [11]:
query = Query(-200)
k1 = sm['sh000001'].get_kdata(query)
k2 = sm['sz000001'].get_kdata(query)

w = WEAVE(CLOSE(k1), CLOSE(k2))
print(w.get_result_num())

cr = TA_CORREL(w)
cr.plot()

2

In [ ]: