'Internal method that pre-calculates the benchmark return series for
use in the simulation.
Parameters
asset:  Asset to use
trading_calendar: TradingCalendar
trading_days: pd.DateTimeIndex
data_portal: DataPortal
Notes
If the benchmark asset started trading after the simulation start,
or finished trading before the simulation end, exceptions are raised.
If the benchmark asset started trading the same day as the simulation
start, the first available minute price on that day is used instead
of the previous close.
We use history to get an adjusted price history for each day\'s close,
as of the look-back date (the last day of the simulation).  Prices are
fully adjusted for dividends, splits, and mergers.
Returns
A pd.Series, indexed by trading day, whose values represent the %
change from close to close.'

def _initialize_precalculated_series(self, asset, trading_calendar, trading_days, data_portal):

    if (self.emission_rate == 'minute'):
        minutes = trading_calendar.minutes_for_sessions_in_range(self.sessions[0], self.sessions[(-1)])
        benchmark_series = data_portal.get_history_window([asset], minutes[(-1)], bar_count=(len(minutes) + 1), frequency='1m', field='price', data_frequency=self.emission_rate, ffill=True)[asset]
        return benchmark_series.pct_change()[1:]
    else:
        start_date = asset.start_date
        if (start_date < trading_days[0]):
            benchmark_series = data_portal.get_history_window([asset], trading_days[(-1)], bar_count=(len(trading_days) + 1), frequency='1d', field='price', data_frequency=self.emission_rate, ffill=True)[asset]
            return benchmark_series.pct_change()[1:]
        elif (start_date == trading_days[0]):
            benchmark_series = data_portal.get_history_window([asset], trading_days[(-1)], bar_count=len(trading_days), frequency='1d', field='price', data_frequency=self.emission_rate, ffill=True)[asset]
            first_open = data_portal.get_spot_value(asset, 'open', trading_days[0], 'daily')
            first_close = data_portal.get_spot_value(asset, 'close', trading_days[0], 'daily')
            first_day_return = ((first_close - first_open) / first_open)
            returns = benchmark_series.pct_change()[:]
            returns[0] = first_day_return
            return returns
