コード例 #1
0
def run_daily_best_param(Tele2, trade_day_list, criteria_df):
    base_time_start = datetime.time(8,30,0)

    base_time_end_list = [datetime.time(8,40,0)]
    
    test_length_list = [300]
    
    sma_list = [0.5]
    
    over_limit_time_list = [100]
    
    bounce_stop_list = [80]
    
    column_list = [['Date','Max Profit', 'Above Ratio', 'Below Ratio', 'Max min Spread','Average Theo', 'Std of Theo', 'Base Start', 'Base End', 'Test Start', 'Test End', 'SMA Side Ratio Threshold', 'Bounce Stop Threshold', 'Over Limit Time', 'Trading Cost']]
    for test_trade_day in trade_day_list:
        test_trade_day_list = [test_trade_day]
        max_profit = -10000
        for base_time_end in base_time_end_list:
            test_time_start = base_time_end
            for test_length in test_length_list:
                test_time_end = (ts.cal_datetime(test_time_start) + datetime.timedelta(minutes = test_length)).time()
                for sma in sma_list:
                    for bounce_stop in bounce_stop_list:
                        for over_limit_time in tqdm(over_limit_time_list):
                   
                            [daily_profit_result, trade_count, limit_stop_count, bounce_stop_count, time_stop_count, loss_record, above_ratio, below_ratio, theo_change, absolute_theo_change, max_min_spread, average, std_dev] = dbfrt.daily_best_feature_record_test(Tele2, test_trade_day_list, base_time_start, base_time_end, test_time_start, test_time_end, sma_threshold = sma, stop_threshold = bounce_stop, bounce_stop_threshold = bounce_stop, over_limit_length = over_limit_time, trade_cost = 30)
                    
                            if daily_profit_result[0] > max_profit:
                                max_profit = daily_profit_result[0]
                                single_column = [test_trade_day, daily_profit_result[0], above_ratio, below_ratio, max_min_spread, average, std_dev, base_time_start, base_time_end, test_time_start, test_time_end, sma, bounce_stop, over_limit_time, 30]         
        column_list.append(single_column)
    loss_df = pd.DataFrame(loss_record[1:], columns=loss_record[0])
    
    result_df = pd.DataFrame(column_list[1:], columns=column_list[0])
    result_df.index = result_df['Date']
    del result_df['Date']
    final_result_df = result_df.join(criteria_df, how = 'left')
    del final_result_df['date']
    del final_result_df['time']
コード例 #2
0
def run_bounce_match_strategy(Tele2, trade_day_list):
    base_time_start = datetime.time(8,30,0)
    
    base_time_end_list = [datetime.time(8,40,0)]
    
    test_length_list = [300,310,320,330,340,350,360,370,380,390]
    
    sma_list = [0.5]
    
    over_limit_time_list = [100]
    
    bounce_stop_list = [80]
    
    theo_diff_list = [50]
    
    column_list = [['Profit', 'Trade Number', ' Time Stop Count', 'Limite Stop Count', 'Bounce Stop Count', 'Base Start', 'Base End', 'Test Start', 'Test End', 'SMA Side Ratio Threshold', 'Bounce Stop Threshold', 'Over Limit Time', 'Theo Diff Threshold', 'Trading Cost']]
    for base_time_end in base_time_end_list:
        test_time_start = base_time_end
        for test_length in test_length_list:
            test_time_end = (ts.cal_datetime(test_time_start) + datetime.timedelta(minutes = test_length)).time()
            for sma in sma_list:
                for bounce_stop in bounce_stop_list:
                    for theo_diff in theo_diff_list:
                        for over_limit_time in tqdm(over_limit_time_list):
               
                            [daily_profit_result, trade_count, limit_stop_count, bounce_stop_count, time_stop_count, daily_record, bounce_stop_threshold] = bms.bounce_match_strategy(Tele2, trade_day_list, base_time_start, base_time_end, test_time_start, test_time_end, sma_threshold = sma, stop_threshold = bounce_stop, bounce_stop_threshold = bounce_stop, over_limit_length = over_limit_time, theo_diff = theo_diff, trade_cost = 30)
                
                            total_profit = np.sum(daily_profit_result[~np.isnan(daily_profit_result)])
                    
                            single_column = [total_profit, trade_count, time_stop_count, limit_stop_count, bounce_stop_count, base_time_start, base_time_end, test_time_start, test_time_end, sma, bounce_stop, over_limit_time, theo_diff, 30]
                            column_list.append(single_column)
    #
    daily_df = pd.DataFrame(daily_record[1:], columns=daily_record[0])
    result_df = pd.DataFrame(column_list[1:], columns=column_list[0])
    
    
#    result_df.to_excel("C:\\Users\\achen\\Desktop\\Monocular\\Automatic Backtest\\RTY Morning Trend PL\\RTY_20190101_20190725_BOUNCE_MATCH_VARIOUS_PARAM_NO_FILTER.xlsx")
    return daily_df, result_df
コード例 #3
0
bounce_stop_list = [80]

theo_diff_list = [50]

loss_stop_list = [500]

column_list = [[
    'Profit', 'Trade Number', ' Time Stop Count', 'Limite Stop Count',
    'Bounce Stop Count', 'Base Start', 'Base End', 'Test Start', 'Test End',
    'SMA Side Ratio Threshold', 'Bounce Stop Threshold', 'Over Limit Time',
    'Theo Diff Threshold', 'Loss Stop', 'Trading Cost'
]]
for base_time_end in base_time_end_list:
    test_time_start = base_time_end
    for test_length in test_length_list:
        test_time_end = (ts.cal_datetime(test_time_start) +
                         datetime.timedelta(minutes=test_length)).time()
        for sma in sma_list:
            for bounce_stop in bounce_stop_list:
                for theo_diff in theo_diff_list:
                    for over_limit_time in tqdm(over_limit_time_list):
                        for loss_stop in tqdm(loss_stop_list):

                            [
                                daily_profit_result, trade_count,
                                limit_stop_count, bounce_stop_count,
                                time_stop_count, daily_record,
                                bounce_stop_threshold
                            ] = bms.bounce_match_strategy(
                                Tele2,
                                trade_day_list,
コード例 #4
0
def rolling_backtest(Tele2, end_datetime, trade_day_list):
    import telescope as ts
    #    import telescope_metrics as tm
    import strategy_backtest2_loss as sbtest2l

    #dmb.dir_match_backtest(r"Z:\KaiData\Theo\2019\RTY.ESTheo.mpk", datetime.datetime(2019, 7, 16, 0, 0), datetime.datetime(2019, 7, 17, 23, 59))
    START = end_datetime - datetime.timedelta(days=6)
    END = end_datetime

    print(START)
    print(END)

    #    Tele2 = ts.Telescope()
    #    Tele2.load_dfs([r"Z:\KaiData\Theo\2019\RTY.ESTheo.mpk"])
    #    Tele2.df = Tele2.df.resample('5S', fill_method = 'bfill')
    #    tm.apply_all_metrics(Tele2.df, sma_period = 600)
    #    Tele2.clip_time(datetime.time(8,30,0), datetime.time(12,0,0))

    chosen_Tele = ts.Telescope(df=Tele2.choose_date(START, END))

    #    date_list = chosen_Tele.df['date'].unique()
    #
    #    trade_day_list = []
    #    not_trade_day_list = []
    #    for date in date_list:
    #        if ts.select_date_time(chosen_Tele.df, date, datetime.time(8,30,0))['TheoPrice'].empty:
    #            not_trade_day_list.append(date)
    #        else:
    #            trade_day_list.append(date)
    test_day = trade_day_list[-1]
    trade_day_list = trade_day_list[:-1]
    #
    #    Tele2.df = Tele2.df.resample('5S', fill_method = 'bfill')
    #
    #    Tele2.parse_datetime()

    #    tm.apply_all_metrics(Tele2.df, sma_period = 600)
    #
    #    Tele2.clip_time(datetime.time(8,30,0), datetime.time(12,0,0))

    base_time_start = datetime.time(8, 30, 0)

    base_time_end_list = [
        datetime.time(8, 31, 0),
        datetime.time(8, 33, 0),
        datetime.time(8, 35, 0),
        datetime.time(8, 40, 0),
        datetime.time(8, 45, 0)
    ]

    test_length_list = [360]

    sma_list = [0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]

    stop_list = [0]

    bounce_stop_list = [80, 120, 160, 200, 240, 280, 320]

    column_list = [[
        'Profit', 'Trade Number', ' Time Stop Count', 'Limite Stop Count',
        'Bounce Stop Count', 'Base Start', 'Base End', 'Test Start',
        'Test End', 'SMA Side Ratio Threshold', 'Stop Threshold',
        'Bounce Stop Threshold', 'Trading Cost'
    ]]
    for base_time_end in base_time_end_list:
        test_time_start = base_time_end
        for test_length in test_length_list:
            test_time_end = (ts.cal_datetime(test_time_start) +
                             datetime.timedelta(minutes=test_length)).time()
            for sma in sma_list:
                for stop in stop_list:
                    for bounce_stop in bounce_stop_list:
                        absolute_stop = bounce_stop - stop
                        [
                            daily_profit_result, trade_count, limit_stop_count,
                            bounce_stop_count, time_stop_count, loss_record
                        ] = sbtest2l.strategy_backtest2_loss(
                            chosen_Tele,
                            trade_day_list,
                            base_time_start,
                            base_time_end,
                            test_time_start,
                            test_time_end,
                            sma_threshold=sma,
                            stop_threshold=absolute_stop,
                            bounce_stop_threshold=bounce_stop,
                            trade_cost=30)

                        total_profit = np.sum(
                            daily_profit_result[~np.isnan(daily_profit_result)]
                        )

                        single_column = [
                            total_profit, trade_count, time_stop_count,
                            limit_stop_count, bounce_stop_count,
                            base_time_start, base_time_end, test_time_start,
                            test_time_end, sma, stop, bounce_stop, 30
                        ]
                        column_list.append(single_column)

    result_df = pd.DataFrame(column_list[1:], columns=column_list[0])

    parameter_series = result_df.loc[result_df['Profit'].idxmax(), :]
    print(result_df['Profit'].max())

    base_time_start = parameter_series['Base Start']
    base_time_end = parameter_series['Base End']
    test_time_start = parameter_series['Test Start']
    test_time_end = parameter_series['Test End']
    sma = parameter_series['SMA Side Ratio Threshold']
    stop = parameter_series['Stop Threshold']
    bounce_stop = parameter_series['Bounce Stop Threshold']
    trading_cost = parameter_series['Trading Cost']

    [
        test_profit_result, trade_count, limit_stop_count, bounce_stop_count,
        time_stop_count, loss_record
    ] = sbtest2l.strategy_backtest2_loss(chosen_Tele, [test_day],
                                         base_time_start,
                                         base_time_end,
                                         test_time_start,
                                         test_time_end,
                                         sma_threshold=sma,
                                         stop_threshold=absolute_stop,
                                         bounce_stop_threshold=bounce_stop,
                                         trade_cost=trading_cost)
    return base_time_start, base_time_end, sma, bounce_stop, result_df[
        'Profit'].max(), test_profit_result[0]
コード例 #5
0
base_time_end_list = [datetime.time(8,40,0)]

test_length_list = [100]

sma_list = [0.7]

stop_list = [0]

bounce_stop_list = [120]

column_list = [['Profit', 'Trade Number', ' Time Stop Count', 'Limite Stop Count', 'Bounce Stop Count', 'Base Start', 'Base End', 'Test Start', 'Test End', 'SMA Side Ratio Threshold', 'Stop Threshold', 'Bounce Stop Threshold','Trading Cost']]
for base_time_end in base_time_end_list:
    test_time_start = base_time_end
    for test_length in test_length_list:
        test_time_end = (ts.cal_datetime(test_time_start) + datetime.timedelta(minutes = test_length)).time()
        for sma in sma_list:
            for stop in stop_list:
                for bounce_stop in tqdm(bounce_stop_list):
                    absolute_stop = bounce_stop - stop 
                    [daily_profit_result, trade_count, limit_stop_count, bounce_stop_count, time_stop_count, loss_record] = sbtest2l.strategy_backtest2_loss(Tele2, trade_day_list, base_time_start, base_time_end, test_time_start, test_time_end, sma_threshold = sma, stop_threshold = absolute_stop, bounce_stop_threshold = bounce_stop, trade_cost = 30)
                
                
                    total_profit = np.sum(daily_profit_result[~np.isnan(daily_profit_result)])
                
                    single_column = [total_profit, trade_count, time_stop_count, limit_stop_count, bounce_stop_count, base_time_start, base_time_end, test_time_start, test_time_end, sma, stop, bounce_stop, 30]
                    column_list.append(single_column)

loss_df = pd.DataFrame(loss_record[1:], columns=loss_record[0])
result_df = pd.DataFrame(column_list[1:], columns=column_list[0])
#result_df.to_excel("C:\\Users\\achen\\Desktop\\Monocular\\Automatic Backtest\\RTY Morning Trend PL\\RTY_20190401_20190717_PARAMETER_TEST3_PL.xlsx")
def rolling_pairwise_theo_diff_sum_profit(corr_df, data_mpk_file_path, start_date, end_date, start_time = datetime.time(8, 0, 0), end_time = datetime.time(15, 15, 0), time_frame_length = 5, rolling_step = '1T', corr_threshold = 3, up_threshold = 100, down_threshold = 100, trade_cost = 30, output_excel_path= r"C:\Users\achen\Desktop\Monocular\rolling_pairwise_theo_diff_corr.xlsx"):

    import numpy as np
    import pandas as pd
    import telescope as ts
    import telescope_metrics as tm
    import datetime
    from scipy.stats.stats import pearsonr  
    from tqdm import tqdm
    
    #--------------Input Parameters---------------
    CORR_DF = corr_df
    df_2019 = data_mpk_file_path
    START = start_date
    END = end_date
    STIME = start_time
    ETIME = end_time
    TIME_FRAME_LENGTH = time_frame_length
    ROLLING_STEP = rolling_step #(has to be a factor of TIME_FRAME_LENGTH)
    UP_THRESHOLD = up_threshold
    DOWN_THRESHOLD = down_threshold
    TRADE_COST = trade_cost
    CORR_THRESHOLD = corr_threshold
    #---------------------------------------------
    
    #--------------Input Parameters---------------
    #df_2019 = r"Z:\KaiData\Theo\2019\YM.ESTheo.mpk"
    #START = datetime.datetime(2019, 7, 8, 0, 0)
    #END = datetime.datetime(2019, 7, 12, 23, 59)
    #STIME = datetime.time(8, 0, 0)
    #ETIME = datetime.time(15, 15, 0)
    #TIME_FRAME_LENGTH = 5
    #ROLLING_STEP = '1T' #(has to be a factor of TIME_FRAME_LENGTH)
    #---------------------------------------------
    
    
    
    df_lst = [df_2019]
    
    Tele2 = ts.Telescope()
    Tele2.load_dfs(df_lst)
    Tele2.choose_range(START, END)
    Tele2.resample(ROLLING_STEP)
    
    tm.apply_all_metrics(Tele2.df, sma_period = 10)
    
    Tele2.parse_datetime()
    
    Tele2.specific_timeframe(STIME, ETIME)
    
    #print(Tele2.df)
    #print(Tele2.grouped)
    #print(Tele2.num_of_groups)
    #print(Tele2.group_names)
    date_list = Tele2.df['date'].unique()
    time_list = Tele2.df['time'].unique()
    
    ignore_col_num = int(TIME_FRAME_LENGTH/int(ROLLING_STEP[0])+1)
#    change_direction_match_ratio_list = []
#    correlatation_list = []
    print(time_list[:-ignore_col_num])
    #print(Tele2.df['TheoPrice'])
    column_list = [time_list[:-ignore_col_num]]
    for first_time in tqdm(time_list[:-ignore_col_num]):
        if first_time < (ts.cal_datetime(ETIME) - datetime.timedelta(minutes = TIME_FRAME_LENGTH)).time():
            first_start = first_time;
            first_end = (ts.cal_datetime(first_time) + datetime.timedelta(minutes = TIME_FRAME_LENGTH)).time();
            print(first_start)
            
            first_temp_df = ts.select_timeframe(Tele2.df, first_start, first_end)
            
            single_column = []
            
            for second_time in time_list[:-ignore_col_num]:
                if (second_time >= (ts.cal_datetime(first_time)  + datetime.timedelta(minutes = TIME_FRAME_LENGTH)).time()) & (second_time < (ts.cal_datetime(ETIME) - datetime.timedelta(minutes = TIME_FRAME_LENGTH)).time()):
                    second_start = second_time
                    second_end = (ts.cal_datetime(second_start) + datetime.timedelta(minutes = TIME_FRAME_LENGTH)).time()
            
                    second_temp_df = ts.select_timeframe(Tele2.df, second_start, second_end)
                
                    first_time_move_list = []
                    second_time_move_list = []
                    
                    for date in date_list:
    #            print(first_temp_df[(first_temp_df['date'] == date) & (first_temp_df['time'] == first_end)]['TheoPrice'])
    #            print(ts.select_date_time(first_temp_df, date, first_end)['TheoPrice'][0])
                        first_move_theo = ts.select_date_time(first_temp_df, date, first_end)['TheoPrice'][0] - ts.select_date_time(first_temp_df, date, first_start)['TheoPrice'][0]
                        second_move_theo = ts.select_date_time(second_temp_df, date, second_end)['TheoPrice'][0] - ts.select_date_time(second_temp_df, date, second_start)['TheoPrice'][0]
                
                        if ((not np.isnan(first_move_theo)) & (not np.isnan(second_move_theo))):
                            first_time_move_list.append(first_move_theo)
                            second_time_move_list.append(second_move_theo)
            
    #        print(first_time_move_list)
    #        print(second_time_move_list)
    #            count = 0
    #        for i in range(len(first_time_move_list)):
    #            if ((first_time_move_list[i] > 0) & (second_time_move_list[i] > 0)) or ((first_time_move_list[i] < 0) & (second_time_move_list[i] < 0)):
    #                count += 1
    #        match_ratio = count/len(first_time_move_list)
    #        change_direction_match_ratio_list.append(match_ratio)
                    score = 0
                    correlation = corr_df.loc[first_start,second_start]
                    for i in range(len(first_time_move_list)):
                        if first_time_move_list[i] >= UP_THRESHOLD:
                            if correlation >= CORR_THRESHOLD:
                                print(correlation)
                                score += second_time_move_list[i]
                                score = score - TRADE_COST
                            if correlation <= -CORR_THRESHOLD:
                                score += -second_time_move_list[i]
                                score = score - TRADE_COST
                        if first_time_move_list[i] <= -DOWN_THRESHOLD:
                            if correlation >= CORR_THRESHOLD:
                                score += -second_time_move_list[i]
                                score = score - TRADE_COST
                            if correlation <= -CORR_THRESHOLD:
                                score += second_time_move_list[i]
                                score = score - TRADE_COST
#                    corr = pearsonr(first_time_move_list,second_time_move_list)[0]
                    single_column.append(score)
            
                else:
                    single_column.append(np.nan) 
            print(single_column)
            column_list.append(single_column)
    
    result = pd.DataFrame(column_list[1:], columns=column_list[0])
    result['time'] = time_list[:-ignore_col_num]
    result.set_index('time', inplace=True)
    #output_df = pd.DataFrame(list(zip(time_list, correlatation_list, change_direction_match_ratio_list)), columns =['start time', 'correlation with next 5 min', 'change direction match ratio']) 
    #output_df.to_csv(r"C:\Users\achen\Desktop\Monocular\5_min_rolling_correlation_direction_match.csv")
    result.to_excel(output_excel_path)
    return result
コード例 #7
0
#print(Tele2.num_of_groups)
#print(Tele2.group_names)
date_list = Tele2.df['date'].unique()
time_list = Tele2.df['time'].unique()

change_direction_match_ratio_list = []
correlatation_list = []

first_move_abs_sum = []
second_move_abs_sum = []

#print(Tele2.df['TheoPrice'])

for time in time_list:

    if time < (ts.cal_datetime(datetime.time(15, 15, 0)) - datetime.timedelta(minutes = 10)).time():
        print(time)
        first_start = time;
        first_end = (ts.cal_datetime(time) + datetime.timedelta(minutes = 5)).time();
        second_start = (ts.cal_datetime(time) + datetime.timedelta(minutes = 5)).time()
        second_end = (ts.cal_datetime(time) + datetime.timedelta(minutes = 10)).time()
        
        first_temp_df = ts.select_timeframe(Tele2.df, first_start, first_end)
        second_temp_df = ts.select_timeframe(Tele2.df, second_start, second_end)
        
        first_time_move_list = []
        second_time_move_list = []
        
        for date in date_list:
#            print(first_temp_df[(first_temp_df['date'] == date) & (first_temp_df['time'] == first_end)]['TheoPrice'])
#            print(ts.select_date_time(first_temp_df, date, first_end)['TheoPrice'][0])
correlatation_list = []

time_index_list = []

#print(Tele2.df['TheoPrice'])

trade_day_list = []
for date in date_list:
    noon_theo_price = ts.select_date_time(Tele2.df, date, datetime.time(12,0,0))['TheoPrice'][0]
    if (not np.isnan(noon_theo_price)):
        trade_day_list.append(date)

column_list = [trade_day_list]
    
for first_time in tqdm(time_list):
    if first_time < (ts.cal_datetime(ETIME) - datetime.timedelta(minutes = TIME_FRAME_LENGTH)).time():
        time_index_list.append(first_time)
        
        first_start = first_time;
        first_end = (ts.cal_datetime(first_time) + datetime.timedelta(minutes = TIME_FRAME_LENGTH)).time();
        
        first_temp_df = ts.select_timeframe(Tele2.df, first_start, first_end)
#        
        single_column = []
        
        first_move_theo = ts.select_date_time(first_temp_df, date, first_end)[METRIC][0] - ts.select_date_time(first_temp_df, date, first_start)[METRIC][0]
        
        first_time_move_list = []
        
        for date in trade_day_list:
            first_move_theo = ts.select_date_time(first_temp_df, date, first_end)[METRIC][0] - ts.select_date_time(first_temp_df, date, first_start)[METRIC][0]
コード例 #9
0
#print(Tele2.df)
#print(Tele2.grouped)
#print(Tele2.num_of_groups)
#print(Tele2.group_names)
date_list = Tele2.df['date'].unique()
time_list = Tele2.df['time'].unique()

change_direction_match_ratio_list = []
correlatation_list = []

time_index_list = []

#print(Tele2.df['TheoPrice'])
column_list = [time_list[:-(TIME_FRAME_LENGTH + 1)]]
for first_time in tqdm(time_list):
    if first_time < (ts.cal_datetime(ETIME) -
                     datetime.timedelta(minutes=TIME_FRAME_LENGTH)).time():
        time_index_list.append(first_time)

        first_start = first_time
        first_end = (ts.cal_datetime(first_time) +
                     datetime.timedelta(minutes=TIME_FRAME_LENGTH)).time()
        print(first_start)
        print(first_end)

        first_temp_df = ts.select_timeframe(Tele2.df, first_start, first_end)

        single_column = []

        for second_time in time_list[:-(TIME_FRAME_LENGTH + 1)]:
            if (second_time >= first_time):