本节介绍一些常见的股票特征,可用于股票分析和预测模型训练
顺势指标CCI
CCI中文译名为:随顺市势指标。它属于超买超卖指标中较特殊的一种。波动于广向正值无限大和微向负值无限小之间。本指标专门用以测量股价是否已超出常态分布范围
计算公式
CCI(N日)=(TP-MA)÷Std÷0.015
其中,TP=(最高价+最低价+收盘价)÷3
MA=近N日收盘价的累计之和÷N
Std=近N日数据的标准差
# Load the necessary packages and modules
import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt
# Commodity Channel Index
def CCI(data, ndays): TP = (data['High'] + data['Low'] + data['Close']) / 3 CCI = pd.Series((TP - pd.rolling_mean(TP, ndays)) / (0.015 * pd.rolling_std(TP, ndays)),name = 'CCI') data = data.join(CCI) return data# Retrieve the Nifty data from Yahoo finance:
data = web.DataReader('^NSEI',data_source='yahoo',start='1/1/2014', end='1/1/2016')
data = pd.DataFrame(data)# Compute the Commodity Channel Index(CCI) for NIFTY based on the 20-day Moving average
n = 20
NIFTY_CCI = CCI(data, n)
CCI = NIFTY_CCI['CCI']# Plotting the Price Series chart and the Commodity Channel index below
fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(2, 1, 1)
ax.set_xticklabels([])
plt.plot(data['Close'],lw=1)
plt.title('NSE Price Chart')
plt.ylabel('Close Price')
plt.grid(True)
bx = fig.add_subplot(2, 1, 2)
plt.plot(CCI,'k',lw=0.75,linestyle='-',label='CCI')
plt.legend(loc=2,prop={'size':9.5})
plt.ylabel('CCI values')
plt.grid(True)
plt.setp(plt.gca().get_xticklabels(), rotation=30)
简易波动指标EVM
简易波动指标(Ease of Movement Value)又称EMV指标,它是由RichardW.ArmJr.根据等量图和压缩图的原理设计而成,目的是将价格与成交量的变化结合成一个波动指标来反映股价或指数的变动状况。由于股价的变化和成交量的变化都可以引发该指标数值的变动,因此,EMV实际上也是一个量价合成指标。
计算公式
EVM=(Current High Price - Current Low Price)/2 - (Prior High Price - Prior Low Price)/2
# Load the necessary packages and modules
import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt# Ease of Movement
def EVM(data, ndays): dm = ((data['High'] + data['Low'])/2) - ((data['High'].shift(1) + data['Low'].shift(1))/2)br = (data['Volume'] / 100000000) / ((data['High'] - data['Low']))EVM = dm / br EVM_MA = pd.Series(pd.rolling_mean(EVM, ndays), name = 'EVM') data = data.join(EVM_MA) return data # Retrieve the AAPL data from Yahoo finance:
data = web.DataReader('AAPL',data_source='yahoo',start='1/1/2015', end='1/1/2016')
data = pd.DataFrame(data)# Compute the 14-day Ease of Movement for AAPL
n = 14
AAPL_EVM = EVM(data, n)
EVM = AAPL_EVM['EVM']# Plotting the Price Series chart and the Ease Of Movement below
fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(2, 1, 1)
ax.set_xticklabels([])
plt.plot(data['Close'],lw=1)
plt.title('AAPL Price Chart')
plt.ylabel('Close Price')
plt.grid(True)
bx = fig.add_subplot(2, 1, 2)
plt.plot(EVM,'k',lw=0.75,linestyle='-',label='EVM(14)')
plt.legend(loc=2,prop={'size':9})
plt.ylabel('EVM values')
plt.grid(True)
plt.setp(plt.gca().get_xticklabels(), rotation=30)
移动平均线指标MA
移动平均线(MA)具有趋势的特性,它比较平稳,不像日K线会起起落落地震荡。越长期的移动平均线,越能表现稳定的特性。不轻易向上向下,必须等股价趋势的真正明朗。移动平均线说到底是一种趋势追踪工具,便于识别趋势已经终结或者反转,新的趋势是否正在形成。
计算公式
.N日MA=N日收市价的总和/N(即算术平均数)
# Moving Averages Code# Load the necessary packages and modules
import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt# Simple Moving Average
def SMA(data, ndays): SMA = pd.Series(pd.rolling_mean(data['Close'], ndays), name = 'SMA') data = data.join(SMA) return data# Exponentially-weighted Moving Average
def EWMA(data, ndays): EMA = pd.Series(pd.ewma(data['Close'], span = ndays, min_periods = ndays - 1), name = 'EWMA_' + str(ndays)) data = data.join(EMA) return data# Retrieve the Nifty data from Yahoo finance:
data = web.DataReader('^NSEI',data_source='yahoo',start='1/1/2013', end='1/1/2016')
data = pd.DataFrame(data)
close = data['Close']# Compute the 50-day SMA for NIFTY
n = 50
SMA_NIFTY = SMA(data,n)
SMA_NIFTY = SMA_NIFTY.dropna()
SMA = SMA_NIFTY['SMA']# Compute the 200-day EWMA for NIFTY
ew = 200
EWMA_NIFTY = EWMA(data,ew)
EWMA_NIFTY = EWMA_NIFTY.dropna()
EWMA = EWMA_NIFTY['EWMA_200']# Plotting the NIFTY Price Series chart and Moving Averages below
plt.figure(figsize=(9,5))
plt.plot(data['Close'],lw=1, label='NSE Prices')
plt.plot(SMA,'g',lw=1, label='50-day SMA (green)')
plt.plot(EWMA,'r', lw=1, label='200-day EWMA (red)')
plt.legend(loc=2,prop={'size':11})
plt.grid(True)
plt.setp(plt.gca().get_xticklabels(), rotation=30)
布林线指标(BB)
布林线(BOLL)由约翰布林先生创造,其利用统计原理,求出股价的标准差及其信赖区间,从而确定股价的波动范围及未来走势,利用波带显示股价的安全高低价位,因而也被称为布林带。其上下限范围不固定,随股价的滚动而变化。布林指标和麦克指标MIKE一样同属路径指标,股价波动在上限和下限的区间之内,这条带状区的宽窄,随着股价波动幅度的大小而变化,股价涨跌幅度加大时,带状区变宽,涨跌幅度狭小盘整时,带状区则变窄。
计算公式
中轨线=N日的移动平均线
上轨线=中轨线+两倍的标准差
下轨线=中轨线-两倍的标准差
################ Bollinger Bands ############################## Load the necessary packages and modules
import pandas as pd
import pandas.io.data as web# Compute the Bollinger Bands
def BBANDS(data, ndays):MA = pd.Series(pd.rolling_mean(data['Close'], ndays)) SD = pd.Series(pd.rolling_std(data['Close'], ndays))b1 = MA + (2 * SD)B1 = pd.Series(b1, name = 'Upper BollingerBand') data = data.join(B1) b2 = MA - (2 * SD)B2 = pd.Series(b2, name = 'Lower BollingerBand') data = data.join(B2) return data# Retrieve the Nifty data from Yahoo finance:
data = web.DataReader('^NSEI',data_source='yahoo',start='1/1/2010', end='1/1/2016')
data = pd.DataFrame(data)# Compute the Bollinger Bands for NIFTY using the 50-day Moving average
n = 50
NIFTY_BBANDS = BBANDS(data, n)
print(NIFTY_BBANDS)
强力指数FI
强力指数指标(Force Index)由Alexander Elder发明,该技术指标用来指示上升或下降趋势的力量大小,在零线上下移动来表示趋势的强弱。
计算公式
FORCE INDEX(i)=VOLUME(i)*[MA(ApPRICE,N,i)-MA(ApPRICE,N,i-1)]
FORCE INDEX(i):当前柱的力量指数
VOLUME(i):当前柱的交易量;
MA(ApPRICE,N,i):在任何一个时段内当前柱的任何移动平均线:
MA(ApPRICE,N,i-1)——前一柱的任何移动平均线。
################# Force Index ######################################################### Load the necessary packages and modules
import pandas as pd
import pandas.io.data as web# Force Index
def ForceIndex(data, ndays): FI = pd.Series(data['Close'].diff(ndays) * data['Volume'], name = 'ForceIndex') data = data.join(FI) return data# Retrieve the Apple data from Yahoo finance:
data = web.DataReader('AAPL',data_source='yahoo',start='1/1/2010', end='1/1/2016')
data = pd.DataFrame(data)# Compute the Force Index for Apple
n = 1
AAPL_ForceIndex = ForceIndex(data,n)
print(AAPL_ForceIndex)
变化速率ROC
OC(Price Rate of Change)又称变动率指标,乃以今天的收盘价比较其N天前的收盘价的差除以N天前的收盘(本质其实是N日K线的涨幅),以比率表示之,此指标经由Gerald Apple 和 Fred Hitschler 两人于"Stock Market Trading Systems"一书中介绍,采用12天及25天周期可达到相当的效果。
计算公式
(close-ref(close,N))/ref(close,N)*100
# Rate of Change code# Load the necessary packages and modules
import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt# Rate of Change (ROC)
def ROC(data,n):N = data['Close'].diff(n)D = data['Close'].shift(n)ROC = pd.Series(N/D,name='Rate of Change')data = data.join(ROC)return data # Retrieve the NIFTY data from Yahoo finance:
data = web.DataReader('^NSEI',data_source='yahoo',start='6/1/2015',end='1/1/2016')
data = pd.DataFrame(data)# Compute the 5-period Rate of Change for NIFTY
n = 5
NIFTY_ROC = ROC(data,n)
ROC = NIFTY_ROC['Rate of Change']# Plotting the Price Series chart and the Ease Of Movement below
fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(2, 1, 1)
ax.set_xticklabels([])
plt.plot(data['Close'],lw=1)
plt.title('NSE Price Chart')
plt.ylabel('Close Price')
plt.grid(True)
bx = fig.add_subplot(2, 1, 2)
plt.plot(ROC,'k',lw=0.75,linestyle='-',label='ROC')
plt.legend(loc=2,prop={'size':9})
plt.ylabel('ROC values')
plt.grid(True)
plt.setp(plt.gca().get_xticklabels(), rotation=30)