Source code for tradeexecutor.backtest.notebook

"""Jupyter notebook utilities for backtesting."""
import enum
import logging

import matplotlib_inline

[docs]class OutputMode(enum.Enum): """What is the output mode for the notebook visualisations. Interactive visualisations work only on the HTML pages that are able to load Plotly.js JavaScripts. """ #: Output charts as static images static = "static" #: Output charts as interactive Plotly.js visualisations interactive = "interactive"
[docs]def setup_charting_and_output(mode: OutputMode=OutputMode.interactive, image_format="svg"): """Sets up Jupyter Notebook based charting. - `Set Quantstats chart to SVG output and for high-resolution screens <>`__ - Mute common warnings like `Matplotlib font loading <>`__ Example: .. code-block:: python # Set Jupyter Notebook output mode parameters from tradeexecutor.backtest.notebook import setup_charting_and_output setup_charting_and_output() :param mode: What kind of viewing context we have for this notebook output """ # Get rid of findfont: Font family 'Arial' not found. # when running a remote notebook on Jupyter Server on Ubuntu Linux server # logging.getLogger("matplotlib.font_manager").setLevel(logging.ERROR) # Render charts from quantstats in high resolution # matplotlib_inline.backend_inline.set_matplotlib_formats(image_format) # Set Plotly to offline (static image mode) if mode == OutputMode.static: # from plotly.offline import init_notebook_mode init_notebook_mode() # import as pio pio.kaleido.scope.default_format = image_format # pio.renderers.default = image_format svg_renderer = pio.renderers[image_format] # Have SVGs default pixel with svg_renderer.width = 1500