Notebook
In [131]:
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'})
100% Time: 0:00:30|###########################################################|
In [132]:
#################################################
#
#  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
Out[132]:
val mom mgd fut total return
annual_return 0.125333 0.132555 0.129139
annual_volatility 0.153912 0.200865 0.128830
sharpe_ratio 0.844484 0.718444 1.007630
calmar_ratio 0.763924 0.545449 0.948911
stability 0.937036 0.811006 0.968768
max_drawdown -0.164065 -0.243020 -0.136092
omega_ratio 1.179753 1.152797 1.205573
sortino_ratio 1.199110 1.110931 1.431561
skewness 0.011437 2.761032 -0.130778
kurtosis 12.308618 61.231950 10.006550
In [142]:
#################################################
#
#  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
Out[142]:
val mom res_mgd fut restricted total return
annual_return 0.125333 0.109658 0.122937
annual_volatility 0.153912 0.155787 0.127510
sharpe_ratio 0.844484 0.744017 0.973507
calmar_ratio 0.763924 0.685656 0.891454
stability 0.937036 0.852478 0.956363
max_drawdown -0.164065 -0.159932 -0.137906
omega_ratio 1.179753 1.167478 1.200235
sortino_ratio 1.199110 1.213101 1.379912
skewness 0.011437 4.662520 -0.149094
kurtosis 12.308618 107.579982 10.013070
In [152]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 			 Total Returns
Out[152]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faea40b8050>
In [154]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 		 Total Returns (restricted)
Out[154]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faea5e41710>
In [156]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 			 Total Returns
Out[156]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fae9fa95750>
In [157]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 		 Total Returns (restricted)
Out[157]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fae9f704ed0>
In [126]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 			 Total Returns
Out[126]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faea6cbf0d0>
In [147]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 		 Total Returns (restricted)
Out[147]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faea5d95210>
In [122]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 			 Total Returns
Out[122]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faea7a843d0>
In [148]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 		 Total Returns (restricted)
Out[148]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faea5f60610>
In [127]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 			 Total Returns
Out[127]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faea69692d0>
In [150]:
#################################################
#
#  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)

	 Value/Momentum 			 Managed Futures 		 Total Returns (restricted)
Out[150]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faea58efb10>