import matplotlib.pyplot as plt
#import matplotlib.gridspec as gridspec
import pyfolio as pf
#################################################
#
# Value Momentum Returns
#
#################################################
#Get returns from value momentum strategy
val_mom_bt = get_backtest('56e2f30b09fc0b0df60582ad') #val/mom 50/50
#get daily returns from 2007-2015
val_mom_returns = val_mom_bt.daily_performance.returns
val_mom_returns = val_mom_returns['01/01/2007':'12/31/2015']
#get value momentum stats
val_mom_stats = pf.timeseries.perf_stats(val_mom_returns)
val_mom_stats = val_mom_stats.rename(columns={'perf_stats':'val mom'})
#################################################
#
# Managed Futures Returns
#
#################################################
#Get returns of managed futures strategy from data folder
mgd_futures_returns = local_csv('Returns-2007-2015-all.csv',date_column='Date',use_date_column_as_index=True, timezone='UTC')
#get daily returns
mgd_futures_returns = mgd_futures_returns.ix[:,'Daily Return']
#to ensure certain reports are generated correctly
#before creating a report need to ensure daily returns data provided for every day that relevant indexes are traded
return_normalizer = val_mom_bt.daily_performance.returns * 0.0
mgd_futures_returns = return_normalizer.add(mgd_futures_returns,fill_value=0)
#get managed futures stats
mgd_fut_stats = pf.timeseries.perf_stats(mgd_futures_returns)
mgd_fut_stats = mgd_fut_stats.rename(columns={'perf_stats':'mgd fut'})
#################################################
#
# Value Momentum and Managed Futures Returns
#
#################################################
# set strategy allocations
vm_alloc = 0.8 #value/momentum allocation
mf_alloc = 0.2 #managed futures allocation
#calculate daily returns of combined strategies
mgd_futures_returns_2 = mgd_futures_returns * mf_alloc
val_mom_returns_2 = val_mom_returns * vm_alloc
total_returns = val_mom_returns_2.add(mgd_futures_returns_2,fill_value=0)
#get total returns stats
total_ret_stats = pf.timeseries.perf_stats(total_returns)
total_ret_stats = total_ret_stats.rename(columns={'perf_stats':'total return'})
#################################################
#
# Restricted Managed Futures Returns
# (only trade e-minis and currencies)
#################################################
#Get returns of managed futures strategy from data folder
res_mgd_futures_returns = local_csv('Returns-2007-2015-currencies and minis.csv',date_column='Date',use_date_column_as_index=True, timezone='UTC')
#get daily returns
res_mgd_futures_returns = res_mgd_futures_returns.ix[:,'Daily Return']
#to ensure certain reports are generated correctly
#before creating a report need to ensure daily returns data provided for every day that relevant indexes are traded
return_normalizer = val_mom_bt.daily_performance.returns * 0.0
res_mgd_futures_returns = return_normalizer.add(res_mgd_futures_returns,fill_value=0)
#get restricted managed futures stats
res_mgd_fut_stats = pf.timeseries.perf_stats(res_mgd_futures_returns)
res_mgd_fut_stats = res_mgd_fut_stats.rename(columns={'perf_stats':'res mgd fut'})
#################################################
#
# Value Momentum and Restricted Managed Futures Returns
#
#################################################
#calculate daily returns of combined strategies
res_mgd_futures_returns_2 = res_mgd_futures_returns * mf_alloc
res_total_returns = val_mom_returns_2.add(res_mgd_futures_returns_2,fill_value=0)
#get total returns stats
res_total_ret_stats = pf.timeseries.perf_stats(res_total_returns)
res_total_ret_stats = res_total_ret_stats.rename(columns={'perf_stats':'restricted total return'})
#################################################
#
# Show Performance statistics
#
#################################################
#show performance statistics
perf_stats = [val_mom_stats,mgd_fut_stats,total_ret_stats]
perf_stats = pd.concat(perf_stats,axis=1)
#show dataframe
perf_stats
#################################################
#
# Show Performance statistics (with restricted managed futures)
#
#################################################
#show performance statistics
perf_stats = [val_mom_stats,res_mgd_fut_stats,res_total_ret_stats]
perf_stats = pd.concat(perf_stats,axis=1)
#show dataframe
perf_stats
#################################################
#
# Show Cumulative Returns
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0))
ax2 = plt.subplot2grid((2,3), (0,1))
ax3 = plt.subplot2grid((2,3), (0,2))
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t\t Total Returns'
pf.plotting.plot_rolling_returns(val_mom_returns,ax=ax1)
pf.plotting.plot_rolling_returns(mgd_futures_returns,ax=ax2)
pf.plotting.plot_rolling_returns(total_returns,ax=ax3)
#################################################
#
# Show Cumulative Returns (restricted)
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0))
ax2 = plt.subplot2grid((2,3), (0,1))
ax3 = plt.subplot2grid((2,3), (0,2))
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t Total Returns (restricted)'
pf.plotting.plot_rolling_returns(val_mom_returns,ax=ax1)
pf.plotting.plot_rolling_returns(res_mgd_futures_returns,ax=ax2)
pf.plotting.plot_rolling_returns(res_total_returns,ax=ax3)
#################################################
#
# Show Top 5 Drawdown Periods
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0) )
ax2 = plt.subplot2grid((2,3), (0,1) )
ax3 = plt.subplot2grid((2,3), (0,2) )
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t\t Total Returns'
pf.plotting.plot_drawdown_periods(val_mom_returns,ax=ax1)
pf.plotting.plot_drawdown_periods(mgd_futures_returns,ax=ax2)
pf.plotting.plot_drawdown_periods(total_returns,ax=ax3)
#################################################
#
# Show Top 5 Drawdown Periods (restricted)
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0) )
ax2 = plt.subplot2grid((2,3), (0,1) )
ax3 = plt.subplot2grid((2,3), (0,2) )
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t Total Returns (restricted)'
pf.plotting.plot_drawdown_periods(val_mom_returns,ax=ax1)
pf.plotting.plot_drawdown_periods(res_mgd_futures_returns,ax=ax2)
pf.plotting.plot_drawdown_periods(res_total_returns,ax=ax3)
#################################################
#
# Show Annual Returns
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0) )
ax2 = plt.subplot2grid((2,3), (0,1) )
ax3 = plt.subplot2grid((2,3), (0,2) )
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t\t Total Returns'
pf.plotting.plot_annual_returns(val_mom_returns,ax=ax1)
pf.plotting.plot_annual_returns(mgd_futures_returns,ax=ax2)
pf.plotting.plot_annual_returns(total_returns,ax=ax3)
#################################################
#
# Show Annual Returns (restricted)
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0) )
ax2 = plt.subplot2grid((2,3), (0,1) )
ax3 = plt.subplot2grid((2,3), (0,2) )
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t Total Returns (restricted)'
pf.plotting.plot_annual_returns(val_mom_returns,ax=ax1)
pf.plotting.plot_annual_returns(res_mgd_futures_returns,ax=ax2)
pf.plotting.plot_annual_returns(res_total_returns,ax=ax3)
#################################################
#
# Show Monthly Returns Heat Map
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0) )
ax2 = plt.subplot2grid((2,3), (0,1) )
ax3 = plt.subplot2grid((2,3), (0,2) )
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t\t Total Returns'
pf.plotting.plot_monthly_returns_heatmap(val_mom_returns,ax=ax1)
pf.plotting.plot_monthly_returns_heatmap(mgd_futures_returns,ax=ax2)
pf.plotting.plot_monthly_returns_heatmap(total_returns,ax=ax3)
#################################################
#
# Show Monthly Returns Heat Map (restricted)
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0) )
ax2 = plt.subplot2grid((2,3), (0,1) )
ax3 = plt.subplot2grid((2,3), (0,2) )
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t Total Returns (restricted)'
pf.plotting.plot_monthly_returns_heatmap(val_mom_returns,ax=ax1)
pf.plotting.plot_monthly_returns_heatmap(res_mgd_futures_returns,ax=ax2)
pf.plotting.plot_monthly_returns_heatmap(res_total_returns,ax=ax3)
#################################################
#
# Show Distribution Monthly Returns
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0) )
ax2 = plt.subplot2grid((2,3), (0,1) )
ax3 = plt.subplot2grid((2,3), (0,2) )
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t\t Total Returns'
pf.plotting.plot_monthly_returns_dist(val_mom_returns,ax=ax1)
pf.plotting.plot_monthly_returns_dist(mgd_futures_returns,ax=ax2)
pf.plotting.plot_monthly_returns_dist(total_returns,ax=ax3)
#################################################
#
# Show Distribution Monthly Returns (restricted)
#
#################################################
ax1 = plt.subplot2grid((2,3), (0,0) )
ax2 = plt.subplot2grid((2,3), (0,1) )
ax3 = plt.subplot2grid((2,3), (0,2) )
print '\n\n\t Value/Momentum \t\t\t Managed Futures \t\t Total Returns (restricted)'
pf.plotting.plot_monthly_returns_dist(val_mom_returns,ax=ax1)
pf.plotting.plot_monthly_returns_dist(res_mgd_futures_returns,ax=ax2)
pf.plotting.plot_monthly_returns_dist(res_total_returns,ax=ax3)