tensortrade.env.default.renderers module

class tensortrade.env.default.renderers.BaseRenderer[source]

Bases: tensortrade.env.generic.components.renderer.Renderer

The abstract base renderer to be subclassed when making a renderer the incorporates a Portfolio.

render(env: tensortrade.env.generic.environment.TradingEnv, **kwargs)[source]

Renders a view of the environment at the current step of an episode.

Parameters:
  • env ('TradingEnv') – The trading environment.
  • kwargs (keyword arguments) – Additional keyword arguments for rendering the environment.
render_env(episode: int = None, max_episodes: int = None, step: int = None, max_steps: int = None, price_history: Optional[pandas.core.frame.DataFrame] = None, net_worth: Optional[pandas.core.series.Series] = None, performance: Optional[pandas.core.frame.DataFrame] = None, trades: Optional[collections.OrderedDict] = None) → None[source]

Renderers the current state of the environment.

Parameters:
  • episode (int) – The episode that the environment is being rendered for.
  • max_episodes (int) – The maximum number of episodes that will occur.
  • step (int) – The step of the current episode that is happening.
  • max_steps (int) – The maximum number of steps that will occur in an episode.
  • price_history (pd.DataFrame) – The history of instrument involved with the environment. The required columns are: date, open, high, low, close, and volume.
  • net_worth (pd.Series) – The history of the net worth of the portfolio.
  • performance (pd.Series) – The history of performance of the portfolio.
  • trades (OrderedDict) – The history of trades for the current episode.
reset() → None[source]

Resets the renderer.

save() → None[source]

Saves the rendering of the TradingEnv.

class tensortrade.env.default.renderers.EmptyRenderer[source]

Bases: tensortrade.env.generic.components.renderer.Renderer

A renderer that does renders nothing.

Needed to make sure that environment can function without requiring a renderer.

render(env, **kwargs)[source]

Renders a view of the environment at the current step of an episode.

Parameters:
  • env ('TradingEnv') – The trading environment.
  • kwargs (keyword arguments) – Additional keyword arguments for rendering the environment.
class tensortrade.env.default.renderers.FileLogger(filename: str = None, path: str = 'log', log_format: str = None, timestamp_format: str = None)[source]

Bases: tensortrade.env.default.renderers.BaseRenderer

Logs information to a file.

Parameters:
  • filename (str) – The file name of the log file. If omitted, a file name will be created automatically.
  • path (str) – The path to save the log files to. None to save to same script directory.
  • log_format (str) – The log entry format as per Python logging. None for default. For more details, refer to https://docs.python.org/3/library/logging.html
  • timestamp_format (str) – The format of the timestamp of the log entry. Node for default.
DEFAULT_LOG_FORMAT = '[%(asctime)-15s] %(message)s'
DEFAULT_TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
log_file

The filename information is being logged to. (str, read-only)

render_env(episode: int = None, max_episodes: int = None, step: int = None, max_steps: int = None, price_history: pandas.core.frame.DataFrame = None, net_worth: pandas.core.series.Series = None, performance: pandas.core.frame.DataFrame = None, trades: Optional[collections.OrderedDict] = None) → None[source]

Renderers the current state of the environment.

Parameters:
  • episode (int) – The episode that the environment is being rendered for.
  • max_episodes (int) – The maximum number of episodes that will occur.
  • step (int) – The step of the current episode that is happening.
  • max_steps (int) – The maximum number of steps that will occur in an episode.
  • price_history (pd.DataFrame) – The history of instrument involved with the environment. The required columns are: date, open, high, low, close, and volume.
  • net_worth (pd.Series) – The history of the net worth of the portfolio.
  • performance (pd.Series) – The history of performance of the portfolio.
  • trades (OrderedDict) – The history of trades for the current episode.
class tensortrade.env.default.renderers.MatplotlibTradingChart(display: bool = True, save_format: str = None, path: str = 'charts', filename_prefix: str = 'chart_')[source]

Bases: tensortrade.env.default.renderers.BaseRenderer

Trading visualization for TensorTrade using Matplotlib :param display: True to display the chart on the screen, False for not. :type display: bool :param save_format: A format to save the chart to. Acceptable formats are

png, jpg, svg, pdf.
Parameters:
  • path (str) – The path to save the char to if save_format is not None. The folder will be created if not found.
  • filename_prefix (str) – A string that precedes automatically-created file name when charts are saved. Default ‘chart_’.
render_env(episode: int = None, max_episodes: int = None, step: int = None, max_steps: int = None, price_history: Optional[pandas.core.frame.DataFrame] = None, net_worth: Optional[pandas.core.series.Series] = None, performance: Optional[pandas.core.frame.DataFrame] = None, trades: Optional[collections.OrderedDict] = None) → None[source]

Renderers the current state of the environment.

Parameters:
  • episode (int) – The episode that the environment is being rendered for.
  • max_episodes (int) – The maximum number of episodes that will occur.
  • step (int) – The step of the current episode that is happening.
  • max_steps (int) – The maximum number of steps that will occur in an episode.
  • price_history (pd.DataFrame) – The history of instrument involved with the environment. The required columns are: date, open, high, low, close, and volume.
  • net_worth (pd.Series) – The history of the net worth of the portfolio.
  • performance (pd.Series) – The history of performance of the portfolio.
  • trades (OrderedDict) – The history of trades for the current episode.
reset() → None[source]

Resets the renderer.

save() → None[source]

Saves the rendering of the TradingEnv.

class tensortrade.env.default.renderers.PlotlyTradingChart(display: bool = True, height: int = None, timestamp_format: str = '%Y-%m-%d %H:%M:%S', save_format: str = None, path: str = 'charts', filename_prefix: str = 'chart_', auto_open_html: bool = False, include_plotlyjs: Union[bool, str] = 'cdn')[source]

Bases: tensortrade.env.default.renderers.BaseRenderer

Trading visualization for TensorTrade using Plotly.

Parameters:
  • display (bool) – True to display the chart on the screen, False for not.
  • height (int) – Chart height in pixels. Affects both display and saved file charts. Set to None for 100% height. Default is None.
  • save_format (str) – A format to save the chart to. Acceptable formats are html, png, jpeg, webp, svg, pdf, eps. All the formats except for ‘html’ require Orca. Default is None for no saving.
  • path (str) – The path to save the char to if save_format is not None. The folder will be created if not found.
  • filename_prefix (str) – A string that precedes automatically-created file name when charts are saved. Default ‘chart_’.
  • timestamp_format (str) – The format of the date shown in the chart title.
  • auto_open_html (bool) – Works for save_format=’html’ only. True to automatically open the saved chart HTML file in the default browser, False otherwise.
  • include_plotlyjs (Union[bool, str]) – Whether to include/load the plotly.js library in the saved file. ‘cdn’ results in a smaller file by loading the library online but requires an Internet connect while True includes the library resulting in much larger file sizes. False to not include the library. For more details, refer to https://plot.ly/python-api-reference/generated/plotly.graph_objects.Figure.html

Notes

Possible Future Enhancements:
  • Saving images without using Orca.
  • Limit displayed step range for the case of a large number of steps and let the shown part of the chart slide after filling that range to keep showing recent data as it’s being added.

References

[1]https://plot.ly/python-api-reference/generated/plotly.graph_objects.Figure.html
[2]https://plot.ly/python/figurewidget/
[3]https://plot.ly/python/subplots/
[4]https://plot.ly/python/reference/#candlestick
[5]https://plot.ly/python/#chart-events
render_env(episode: int = None, max_episodes: int = None, step: int = None, max_steps: int = None, price_history: pandas.core.frame.DataFrame = None, net_worth: pandas.core.series.Series = None, performance: pandas.core.frame.DataFrame = None, trades: Optional[collections.OrderedDict] = None) → None[source]

Renderers the current state of the environment.

Parameters:
  • episode (int) – The episode that the environment is being rendered for.
  • max_episodes (int) – The maximum number of episodes that will occur.
  • step (int) – The step of the current episode that is happening.
  • max_steps (int) – The maximum number of steps that will occur in an episode.
  • price_history (pd.DataFrame) – The history of instrument involved with the environment. The required columns are: date, open, high, low, close, and volume.
  • net_worth (pd.Series) – The history of the net worth of the portfolio.
  • performance (pd.Series) – The history of performance of the portfolio.
  • trades (OrderedDict) – The history of trades for the current episode.
reset() → None[source]

Resets the renderer.

save() → None[source]

Saves the current chart to a file.

Notes

All formats other than HTML require Orca installed and server running.

class tensortrade.env.default.renderers.ScreenLogger(date_format: str = '%Y-%m-%d %-I:%M:%S %p')[source]

Bases: tensortrade.env.default.renderers.BaseRenderer

Logs information the screen of the user.

Parameters:date_format (str) – The format for logging the date.
DEFAULT_FORMAT = '[%(asctime)-15s] %(message)s'
render_env(episode: int = None, max_episodes: int = None, step: int = None, max_steps: int = None, price_history: pandas.core.frame.DataFrame = None, net_worth: pandas.core.series.Series = None, performance: pandas.core.frame.DataFrame = None, trades: Optional[collections.OrderedDict] = None)[source]

Renderers the current state of the environment.

Parameters:
  • episode (int) – The episode that the environment is being rendered for.
  • max_episodes (int) – The maximum number of episodes that will occur.
  • step (int) – The step of the current episode that is happening.
  • max_steps (int) – The maximum number of steps that will occur in an episode.
  • price_history (pd.DataFrame) – The history of instrument involved with the environment. The required columns are: date, open, high, low, close, and volume.
  • net_worth (pd.Series) – The history of the net worth of the portfolio.
  • performance (pd.Series) – The history of performance of the portfolio.
  • trades (OrderedDict) – The history of trades for the current episode.
tensortrade.env.default.renderers.get(identifier: str) → tensortrade.env.default.renderers.BaseRenderer[source]

Gets the BaseRenderer that matches the identifier.

Parameters:identifier (str) – The identifier for the BaseRenderer
Returns:BaseRenderer – The renderer associated with the identifier.
Raises:KeyError: – Raised if identifier is not associated with any BaseRenderer