Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
before the start date
slippage (float): Percent price slippage when executing order
commission (float): Amount of commission paid per order
cash (int): Amount of starting cash
portfolio (prophet.portfolio.Portfolio): Starting portfolio
Return:
prophet.backtest.BackTest
"""
# Setup
if not end:
today = dt.date.today()
end = dt.datetime.combine(today, dt.time())
if not self._order_generator:
raise ProphetException("Must set an order generator by calling"
"set_order_generator.")
timestamps = trading_days[(trading_days >= start) &
(trading_days <= end)]
effective_start = timestamps[0]
data = self._generate_data(start=effective_start,
end=end,
lookback=lookback)
# Run backtest
return backtest(cash=cash,
data=data,
start=effective_start,
end=end,
slippage=slippage,
generate orders for.
lookback (int): Number of trading days you want data for before the
(target_datetime - buffer_days)
cash (int): Amount of starting cash
buffer_days (int): number of trading days you want extra data
generated for. Acts as a data start date.
portfolio (prophet.portfolio.Portfolio): Starting portfolio
"""
target_datetime_index = trading_days.get_loc(target_datetime)
start = trading_days[target_datetime_index - buffer_days]
data = self._generate_data(start,
target_datetime,
lookback)
prices = data.get('prices')
if prices is None:
raise ProphetException("Price data is required to run a backtest. "
"Please add a data generator with the name "
"property set to 'prices'.")
return self._order_generator.run(prices=prices,
data=data,
timestamp=target_datetime,
cash=cash,
portfolio=portfolio)
cache_filename = "{stock}-{start}-{end}.csv".format(
stock=symbol_path, start=data_start, end=end
).replace(':', '-')
cache_filepath = self.get_cache_filepath(cache_filename)
if os.path.exists(cache_filepath):
symbol_data = pd.DataFrame.from_csv(cache_filepath)
else:
symbol_data = pdr.DataReader(symbol, source,
data_start, end).sort_index()
symbol_data.to_csv(cache_filepath)
symbols_data[symbol] = symbol_data
symbols_panel = pd.concat(symbols_data).to_panel()
symbols_panel = symbols_panel.swapaxes('minor', 'major')
if symbols_panel.empty:
ProphetException("No data for the range specified:"
" %s to %s" % (data_start, end))
symbols_panel = symbols_panel.fillna(method='ffill')
symbols_panel = symbols_panel.fillna(method='bfill')
symbols_panel = symbols_panel.fillna(1.0)
return symbols_panel.loc[:, ((symbols_panel.major_axis >= data_start)
& (symbols_panel.major_axis <= end))]