安装 gm
pip install gm
安装 TaLib
pip install talib
windows 64位 下载参考链接
用于获取若干段的时间序列内容
参数 | 描述 |
---|---|
df=True | 获取一个pandas类型的返回值 |
from gm.api import *
import numpy as np
import talib
set_token("xxxxxxxxxxxxxxxxxxxxxxxxx")
# 399006 创业版
data = history_n(symbol="SZSE.399006",frequency="1d",count=100,end_time="2017-12-31",fields="close",fill_missing="last",adjust=ADJUST_PREV,df=True)
close = np.asarray(data["close"].values)
talib.MA()
计算滑动平均值参数 | 描述 |
---|---|
timeperiod | 可以计算相应的滑动平均周期 |
np.nan_to_sum()
将nan型改成零ma3 = np.nan_to_num(ma3)
print(ma3)
from gm.api import *
import numpy as np
import talib
set_token("xxxxxxxxxxxxxxxxxxxxxx")
data = history_n(symbol="SZSE.399006",frequency="1d",count=100,end_time="2017-12-31",fields="close",fill_missing="last",adjust=ADJUST_PREV,df=True)
close = np.asarray(data["close"].values)
ma5 = talib.MA(close,timeperiod=5)
print(ma5)
talib.EMA()
指数平均数EMA 指数平均数指标,也叫 EXPMA 指标,是一种趋向类指标,是以指数式递减加权的移动平均
E M A = α × P r i c e N + ( 1 − α ) × P r i c e ( N − 1 ) EMA=\alpha\times Price_N + (1-\alpha)\times Price_{(N-1)} EMA=α×PriceN+(1−α)×Price(N−1)
α \alpha α平滑指数
α = 2 ( N + 1 ) \alpha=\frac{2}{(N+1)} α=(N+1)2
from gm.api import *
import numpy as np
import talib
set_token("xxxxxxxxxxxxxxxxxxx")
data = history_n(symbol="SZSE.399006",frequency="1d",count=100,end_time="2017-12-31",fields="close",fill_missing="last",adjust=ADJUST_PREV,df=True)
close = np.asarray(data["close"].values)
ema = talib.EMA(close,timeperiod=12) # 默认时间30
ema = np.nan_to_num(ema)
print(ema)
talib.MACD()
指数平滑移动平均线用于对股票趋势的分析
MACD的意义和双移动平均线基本相同,即由快、慢均线的离散、聚合表征当前的多空状态和股价可能的发展变化趋势
MACD默认值12、26、9为
快速移动平均值(一般选12日)与慢速移动平均值(一般选26日)
以这个数值作为测量两者(快速与慢速线)间的“差离值”的依据。所谓“差离值”(DIF),即12日EMA数值减去26日EMA值。
MACD的反转信号界定为“差离值”的9日移动平均值(9日EMA)
from gm.api import *
import numpy as np
import talib
set_token("xxxxxxxxxxxxxxxxxxxx")
data = history_n(symbol="SZSE.399006",frequency="1d",count=100,end_time="2017-12-31",fields="close",fill_missing="last",adjust=ADJUST_PREV,df=True)
close = np.asarray(data["close"].values)
macd, signal, hist = talib.MACD(close,fastperiod=12, slowperiod=26, signalperiod=9)
macd = np.nan_to_num(macd)
print(macd)
import matplotlib.pyplot as plt
plt.plot(macd,'r')
plt.savefig("macd.png")
plt.show()
np.diff()
MACD斜率MACD值在单位时间内变化情况的大小
np.diff(a, n=1, axis=-1)
处理相邻数间的差值大小
from gm.api import *
import numpy as np
import talib
set_token("xxxxxxxxxxxxxxxxxxxxxxxxxxx")
data = history_n(symbol="SZSE.399006",frequency="1d",count=100,end_time="2017-12-31",fields="close",fill_missing="last",adjust=ADJUST_PREV,df=True)
close = np.asarray(data["close"].values)
macd, signal, hist = talib.MACD(close,fastperiod=12, slowperiod=26, signalperiod=9)
macd = np.nan_to_num(macd)
macd_gradient = np.diff(macd)
print(macd_gradient)
talib.AD()
累积派发线AD 与 talib.ADX()
平均趋向指数ADXAD(累积派发线)是一种平衡交易量指标,以当日得收盘价来估算成交流量,用于估计一段时间内该证券累积得资金流量
AD为资金趋势函数
通常与ADX一起使用,利用多空趋向的变化差离与总和判定平均趋势,ADX数值不能显示趋势的发展方向。
如果趋势存在,ADX可以衡量趋势的强度
from gm.api import *
import numpy as np
import talib
set_token("xxxxxxxxxxxxxxxxxxxxxxxxxxxx")
# 002310 东方园林
data = history_n(symbol="SZSE.002310",frequency="1d",count=100,end_time="2017-12-31",fields="high,low,close,open,volume",fill_missing="last",adjust=ADJUST_PREV,df=True)
close = np.asarray(data["close"].values)
open = np.asarray(data["open"].values)
high = np.asarray(data["high"].values)
low = np.asarray(data["low"].values)
volume = np.asarray(data["volume"].values).astype(np.double)
adx = talib.ADX(high,low,close)
adx = np.nan_to_num(adx)
adx = adx/np.max(adx)
ad = talib.AD(high,low,close,volume)
ad = np.nan_to_num(ad)
ad = ad/np.max(ad)
import matplotlib.pyplot as plt
plt.plot(ad,"r",adx,"b")
plt.savefig("ad_adx.png")
plt.show()
talib.BBANDS()
布林森线from gm.api import *
import numpy as np
import talib
set_token("61c4ec11af460c782950cd5ed41faacd5ac560f9")
data = history_n(symbol="SZSE.002129",frequency="3600s",count=100,end_time="2017-12-31",fields="high,low,close,open,volume",fill_missing="last",adjust=ADJUST_PREV,df=True)
close = np.asarray(data["close"].values)
open = np.asarray(data["open"].values)
high = np.asarray(data["high"].values)
low = np.asarray(data["low"].values)
volume = np.asarray(data["volume"].values).astype(np.double)
upperband,middleband,lowerband = talib.BBANDS(close)