コード例 #1
0
def get_returns_series(ticker_files, current_date, n_days, rolling_lookback,
                       datetime_format):
    """
    Calculates and returns the VAE embeddings at the given date.
    """
    x_all = {}
    x_mean_all = {}
    x_std_all = {}
    filenames_all = {}
    # tickers_all = []
    lookback_start_dates = {}
    for file in ticker_files:
        df = read_csv(file, datetime_format)

        # Get the lookback period ending at the current date.
        df = df.loc[:current_date]
        df = df.iloc[-(rolling_lookback + n_days):]

        # Calculate normalised returns.
        df['returns'] = df['close'].pct_change()
        df['mean'] = df['returns'].rolling(rolling_lookback).mean()
        df['std'] = df['returns'].rolling(rolling_lookback).std()
        df['returns'] = (df['returns'] - df['mean']) / df['std']
        df = df.dropna()

        # We should be left with the number of lookback days.
        if df.shape[0] != n_days:
            continue

        x = np.array(df['returns'])
        x_mean = np.array(df['mean'])
        x_std = np.array(df['std'])

        ticker = get_ticker_from_filename(file)
        x_all[ticker] = x
        x_mean_all[ticker] = x_mean
        x_std_all[ticker] = x_std
        filenames_all[ticker] = file
        lookback_start_dates[ticker] = df.index[0]
        # x_all.append(x)
        # x_mean_all.append(x_mean)
        # x_std_all.append(x_std)
        # filenames_all.append(file)
        # # tickers_all.append(get_ticker_from_filename(file))
        # lookback_start_dates.append(df.index[0])

    return x_all, x_mean_all, x_std_all, filenames_all, lookback_start_dates
コード例 #2
0
 def data():
   return read_csv("datasets/csv/isbsg10.arff.csv")
コード例 #3
0
ファイル: albrecht.py プロジェクト: dr-bigfatnoob/effort
 def data():
     return read_csv("datasets/csv/albrecht.arff.csv")
コード例 #4
0
 def data():
     return read_csv("datasets/csv/maxwell.arff.csv")
コード例 #5
0
ファイル: walk_forward.py プロジェクト: ajmal017/trading-4
def output_spreads(pairs_vae, filenames_all, current_date, n_backtest_days,
                   returns_lookback, backtest_results, trade_results,
                   backtest_results_file, out_dir, datetime_format):
    # Plot the rolling mean of the spread, for each pair that we backtested.
    # This is for generating features and targets for a supervised "profitability" predictor.
    # This requires the traded pairs to be the same as the backtested pairs (in config file).
    for pair_ind, pair in enumerate(pairs_vae):
        ticker_1, ticker_2 = pair[0], pair[1]
        filename_1 = filenames_all[ticker_1]
        filename_2 = filenames_all[ticker_2]

        df_1 = read_csv(filename_1, datetime_format)
        df_1 = df_1.loc[:current_date]
        df_1 = df_1.iloc[-(returns_lookback + n_backtest_days):]

        df_2 = read_csv(filename_2, datetime_format)
        df_2 = df_2.loc[:current_date]
        df_2 = df_2.iloc[-(returns_lookback + n_backtest_days):]

        # The spread between the two tickers.
        df_1['spread'] = df_1['close'] / df_2['close']

        # The rolling mean and std dev. of the spread.
        df_1['spread_mean'] = df_1['spread'].rolling(20).mean()
        df_1['spread_std'] = df_1['spread'].rolling(20).std()

        # Normalise the mean and std. dev.
        df_1['spread_norm'] = (df_1['spread'] -
                               df_1['spread'].mean()) / df_1['spread'].std()
        df_1['spread_norm_mean'] = df_1['spread_norm'].rolling(20).mean()
        df_1['spread_norm_std'] = df_1['spread_norm'].rolling(20).std()
        # df_1['spread_mean_norm'] = (df_1['spread_mean'] - df_1['spread_mean'].mean()) / df_1['spread_mean'].std()
        # df_1['spread_std_norm'] = (df_1['spread_std'] - df_1['spread_std'].mean()) / df_1['spread_std'].std()

        # backtest_sharpe = backtest_sharpes[pair_ind]
        backtest_sharpe = backtest_results[(pair[0], pair[1])][2]
        backtest_profitable = 1 if backtest_sharpe > 0 else 0

        print(pair)
        trade_sharpe = trade_results[(pair[0], pair[1])][2]
        trade_profitable = 1 if trade_sharpe > 0 else 0

        fig, axes = plt.subplots(2, 1, squeeze=False)
        ax = axes[0, 0]
        ax.plot(df_1['spread'], c='blue')
        ax.plot(df_1['spread_mean'], c='orange')
        ax.plot(df_1['spread_mean'] + 2 * df_1['spread_std'], c='green')
        ax.plot(df_1['spread_mean'] - 2 * df_1['spread_std'], c='green')
        ax.grid()
        ax.set_title(f'Sharpe {backtest_sharpe}')
        ax = axes[1, 0]
        ax.plot(df_1['spread_norm'], c='blue')
        ax.plot(df_1['spread_norm_mean'], c='orange')
        ax.plot(df_1['spread_norm_mean'] + 2 * df_1['spread_norm_std'],
                c='green')
        ax.plot(df_1['spread_norm_mean'] - 2 * df_1['spread_norm_std'],
                c='green')
        ax.grid()
        plt.savefig(out_dir / f'backtest_spread_{ticker_1}-{ticker_2}.png')
        plt.close(fig)

        df_1 = df_1.dropna()
        results_str = f'{current_date}, {ticker_1}, {ticker_2}, '
        spread_norm_means = list(df_1['spread_norm_mean'])
        spread_norm_stds = list(df_1['spread_norm_std'])
        for spread_mean in spread_norm_means:
            results_str += f'{spread_mean}, '
        for spread_std in spread_norm_stds:
            results_str += f'{spread_std}, '
        results_str += f'{backtest_sharpe}, {backtest_profitable}, {trade_sharpe}, {trade_profitable}'
        backtest_results_file.write(f'{results_str}\n')
        backtest_results_file.flush()
コード例 #6
0
ファイル: finnish.py プロジェクト: dr-bigfatnoob/effort
 def data():
     return read_csv("datasets/csv/finnish.arff.csv")
コード例 #7
0
 def data():
     return read_csv("datasets/csv/china.arff.csv")
コード例 #8
0
ファイル: desharnais.py プロジェクト: dr-bigfatnoob/effort
 def data():
     return read_csv("datasets/csv/desharnais.arff.csv")
コード例 #9
0
 def data():
     return read_csv("datasets/csv/miyazaki.arff.csv")
コード例 #10
0
 def data():
     return read_csv("datasets/csv/kemerer.arff.csv")
コード例 #11
0
ファイル: kitchenhamm.py プロジェクト: dr-bigfatnoob/effort
 def data():
     return read_csv("datasets/csv/kitchenhamm.arff.csv")