In [2]:
from matplotlib import rcParams
import matplotlib.pyplot as plt
import numpy as np

np.seterr(all='ignore')
rcParams['figure.figsize'] = (14, 6)

In [3]:
from funcat import *

In [4]:
from funcat.data.tushare_backend import TushareDataBackend
from funcat.data.rqalpha_data_backend import RQAlphaDataBackend

backend = "rqalpha"

if backend == "rqalpha":
    set_data_backend(RQAlphaDataBackend("~/.rqalpha/bundle"))
elif backend == "tushare":
    set_data_backend(TushareDataBackend())

set_start_date("2015-01-01")
S("000001.XSHG")  # 设置当前关注股票
T("2016-06-01")   # 设置当前观察日期

基本API


In [5]:
print(O, H, L, C, V)  # 打印 Open High Low Close


2917.15 2929.08 2909.51 2913.51 18838642100.0

In [19]:
C / C[1] - 1  # 当天涨幅


Out[19]:
-0.00106630277513

In [20]:
MA(C, 60)  # 打印60日均线


Out[20]:
2929.268

In [21]:
C > MA(C, 60)  # 判断收盘价是否大于60日均线


Out[21]:
False

In [22]:
HHV(H, 30)  # 30日最高价


Out[22]:
3055.69

In [23]:
COUNT(C > MA(C, 60), 30)  # 最近30日,收盘价 Close 大于60日均线的天数


Out[23]:
11

In [24]:
CROSS(MA(C, 10), MA(C, 20))  # 10日均线上穿


Out[24]:
False

In [25]:
COUNT(C > O, 10)  # 最近10天收阳线的天数


Out[25]:
7

In [26]:
HHV(MAX(O, C), 60)  # 最近60天K线实体的最高价


Out[26]:
3085.03

In [27]:
CROSS(MA(C, 5), MA(C, 10))  # 5日均线上穿10日均线


Out[27]:
False

自定义公式

0x01 KDJ指标


In [28]:
N, M1, M2 = 9, 3, 3

RSV = (CLOSE - LLV(LOW, N)) / (HHV(HIGH, N) - LLV(LOW, N)) * 100
K = EMA(RSV, (M1 * 2 - 1))
D = EMA(K, (M2 * 2 - 1))
J = K * 3 - D * 2

ax = plt.subplot()

plt.plot(K.series, label="K", linewidth=2)
plt.plot(D.series, label="D", alpha=0.7)
plt.plot(J.series, label="J", alpha=0.7)

print(K, D, J)
plt.legend(loc="best")


75.7914832262 62.0860354856 103.202378707
Out[28]:
<matplotlib.legend.Legend at 0x11171ae10>

0x02 均线金叉死叉


In [29]:
ax = plt.subplot()

ma10 = MA(C, 10)
ma30 = MA(C, 30)
buy_signal = CROSS(ma10, ma30)
sell_signal = CROSS(ma30, ma10)

plt.plot(C.series, label="close", linewidth=2)
plt.plot(ma10.series, label="ma10", alpha=0.7)
plt.plot(ma30.series, label="ma30", alpha=0.7)

plt.plot(np.where(buy_signal.series)[0], C.series[np.where(buy_signal.series)[0]], "^", label="buy", markersize=12, color="red")
plt.plot(np.where(sell_signal.series)[0], C.series[np.where(sell_signal.series)[0]], "v", label="sell", markersize=12, color="green")
plt.legend(loc="best")


Out[29]:
<matplotlib.legend.Legend at 0x1117a7240>

0x03 DMA指标(Different of Moving Average)

DMA指标。DMA指标(Different of Moving Average)又叫平行线差指标,是目前股市分析技术指标中的一种中短期指标,它常用于大盘指数和个股的研判。


In [30]:
M1 = 10
M2 = 50
M3 = 10

DDD = MA(CLOSE, M1) - MA(CLOSE, M2)
AMA = MA(DDD, M3)

print(DDD, AMA)

f, (ax1, ax2) = plt.subplots(2, 1)

ax1.plot(C.series, label="close")
ax1.plot(MA(C, 30).series, label="ma60")
ax1.set_xlim(50)

ax2.plot(DDD.series, label="DDD")
ax2.plot(AMA.series, label="AMA")
ax2.set_xlim(50)

plt.legend(loc="best")


-97.1932 -112.57508
Out[30]:
<matplotlib.legend.Legend at 0x11213c400>

选股


In [6]:
# 选出涨停股
select(
    lambda : C / C[1] - 1 >= 0.0995,
    start_date=20170104,
	end_date=20170104,
)


lambda : C / C[1] - 1 >= 0.0995,
20170104 20170104
[20170104]
20170104 000017.XSHE 深中华A
20170104 000026.XSHE 飞亚达A
20170104 000045.XSHE 深纺织A
20170104 000585.XSHE 东北电气
20170104 000595.XSHE *ST宝实
20170104 000678.XSHE 襄阳轴承
20170104 000695.XSHE 滨海能源
20170104 000710.XSHE *ST天仪
20170104 000755.XSHE *ST三维
20170104 000885.XSHE 同力水泥
20170104 000959.XSHE 首钢股份
20170104 002245.XSHE 澳洋顺昌
20170104 002297.XSHE 博云新材
20170104 002300.XSHE 太阳电缆
20170104 002461.XSHE 珠江啤酒
20170104 002530.XSHE 丰东股份
20170104 002703.XSHE 浙江世宝
20170104 002709.XSHE 天赐材料
20170104 002738.XSHE 中矿资源
20170104 002833.XSHE 弘亚数控
20170104 002835.XSHE 同为股份
20170104 002836.XSHE 新宏泽
20170104 002837.XSHE 英维克
20170104 300073.XSHE 当升科技
20170104 300130.XSHE 新国都
20170104 300175.XSHE 朗源股份
20170104 300219.XSHE 鸿利智汇
20170104 300243.XSHE 瑞丰高材
20170104 300304.XSHE 云意电气
20170104 300340.XSHE 科恒股份
20170104 300402.XSHE 宝色股份
20170104 300503.XSHE 昊志机电
20170104 300571.XSHE 平治信息
20170104 300573.XSHE 兴齐眼药
20170104 300576.XSHE 容大感光
20170104 300577.XSHE 开润股份
20170104 300579.XSHE 数字认证
20170104 300581.XSHE 晨曦航空
20170104 300582.XSHE 英飞特
20170104 300585.XSHE 奥联电子
No data for 300604.XSHE
No data for 300640.XSHE
20170104 600183.XSHG 生益科技
20170104 600209.XSHG 罗顿发展
20170104 600215.XSHG 长春经开
20170104 600249.XSHG 两面针
20170104 600416.XSHG 湘电股份
20170104 600571.XSHG 信雅达
20170104 600680.XSHG ST上普
20170104 600698.XSHG 湖南天雁
20170104 600800.XSHG 天津磁卡
20170104 600810.XSHG 神马股份
20170104 601188.XSHG 龙江交通
20170104 601375.XSHG 中原证券
20170104 601965.XSHG 中国汽研
20170104 603009.XSHG 北特科技
20170104 603058.XSHG 永吉股份
20170104 603186.XSHG 华正新材
20170104 603218.XSHG 日月股份
20170104 603239.XSHG 浙江仙通
20170104 603298.XSHG 杭叉集团
20170104 603577.XSHG 汇金通
20170104 603779.XSHG 威龙股份
20170104 603823.XSHG 百合花
20170104 603886.XSHG 元祖股份
20170104 603929.XSHG 亚翔集成
20170104 603988.XSHG 中电电机


In [8]:
# 选出最近30天K线实体最高价最低价差7%以内,最近100天K线实体最高价最低价差25%以内,
# 最近10天,收盘价大于60日均线的天数大于3天
select(
    lambda : ((HHV(MAX(C, O), 30) / LLV(MIN(C, O), 30) - 1 < 0.07)
              & (HHV(MAX(C, O), 100) / LLV(MIN(C, O), 100) - 1 > 0.25)
              & (COUNT(C > MA(C, 60), 10) > 3)
             ),
    start_date=20170104,
	end_date=20170104,
)


lambda : ((HHV(MAX(C, O), 30) / LLV(MIN(C, O), 30) - 1 < 0.07)
              & (HHV(MAX(C, O), 100) / LLV(MIN(C, O), 100) - 1 > 0.25)
              & (COUNT(C > MA(C, 60), 10) > 3)
             ),
20170104 20170104
[20170104]
20170104 600512.XSHG 腾达建设


In [7]:
# 选出最近3天每天的成交量小于20日成交量均线,最近3天最低价低于20日均线,最高价高于20日均线
# 自定义选股回调函数
def callback(date, order_book_id, symbol):
    print("Cool, 在", date, "选出", order_book_id, symbol)

    
select(
    lambda : (EVERY(V < MA(V, 20) / 2, 3) & EVERY(L < MA(C, 20), 3) & EVERY(H > MA(C, 20), 3)),
    start_date=20170104,
	end_date=20170104,
    callback=callback,
)


lambda : (EVERY(V < MA(V, 20) / 2, 3) & EVERY(L < MA(C, 20), 3) & EVERY(H > MA(C, 20), 3)),
[20170104]
Cool, 在 20170104 选出 002633.XSHE 申科股份
No data for 300604.XSHE
No data for 300640.XSHE
Cool, 在 20170104 选出 600857.XSHG 宁波中百