コード例 #1
0
def backtest_Record_high(mainParament):
    Temp_reset = 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_reset > 0 and len(userInfo.handle_stock) == 0:
            print(
                str(userInfo.now_day) + ' is reset time:第' + str(Temp_reset) +
                '天')
            if userInfo.add_one_day() == False:  #加一天
                break
            Temp_reset = Temp_reset - 1
            continue
        #出場訊號篩選--------------------------------------
        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):
                    userInfo.sell_stock(key, value.amount)
        #開始篩選--------------------------------------
        Temp_result = pd.DataFrame()
        Temp_result0 = {}
        if Temp_reset <= 0:
            if bool_check_ROE_pick == True:
                Temp_result0['ROE'] = get_stock_history.get_ROE_range(
                    userInfo.now_day, mainParament.ROE_start,
                    mainParament.ROE_end)
            if bool_check_ROE_pick == True:
                Temp_result0[
                    'ROE_last_seson'] = get_stock_history.get_ROE_range(
                        userInfo.now_day - timedelta(weeks=4), 1, 10000)
            Temp_result = tools.MixDataFrames(Temp_result0)

        #入場訊號篩選--------------------------------------
        if Temp_reset <= 0 and len(Temp_result) > 0:
            Temp_buy0 = {'result': Temp_result}
            if bool_check_PBR_pick:
                Temp_buy0['PBR'] = get_stock_history.get_PBR_range(
                    userInfo.now_day, mainParament.PBR_start,
                    mainParament.PBR_end)
            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)
            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['point'] = pd.DataFrame(columns=['公司代號', 'point'])
            for index, row in Temp_result.iterrows():
                try:
                    temp = Temp_buy0['PBR'].at[index, 'PBR']
                except:
                    continue
                if get_stock_history.check_no_use_stock(index):
                    continue
                Temp_point = (row['ROE_x'] /
                              row['ROE_y']) / Temp_buy0['PBR'].at[index, 'PBR']
                Temp_buy0['point'] = Temp_buy0['point'].append(
                    {
                        '公司代號': index,
                        'point': Temp_point
                    }, ignore_index=True)
            Temp_buy0['point'] = Temp_buy0['point'].astype({
                '公司代號': 'int',
                'point': 'float'
            })
            Temp_buy0['point'] = Temp_buy0['point'].set_index('公司代號')
            Temp_buy = tools.MixDataFrames(Temp_buy0)

            Temp_buy1 = {'result': Temp_buy}
            Temp_buy1['high'] = get_stock_history.get_RecordHigh_range(
                userInfo.now_day, mainParament.change_days,
                mainParament.Record_high_day, Temp_buy)
            Temp_buy = tools.MixDataFrames(Temp_buy1)

            if Temp_buy.empty == False:
                Temp_buy = Temp_buy.sort_values(by='point', ascending=False)
                Temp_buy = Temp_buy.head(3)
                userInfo.buy_all_stock(Temp_buy)
        #更新資訊--------------------------------------
        if Temp_reset <= 0:
            Temp_reset = 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_reset = Temp_reset - 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')
コード例 #2
0
def backtest_PERandPBR(mainParament):
    Temp_reset = 0  #休息日剩餘天數

    Temp_result_pick = pd.DataFrame(columns=['date', '選股數量'])

    userInfo = get_user_info.data_user_info(mainParament.money_start,
                                            mainParament.date_start,
                                            mainParament.date_end)

    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_reset > 0 and len(userInfo.handle_stock) == 0:
            print(
                str(userInfo.now_day) + ' is reset time:第' + str(Temp_reset) +
                '天')
            if userInfo.add_one_day() == False:  #加一天
                break
            Temp_reset = Temp_reset - 1
            continue

        #開始篩選--------------------------------------
        Temp_result0 = {}
        Temp_result = pd.DataFrame()
        if bool_check_PER_pick:  #PER pick
            Temp_result0['PER'] = get_stock_history.get_PER_range(
                userInfo.now_day, mainParament.PER_start, mainParament.PER_end)
        if bool_check_PBR_pick:  #PBR pick
            Temp_result0['PBR'] = get_stock_history.get_PBR_range(
                userInfo.now_day, mainParament.PBR_start, mainParament.PBR_end)
        Temp_result = tools.MixDataFrames(Temp_result0)

        #出場訊號篩選--------------------------------------
        if len(Temp_result) < mainParament.Pick_amount and len(
                userInfo.handle_stock) > 0:
            userInfo.sell_all_stock()
            Temp_reset = mainParament.change_days

        #入場訊號篩選--------------------------------------
        if len(Temp_result
               ) > mainParament.Pick_amount and Temp_reset == 0 and len(
                   userInfo.handle_stock) == 0:
            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)
            Temp_reset = 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

    #最後總結算----------------------------
    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')
コード例 #3
0
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
コード例 #4
0
def backtest_PERandPBR_Fast(mainParament):
    Temp_reset = 0#休息日剩餘天數
    Temp_changeDays = 0#換股剩餘天數
    Temp_result_pick = pd.DataFrame(columns=['date','選股數量'])

    userInfo = get_user_info.data_user_info(mainParament.money_start,mainParament.date_start,mainParament.date_end)

    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_reset > 0 and len(userInfo.handle_stock) == 0:
            print(str(userInfo.now_day) + ' is reset time:第' + str(Temp_reset) + '天')
            if add_one_day() == False:#加一天
                break
            Temp_reset = Temp_reset - 1
            continue
        
        #開始篩選--------------------------------------
        Temp_result0 = {}
        Temp_result = pd.DataFrame()
        if bool_check_PER_pick:#PER pick
            Temp_result0['PER'] = get_stock_history.get_PER_range(userInfo.now_day,mainParament.PER_start,mainParament.PER_end)
        if bool_check_PBR_pick:#PBR pick    
            Temp_result0['PBR'] = get_stock_history.get_PBR_range(userInfo.now_day,mainParament.PBR_start,mainParament.PBR_end)
        Temp_result = tools.MixDataFrames(Temp_result0)

        #出場訊號篩選--------------------------------------
        if len(Temp_result) < mainParament.Pick_amount and len(userInfo.handle_stock) > 0:
            sell_all_stock()
            Temp_reset = 120
            has_trade = True
        #出場訊號篩選--------------------------------------
        if Temp_changeDays <= 0 and len(userInfo.handle_stock) > 0:
            sell_all_stock()
            has_trade = True
        
        #入場訊號篩選--------------------------------------
        if len(Temp_result) >= mainParament.Pick_amount and Temp_reset == 0 and len(userInfo.handle_stock) == 0:
            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)
            Temp_changeDays = mainParament.change_days
            has_trade = True
        #更新資訊--------------------------------------
        if len(userInfo.handle_stock) > 0 or has_trade == True:
            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_changeDays = Temp_changeDays - 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
コード例 #5
0
def backtest_Record_high_Fast(mainParament):
    Temp_reset = 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
    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_result = pd.DataFrame()
        Temp_result0 = {}
        if Temp_reset <= 0:
            if bool_check_ROE_pick == True:
                Temp_result0['ROE'] = get_stock_history.get_ROE_range(userInfo.now_day,mainParament.ROE_start,mainParament.ROE_end)
            if bool_check_ROE_pick == True:
                Temp_result0['ROE_last_seson'] = get_stock_history.get_ROE_range(userInfo.now_day - timedelta(weeks = 4),1,10000)
            if bool_check_PBR_pick:
                Temp_result0['PBR'] = get_stock_history.get_PBR_range(userInfo.now_day,mainParament.PBR_start,mainParament.PBR_end)
            Temp_result = MixDataFrames(Temp_result0)
            
        #入場訊號篩選--------------------------------------
        if Temp_reset <= 0 and len(Temp_result) > 0:
            Temp_buy0 = {'result':Temp_result}
            # if bool_check_PBR_pick:
            #     Temp_buy0['PBR'] = get_stock_history.get_PBR_range(userInfo.now_day,mainParament.PBR_start,mainParament.PBR_end,Temp_result)
            #     Temp_buy0['result'] = MixDataFrames(Temp_buy0)
            
            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['result'] = MixDataFrames(Temp_buy0)
            
            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['result'] = MixDataFrames(Temp_buy0)
            
            Temp_buy0['result']['point'] = (Temp_result['ROE'] / Temp_result['ROE_R']) / Temp_result['PBR']
            # for index,row in Temp_result.iterrows():
            #     try:
            #         temp = Temp_buy0['PBR'].at[index,'PBR']
            #     except:
            #         continue
            #     if get_stock_history.check_no_use_stock(index):
            #         continue
            #     Temp_point = (row['ROE'] / row['ROE_R']) / Temp_buy0['PBR'].at[index,'PBR']
            #     Temp_buy0['point'] = Temp_buy0['point'].append({'code':index,'point':Temp_point},ignore_index=True)
            # Temp_buy0['point'] = Temp_buy0['point'].astype({'code':'int','point':'float'})
            # Temp_buy0['point'] = Temp_buy0['point'].set_index('code')
            Temp_buy = MixDataFrames(Temp_buy0)

            Temp_buy = Temp_buy.sort_values(by='point', ascending=False)
            
            Temp_buy1 = {'result':Temp_buy}
            Temp_buy1['high'] = get_stock_history.get_RecordHigh_range(userInfo.now_day,mainParament.change_days,mainParament.Record_high_day,Temp_buy)
            Temp_buy = MixDataFrames(Temp_buy1)

            if Temp_buy.empty == False:
                Temp_buy = Temp_buy.sort_values(by='point', ascending=False)
                Temp_buy = Temp_buy.head(3)
                buy_all_stock(Temp_buy)
                has_trade = True
        #更新資訊--------------------------------------
        if Temp_reset <= 0:
            Temp_reset = mainParament.change_days
        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_result)},ignore_index = True)
        #加一天----------------------------
        if add_one_day() == False:
            break
        else:
            Temp_reset = Temp_reset - 1

    #最後總結算----------------------------
    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
コード例 #6
0
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.__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)
            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_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')
コード例 #7
0
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)