Пример #1
0
def benchmark():
    # Getting session variables
    start_val = session.get('start_val', None)
    symbol = session.get('symbol', None)
    start_d = session.get('start_d', None)
    start_d = dt.datetime.strptime(start_d, '%Y/%m/%d').date()
    #start_d = start_d.strftime("%Y/%m/%d")
    num_shares = session.get('num_shares', None)
    commission = session.get('commission', None)
    impact = session.get('impact', None)

    # Specify the start and end dates for this period. For traininig we'll get 66% of dates.
    n_days_training = ((dt.date.today() - start_d).days) / 3 * 2
    end_d = dt.date.today() - dt.timedelta(n_days_training)

    # Get benchmark data
    benchmark_prices = fetchOnlineData(start_d, end_d, symbol)

    # Create benchmark data: Benchmark is a portfolio starting with $100,000, investing in 1000 shares of symbol and holding that position
    df_benchmark_trades = create_df_benchmark(symbol, start_d, end_d,
                                              num_shares)

    # Train a StrategyLearner
    # Set verbose to True will print out and plot the cumulative return for each training epoch
    stl = strategyLearner(num_shares=num_shares,
                          impact=impact,
                          commission=commission,
                          verbose=True,
                          num_states=3000,
                          num_actions=3)
    stl.add_evidence(df_prices=benchmark_prices,
                     symbol=symbol,
                     start_val=start_val)
    df_trades = stl.test_policy(symbol=symbol,
                                start_date=start_d,
                                end_date=end_d)

    return render_template(
        # name of template
        "benchmark.html",
        start_date=start_d,
        end_training_d=end_d,
        symbol=symbol,
        div_placeholder_cum_return=Markup(df_trades))
Пример #2
0
if __name__=="__main__":
    sv = 100000
    symbol = "JPM"
    commission = 0.00
    impact = 0.0
    num_shares = 1000

    # In-sample or training period
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)

    # Get a dataframe of benchmark data. Benchmark is a portfolio starting with
    # $100,000, investing in 1000 shares of symbol and holding that position


    df_benchmark_trades = create_df_benchmark(symbol, sd, ed,
                                              num_shares)

    # Train and test a StrategyLearner
    stl = StrategyLearner(num_shares=num_shares, impact=impact,
                          commission=commission, verbose=True,
                          num_states=3000, num_actions=3)

    stl.addEvidence(symbol=symbol,
                     sd=sd, ed=ed,sv=sv)

    df_trades = stl.test_policy(symbol=symbol, sd=sd,
                                ed=ed)

    # Retrieve performance stats via a market simulator
    print ("Performances during training period for {}".format(symbol))
    print ("Date Range: {} to {}".format(sd, ed))
Пример #3
0

if __name__ == "__main__":
    start_val = 100000
    symbol = "JPM"
    commission = 0.00
    impact = 0.0
    num_shares = 1000

    # In-sample or training period
    start_date = dt.datetime(2008, 1, 1)
    end_date = dt.datetime(2009, 12, 31)

    # Get a dataframe of benchmark data. Benchmark is a portfolio starting with
    # $100,000, investing in 1000 shares of symbol and holding that position
    df_benchmark_trades = create_df_benchmark(symbol, start_date, end_date,
                                              num_shares)

    # Train and test a StrategyLearner
    stl = strategyLearner(num_shares=num_shares,
                          impact=impact,
                          commission=commission,
                          verbose=True,
                          num_states=3000,
                          num_actions=3)
    stl.add_evidence(symbol=symbol,
                     start_val=start_val,
                     start_date=start_date,
                     end_date=end_date)
    df_trades = stl.test_policy(symbol=symbol,
                                start_date=start_date,
                                end_date=end_date)
Пример #4
0
def training():
    # **** Training ***
    # Getting session variables
    start_d = str(session.get('start_d', None))
    start_val = int(session.get('start_val', None))
    symbol = session.get('symbol', None)
    num_shares = int(session.get('num_shares', None))
    commission = float(session.get('commission', None))
    impact = float(session.get('impact', None))

    # Create a dataframe from csv file
    #df = get_data(symbol)
    # Import data from Alpha Vantage
    dates = pd.date_range(start_d, dt.date.today())
    df = get_data_av(symbol, dates, del_cols=True)

    # Rename column Adj Close
    df.rename(columns={'Close': symbol}, inplace=True)

    if not isinstance(df, pd.DataFrame):
        return render_template(
            # name of template
            "error.html",
            # now we pass in our variables into the template
            form='showvalues',
            error="Data source can't be reached at this moment. Try again.")

    # We'll get 80% data to train
    split_percentage = 0.8
    split = int(split_percentage * len(df))

    # Train data set
    df_training = df[:split]

    # Test data set
    df_testing = df[split:]

    # Training dates
    start_date_training = df_training.index[0]
    end_date_training = df_training.index[-1]

    # Testing dates
    start_date_testing = df_testing.index[0]
    end_date_testing = df_testing.index[-1]

    # Get a dataframe of benchmark data. Benchmark is a portfolio starting with
    # $100,000, investing in 1000 shares of symbol and holding that position
    df_benchmark_trades = create_df_benchmark(df_training, num_shares)

    # **** Training ****
    # Train a StrategyLearner
    stl = strategyLearner(num_shares=num_shares,
                          impact=impact,
                          commission=commission,
                          verbose=True,
                          num_states=3000,
                          num_actions=3,
                          dyna=200)

    epochs, cum_returns, trades, portvals, df_features, thresholds = stl.add_evidence(
        df_training,
        symbol,
        start_val=start_val,
        start_date=start_date_training,
        end_date=end_date_training)
    df_trades = stl.test_policy(df_training,
                                symbol,
                                start_date=start_date_training,
                                end_date=end_date_training)

    # cum_returns = map(lambda cum_returns: cum_returns * 100, cum_returns)
    plot_cum = plot_cum_return(epochs, cum_returns)

    # Retrieve performance stats via a market simulator
    print("Performances during training period for {}".format(symbol))
    print("Date Range: {} to {}".format(start_date_training,
                                        end_date_training))
    orders_count, sharpe_ratio, cum_ret, std_daily_ret, avg_daily_ret, final_value, cum_ret_bm, avg_daily_ret_bm, std_daily_ret_bm, sharpe_ratio_bm, final_value_bm, portvals, portvals_bm, df_orders = \
        market_simulator(df_training, df_trades, df_benchmark_trades, symbol=symbol,
                         start_val=start_val, commission=commission, impact=impact)

    plot_norm_data = plot_norm_data_vertical_lines(
        df_orders,
        portvals,
        portvals_bm,
        vert_lines=False,
        title="Training Portfolio Value",
        xtitle="Dates",
        ytitle="Value ($)")

    #TODO Check test results

    # **** Testing ****
    # Out-of-sample or testing period: Perform similiar steps as above,
    # except that we don't train the data (i.e. run add_evidence again)

    df_benchmark_trades = create_df_benchmark(df_testing, num_shares)
    df_trades = stl.test_policy(df_testing,
                                symbol,
                                start_date=start_date_testing,
                                end_date=end_date_testing)
    print("\nPerformances during testing period for {}".format(symbol))
    print("Date Range: {} to {}".format(start_date_testing, end_date_testing))

    # Retrieve performance stats via a market simulator
    test_orders_count, test_sharpe_ratio, test_cum_ret, test_std_daily_ret, test_avg_daily_ret, test_final_value, test_cum_ret_bm, test_avg_daily_ret_bm, test_std_daily_ret_bm, test_sharpe_ratio_bm, test_final_value_bm, test_portvals, test_portvals_bm, test_df_orders = \
        market_simulator(df_testing, df_trades, df_benchmark_trades, symbol=symbol,
                         start_val=start_val, commission=commission, impact=impact)

    plot_norm_data_test = plot_norm_data_vertical_lines(
        test_df_orders,
        test_portvals,
        test_portvals_bm,
        vert_lines=True,
        title="Testing Portfolio Value",
        xtitle="Dates",
        ytitle="Value ($)")

    return render_template(
        # name of template
        "training.html",

        # Training data
        start_date=start_date_training,
        end_date=end_date_training,
        symbol=symbol,
        div_placeholder_plot_cum=Markup(plot_cum),
        div_placeholder_plot_norm_data=Markup(plot_norm_data),
        orders_count_p=orders_count,
        sharpe_ratio_p=round(sharpe_ratio, 3),
        cum_ret_p=round(cum_ret, 3),
        std_daily_ret_p=round(std_daily_ret, 3),
        avg_daily_ret_p=round(avg_daily_ret, 3),
        final_value_p=round(final_value, 3),
        sharpe_ratio_b=round(sharpe_ratio_bm, 3),
        cum_ret_b=round(cum_ret_bm, 3),
        std_daily_ret_b=round(std_daily_ret_bm, 3),
        avg_daily_ret_b=round(avg_daily_ret_bm, 3),
        final_value_b=round(final_value_bm, 3),

        # Testing datasets
        start_date_testing=start_date_testing,
        end_date_testing=end_date_testing,
        div_placeholder_plot_norm_data_testing=Markup(plot_norm_data_test),
        orders_count_p_testing=test_orders_count,
        sharpe_ratio_p_testing=round(test_sharpe_ratio, 3),
        cum_ret_p_testing=round(test_cum_ret, 3),
        std_daily_ret_p_testing=round(test_std_daily_ret, 3),
        avg_daily_ret_p_testing=round(test_avg_daily_ret, 3),
        final_value_p_testing=round(test_final_value, 3),
        sharpe_ratio_b_testing=round(test_sharpe_ratio_bm, 3),
        cum_ret_b_testing=round(test_cum_ret_bm, 3),
        std_daily_ret_b_testing=round(test_std_daily_ret_bm, 3),
        avg_daily_ret_b_testing=round(test_avg_daily_ret_bm, 3),
        final_value_b_testing=round(test_final_value_bm, 3))