技术指标
def MA(series,days):
'''
n日简单移动平均
'''
dict={
}
dict[0] = series
for day in range(1,days):
dict[day] = series.shift(day)
temp = pd.DataFrame(dict)
temp = pd.DataFrame(temp.mean(skipna=False, axis=1),columns=['MA'])
return temp['MA']
def EMA(series,n):
'''
n日指数移动平均,由于需要用到历史移动平均值,故该值将逐步回归真实值
'''
ema = series[0]
EMA = []
for i in series:
ema = (2*i + (n-1)*ema)/(n+1)
EMA.append(ema)
return pd.Series(EMA)
def MACD(series):
'''
MACD值
'''
quick_line = EMA(series, 12)
slow_line = EMA(series, 26)
DIF = quick_line - slow_line
DEA = EMA(DIF, 9)
macd = 2 * (DIF - DEA)
return macd
def macd_trigger(series):
'''
基于MACD值构建的买卖信号
'''
def gen_flag(dataframe):
muti = dataframe['pre_macd'] * dataframe['macd']
if muti <0:
if dataframe['pre_macd']<0:
return 'buy'
else:
return 'sale'
else:
return 'waitting'
pre_macd = series.shift(1)
data = pd.DataFrame({
'pre_macd':pre_macd,'macd':series})
data['macd_trigger'] = data.apply(gen_flag,axis=1)
return data['macd_trigger']
def SAR(price_high, price_low, price_close, n=4, af=