Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various fix to be able to dump correctly an html report in local batch mode #646

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions pyfolio/tears.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from . import timeseries
from . import txn
from . import utils
import base64
from io import BytesIO

FACTOR_PARTITIONS = {
'style': ['momentum', 'size', 'value', 'reversal_short_term',
Expand All @@ -52,6 +54,16 @@ def timer(msg_body, previous_time):
return current_time


def fig_as_html(fig):
tmpfile = BytesIO()
fig.savefig(tmpfile, format='png')
encoded = base64.b64encode(tmpfile.getvalue()).decode('utf-8')
html = '''<div>
<img src=\'data:image/png;base64, %s\'>
</div>''' % (encoded)
return html


def create_full_tear_sheet(returns,
positions=None,
transactions=None,
Expand Down Expand Up @@ -166,7 +178,12 @@ def create_full_tear_sheet(returns,
dict specifying how factors should be separated in perf attrib
factor returns and risk exposures plots
- See create_perf_attrib_tear_sheet().
return_fig : boolean, optional
If True, returns the figure that was plotted on.
html_dump : boolean, optional
If True, print html to stdout
"""
fig = None

if (unadjusted_returns is None) and (slippage is not None) and\
(transactions is not None):
Expand All @@ -177,7 +194,7 @@ def create_full_tear_sheet(returns,
positions = utils.check_intraday(estimate_intraday, returns,
positions, transactions)

create_returns_tear_sheet(
fig = create_returns_tear_sheet(
returns,
positions=positions,
transactions=transactions,
Expand All @@ -187,44 +204,65 @@ def create_full_tear_sheet(returns,
bootstrap=bootstrap,
turnover_denom=turnover_denom,
header_rows=header_rows,
set_context=set_context)
set_context=set_context,
return_fig=return_fig)
if html_dump and fig is not None:
print(fig_as_html(fig))

create_interesting_times_tear_sheet(returns,
fig = create_interesting_times_tear_sheet(returns,
benchmark_rets=benchmark_rets,
set_context=set_context)
set_context=set_context,
return_fig=return_fig)
if html_dump and fig is not None:
print(fig_as_html(fig))

if positions is not None:
create_position_tear_sheet(returns, positions,
if html_dump and positions is not None:
fig = create_position_tear_sheet(returns, positions,
hide_positions=hide_positions,
set_context=set_context,
sector_mappings=sector_mappings,
estimate_intraday=False)
estimate_intraday=False,
return_fig=return_fig)
if html_dump and fig is not None:
print(fig_as_html(fig))

if transactions is not None:
create_txn_tear_sheet(returns, positions, transactions,
fig = create_txn_tear_sheet(returns, positions, transactions,
unadjusted_returns=unadjusted_returns,
estimate_intraday=False,
set_context=set_context)
set_context=set_context,
return_fig=return_fig)
if html_dump and fig is not None:
print(fig_as_html(fig))
if round_trips:
create_round_trip_tear_sheet(
fig = create_round_trip_tear_sheet(
returns=returns,
positions=positions,
transactions=transactions,
sector_mappings=sector_mappings,
estimate_intraday=False)
estimate_intraday=False,
return_fig=return_fig)
if html_dump and fig is not None:
print(fig_as_html(fig))

if market_data is not None:
create_capacity_tear_sheet(returns, positions, transactions,
fig = create_capacity_tear_sheet(returns, positions, transactions,
market_data,
liquidation_daily_vol_limit=0.2,
last_n_days=125,
estimate_intraday=False)
estimate_intraday=False,
return_fig=return_fig)
if html_dump and fig is not None:
print(fig_as_html(fig))

if factor_returns is not None and factor_loadings is not None:
create_perf_attrib_tear_sheet(returns, positions, factor_returns,
fig = create_perf_attrib_tear_sheet(returns, positions, factor_returns,
factor_loadings, transactions,
pos_in_dollars=pos_in_dollars,
factor_partitions=factor_partitions)
factor_partitions=factor_partitions,
return_fig=return_fig)
if html_dump and fig is not None:
print(fig_as_html(fig))


@plotting.customize
Expand Down
2 changes: 1 addition & 1 deletion pyfolio/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ def extract_interesting_date_ranges(returns, periods=None):
if periods is None:
periods = PERIODS
returns_dupe = returns.copy()
returns_dupe.index = returns_dupe.index.map(pd.Timestamp)
returns_dupe.index = returns_dupe.index.map(lambda t: pd.to_datetime(t.replace(tzinfo=None)).to_pydatetime())
ranges = OrderedDict()
for name, (start, end) in periods.items():
try:
Expand Down
2 changes: 1 addition & 1 deletion pyfolio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def print_table(table,
# Inject the new HTML
html = html.replace('<thead>', '<thead>' + rows)

display(HTML(html))
display(HTML(html).data)


def standardize_data(x):
Expand Down