def batch_position(codelist, period=240, value='position', Cycle='D'): colNames = ['code', 'name', 'position', 'curup', 'curdown'] dfPosition = DataFrame([], columns=colNames) index_date = DP.get_last_trade_time() for code, name in zip(codelist['code'], codelist['name']): code = DP.codeType(code) print(code) if (DI.is_tingpai(code)): continue df = ts.get_k_data(code, ktype=Cycle) if (len(df) == 0): continue closes = df['close'].values last_date = df.loc[len(df) - 1, 'date'] if (len(df) < period or last_date != index_date): continue closes = closes[-period:] position, curup, curdown = position_in_period(closes) s = Series([code, name, position, curup, curdown], index=colNames) dfPosition = dfPosition.append(s, ignore_index=True) dfPosition = dfPosition.sort_values(by=value, ascending=True) dfPosition = DataFrame(dfPosition.values, columns=dfPosition.columns) return dfPosition
def batch_invaluezone_divergence(dataset='hs300', tradeCycle='D', recent=5): lasttradetimestr = DP.get_last_trade_time('D') colNames = [ 'code', 'name', 'period', 'curclose', 'sma', 'ATR', 'ATRRatio', 'rewardrisk', 'primImpScore', 'tradeImpScore', 'primdivscore', 'divscore', 'difdeascore', 'macdscore', 'totalscore' ] filename = StrategyName + lasttradetimestr + '-' + dataset + '-' + tradeCycle RootDir = os.path.join(os.pardir, 'data', lasttradetimestr) DP.mkdir(RootDir) filepath = os.path.join(RootDir, filename + '.xls') codelist = DI.get_stockcode_list(dataset) dfSelected = pd.DataFrame([], columns=colNames) dfScreen = codelist[['code', 'name']] totalrows = len(dfScreen) for i in range(totalrows): code = dfScreen.loc[i, 'code'] code = DP.codeType(code) name = dfScreen.loc[i, 'name'] if (DI.is_tingpai(code)): continue dfdata = ts.get_k_data(code, ktype=tradeCycle) if (len(dfdata) < 120): #剔除交易时间少于120天的个股 continue dates = dfdata['date'].values closes = dfdata['close'].values highs = dfdata['high'].values lows = dfdata['low'].values if (not ta.invaluezone(closes, 10, 20)): continue macddiclist = ta.cal_macd(dates, closes) macd, dif, dea = DP.get_macd_list(macddiclist) if (min(macd[-3:]) > 0): continue divStatus = ta.macd_in_bulldivergence(dates, macd) divscore = divStatus['div'] macdscore = divStatus['macd'] period = divStatus['period'] if (divscore != 0): print(code, name, divscore, macdscore, period) absdis, perdis, maprice = ta.curdistosma(closes, 20) curATR = ta.last_atr(dates, closes, highs, lows) ATRWZ = absdis / curATR curClose = closes[-1] ATRRatio = curATR / curClose primCycle = GetPrimaryCycle(tradeCycle) dataprimCycle = ts.get_k_data(code, ktype=primCycle) primCloses = dataprimCycle['close'].values primdates = dataprimCycle['date'].values primlows = dataprimCycle['low'].values primImpScore = FBD.ImpluseScore(primdates, primCloses) tradeImpScore = FBD.ImpluseScore(dates, closes) primmacd = pd.Series(ta.cal_macd(primdates, primCloses)) primmacd, primdif, primdea = DP.get_macd_list(primmacd) primdivStatus = ta.macd_in_bulldivergence_signal( primdates, primmacd) primdivscore = primdivStatus['div'] if (primdivscore != 0): primdivscore = 1 difdeadivscore = 0 if (ta.in_loose_bulldivergence(dif, period)): difdeadivscore += 1 if (ta.in_loose_bulldivergence(dea, period)): difdeadivscore += 1 totalscore = primImpScore + tradeImpScore + primdivscore + divscore + difdeadivscore + macdscore curATR = round(curATR, 2) ATRRatio = round(ATRRatio * 100, 2) ATRWZ = round(ATRWZ, 2) maprice = round(maprice, 2) colNames = [ 'code', 'name', 'period', 'curclose', 'sma', 'ATR', 'ATRRatio', 'ATRWZ', 'primImpScore', 'tradeImpScore', 'primdivscore', 'divscore', 'difdeascore', 'macdscore', 'totalscore' ] s = Series([ code, name, period, curClose, maprice, curATR, ATRRatio, ATRWZ, primImpScore, tradeImpScore, primdivscore, divscore, difdeadivscore, macdscore, totalscore ], index=colNames) dfSelected = dfSelected.append(s, ignore_index=True) dfSelected = dfSelected.sort_values(by='totalscore', ascending=False) dfSelected = pd.DataFrame(dfSelected.values, columns=dfSelected.columns) DI.Write_DF_T0_Excel(filepath, dfSelected, 'selected')
def batch_divergence_monitor(stocklist=None): lasttradetimestr = DP.get_last_trade_time('D') if stocklist is None: stocklist = ['longtou', 'etf'] for dataset in stocklist: for tradeCycle in ['W', 'D', '60', '30', '15']: colNames = [ 'code', 'name', 'cycle', 'period', 'curclose', 'sma', 'ATR', 'ATRRatio', 'rewardrisk', 'primImpScore', 'tradeImpScore', 'primdivscore', 'divscore', 'difdeascore', 'macdscore', 'totalscore' ] filename = StrategyName + '-' + dataset RootDir = os.path.join(os.pardir, 'data', lasttradetimestr) DP.mkdir(RootDir) filepath = os.path.join(RootDir, filename + '.csv') codelist = DI.get_stockcode_list(dataset) dfSelected = pd.DataFrame([], columns=colNames) dfScreen = codelist[['code', 'name']] totalrows = len(dfScreen) for i in range(totalrows): code = dfScreen.loc[i, 'code'] code = DP.codeType(code) name = dfScreen.loc[i, 'name'] #if(DI.is_tingpai(code)): # continue dfdata = ts.get_k_data(code, ktype=tradeCycle) if (len(dfdata) < 120): #剔除交易时间少于120天的个股 continue 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 = DP.get_macd_list(macddiclist) divStatus = ta.macd_in_bulldivergence_signal(dates, macd) divscore = divStatus['div'] macdscore = divStatus['macd'] period = divStatus['period'] if (divscore != 0): print(code, name, tradeCycle, divscore, macdscore, period) absdis, perdis, maprice = ta.curdistosma(closes, 20) curATR = ta.last_atr(dates, closes, highs, lows) ATRWZ = absdis / curATR curClose = closes[-1] ATRRatio = curATR / curClose primCycle = GetPrimaryCycle(tradeCycle) dataprimCycle = ts.get_k_data(code, ktype=primCycle) primCloses = dataprimCycle['close'].values primdates = dataprimCycle['date'].values primlows = dataprimCycle['low'].values primImpScore = FBD.ImpluseScore(primdates, primCloses) tradeImpScore = FBD.ImpluseScore(dates, closes) primmacd = pd.Series(ta.cal_macd(primdates, primCloses)) primmacd, primdif, primdea = DP.get_macd_list(primmacd) primdivStatus = ta.macd_in_bulldivergence_signal( primdates, primmacd) primdivscore = primdivStatus['div'] if (primdivscore != 0): primdivscore = 1 difdeadivscore = 0 if (ta.in_loose_bulldivergence(dif, period)): difdeadivscore += 1 if (ta.in_loose_bulldivergence(dea, period)): difdeadivscore += 1 totalscore = primImpScore + tradeImpScore + primdivscore + divscore + difdeadivscore + macdscore curATR = round(curATR, 2) ATRRatio = round(ATRRatio * 100, 2) ATRWZ = round(ATRWZ, 2) maprice = round(maprice, 2) s = Series([ code, name, tradeCycle, period, curClose, maprice, curATR, ATRRatio, ATRWZ, primImpScore, tradeImpScore, primdivscore, divscore, difdeadivscore, macdscore, totalscore ], index=colNames) dfSelected = dfSelected.append(s, ignore_index=True) dfSelected = dfSelected.sort_values(by='totalscore', ascending=False) dfSelected = pd.DataFrame(dfSelected.values, columns=dfSelected.columns) if os.path.exists(filepath): dfSelected.to_csv(filepath, mode='a', header=None) else: dfSelected.to_csv(filepath)
def batch_falsebreakout_divergence(dataset='hs300', tradeCycle='D', recent=3): lasttradetimestr = DP.get_last_trade_time('D') ''' colNames = ['code', 'time', 'period', 'curclose', 'sma', 'ATR', 'ATRRatio', 'rewardratio', 'riskratio', 'targetprice', 'cutshort', 'ATRWZ', 'rewardrisk', 'primImpScore', 'tradeImpScore', 'valuescore', 'primdivscore', 'divscore', 'difdeascore', 'macdscore', 'totalscore','targetprice1','targetprice2'] ''' colNames = [ 'code', 'name', 'time', 'period', 'curclose', 'sma', 'ATR', 'ATRRatio', 'rewardratio', 'riskratio', 'targetprice', 'cutshort', 'ATRWZ', 'rewardrisk', 'primImpScore', 'tradeImpScore', 'valuescore', 'primdivscore', 'divscore', 'difdeascore', 'macdscore', 'totalscore', 'targetprice2', 'targetprice3' ] filename = StrategyName + lasttradetimestr + '-' + dataset + '-' + tradeCycle RootDir = os.path.join(os.pardir, 'data', lasttradetimestr) DP.mkdir(RootDir) filepath = os.path.join(RootDir, filename + '.xls') codelist = DI.get_stockcode_list(dataset) dfSelected = pd.DataFrame([], columns=colNames) dfScreen = codelist[['code', 'name']] totalrows = len(dfScreen) for i in range(totalrows): code = dfScreen.loc[i, 'code'] code = DP.codeType(code) name = dfScreen.loc[i, 'name'] if (DI.is_tingpai(code)): continue dfdata = ts.get_k_data(code, ktype=tradeCycle) if (len(dfdata) < 120): #剔除交易时间少于120天的个股 continue 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 = DP.get_macd_list(macddiclist) if (not (macd[-2] < 0 and macd[-2] < macd[-1] and macd[-2] < macd[-3])): continue if (code in dfSelected['code'].values): continue divStatus = ta.macd_in_bulldivergence_signal(dates, macd) divscore = divStatus['div'] macdscore = divStatus['macd'] period = divStatus['period'] if (divscore != 0 and isPriceNewLow(lows, recent, period)): absdis, perdis, maprice = ta.curdistosma(closes, 20) curATR = ta.last_atr(dates, closes, highs, lows) targetprice = maprice + 2 * curATR prelow = min(lows[-recent:]) #因此此处要求近recent天创period天新低 ATRWZ = absdis / curATR curClose = closes[-1] #cutshort = min(prelow, curClose - curATR) cutshort = prelow ATRRatio = curATR / curClose #rewardratio = (targetprice-curClose)*100/curClose riskratio = (curClose - cutshort) * 100 / curClose #rewardrisk = (targetprice-curClose)/(curClose-cutshort) primCycle = GetPrimaryCycle(tradeCycle) dataprimCycle = ts.get_k_data(code, ktype=primCycle) primCloses = dataprimCycle['close'].values primdates = dataprimCycle['date'].values primlows = dataprimCycle['low'].values primImpScore = ImpluseScore(primdates, primCloses) tradeImpScore = ImpluseScore(dates, closes) valuescore = value_score(dates, closes) primmacd = pd.Series(ta.cal_macd(primdates, primCloses)) primmacd, primdif, primdea = DP.get_macd_list(primmacd) isPrimNewLow = isPriceNewLow(primlows, recent, 30) primdivStatus = ta.macd_in_bulldivergence_signal( primdates, primmacd) primdivscore = primdivStatus['div'] if (primdivscore != 0 and isPrimNewLow != 0): primdivscore = 2 elif (primdivscore != 0): primdivscore = 1 difdeadivscore = 0 if (ta.in_loose_bulldivergence(dif, period)): difdeadivscore += 1 if (ta.in_loose_bulldivergence(dea, period)): difdeadivscore += 1 totalscore = primImpScore + tradeImpScore + valuescore + primdivscore + divscore + difdeadivscore + macdscore curATR = round(curATR, 4) targetprice2 = curClose + 2 * curATR targetprice3 = np.max(closes[-period:]) target = np.min( [float(targetprice), float(targetprice2), float(targetprice3)]) rewardratio = (target - curClose) * 100 / curClose rewardrisk = (target - curClose) / (curClose - cutshort) ATRRatio = round(ATRRatio * 100, 3) rewardratio = round(rewardratio, 3) riskratio = round(riskratio, 3) ATRWZ = round(ATRWZ, 3) rewardrisk = round(rewardrisk, 3) targetprice = round(targetprice, 3) maprice = round(maprice, 3) cutshort = round(cutshort, 3) import datetime curtime = datetime.datetime.now() selectedcolNames = [ 'ATR', 'ATRRatio', 'code', 'name', 'time', 'curclose', 'cutshort', 'rewardratio', 'riskratio', 'rewardrisk', 'sma', 'targetprice', 'targetprice2', 'targetprice3' ] print(curATR, ATRRatio, code, tradeCycle, name, curtime, curClose, cutshort, rewardratio, riskratio, rewardrisk, maprice, targetprice, targetprice2, targetprice3) s = Series([ code, name, curtime, period, curClose, maprice, curATR, ATRRatio, rewardratio, riskratio, targetprice, cutshort, ATRWZ, rewardrisk, primImpScore, tradeImpScore, valuescore, primdivscore, divscore, difdeadivscore, macdscore, totalscore, targetprice2, targetprice3 ], index=colNames) dfSelected = dfSelected.append(s, ignore_index=True) dfSelected = dfSelected[selectedcolNames] dfSelected = dfSelected.sort_values(by='time', ascending=False) dfSelected = pd.DataFrame(dfSelected.values, columns=dfSelected.columns) if (not os.path.exists(filepath)): DI.Write_DF_T0_Excel(filepath, dfSelected, 'selected') else: dftotal = DI.Get_TrdData_FromExcel(filepath, 'selected') dftotal = pd.concat([dftotal, dfSelected]) DI.Write_DF_T0_Excel(filepath, dftotal, 'selected')