Skip to content

Commit

Permalink
revert the wrong refacor of iteritems of series
Browse files Browse the repository at this point in the history
add cap_buckets & sector_dict into tear function
  • Loading branch information
huaiweicheng committed Aug 28, 2019
1 parent f265fec commit 23c4a64
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyfolio/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def show_perf_stats(returns, factor_returns=None, positions=None,
perf_stats = pd.DataFrame(perf_stats_all, columns=['Backtest'])

for column in perf_stats.columns:
for stat, value in perf_stats[column].items():
for stat, value in perf_stats[column].iteritems():
if stat in STAT_FUNCS_PCT:
perf_stats.loc[stat, column] = str(np.round(value * 100,
1)) + '%'
Expand Down
16 changes: 11 additions & 5 deletions pyfolio/risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ def compute_sector_exposures(positions, sectors, sector_dict=None):
sector_dict : dict or OrderedDict
Dictionary of all sectors
- Keys are sector codes (e.g. ints or strings) and values are sector
names (which must be strings)
- Defaults to Morningstar sectors
- See full explanation in create_risk_tear_sheet
"""

if sector_dict is None:
Expand Down Expand Up @@ -282,7 +280,7 @@ def plot_sector_exposures_net(net_exposures, sector_dict=None, ax=None):
return ax


def compute_cap_exposures(positions, caps):
def compute_cap_exposures(positions, caps, cap_buckets=None):
"""
Returns arrays of long, short and gross market cap exposures of an
algorithm's positions
Expand All @@ -296,8 +294,16 @@ def compute_cap_exposures(positions, caps):
caps : pd.DataFrame
Daily Morningstar sector code per asset
- See full explanation in create_risk_tear_sheet
cap_buckets : dict or OrderedDict
Dictionary of all cap buckets
- See full explanation in create_risk_tear_sheet
"""

if cap_buckets is None:
cap_buckets = CAP_BUCKETS

long_exposures = []
short_exposures = []
gross_exposures = []
Expand All @@ -310,7 +316,7 @@ def compute_cap_exposures(positions, caps):
tot_short_exposure = positions_wo_cash[positions_wo_cash < 0] \
.abs().sum(axis='columns')

for bucket_name, boundaries in CAP_BUCKETS.items():
for bucket_name, boundaries in cap_buckets.items():
in_bucket = positions_wo_cash[(caps >= boundaries[0]) &
(caps <= boundaries[1])]

Expand Down
18 changes: 16 additions & 2 deletions pyfolio/tears.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,9 @@ def create_capacity_tear_sheet(returns, positions, transactions,
def create_risk_tear_sheet(positions,
style_factors,
sectors=None,
sector_dict=None,
caps=None,
cap_buckets=None,
shares_held=None,
volumes=None,
percentile=None,
Expand Down Expand Up @@ -1110,6 +1112,12 @@ def create_risk_tear_sheet(positions,
2017-04-04 311.0 206.0
2017-04-05 311.0 206.0
sector_dict : dict or OrderedDict
Dictionary of all sectors
- Keys are sector codes (e.g. ints or strings) and values are sector
names (which must be strings)
- Defaults to Morningstar sectors, see SECTORS in risk.py
caps : pd.DataFrame
Daily market cap per asset
- DataFrame with dates as index and equities as columns
Expand All @@ -1120,6 +1128,12 @@ def create_risk_tear_sheet(positions,
2017-04-04 1.329620e+10 6.403694e+10
2017-04-05 1.297464e+10 6.397187e+10
cap_buckets : dict or OrderedDict
Dictionary of all cap buckets
- Keys are Micro/Small/Mid/Large/Mega
- Values are capital value range tuple
- Defaults to CAP_BUCKETS in risk.py
shares_held : pd.DataFrame
Daily number of shares held by an algorithm.
- Example:
Expand Down Expand Up @@ -1199,7 +1213,7 @@ def create_risk_tear_sheet(positions,
i += 1
ax_sector_net = plt.subplot(gs[i, :], sharex=style_axes[0])
long_exposures, short_exposures, gross_exposures, net_exposures \
= risk.compute_sector_exposures(positions, sectors)
= risk.compute_sector_exposures(positions, sectors, sector_dict)
risk.plot_sector_exposures_longshort(long_exposures, short_exposures,
ax=ax_sector_longshort)
risk.plot_sector_exposures_gross(gross_exposures, ax=ax_sector_gross)
Expand All @@ -1213,7 +1227,7 @@ def create_risk_tear_sheet(positions,
i += 1
ax_cap_net = plt.subplot(gs[i, :], sharex=style_axes[0])
long_exposures, short_exposures, gross_exposures, net_exposures \
= risk.compute_cap_exposures(positions, caps)
= risk.compute_cap_exposures(positions, caps, cap_buckets)
risk.plot_cap_exposures_longshort(long_exposures, short_exposures,
ax_cap_longshort)
risk.plot_cap_exposures_gross(gross_exposures, ax_cap_gross)
Expand Down

0 comments on commit 23c4a64

Please sign in to comment.