code: def _run(
      handle_data,
      initialize,
      before_trading_start,
      analyze,
      algofile,
      algotext,
      defines,
      data_frequency,
      capital_base,
      bundle,
      bundle_timestamp,
      start,
      end,
      output,
      trading_calendar,
      print_algo,
      metrics_set,
      local_namespace,
      environ,
      blotter,
      benchmark_spec,
  ):
      """Run a backtest for the given algorithm.
  
      This is shared between the cli and :func:`zipline.run_algo`.
      """
  
      bundle_data = bundles.load(
          bundle,
          environ,
          bundle_timestamp,
      )
  
      if trading_calendar is None:
          trading_calendar = get_calendar("XNYS")
  
      # date parameter validation
      if trading_calendar.session_distance(start, end) < 1:
          raise _RunAlgoError(
              "There are no trading days between %s and %s"
              % (
                  start.date(),
                  end.date(),
              ),
          )
  
      benchmark_sid, benchmark_returns = benchmark_spec.resolve(
          asset_finder=bundle_data.asset_finder,
          start_date=start,
          end_date=end,
      )
  
      if algotext is not None:
          if local_namespace:
              ip = get_ipython()  # noqa
              namespace = ip.user_ns
          else:
              namespace = {}
  
          for assign in defines:
              try:
                  name, value = assign.split("=", 2)
              except ValueError:
                  raise ValueError(
                      "invalid define %r, should be of the form name=value"
                      % assign,
                  )
              try:
                  # evaluate in the same namespace so names may refer to
                  # eachother
                  namespace[name] = eval(value, namespace)
              except Exception as e:
                  raise ValueError(
                      "failed to execute definition for name %r: %s"
                      % (name, e),
                  )
      elif defines:
          raise _RunAlgoError(
              "cannot pass define without `algotext`",
              "cannot pass '-D' / '--define' without '-t' / '--algotext'",
          )
      else:
          namespace = {}
          if algofile is not None:
              algotext = algofile.read()
  
      if print_algo:
          if PYGMENTS:
              highlight(
                  algotext,
                  PythonLexer(),
                  TerminalFormatter(),
                  outfile=sys.stdout,
              )
          else:
              click.echo(algotext)
  
      first_trading_day = (
          bundle_data.equity_minute_bar_reader.first_trading_day
      )
  
      data = DataPortal(
          bundle_data.asset_finder,
          trading_calendar=trading_calendar,
          first_trading_day=first_trading_day,
          equity_minute_reader=bundle_data.equity_minute_bar_reader,
          equity_daily_reader=bundle_data.equity_daily_bar_reader,
          adjustment_reader=bundle_data.adjustment_reader,
      )
  
      pipeline_loader = USEquityPricingLoader.without_fx(
          bundle_data.equity_daily_bar_reader,
          bundle_data.adjustment_reader,
      )
  
      def choose_loader(column):
          if column in USEquityPricing.columns:
              return pipeline_loader
          raise ValueError(
              "No PipelineLoader registered for column %s." % column
          )
  
      if isinstance(metrics_set, six.string_types):
          try:
              metrics_set = metrics.load(metrics_set)
          except ValueError as e:
              raise _RunAlgoError(str(e))
  
      if isinstance(blotter, six.string_types):
          try:
              blotter = load(Blotter, blotter)
          except ValueError as e:
              raise _RunAlgoError(str(e))
  
      try:
          perf = TradingAlgorithm(
              namespace=namespace,
              data_portal=data,
              get_pipeline_loader=choose_loader,
              trading_calendar=trading_calendar,
              sim_params=SimulationParameters(
                  start_session=start,
                  end_session=end,
                  trading_calendar=trading_calendar,
                  capital_base=capital_base,
                  data_frequency=data_frequency,
              ),
              metrics_set=metrics_set,
              blotter=blotter,
              benchmark_returns=benchmark_returns,
              benchmark_sid=benchmark_sid,
              **{
                  "initialize": initialize,
                  "handle_data": handle_data,
                  "before_trading_start": before_trading_start,
                  "analyze": analyze,
              }
              if algotext is None
              else {
                  "algo_filename": getattr(
                      algofile, "name", "<algorithm>"
                  ),
                  "script": algotext,
              },
          ).run()
      except NoBenchmark:
          raise _RunAlgoError(
              (
                  "No ``benchmark_spec`` was provided, and"
                  " ``zipline.api.set_benchmark`` was not called in"
                  " ``initialize``."
              ),
              (
                  "Neither '--benchmark-symbol' nor '--benchmark-sid' was"
                  " provided, and ``zipline.api.set_benchmark`` was not called"
                  " in ``initialize``. Did you mean to pass '--no-benchmark'?"
              ),
          )
  
      if output == "-":
          click.echo(str(perf))
      elif (
          output != os.devnull
      ):  # make the zipline magic not write any data
          perf.to_pickle(output)
  
      return perf
  
imports: import click
  import os
  import sys
  import warnings
  import logbook
  import pandas
  import six
  from toolz import concatv
  from trading_calendars import get_calendar
  from zipline.data import bundles
  from zipline.data.benchmarks import get_benchmark_returns_from_file
  from zipline.data.data_portal import DataPortal
  from zipline.finance import metrics
  from zipline.finance.trading import SimulationParameters
  from zipline.pipeline.data import USEquityPricing
  from zipline.pipeline.loaders import USEquityPricingLoader
  import zipline.utils.paths
  from zipline.extensions import load
  from zipline.errors import SymbolNotFound
  from zipline.algorithm import TradingAlgorithm, NoBenchmark
  from zipline.finance.blotter import Blotter
label_desc: Use image node_id 1 for calling a global function with example usage: _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter, benchmark_spec) and returns: perf, pipeline_loader