sv = 100000

impact_list = [0, 0.005, 0.01, 0.03, 0.05, 0.07, 0.1]
summary = pd.DataFrame(0, index = impact_list, columns = ['Trade#'] + ['Cumulative Return'] + ['Daily Standard Deviation'] + ['Average Daily Return'])
'''
strategy learner
'''
i = 0

plt.figure(figsize=(20,5))
random.seed(12)
for impact in impact_list:
    random.seed(12)
    learner = sl.StrategyLearner(verbose = False, impact = impact) # constructor
    learner.addEvidence(symbol = symbol, sd=sd, ed=ed, sv = sv) # training phase
    df_trades = learner.testPolicy(symbol = symbol, sd=sd, ed=ed, sv = sv) # testing phase
    daily_value_strategy = msc.compute_portvals(order = df_trades, symbol = symbol, start_val = sv, commission=0, impact=impact)/sv
    num_trade = df_trades.loc[df_trades['Order'] != 0].shape[0]
    cr_strategy, adr_strategy, sddr_strategy = ms.portstats(daily_value_strategy)
    summary.loc[impact_list[i],'Trade#'] = num_trade
    summary.loc[impact_list[i],'Cumulative Return'] = cr_strategy
    summary.loc[impact_list[i],'Daily Standard Deviation'] = sddr_strategy
    summary.loc[impact_list[i],'Average Daily Return'] = adr_strategy
    i += 1
    plt.plot(daily_value_strategy.index,daily_value_strategy, label = "Impact = " + str(impact))
plt.title("Learner behaviour with respect to the change of impact facotr")
plt.legend()
plt.xlabel("Date")
plt.show()

Example #2
0
date = pd.date_range(sd, ed)
stockvals = ut.get_data([symbol], date)
stockvals = stockvals.loc[:, [symbol]]
Benchmark = pd.DataFrame(0, index = stockvals.index[[0,-1]], columns = ['Order'])
Benchmark['Order'].iloc[0] = 1000
Benchmark['Order'].iloc[-1] = -1000
daily_value_benchmark = msc.compute_portvals(order = Benchmark, symbol = 'JPM', start_val = sv, commission=0, impact=0.000)/sv


'''
manuel strategy
'''
order = ms.testPolicy(symbol = 'JPM', sd = sd, ed = ed, sv = sv)
daily_value_manual = msc.compute_portvals(order = order, symbol = 'JPM', start_val = sv, commission=0, impact=0.000)/sv

cr_benchmark, adr_benchmark, sddr_benchmark = ms.portstats(daily_value_benchmark)
cr_manual, adr_manual, sddr_manual = ms.portstats(daily_value_manual)
cr_strategy, adr_strategy, sddr_strategy = ms.portstats(daily_value_strategy)




buy_m = order[order['Order'] > 0]
sell_m = order[order['Order'] < 0]

plt.figure(figsize=(20,5))
plt.plot(daily_value_benchmark.index, daily_value_benchmark, label = "Benchmark", color = "Blue")
plt.plot(daily_value_manual.index,daily_value_manual, label = "Manual Strategy", color = "Black")
buy_date = buy_m.shape[0]
for td in range(0,buy_date):
    plt.axvline(pd.to_datetime(buy_m.index[td]), color = "Green")