def average_realized_volatility( self, tenor: str, returns_type: Returns = Returns.SIMPLE, *, real_time: bool = False, request_id: Optional[str] = None) -> pd.Series: """ Weighted average realized volatility :param tenor: relative date representation of expiration date e.g. 1m :param returns_type: returns type :param real_time: whether to retrieve intraday data instead of EOD :param request_id: service request id, if any :return: time series of the average realized volatility """ if real_time: raise NotImplementedError( 'real-time basket realized vol not implemented') spot_df = self.get_spot_data(request_id=request_id) actual_weights = self.get_actual_weights(request_id=request_id) vols = [ volatility(spot_df[asset_id], Window(tenor, tenor), returns_type) for asset_id in spot_df ] vols = pd.concat(vols, axis=1) vols.columns = self.get_marquee_ids() return actual_weights.mul(vols).sum(axis=1, skipna=False)
def average_realized_volatility(self, tenor: str, returns_type: Returns = Returns.SIMPLE, *, real_time: bool = False, request_id: Optional[str] = None) -> pd.Series: """ Weighted average realized volatility :param tenor: relative date representation of expiration date e.g. 1m :param returns_type: returns type :param real_time: whether to retrieve intraday data instead of EOD :param request_id: service request id, if any :return: time series of the average realized volatility """ if real_time: raise NotImplementedError('real-time basket realized vol not implemented') spot_df = self.get_spot_data(request_id=request_id) actual_weights = self.get_actual_weights(request_id=request_id) # Add in today's data if not real_time and DataContext.current.end_date >= datetime.date.today(): recent_spot = ts.get_last_for_measure(self.get_marquee_ids(), QueryType.SPOT, {}, request_id=request_id) if recent_spot is not None and len(recent_spot): recent_spot.index.rename('date', inplace=True) spot_df = spot_df.append(recent_spot.pivot_table('spot', ['date'], 'assetId')) vols = [volatility(spot_df[asset_id], Window(tenor, tenor), returns_type) for asset_id in spot_df] vols = pd.concat(vols, axis=1) vols.columns = list(spot_df) # Necessary when current values appended - set weights index to match vols index actual_weights = actual_weights.reindex(vols.index).fillna(method='pad') return actual_weights.mul(vols, axis=1).sum(axis=1, skipna=False)
def average_realized_volatility( self, tenor: str, returns_type: Returns = Returns.LOGARITHMIC, *, real_time: bool = False, request_id: Optional[str] = None) -> pd.Series: """ Weighted average realized volatility :param tenor: relative date representation of expiration date e.g. 1m :param returns_type: returns type :param real_time: whether to retrieve intraday data instead of EOD :param request_id: service request id, if any :return: time series of the average realized volatility """ if real_time: raise NotImplementedError( 'real-time basket realized vol not implemented') spot_df = self.get_spot_data(request_id=request_id) actual_weights = self.get_actual_weights(request_id=request_id) vols = [ volatility(spot_df[asset_id], Window(tenor, tenor), returns_type) for asset_id in spot_df ] vols = pd.concat(vols, axis=1) vols.columns = list(spot_df) # Necessary when current values appended - set weights index to match vols index actual_weights = actual_weights.reindex( vols.index).fillna(method='pad') return actual_weights.mul(vols, axis=1).sum(axis=1, skipna=False)
def process(self): a_data = self.children_data.get('a') if isinstance(a_data, ProcessorResult): if a_data.success: result = volatility(a_data.data, self.w, self.returns_type) self.value = ProcessorResult(True, result) else: self.value = ProcessorResult(False, 'Could not compute volatility') else: self.value = ProcessorResult(False, 'Processor does not have data')
def average_realized_volatility( self, tenor: str, returns_type: Returns = Returns.SIMPLE, *, real_time: bool = False, request_id: Optional[str] = None) -> pd.Series: """ Weighted average realized volatility :param tenor: relative date representation of expiration date e.g. 1m :param returns_type: returns type :param real_time: whether to retrieve intraday data instead of EOD :param request_id: service request id, if any :return: time series of the average realized volatility """ if real_time: raise NotImplementedError( 'real-time basket realized vol not implemented') q = GsDataApi.build_market_data_query(self.get_marquee_ids(), QueryType.SPOT) log_debug(request_id, _logger, 'q %s', q) spot_data = GsDataApi.get_market_data(q, request_id=request_id) grouped = spot_data.groupby('assetId') spot_series = [ grouped.get_group(mqid)['spot'] for mqid in self.get_marquee_ids() ] actual_weights = _calculate_basket(spot_series, self.weights, rebal_freq=self.rebal_freq)[1] actual_weights.columns = self.get_marquee_ids() vols = [ volatility(s, Window(tenor, tenor), returns_type) for s in spot_series ] vols = pd.concat(vols, axis=1) vols.columns = self.get_marquee_ids() return actual_weights.mul(vols).sum(axis=1, skipna=False)