コード例 #1
0
ファイル: portfolio_optimization.py プロジェクト: zoding/ML4T
def main():
    symbols = ["AAPL", "FB", "GOOG", "SPY"]
    start_date = "01/01/2017"
    end_date = "31/12/2017"

    #Portfolio and SPY Dataframes
    df_portfolio = get_data(symbols, start_date, end_date)
    df_SPY = df_portfolio.ix[:, "SPY"]
    df_SPY = df_SPY / df_SPY.ix[0]

    #Optimized Allocations
    optimized_allocations = compute_optimal_allocations(df_portfolio)
    optimized_portfolio = compute_daily_portfolio_value(
        df_portfolio, 100000, optimized_allocations)
    optimized_portfolio = optimized_portfolio / optimized_portfolio.ix[0]

    #Default Allocations
    default_allocations = [0.25, 0.25, 0.25, 0.25]
    default_portfolio = compute_daily_portfolio_value(df_portfolio, 100000,
                                                      default_allocations)
    default_portfolio = default_portfolio / default_portfolio.ix[0]

    df_comparsion = pd.concat(
        [optimized_portfolio, default_portfolio, df_SPY],
        keys=["Optimized Portfolio", "Default Portfolio", "S&P500"],
        axis=1)

    plot_data(df_comparsion, "Portfolio Optimization", "Date", "Price")
コード例 #2
0
def optimize_portfolio(start_date, end_date, symbols):
    dates = pd.date_range(start_date, end_date)
    prices_all = get_data(symbols, dates)

    prices = prices_all[symbols]  # only portfolio symbols
    prices_SPY = prices_all['SPY']  # only SPY, for comparison later

    # Get optimal allocations
    allocs = find_optimal_allocations(prices)
    allocs = allocs / np.sum(allocs)  # normalize allocations, if they don't sum to 1.0

    # Get daily portfolio value (already normalized since we use default start_val=1.0)
    port_val = get_portfolio_value(prices, allocs)

    # Get portfolio statistics (note: std_daily_ret = volatility)
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = get_portfolio_stats(port_val)

    # Print statistics
    print "Start Date:", start_date
    print "End Date:", end_date
    print "Symbols:", symbols
    print "Optimal allocations:", allocs
    print "Sharpe Ratio:", sharpe_ratio
    print "Volatility (stdev of daily returns):", std_daily_ret
    print "Average Daily Return:", avg_daily_ret
    print "Cumulative Return:", cum_ret

    # Compare daily portfolio value with normalized SPY
    normed_SPY = prices_SPY / prices_SPY.ix[0, :]
    df_temp = pd.concat([port_val, normed_SPY], keys=['Portfolio', 'SPY'], axis=1)
    plot_data(df_temp, title="Daily Portfolio Value and SPY")
コード例 #3
0
ファイル: bollinger_bands.py プロジェクト: zoding/ML4T
def main():
    start_date = "01/01/2017"
    end_date = "31/12/2017"
    symbols = ["FB"]
    stock_symbol = "FB"
    df = get_data(symbols, start_date, end_date, include_SPY=False)
    print(df.head())
    print(df.tail())

    window = 20
    rolling_mean = df[stock_symbol].rolling(window=window).mean()
    rolling_std = df[stock_symbol].rolling(window=window).std()
    df["Rolling Mean"] = rolling_mean
    df["Upper Bollinger Band"], df[
        "Lower Bollinger Band"] = get_bollinger_bands(rolling_mean,
                                                      rolling_std)
    plot_data(df, stock_symbol + " Bollinger Bands", "Date", "Price")
コード例 #4
0
ファイル: portfolio_statistics.py プロジェクト: zoding/ML4T
def main():
    capital = 100000
    symbols = ["AAPL", "FB", "GOOG", "SPY"]
    allocations = [0.25, 0.25, 0.25, 0.25]
    start_date = "01/01/2017"
    end_date = "31/12/2017"

    #Portfolio Dataframe
    df_portfolio = get_data(symbols, start_date, end_date)
    df_SPY = df_portfolio.ix[:, "SPY"]

    #Daily Portfolio Value
    daily_portfolio_value = compute_daily_portfolio_value(df_portfolio, capital, allocations)
    print(daily_portfolio_value.head())

    #Daily Portfolio Return
    daily_portfolio_return = compute_daily_portfolio_return(daily_portfolio_value)

    #Cummulative Portfolio Return
    cummulative_portfolio_return = compute_cummulative_portfolio_return(daily_portfolio_value)
    print("Cummulative Portfolio Return:", cummulative_portfolio_return)

    #Daily Portfolio Return Mean
    mean_daily_portfolio_return = compute_mean_daily_portfolio_return(daily_portfolio_return)
    print("Daily Portfolio Return Mean:", mean_daily_portfolio_return)

    #Daily Portfolio Return Standard Deviation
    std_daily_portfolio_return = compute_std_daily_portfolio_return(daily_portfolio_return)
    print("Daily Portfolio Return Standard Deviation:", std_daily_portfolio_return)

    #Daily Sampled Sharpe Ratio
    daily_sampled_sharpe_ratio = compute_daily_sampled_sharpe_ratio(mean_daily_portfolio_return, std_daily_portfolio_return)
    print("Daily Sampled Sharpe Ratio:", daily_sampled_sharpe_ratio)

    #Comparing between the portfolio and S&P500
    daily_portfolio_value_normalized = daily_portfolio_value/daily_portfolio_value.ix[0]
    df_SPY_normalized = df_SPY/df_SPY.ix[0]
    df_comparsion = pd.concat([daily_portfolio_value_normalized, df_SPY_normalized], keys=["Portfolio", "SPY"], axis=1)
    plot_data(df_comparsion, "Portfolio 2017 Normalized Price", "Date", "Price")
コード例 #5
0
ファイル: exp1.py プロジェクト: zoding/ML4T
                                        min_delta=0,
                                        patience=50,
                                        verbose=verbose,
                                        mode='auto')
callbacks = [early_stopping_callback]
start_date = dates_dic[stock][0]
end_date = dates_dic[stock][1]

#LSTM
normalized_metrics, inv_normalized_metrics, df = lstm.final_test_lstm(
    stock, start_date, end_date, window, future_gap, time_steps, neurons,
    drop_out, batch_size, epochs, validation_split, verbose, callbacks)
metrics_dic['LSTM'] = normalized_metrics
plot_data(df,
          stock + " 2017 Price Forecast (LSTM)",
          "Date",
          "Price",
          show_plot=False)

#FFNN
neurons = [256, 256, 64, 1]
batch_size = 128
epochs = 200

normalized_metrics, inv_normalized_metrics, df = ffnn.final_test_ffnn(
    stock, start_date, end_date, window, future_gap, neurons, drop_out,
    batch_size, epochs, validation_split, verbose, callbacks)
metrics_dic['FFNN'] = normalized_metrics
plot_data(df,
          stock + " 2017 Price Forecast (FFNN)",
          "Date",
コード例 #6
0
ファイル: exp4.py プロジェクト: zoding/ML4T
                                        verbose=verbose,
                                        mode='auto')
callbacks = [early_stopping_callback]

lstm_future_gap_metrics = {1: [], 2: [], 3: [], 4: [], 5: []}

for future_gap in future_gap_list:
    start_date = dates_dic[stock][0]
    end_date = dates_dic[stock][1]
    normalized_metrics, _, df = lstm.final_test_lstm(
        stock, start_date, end_date, window, future_gap, time_steps, neurons,
        drop_out, batch_size, epochs, validation_split, verbose, callbacks)
    lstm_future_gap_metrics[future_gap] = normalized_metrics
    plot_data(df,
              'Future Gap = ' + str(future_gap),
              "Date",
              "Price",
              show_plot=False)

#FFNN
neurons = [256, 256, 64, 1]
batch_size = 128
epochs = 200

ffnn_future_gap_metrics = {1: [], 2: [], 3: [], 4: [], 5: []}

for future_gap in future_gap_list:
    start_date = dates_dic[stock][0]
    end_date = dates_dic[stock][1]
    normalized_metrics, _, df = ffnn.final_test_ffnn(
        stock, start_date, end_date, window, future_gap, neurons, drop_out,
コード例 #7
0
    'AMZN': ['2017-08-01', '2018-04-01'],
}
metrics_dic = {'FB': [], 'AAPL': [], 'TSLA': [], 'AMZN': []}

window = 2
future_gap = 1
time_steps = 1
neurons = [256, 256, 32, 1]
drop_out = 0.2
batch_size = 2048
epochs = 300
validation_split = 0.1
verbose = 1
early_stopping_callback = EarlyStopping(monitor='val_loss',
                                        min_delta=0,
                                        patience=50,
                                        verbose=verbose,
                                        mode='auto')
callbacks = [early_stopping_callback]

for stock in stocks_list:
    start_date = dates_dic[stock][0]
    end_date = dates_dic[stock][1]
    normalized_metrics, _, df = lstm.final_test_lstm(
        stock, start_date, end_date, window, future_gap, time_steps, neurons,
        drop_out, batch_size, epochs, validation_split, verbose, callbacks)
    metrics_dic[stock] = normalized_metrics
    plot_data(df, stock + " Price Forecast", "Date", "Price", show_plot=False)

print(metrics_dic)
plt.show()
コード例 #8
0
def test_lstm(stock_symbol, start_date, end_date, window, future_gap,
              time_steps, neurons, drop_out, batch_size, epochs,
              validation_split, verbose, callbacks, show_plot_flg):
    #building the dataset
    print("> building the dataset...")
    df_train, _ = bulid_TIs_dataset(stock_symbol, None, start_date, window)
    df_test, scaler = bulid_TIs_dataset(stock_symbol, start_date, end_date,
                                        window)
    #reshaping the dataset for LSTM
    print("\n> reshaping the dataset for LSTM...")
    ds_train = df_train.values
    ds_test = df_test.values
    X_train, Y_train = lstm_dataset_reshape(ds_train, time_steps, future_gap,
                                            None)
    X_test, Y_test = lstm_dataset_reshape(ds_test, time_steps, future_gap,
                                          None)
    #building the LSTM model
    print("\n> building the LSTM model...")
    features = X_train.shape[2]
    model = build_model(time_steps, features, neurons, drop_out)
    #fitting the training data
    print("\n> fitting the training data...")
    model_fit(model, X_train, Y_train, batch_size, epochs, validation_split,
              verbose, callbacks)
    #predictions
    print("\n> testing the model for predictions...")
    predictions = model.predict(X_test)
    #inverse-scaling
    print("\n> inverse-scaling the scaled values...")
    predictions = predictions.reshape((predictions.shape[0], 1))
    predictions_inv_scaled = scaler.inverse_transform(predictions)
    Y_test = Y_test.reshape((Y_test.shape[0], 1))
    Y_test_inv_scaled = scaler.inverse_transform(Y_test)
    #grouping the actual prices and predictions
    print("\n> grouping the actual prices and predictions...")
    feature_cols = df_test.columns.tolist()
    feature_cols.remove("actual_price")
    df_test.drop(columns=feature_cols, inplace=True)
    df_test.rename(columns={"actual_price": 'Actual'}, inplace=True)
    df_test = df_test.iloc[time_steps + future_gap - 1:]
    df_test['Actual'] = Y_test_inv_scaled
    df_test['Prediction'] = predictions_inv_scaled
    #ploting the forecast vs the actual
    print("\n> plotting the results...")
    lookup = 5
    lag_list = compute_lag_metric(df_test['Actual'], df_test['Prediction'],
                                  lookup, stock_symbol)

    df_test = df_test[:len(df_test) - lookup + 1]
    plot_data(df_test,
              stock_symbol + " Price Forecast",
              "Date",
              "Price",
              show_plot=False)

    ax = df_test.plot(title=stock_symbol + " Price Forecast and PAL Overlay")
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")
    ax.legend(loc="best")
    ax.grid(True)
    #sudden vs normal plot annotation
    ax.annotate('Normal Movement',
                xy=('2013-02-15', 40),
                xytext=('2013-03-05', 50),
                fontsize=10,
                arrowprops=dict(facecolor='black', shrink=0.1, headwidth=8))
    ax.annotate('Sudden Change',
                xy=('2013-05-10', 55),
                xytext=('2013-03-05', 70),
                fontsize=10,
                arrowprops=dict(facecolor='black', shrink=0.1, headwidth=8))
    ax1 = ax.twinx()
    ax1.scatter(df_test.index, lag_list, c='g')
    ax1.set_ylabel("PAL")

    if show_plot_flg:
        plt.show()