def get_EMA(df,N): for i in range(len(df)): if i==0: df.ix[i,'ema']=df.ix[i,'close'] if i>0: df.ix[i,'ema']=(2*df.ix[i-1,'close']+(N-1)*df.ix[i,'close'])/(N+1) return df def get_DMA(df): #大智慧动态移动平均,移动平均值是根据相应的指标不断变动的,a就是动态值, #假设为最低价/最高价,可以根据实际情况调整 df['ema']= get_EMA(df,10)['ema'] df['5vol']=df['volume'].rolling(window=5,min_periods=1).sum() dma=[] for i in range(len(df)): if i==0: dma.append(df.ix[i,'close']) if i>0: a=df.ix[i,'5vol']/(3*df.ix[i,'amt']) #print (a) a1=(1-a)*df.ix[i,'ema'] a2=a*float(dma[len(dma)-1]) dma.append(a1+a2) df['dma']=dma return df def get_FSL(df): #大智慧分水岭,在上做多,在下做空 df['sw1']=(get_EMA(df,5)['ema']*7+get_EMA(df,10)['ema']*3)/10 df['sw']=get_DMA(df)['dma'] return df from WindPy import* import pandas as pd import numpy as np import datetime import time #获取数据 w.start(60) symbol="000001.SZ" start_date="2016-01-01" end_date="2017-05-02" stock=w.wsd(symbol, "trade_code,open,high,low,close,pre_close,\ volume,amt", start_date,end_date) df = pd.DataFrame() df['date']=stock.Times df['stock_code']=stock.Data[0] #index_data['stock_code'] =symbol df['open'] =stock.Data[1] df['high'] =stock.Data[2] df['low'] =stock.Data[3] df['close']=stock.Data[4] df['pre_close']=stock.Data[5] df['volume']=stock.Data[6] df['amt']=stock.Data[7] df.head() get_FSL(df)