def backtest_monthRP_Up(mainParament): Temp_change = 0 #換股剩餘天數 userInfo = get_user_info.data_user_info(mainParament.money_start, mainParament.date_start, mainParament.date_end) Temp_result_pick = pd.DataFrame(columns=['date', '選股數量']) while userInfo.now_day <= userInfo.end_day: #週末直接跳過 if userInfo.now_day.isoweekday() in [6, 7]: print( str(userInfo.now_day) + 'is 星期' + str(userInfo.now_day.isoweekday())) if userInfo.add_one_day() == False: #加一天 break continue #先看看台積有沒有資料,如果沒有表示這天是非週末假日跳過 if get_stock_history.get_stock_price( 2330, userInfo.now_day, get_stock_history.stock_data_kind.AdjClose) == None: print(str(userInfo.now_day) + "這天沒開市") if userInfo.add_one_day() == False: #加一天 break continue #出場訊號篩選-------------------------------------- if Temp_change <= 0 and len(userInfo.handle_stock) > 0: userInfo.sell_all_stock() #開始篩選-------------------------------------- Temp_result0 = {} Temp_result = pd.DataFrame() if Temp_change <= 0: if bool_check_monthRP_pick: #月營收升高篩選(月為單位) Temp_result0['month'] = get_stock_history.get_monthRP_up( userInfo.now_day, mainParament.smoothAVG, mainParament.upMonth) if bool_check_ROE_pick: #ROE Temp_result0['ROE'] = get_stock_history.get_ROE_range( userInfo.now_day, mainParament.ROE_start, mainParament.ROE_end) if bool_check_PBR_pick: #PBR Temp_result0['PBR'] = get_stock_history.get_PBR_range( userInfo.now_day, mainParament.PBR_start, mainParament.PBR_end) if bool_check_PER_pick: #PER Temp_result0['PER'] = get_stock_history.get_PER_range( userInfo.now_day, mainParament.PER_start, mainParament.PER_end) Temp_result = tools.MixDataFrames(Temp_result0) #入場訊號篩選-------------------------------------- if Temp_change <= 0 and len(userInfo.handle_stock) <= 0 and len( Temp_result) > mainParament.Pick_amount: Temp_buy0 = {'result': Temp_result} if bool_check_price_pick: Temp_buy0['price'] = get_stock_history.get_price_range( userInfo.now_day, mainParament.price_high, mainParament.price_low, Temp_result) Temp_buy0['price'] = Temp_buy0['price'].sort_values( by='price', ascending=False) if bool_check_volume_pick: Temp_buy0['volume'] = get_stock_history.get_AVG_value( userInfo.now_day, mainParament.volumeAVG, mainParament.volumeDays, Temp_result) Temp_buy0['volume'] = Temp_buy0['volume'].sort_values( by='volume', ascending=False) Temp_buy = tools.MixDataFrames(Temp_buy0) #if Temp_buy0['price'].empty == False: # Temp_buy = Temp_buy.sort_values(by='price', ascending=False) if Temp_buy0['volume'].empty == False: Temp_buy = Temp_buy.sort_values(by='volume', ascending=False) userInfo.buy_all_stock(Temp_buy) #更新資訊-------------------------------------- if Temp_change <= 0 and len(userInfo.handle_stock) > 0: Temp_change = mainParament.change_days userInfo.Record_userInfo() userInfo.Recod_tradeInfo() Temp_result_pick = Temp_result_pick.append( { 'date': userInfo.now_day, '選股數量': len(Temp_result) }, ignore_index=True) #加一天---------------------------- if userInfo.add_one_day() == False: break else: Temp_change = Temp_change - 1 #最後總結算---------------------------- Temp_result_pick.set_index('date', inplace=True) Temp_alldata = tools.MixDataFrames( { 'draw': userInfo.Temp_result_draw, 'pick': Temp_result_pick }, 'date') draw_figur.draw_backtest(userInfo.Temp_result_draw) Temp_alldata = tools.MixDataFrames( { 'all': Temp_alldata, 'userinfo': userInfo.Temp_result_All }, 'date') userInfo.Temp_result_draw.to_csv('backtestdata.csv') userInfo.Temp_trade_info.to_csv('backtesttrade.csv') Temp_alldata.to_csv('backtestAll.csv')
def backtest_PEG_pick_Fast(mainParament): userInfo = get_user_info.data_user_info(mainParament.money_start,mainParament.date_start,mainParament.date_end) buy_month = mainParament.date_start Temp_result_pick = pd.DataFrame(columns=['date','選股數量']) All_data = get_stock_history.get_stock_history(mainParament.buy_number,mainParament.date_start) add_one_day = userInfo.add_one_day Record_userInfo = userInfo.Record_userInfo Recod_tradeInfo = userInfo.Recod_tradeInfo sell_stock = userInfo.sell_stock buy_all_stock = userInfo.buy_all_stock for index,row in All_data.iterrows(): has_trade = False while(userInfo.now_day != index): #加一天---------------------------- if add_one_day() == False: break #出場訊號篩選----------------------------------- if len(userInfo.handle_stock) > 0: Temp_data = userInfo.handle_stock for key,value in list(Temp_data.items()): if get_stock_history.get_stock_price(key,userInfo.now_day,get_stock_history.stock_data_kind.AdjClose) < get_stock_history.get_stock_MA(key,userInfo.now_day,20): sell_stock(key,value.amount) has_trade = True #開始篩選-------------------------------------- #入場訊號篩選-------------------------------------- Temp_buy = pd.DataFrame() if userInfo.now_day.day >= mainParament.buy_day: if userInfo.now_day.month != buy_month.month or userInfo.now_day.year != buy_month.year: Temp_result0 = {} if bool_check_monthRP_pick: Temp_result0['month'] = get_stock_history.get_monthRP_up(userInfo.now_day,mainParament.smoothAVG,mainParament.upMonth) Temp_result0['PEG'] = get_stock_history.get_PEG_range(userInfo.now_day,0.66,1) Temp_result0['result'] = MixDataFrames(Temp_result0) Temp_buy = Temp_result0['result'] if Temp_buy.empty == False: Temp_buy = Temp_buy.sort_values(by='PEG') Temp_buy = Temp_buy.head(10) buy_all_stock(Temp_buy) has_trade = True buy_month = userInfo.now_day #更新資訊-------------------------------------- if len(userInfo.handle_stock) > 0 or has_trade: Record_userInfo() Recod_tradeInfo() Temp_result_pick = Temp_result_pick.append({'date':userInfo.now_day, '選股數量':len(Temp_buy)},ignore_index = True) #加一天---------------------------- if userInfo.add_one_day() == False: break else: continue #最後總結算---------------------------- Temp_result_pick.set_index('date',inplace=True) Temp_alldata = MixDataFrames({'draw':userInfo.Temp_result_draw,'pick':Temp_result_pick},'date') Temp_alldata = MixDataFrames({'all':Temp_alldata,'userinfo':userInfo.Temp_result_All},'date') userInfo.Temp_result_draw.to_csv('backtestdata.csv') userInfo.Temp_trade_info.to_csv('backtesttrade.csv') Temp_alldata.to_csv('backtestAll.csv') return userInfo.Temp_result_draw
def backtest_monthRP_Up_Fast(mainParament): Temp_change = 0#換股剩餘天數 userInfo = get_user_info.data_user_info(mainParament.money_start,mainParament.date_start,mainParament.date_end) Temp_result_pick = pd.DataFrame(columns=['date','選股數量']) All_data = get_stock_history.get_stock_history(mainParament.buy_number,mainParament.date_start) add_one_day = userInfo.add_one_day sell_all_stock = userInfo.sell_all_stock buy_all_stock = userInfo.buy_all_stock for index,row in All_data.iterrows(): has_trade = False while(userInfo.now_day != index): #加一天---------------------------- if add_one_day() == False: break #出場訊號篩選-------------------------------------- if Temp_change <= 0 and len(userInfo.handle_stock) > 0: sell_all_stock() has_trade = True #開始篩選-------------------------------------- Temp_result0 = {} Temp_result = pd.DataFrame() if Temp_change <= 0: if bool_check_monthRP_pick:#月營收升高篩選(月為單位) Temp_result0['month'] = get_stock_history.get_monthRP_up(userInfo.now_day,mainParament.smoothAVG,mainParament.upMonth) if bool_check_ROE_pick:#ROE Temp_result0['ROE'] = get_stock_history.get_ROE_range(userInfo.now_day,mainParament.ROE_start,mainParament.ROE_end) if bool_check_PBR_pick:#PBR Temp_result0['PBR'] = get_stock_history.get_PBR_range(userInfo.now_day,mainParament.PBR_start,mainParament.PBR_end) if bool_check_PER_pick:#PER Temp_result0['PER'] = get_stock_history.get_PER_range(userInfo.now_day,mainParament.PER_start,mainParament.PER_end) Temp_result = tools.MixDataFrames(Temp_result0) #入場訊號篩選-------------------------------------- if Temp_change <= 0 and len(userInfo.handle_stock) <= 0 and len(Temp_result) > mainParament.Pick_amount: Temp_buy0 = {'result':Temp_result} if bool_check_price_pick: Temp_buy0['price'] = get_stock_history.get_price_range(userInfo.now_day,mainParament.price_high,mainParament.price_low,Temp_result) Temp_buy0['price'] = Temp_buy0['price'].sort_values(by='price', ascending=False) if bool_check_volume_pick: Temp_buy0['volume'] = get_stock_history.get_AVG_value(userInfo.now_day,mainParament.volumeAVG,mainParament.volumeDays,Temp_result) Temp_buy0['volume'] = Temp_buy0['volume'].sort_values(by='volume', ascending=False) Temp_buy = tools.MixDataFrames(Temp_buy0) if Temp_buy0.__contains__('price') and Temp_buy0['price'].empty == False: Temp_buy = Temp_buy.sort_values(by='price', ascending=False) if Temp_buy0.__contains__('volume') and Temp_buy0['volume'].empty == False: Temp_buy = Temp_buy.sort_values(by='volume', ascending=False) buy_all_stock(Temp_buy) has_trade = True #更新資訊-------------------------------------- if Temp_change <= 0: Temp_change = mainParament.change_days if has_trade or len(userInfo.handle_stock) > 0: userInfo.Record_userInfo() userInfo.Recod_tradeInfo() Temp_result_pick = Temp_result_pick.append({'date':userInfo.now_day, '選股數量':len(Temp_result)},ignore_index = True) #加一天---------------------------- if add_one_day() == False: break else: Temp_change = Temp_change - 1 #最後總結算---------------------------- Temp_result_pick.set_index('date',inplace=True) Temp_alldata = tools.MixDataFrames({'draw':userInfo.Temp_result_draw,'pick':Temp_result_pick},'date') Temp_alldata = tools.MixDataFrames({'all':Temp_alldata,'userinfo':userInfo.Temp_result_All},'date') userInfo.Temp_result_draw.to_csv('backtestdata.csv') userInfo.Temp_trade_info.to_csv('backtesttrade.csv') Temp_alldata.to_csv('backtestAll.csv') return userInfo.Temp_result_draw
def button_monthRP_Up_click(): #月營收逐步升高篩選 date = tools.QtDate2DateTime(myshow.date_endDate.date()) date_5 = date + timedelta(days=-14) #加一天 if date.isoweekday() == 6: date = date + timedelta(days=-1) #加一天 elif date.isoweekday() == 7: date = date + timedelta(days=-2) #加2天 else: pass GPM = mypick.input_GPM.toPlainText() OPR = mypick.input_OPR.toPlainText() EPS = mypick.input_EPS.toPlainText() RPS = mypick.input_RPS.toPlainText() FS_data = pd.DataFrame() result_data = pd.DataFrame() BOOK_data = pd.DataFrame() PER_data = pd.DataFrame() yield_data = pd.DataFrame() FS_data = get_financial_statement(date, GPM, OPR, EPS, RPS) result_data = get_stock_history.get_monthRP_up( tools.changeDateMonth(date, 0), int(mypick.input_monthRP_smoothAVG.toPlainText()), int(mypick.input_monthRP_UpMpnth.toPlainText())) BOOK_data = get_stock_history.get_PBR_range( tools.changeDateMonth(date, 0), float(mypick.input_PBR_low.toPlainText()), float(mypick.input_PBR_high.toPlainText())) PER_data = get_stock_history.get_PER_range( tools.changeDateMonth(date, 0), float(mypick.input_PER_low.toPlainText()), float(mypick.input_PER_high.toPlainText())) ROE_data = get_stock_history.get_ROE_range( tools.changeDateMonth(date, 0), float(mypick.input_ROE_low.toPlainText()), float(mypick.input_ROE_high.toPlainText())) yield_data = get_stock_history.get_yield_range( tools.changeDateMonth(date, 0), float(mypick.input_yiled_high.toPlainText()), float(mypick.input_yiled_low.toPlainText())) pick_data = FS_data if result_data.empty == False: pick_data = pd.merge(pick_data, result_data, left_index=True, right_index=True, how='left') pick_data = pick_data.dropna(axis=0, how='any') if BOOK_data.empty == False: pick_data = pd.merge(pick_data, BOOK_data, left_index=True, right_index=True, how='left') pick_data = pick_data.dropna(axis=0, how='any') if PER_data.empty == False: pick_data = pd.merge(pick_data, PER_data, left_index=True, right_index=True, how='left') pick_data = pick_data.dropna(axis=0, how='any') if ROE_data.empty == False: pick_data = pd.merge(pick_data, ROE_data, left_index=True, right_index=True, how='left') pick_data = pick_data.dropna(axis=0, how='any') if yield_data.empty == False: pick_data = pd.merge(pick_data, yield_data, left_index=True, right_index=True, how='left') pick_data = pick_data.dropna(axis=0, how='any') price_data = get_stock_history.get_price_range( date, int(mypick.input_price_high.toPlainText()), int(mypick.input_price_low.toPlainText()), pick_data) if price_data.empty == False: pick_data = tools.MixDataFrames({ 'pick': pick_data, 'price': price_data }) pick_data = pick_data.dropna(axis=0, how='any') record_data = get_stock_history.get_RecordHigh_range( date, int(mypick.input_flash_Day.toPlainText()), int(mypick.input_record_Day.toPlainText()), pick_data) if record_data.empty == False: pick_data = tools.MixDataFrames({ 'pick': pick_data, 'recordHigh': record_data }) pick_data = pick_data.dropna(axis=0, how='any') pick_data = get_volume(int(mypick.input_volum.toPlainText()), tools.changeDateMonth(date_5, 0), pick_data) pick_data = pick_data.dropna(axis=0, how='any') print("總挑選數量:" + str(len(pick_data))) mypick.treeView_pick.setModel( creat_treeView_model(mypick.treeView_pick, pick_titalList)) #設定treeView功能 set_treeView2(mypick.treeView_pick.model(), pick_data)