Beispiel #1
0
Tele2 = ts.Telescope()
Tele2.load_dfs([r"Z:\KaiData\Theo\2019\RTY.ESTheo.mpk"])

START = datetime.datetime(2019, 4, 1, 0, 0)
END = datetime.datetime(2019, 7, 22, 23, 59)

Tele2.choose_range(START, END)

Tele2.parse_datetime()

date_list = Tele2.df['date'].unique()

trade_day_list = []
not_trade_day_list = []
for date in date_list:
    if ts.select_date_time(Tele2.df, date,
                           datetime.time(8, 30, 0))['TheoPrice'].empty:
        not_trade_day_list.append(date)
    else:
        trade_day_list.append(date)

test_day_list = trade_day_list[4:]
print(trade_day_list)

Tele2.df = Tele2.df.resample('5S', fill_method='bfill')
tm.apply_all_metrics(Tele2.df, sma_period=600)

Tele2.parse_datetime()

Tele2.clip_time(datetime.time(8, 30, 0), datetime.time(12, 0, 0))

date_list = []
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
Beispiel #3
0
        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])
            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
        
        first_move_abs_mean = np.mean(np.absolute(first_time_move_list)) 
        second_move_abs_mean = np.mean(np.absolute(second_time_move_list)) 
        
#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'])

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 = []
                    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)[METRIC][0] - ts.select_date_time(
                                first_temp_df, date, first_start)[METRIC][0]
                        second_move_theo = ts.select_date_time(
                            second_temp_df, date,
                            second_end)[METRIC][0] - ts.select_date_time(
                                second_temp_df, date, second_start)[METRIC][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
Beispiel #6
0
def dir_match_backtest(df_2019, start_date, end_date):

    import numpy as np
    import pandas as pd
    import telescope as ts
    import telescope_metrics as tm
    import datetime
    from tqdm import tqdm

    import rolling_pairwise_theo_diff_dir_match as rptddm
    import rolling_pairwise_theo_diff_sum_profit as rptdsp
    from datetime import timedelta

    #--------------Input Parameters---------------
    df_2019 = r"Z:\KaiData\Theo\2019\NQ.ESTheo.mpk"
    START = start_date
    END = end_date

    df_lst = [df_2019]

    Tele2 = ts.Telescope()
    Tele2.load_dfs(df_lst)
    Tele2.choose_range(START, END)
    Tele2.parse_datetime()
    #print(Tele2.df)
    #print(Tele2.grouped)
    #print(Tele2.num_of_groups)
    #print(Tele2.group_names)
    date_list = Tele2.df['date'].unique()

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

    trade_day_list = []
    for date in date_list:
        if ts.select_date_time(Tele2.df, date,
                               datetime.time(12, 0, 0))['TheoPrice'].empty:
            continue
        else:
            trade_day_list.append(date)

    time_length = 60
    #corr_threshold_list = [0.5,0.6,0.7,0.8,0.9]
    #up_threshold_list = [50, 75, 100]
    #down_threshold_list = [50, 75, 100]
    corr_threshold_list = [3]
    up_threshold_list = [60]
    down_threshold_list = [60]
    trading_cost = 30

    count = 0
    for corr_thre in corr_threshold_list:
        for up_thre in up_threshold_list:
            for date in tqdm(trade_day_list):
                print(date)

                down_thre = up_thre

                if date.weekday() == 4:
                    corr_start_datetime = date - timedelta(days=4)
                else:
                    corr_start_datetime = date - timedelta(days=6)

                corr_end_datetime = date - timedelta(minutes=1)

                test_start_datetime = date
                test_end_datetime = date + timedelta(days=1) - timedelta(
                    minutes=1)

                output_file_path_corr = "C:\\Users\\achen\\Desktop\\Monocular\\Automatic Backtest\\Direction Match\\NQ_" + str(
                    time_length) + "MIN_" + corr_start_datetime.strftime(
                        "%Y_%m_%d") + "_" + corr_end_datetime.strftime(
                            "%Y_%m_%d") + "_ROLL5_PAIR_DIRMATCH_auto.xlsx"
                output_file_path_backtest = "C:\\Users\\achen\Desktop\\Monocular\\Automatic Backtest\\Dirmatch PL\\NQ_" + str(
                    time_length) + "MIN_" + test_start_datetime.strftime(
                        "%Y_%m_%d") + "_ROLL5_PAIR_DIRMATCH_PROFIT_U" + str(
                            up_thre) + "_D" + str(down_thre) + "_T" + str(
                                trading_cost) + "_auto.xlsx"

                corr_df = rptddm.rolling_pairwise_theo_diff_dir_match(
                    "Z:\\KaiData\\Theo\\2019\\NQ.ESTheo.mpk",
                    corr_start_datetime,
                    corr_end_datetime,
                    datetime.time(8, 0, 0),
                    datetime.time(15, 15, 0),
                    time_frame_length=time_length,
                    rolling_step='5T',
                    output_excel_path=output_file_path_corr)
                if corr_df is None:
                    continue
                else:
                    count += 1
                pl_df = rptdsp.rolling_pairwise_theo_diff_sum_profit(
                    corr_df,
                    "Z:\\KaiData\\Theo\\2019\\NQ.ESTheo.mpk",
                    test_start_datetime,
                    test_end_datetime,
                    datetime.time(8, 0, 0),
                    datetime.time(15, 15, 0),
                    time_frame_length=time_length,
                    rolling_step='5T',
                    corr_threshold=corr_thre,
                    up_threshold=up_thre,
                    down_threshold=down_thre,
                    trade_cost=trading_cost,
                    output_excel_path=output_file_path_backtest)

                if count == 1:
                    result_df = pl_df
                else:
                    result_df = result_df.add(pl_df)