class_docstr: Applies some basic trade analysis for a trading strategy (as defined by TradingModel). Use PyFolio to create some
  basic trading statistics. Also allows you test multiple parameters for a specific strategy (like TC).
class_docstr_tok: ["Applies","some","basic","trade","analysis","for","a","trading","strategy","(","as","defined","by","TradingModel",")",".","Use","PyFolio","to","create","some","basic","trading","statistics",".","Also","allows","you","test","multiple","parameters","for","a","specific","strategy","(","like","TC",")","."]
docstr: Plots useful statistics for the trading strategy using various backends
  
  Parameters
  ----------
  trading_model : TradingModel
      defining trading strategy
  
  engine : str
      'pyfolio' - use PyFolio as a backend
      'finmarketpy' - use finmarketpy as a backend
  
  index: DataFrame
      define strategy by a time series
docstr_tok: ["Plots","useful","statistics","for","the","trading","strategy","using","various","backends","Parameters","--","--","--","--","--","trading_model",":","TradingModel","defining","trading","strategy","engine",":","str","'pyfolio","'","-","use","PyFolio","as","a","backend","'finmarketpy","'","-","use","finmarketpy","as","a","backend","index",":","DataFrame","define","strategy","by","a","time","series"]
code: def run_strategy_returns_stats(
      self, trading_model, index=None, engine="finmarketpy"
  ):
      """Plots useful statistics for the trading strategy using various backends
  
      Parameters
      ----------
      trading_model : TradingModel
          defining trading strategy
  
      engine : str
          'pyfolio' - use PyFolio as a backend
          'finmarketpy' - use finmarketpy as a backend
  
      index: DataFrame
          define strategy by a time series
  
      """
  
      if index is None:
          pnl = trading_model.strategy_pnl()
      else:
          pnl = index
  
      tz = Timezone()
      calculations = Calculations()
  
      if engine == "pyfolio":
          # PyFolio assumes UTC time based DataFrames (so force this localisation)
          try:
              pnl = tz.localize_index_as_UTC(pnl)
          except:
              pass
  
          # set the matplotlib style sheet & defaults
          # at present this only works in Matplotlib engine
          try:
              import matplotlib
              import matplotlib.pyplot as plt
  
              matplotlib.rcdefaults()
              plt.style.use(
                  ChartConstants().chartfactory_style_sheet[
                      "chartpy-pyfolio"
                  ]
              )
          except:
              pass
  
          # TODO for intraday strategies, make daily
  
          # convert DataFrame (assumed to have only one column) to Series
          pnl = calculations.calculate_returns(pnl)
          pnl = pnl.dropna()
          pnl = pnl[pnl.columns[0]]
          fig = pf.create_returns_tear_sheet(pnl, return_fig=True)
  
          try:
              plt.savefig(trading_model.DUMP_PATH + "stats.png")
          except:
              pass
  
          plt.show()
      elif engine == "finmarketpy":
          # assume we have TradingModel
          # to do to take in a time series
          from chartpy import Canvas, Chart
  
          # temporarily make scale factor smaller so fits the window
          old_scale_factor = trading_model.SCALE_FACTOR
          trading_model.SCALE_FACTOR = 0.75
  
          pnl = trading_model.plot_strategy_pnl(
              silent_plot=True
          )  # plot the final strategy
          individual = trading_model.plot_strategy_group_pnl_trades(
              silent_plot=True
          )  # plot the individual trade P&Ls
  
          pnl_comp = trading_model.plot_strategy_group_benchmark_pnl(
              silent_plot=True
          )  # plot all the cumulative P&Ls of each component
          ir_comp = trading_model.plot_strategy_group_benchmark_pnl_ir(
              silent_plot=True
          )  # plot all the IR of each component
  
          leverage = trading_model.plot_strategy_leverage(
              silent_plot=True
          )  # plot the leverage of the portfolio
          ind_lev = trading_model.plot_strategy_group_leverage(
              silent_plot=True
          )  # plot all the individual leverages
  
          canvas = Canvas(
              [
                  [pnl, individual],
                  [pnl_comp, ir_comp],
                  [leverage, ind_lev],
              ]
          )
  
          canvas.generate_canvas(
              page_title=trading_model.FINAL_STRATEGY
              + " Return Statistics",
              silent_display=False,
              canvas_plotter="plain",
              output_filename=trading_model.FINAL_STRATEGY + ".html",
              render_pdf=False,
          )
  
          trading_model.SCALE_FACTOR = old_scale_factor
  
imports: import datetime
  import pandas
  import copy
  from chartpy import Chart, Style, ChartConstants
  from findatapy.timeseries import Calculations, Timezone
  from findatapy.util.loggermanager import LoggerManager
  from finmarketpy.backtest import Backtest
  from finmarketpy.util.marketconstants import MarketConstants
  from findatapy.util.swimpool import SwimPool
label_desc: Use image node_id 2 for calling the TradeAnalysis obj's underlying member method code with example usage: obj.run_strategy_returns_stats(trading_model, index, engine) without return types