def get_stock_basic_parameters(code, Cycle='D', period=480): ''' :param code:股票代码 :param Cycle: 分析周期 :param period: 分析的时间周期 :return:返回指标列表: ['EMADir','SMADir','MACDDir','SMALight','EMALight','valuerel','ATRWz','ATRRatio', 'oneATR','twoATR','threeATR','moneATR','mtwoATR','mthreeATR','position','curup', 'curdown'] ''' code = DP.codeType(code) print(code) dfdata = ts.get_k_data(code, ktype=Cycle) if (len(dfdata) < 60): return None closes = dfdata['close'].values dates = dfdata['date'].values EMADir = ta.EMA_Direction(dates, closes) SMADir = ta.SMA_Direction(dates, closes) MACDDir = ta.MACD_Direction(dates, closes) SMALight = Triple.ImpluseLight(SMADir, MACDDir) EMALight = Triple.ImpluseLight(EMADir, MACDDir) valuerel = ta.SMAValueZone_GX(dates, closes) highs = dfdata['high'].values lows = dfdata['low'].values curatr = ta.last_atr(dates, closes, highs, lows) absdis, perdis, maprice = ta.curdistosma(closes, 20) ATRWz = absdis / curatr ATRRatio = absdis / maprice oneATR = maprice + curatr twoATR = maprice + 2 * curatr threeATR = maprice + 3 * curatr moneATR = maprice - curatr mtwoATR = maprice - 2 * curatr mthreeATR = maprice - 3 * curatr if (len(closes) > period): closes = closes[-period:] position, curup, curdown = Statistical.position_in_period(closes) valuelist = [ EMADir, SMADir, MACDDir, SMALight, EMALight, valuerel, ATRWz, ATRRatio, oneATR, twoATR, threeATR, moneATR, mtwoATR, mthreeATR, position, curup, curdown ] namelist = [ 'EMADir', 'SMADir', 'MACDDir', 'SMALight', 'EMALight', 'valuerel', 'ATRWz', 'ATRRatio', 'oneATR', 'twoATR', 'threeATR', 'moneATR', 'mtwoATR', 'mthreeATR', 'position', 'curup', 'curdown' ] colnames = [] for ele in namelist: name = Cycle + '-' + ele colnames.append(name) s = pd.Series(valuelist, index=colnames) return s
def get_common_parameters(code, tradeCycle): from Analyze import Technical as ta from Strategies import False_Breakout_Divergence as FB smaperiod = 20 colNames = [ 'code', 'tradeCycle', 'curclose', 'sma', 'ATR', 'ATRRatio', 'ATRWZ', 'ImpLight', 'upchannel', 'downchannel', 'valueWZ', 'rewardratio' ] dfdata = ts.get_k_data(code, ktype=tradeCycle) if (len(dfdata) < 120): # 剔除交易时间少于120天的个股 print('No Enough Trading Days') dates = dfdata['date'].values closes = dfdata['close'].values highs = dfdata['high'].values lows = dfdata['low'].values macddiclist = ta.cal_macd(dates, closes) macd, dif, dea = get_macd_list(macddiclist) absdis, perdis, maprice = ta.curdistosma(closes, smaperiod) curATR = ta.last_atr(dates, closes, highs, lows) upchannel = maprice + 2 * curATR downchannel = maprice - 2 * curATR curClose = closes[-1] ATRRatio = curATR / curClose ATRWZ = absdis / curATR smadir = ta.SMA_Direction(dates, closes) macddir = ta.MACD_Direction(dates, closes) ImpLight = FB.ImpluseLight(smadir, macddir) rewardratio = (upchannel - curClose) * 100 / curClose value_re = ta.SMAValueZone_GX(dates, closes) curATR = round(curATR, 2) ATRRatio = round(ATRRatio * 100, 2) rewardratio = round(rewardratio, 2) ATRWZ = round(ATRWZ, 2) maprice = round(maprice, 2) upchannel = round(upchannel, 2) downchannel = round(downchannel, 2) s = pd.Series([ code, tradeCycle, curClose, maprice, curATR, ATRRatio, ATRWZ, ImpLight, upchannel, downchannel, value_re, rewardratio ], index=colNames) return s
def ImpluseScore(dates, closes): ''' :param dates:日期 :param closes: 收盘价 :return: 脉冲评分:红0分,绿1分,蓝2分 ''' smadir = ta.SMA_Direction(dates, closes) macddir = ta.MACD_Direction(dates, closes) lightcolor = ImpluseLight(smadir, macddir) if (lightcolor == 'Red'): score = 0 elif (lightcolor == 'green'): score = 1 else: score = 2 return score
def VecCycleIndicators(stockcode = None,Cycle = 'D'): dftotal = ts.get_k_data(stockcode, ktype=Cycle) totalrows = len(dftotal) if(totalrows <100): print('Insufficiennt Data for Analyze' ) return None startIndex = 0 colNames = ['date',Cycle+'-smadir',Cycle+'-macddir',Cycle+'-light',Cycle+'-bandratio', Cycle+'-upband',Cycle+'-lowband',Cycle+'-valuerel',Cycle+'-rsi'] dfIndicator = DataFrame([],columns=colNames) while(startIndex + 100 <= totalrows): print(startIndex) df = dftotal[:startIndex + 100] dates = df['date'].values closes = df['close'].values lows = df['low'].values highs = df['high'].values date = dates[-1] smadir = ta.SMA_Direction(dates, closes) macddir = ta.MACD_Direction(dates, closes) lightcolor = trpscr.ImpluseLight(smadir,macddir) rsi = ta.get_cur_rsi(dates,closes) uppenetrationCounter, downpenetrationCounter, bandratio, upband, lowband \ = ta.envelop_ratio(dates, highs, lows, closes) value_re = ta.SMAValueZone_GX(dates, closes) s = Series([date,smadir,macddir,lightcolor,bandratio,upband,lowband,value_re,rsi],index = colNames) dfIndicator = dfIndicator.append(s,ignore_index=True) startIndex += 1 FileName = stockcode + Cycle +'.csv' FilePath = os.path.join(DataRoot,FileName) dfIndicator.to_csv(FilePath) return dfIndicator
def sz50Daily(): codelist = DI.get_stockcode_list('sz50') colNames = [ 'stockcode', 'smadir', 'uppenetrationCounter', 'downpenetrationCounter', 'macddir', 'bandratio', 'upband', 'lowband', 'value_re' ] filterdf = DataFrame([], columns=colNames) filepath = os.path.join(os.getcwd(), 'data', 'list') for stockcode in codelist['code']: df = ts.get_k_data(stockcode, ktype='D') dates = df['date'].values closes = df['close'].values lows = df['low'].values highs = df['high'].values smadir = ta.SMA_Direction(dates, closes) macddir = ta.MACD_Direction(dates, closes) uppenetrationCounter, downpenetrationCounter, bandratio, upband, lowband\ = ta.envelop_ratio(dates,highs,lows,closes) #print(stockcode,uppenetrationCounter, downpenetrationCounter, bandratio, upband, lowband) value_re = ta.SMAValueZone_GX(dates, closes) s = Series([ stockcode, smadir, uppenetrationCounter, downpenetrationCounter, macddir, bandratio, upband, lowband, value_re ], index=colNames) filterdf = filterdf.append(s, ignore_index=True) filterdf.to_csv(filepath + '.csv')