def _get_prices_df(self, ticker: Ticker, start_date: datetime, end_date: datetime) -> PricesDataFrame: """ Returns non-adjusted open and close prices, indexed with the Market Open and Market Close time.""" if isinstance(ticker, FutureTicker): ticker.initialize_data_provider(SettableTimer(end_date), self._data_provider) tickers_chain = ticker.get_expiration_dates() if start_date >= tickers_chain.index[-1] or end_date <= tickers_chain.index[0]: # If the futures chain starts after the _end_date or ends before the _start_date - no data available return PricesDataFrame() # Get all tickers from the chain that were valid between the start_date and expiration date of the # currently valid ticker end_date = tickers_chain[tickers_chain == ticker.get_current_specific_ticker()].index[0] tickers_chain = tickers_chain.loc[start_date:end_date] tickers = tickers_chain.values.tolist() open_prices = self._data_provider.get_price(tickers, PriceField.Open, start_date, end_date) close_prices = self._data_provider.get_price(tickers, PriceField.Close, start_date, end_date) else: open_prices = self._data_provider.get_price([ticker], PriceField.Open, start_date, end_date) close_prices = self._data_provider.get_price([ticker], PriceField.Close, start_date, end_date) open_prices.index = [dt + MarketOpenEvent.trigger_time() for dt in open_prices.index] close_prices.index = [dt + MarketCloseEvent.trigger_time() for dt in close_prices.index] prices = concat([open_prices, close_prices]).sort_index() return prices
def ticker_to_contract(self, ticker: Ticker) -> IBContract: ticker = ticker.get_current_specific_ticker() if isinstance( ticker, FutureTicker) else ticker contract = self._ticker_to_contract_dict.get(ticker, None) if not contract and ticker.security_type == SecurityType.FUTURE: contract = self._create_futures_contract(ticker) return contract
def get_signal(self, ticker: Ticker, current_exposure: Exposure) -> Signal: self.ticker_name_to_ticker[ticker.name] = ticker suggested_exposure = self.calculate_exposure(ticker, current_exposure) fraction_at_risk = self.calculate_fraction_at_risk(ticker) specific_ticker = ticker.get_current_specific_ticker() if isinstance( ticker, FutureTicker) else ticker last_available_price = self.data_handler.get_last_available_price( specific_ticker) signal = Signal(ticker, suggested_exposure, fraction_at_risk, last_available_price, alpha_model=self) return signal
def get_signal(self, ticker: Ticker, current_exposure: Exposure, current_time: Optional[datetime] = None, frequency: Frequency = Frequency.DAILY) -> Signal: current_time = current_time or datetime.now() self.ticker_name_to_ticker[ticker.name] = ticker suggested_exposure = self.calculate_exposure(ticker, current_exposure) fraction_at_risk = self.calculate_fraction_at_risk(ticker) specific_ticker = ticker.get_current_specific_ticker() if isinstance( ticker, FutureTicker) else ticker last_available_price = self.data_provider.get_last_available_price( specific_ticker, frequency, current_time) signal = Signal(ticker, suggested_exposure, fraction_at_risk, last_available_price, current_time, alpha_model=self) return signal
def ticker_to_contract(self, ticker: Ticker) -> Ticker: """ Maps contract parameters to corresponding ticker. """ contract = ticker.get_current_specific_ticker() if isinstance( ticker, FutureTicker) else ticker return contract
def _get_specific_ticker(ticker: Ticker): return ticker.get_current_specific_ticker() if isinstance(ticker, FutureTicker) else ticker