code: def test_equity_slippage(
      self, direction, check_portfolio_during_simulation
  ):
      if direction not in ("long", "short"):
          raise ValueError(
              "direction must be either long or short, got: %r"
              % direction,
          )
  
      # the number of shares to order, this will be filled one share at a
      # time
      shares = 100
  
      # random values in the range [0, 5) rounded to 3 decimal points
      st = np.random.RandomState(1868655980)
      per_fill_slippage = st.uniform(0, 5, shares).round(3)
  
      if direction == "short":
          per_fill_slippage = -per_fill_slippage
          shares = -shares
  
      slippage_iter = iter(per_fill_slippage)
  
      class TestingSlippage(api.slippage.SlippageModel):
          @staticmethod
          def process_order(data, order):
              return (
                  self.EQUITY_MINUTE_CONSTANT_CLOSE
                  + next(slippage_iter),
                  1 if direction == "long" else -1,
              )
  
      if check_portfolio_during_simulation:
          portfolio_snapshots = {}
  
          def check_portfolio(context):
              # force the portfolio even on the first bar
              portfolio = context.portfolio
              portfolio_snapshots[
                  api.get_datetime()
              ] = portfolio_snapshot(
                  portfolio,
              )
  
              if context.bar_count < 1:
                  assert_equal(portfolio.positions, {})
                  return
  
              expected_amount = min(context.bar_count, 100)
              if direction == "short":
                  expected_amount = -expected_amount
  
              expected_position = {
                  "asset": self.equity,
                  "last_sale_date": api.get_datetime(),
                  "last_sale_price": self.EQUITY_MINUTE_CONSTANT_CLOSE,
                  "amount": expected_amount,
                  "cost_basis": (
                      self.EQUITY_MINUTE_CONSTANT_CLOSE
                      + per_fill_slippage[: context.bar_count].mean()
                  ),
              }
              expected_positions = {self.equity: [expected_position]}
  
              positions = {
                  asset: [{k: getattr(p, k) for k in expected_position}]
                  for asset, p in portfolio.positions.items()
              }
  
              assert_equal(positions, expected_positions)
  
      else:
  
          def check_portfolio(context):
              pass
  
      def initialize(context):
          api.set_slippage(TestingSlippage())
          api.set_commission(api.commission.NoCommission())
          context.bar_count = 0
  
      def handle_data(context, data):
          if context.bar_count == 0:
              api.order(self.equity, shares)
  
          check_portfolio(context)
          context.bar_count += 1
  
      perf = self.run_algorithm(
          initialize=initialize,
          handle_data=handle_data,
      )
  
      first_day_returns = -(
          abs(per_fill_slippage.sum()) / self.SIM_PARAMS_CAPITAL_BASE
      )
      expected_returns = pd.Series(0.0, index=self.closes)
      expected_returns.iloc[0] = first_day_returns
  
      assert_equal(
          perf["returns"],
          expected_returns,
          check_names=False,
      )
  
      expected_cumulative_returns = pd.Series(
          first_day_returns,
          index=self.closes,
      )
  
      assert_equal(
          perf["algorithm_period_return"],
          expected_cumulative_returns,
          check_names=False,
      )
  
      first_day_capital_used = -(
          shares * self.EQUITY_MINUTE_CONSTANT_CLOSE
          + abs(per_fill_slippage.sum())
      )
      expected_capital_used = pd.Series(0.0, index=self.closes)
      expected_capital_used.iloc[0] = first_day_capital_used
  
      assert_equal(
          perf["capital_used"],
          expected_capital_used,
          check_names=False,
      )
  
      if not check_portfolio_during_simulation:
          return
  
      portfolio_snapshots = pd.DataFrame.from_dict(
          portfolio_snapshots,
          orient="index",
      )
  
      # each minute our cash flow is the share filled (if any) plus the
      # slippage for that minute
      minutely_cash_flow = pd.Series(0.0, index=self.trading_minutes)
      minutely_cash_flow[1 : abs(shares) + 1] = (
          -(per_fill_slippage + self.EQUITY_MINUTE_CONSTANT_CLOSE)
          if direction == "long"
          else (per_fill_slippage + self.EQUITY_MINUTE_CONSTANT_CLOSE)
      )
      expected_cash_flow = minutely_cash_flow.cumsum()
  
      assert_equal(
          portfolio_snapshots["cash_flow"],
          expected_cash_flow,
          check_names=False,
      )
  
      # Our pnl should just be the cost of the slippage incurred. This is
      # because we trade from cash into a position which holds 100% of its
      # value, but we lose the slippage on the way into that position.
      minutely_pnl = pd.Series(0.0, index=self.trading_minutes)
      minutely_pnl[1 : abs(shares) + 1] = -np.abs(per_fill_slippage)
      expected_pnl = minutely_pnl.cumsum()
  
      assert_equal(
          portfolio_snapshots["pnl"],
          expected_pnl,
          check_names=False,
      )
  
      # the divisor is capital base because this is cumulative returns
      expected_returns = expected_pnl / self.SIM_PARAMS_CAPITAL_BASE
  
      assert_equal(
          portfolio_snapshots["returns"],
          expected_returns,
          check_names=False,
      )
  
imports: import unittest
  import numpy
  import pandas
  from zipline import api
  from zipline.assets import Equity, Future
  from zipline.assets.synthetic import make_commodity_future_info
  from zipline.data.data_portal import DataPortal
  from zipline.data.resample import MinuteResampleSessionBarReader
  from zipline.testing import parameter_space, prices_generating_returns, simulate_minutes_for_day
  from zipline.testing.fixtures import WithMakeAlgo, WithConstantEquityMinuteBarData, WithConstantFutureMinuteBarData, WithWerror, ZiplineTestCase
  from zipline.testing.predicates import assert_equal, wildcard
label_desc: Use image node_id 4 for calling the TestConstantPrice obj's underlying member method code with example usage: obj.test_equity_slippage(direction, check_portfolio_during_simulation) and returns: unknown,  unknown