本文整理汇总了Python中pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs.create_mult_index方法的典型用法代码示例。如果您正苦于以下问题:Python TimeSeriesCalcs.create_mult_index方法的具体用法?Python TimeSeriesCalcs.create_mult_index怎么用?Python TimeSeriesCalcs.create_mult_index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs
的用法示例。
在下文中一共展示了TimeSeriesCalcs.create_mult_index方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bus_day_of_month_seasonality
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
def bus_day_of_month_seasonality(
self,
data_frame,
month_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
cum=True,
cal="FX",
partition_by_month=True,
):
tsc = TimeSeriesCalcs()
tsf = TimeSeriesFilter()
data_frame.index = pandas.to_datetime(data_frame.index)
data_frame = tsf.filter_time_series_by_holidays(data_frame, cal)
monthly_seasonality = tsc.average_by_month_day_by_bus_day(data_frame, cal)
monthly_seasonality = monthly_seasonality.loc[month_list]
if partition_by_month:
monthly_seasonality = monthly_seasonality.unstack(level=0)
if cum is True:
monthly_seasonality.ix[0] = numpy.zeros(len(monthly_seasonality.columns))
if partition_by_month:
monthly_seasonality.index = monthly_seasonality.index + 1 # shifting index
monthly_seasonality = monthly_seasonality.sort() # sorting by index
monthly_seasonality = tsc.create_mult_index(monthly_seasonality)
return monthly_seasonality
示例2: calculate_ret_stats
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
def calculate_ret_stats(self, returns_df, ann_factor):
"""
calculate_ret_stats - Calculates return statistics for an asset's returns including IR, vol, ret and drawdowns
Parameters
----------
returns_df : DataFrame
asset returns
ann_factor : int
annualisation factor to use on return statistics
Returns
-------
DataFrame
"""
tsc = TimeSeriesCalcs()
self._rets = returns_df.mean(axis=0) * ann_factor
self._vol = returns_df.std(axis=0) * math.sqrt(ann_factor)
self._inforatio = self._rets / self._vol
self._kurtosis = returns_df.kurtosis(axis=0)
index_df = tsc.create_mult_index(returns_df)
max2here = pandas.expanding_max(index_df)
dd2here = index_df / max2here - 1
self._dd = dd2here.min()
示例3: bus_day_of_month_seasonality
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
def bus_day_of_month_seasonality(self, data_frame,
month_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], cum = True,
cal = "FX", partition_by_month = True, add_average = False, price_index = False):
tsc = TimeSeriesCalcs()
tsf = TimeSeriesFilter()
if price_index:
data_frame = data_frame.resample('B') # resample into business days
data_frame = tsc.calculate_returns(data_frame)
data_frame.index = pandas.to_datetime(data_frame.index)
data_frame = tsf.filter_time_series_by_holidays(data_frame, cal)
monthly_seasonality = tsc.average_by_month_day_by_bus_day(data_frame, cal)
monthly_seasonality = monthly_seasonality.loc[month_list]
if partition_by_month:
monthly_seasonality = monthly_seasonality.unstack(level=0)
if add_average:
monthly_seasonality['Avg'] = monthly_seasonality.mean(axis=1)
if cum is True:
if partition_by_month:
monthly_seasonality.loc[0] = numpy.zeros(len(monthly_seasonality.columns))
# monthly_seasonality.index = monthly_seasonality.index + 1 # shifting index
monthly_seasonality = monthly_seasonality.sort()
monthly_seasonality = tsc.create_mult_index(monthly_seasonality)
return monthly_seasonality
示例4: calculate_ret_stats
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
def calculate_ret_stats(self, returns_df, ann_factor):
tsc = TimeSeriesCalcs()
self._rets = returns_df.mean(axis=0) * ann_factor
self._vol = returns_df.std(axis=0) * math.sqrt(ann_factor)
self._inforatio = self._rets / self._vol
index_df = tsc.create_mult_index(returns_df)
max2here = pandas.expanding_max(index_df)
dd2here = index_df / max2here - 1
self._dd = dd2here.min()
示例5: calculate_vol_adjusted_index_from_prices
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
def calculate_vol_adjusted_index_from_prices(self, prices_df, br):
"""
calculate_vol_adjusted_index_from_price - Adjusts an index of prices for a vol target
Parameters
----------
br : BacktestRequest
Parameters for the backtest specifying start date, finish data, transaction costs etc.
asset_a_df : pandas.DataFrame
Asset prices to be traded
Returns
-------
pandas.Dataframe containing vol adjusted index
"""
tsc = TimeSeriesCalcs()
returns_df, leverage_df = self.calculate_vol_adjusted_returns(prices_df, br, returns = False)
return tsc.create_mult_index(returns_df)
示例6: monthly_seasonality
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
def monthly_seasonality(self, data_frame,
cum = True,
add_average = False, price_index = False):
tsc = TimeSeriesCalcs()
if price_index:
data_frame = data_frame.resample('BM') # resample into month end
data_frame = tsc.calculate_returns(data_frame)
data_frame.index = pandas.to_datetime(data_frame.index)
monthly_seasonality = tsc.average_by_month(data_frame)
if add_average:
monthly_seasonality['Avg'] = monthly_seasonality.mean(axis=1)
if cum is True:
monthly_seasonality.loc[0] = numpy.zeros(len(monthly_seasonality.columns))
monthly_seasonality = monthly_seasonality.sort()
monthly_seasonality = tsc.create_mult_index(monthly_seasonality)
return monthly_seasonality
示例7: calculate_trading_PnL
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
#.........这里部分代码省略.........
signal_df : pandas.DataFrame
Signals for the trading strategy
"""
tsc = TimeSeriesCalcs()
# signal_df.to_csv('e:/temp0.csv')
# make sure the dates of both traded asset and signal are aligned properly
asset_df, signal_df = asset_a_df.align(signal_df, join='left', axis = 'index')
# only allow signals to change on the days when we can trade assets
signal_df = signal_df.mask(numpy.isnan(asset_df.values)) # fill asset holidays with NaN signals
signal_df = signal_df.fillna(method='ffill') # fill these down
asset_df = asset_df.fillna(method='ffill') # fill down asset holidays
returns_df = tsc.calculate_returns(asset_df)
tc = br.spot_tc_bp
signal_cols = signal_df.columns.values
returns_cols = returns_df.columns.values
pnl_cols = []
for i in range(0, len(returns_cols)):
pnl_cols.append(returns_cols[i] + " / " + signal_cols[i])
# do we have a vol target for individual signals?
if hasattr(br, 'signal_vol_adjust'):
if br.signal_vol_adjust is True:
if not(hasattr(br, 'signal_vol_resample_type')):
br.signal_vol_resample_type = 'mean'
leverage_df = self.calculate_leverage_factor(returns_df, br.signal_vol_target, br.signal_vol_max_leverage,
br.signal_vol_periods, br.signal_vol_obs_in_year,
br.signal_vol_rebalance_freq, br.signal_vol_resample_freq,
br.signal_vol_resample_type)
signal_df = pandas.DataFrame(
signal_df.values * leverage_df.values, index = signal_df.index, columns = signal_df.columns)
self._individual_leverage = leverage_df # contains leverage of individual signal (before portfolio vol target)
_pnl = tsc.calculate_signal_returns_with_tc_matrix(signal_df, returns_df, tc = tc)
_pnl.columns = pnl_cols
# portfolio is average of the underlying signals: should we sum them or average them?
if hasattr(br, 'portfolio_combination'):
if br.portfolio_combination == 'sum':
portfolio = pandas.DataFrame(data = _pnl.sum(axis = 1), index = _pnl.index, columns = ['Portfolio'])
elif br.portfolio_combination == 'mean':
portfolio = pandas.DataFrame(data = _pnl.mean(axis = 1), index = _pnl.index, columns = ['Portfolio'])
else:
portfolio = pandas.DataFrame(data = _pnl.mean(axis = 1), index = _pnl.index, columns = ['Portfolio'])
portfolio_leverage_df = pandas.DataFrame(data = numpy.ones(len(_pnl.index)), index = _pnl.index, columns = ['Portfolio'])
# should we apply vol target on a portfolio level basis?
if hasattr(br, 'portfolio_vol_adjust'):
if br.portfolio_vol_adjust is True:
portfolio, portfolio_leverage_df = self.calculate_vol_adjusted_returns(portfolio, br = br)
self._portfolio = portfolio
self._signal = signal_df # individual signals (before portfolio leverage)
self._portfolio_leverage = portfolio_leverage_df # leverage on portfolio
# multiply portfolio leverage * individual signals to get final position signals
length_cols = len(signal_df.columns)
leverage_matrix = numpy.repeat(portfolio_leverage_df.values.flatten()[numpy.newaxis,:], length_cols, 0)
# final portfolio signals (including signal & portfolio leverage)
self._portfolio_signal = pandas.DataFrame(
data = numpy.multiply(numpy.transpose(leverage_matrix), signal_df.values),
index = signal_df.index, columns = signal_df.columns)
if hasattr(br, 'portfolio_combination'):
if br.portfolio_combination == 'sum':
pass
elif br.portfolio_combination == 'mean':
self._portfolio_signal = self._portfolio_signal / float(length_cols)
else:
self._portfolio_signal = self._portfolio_signal / float(length_cols)
self._pnl = _pnl # individual signals P&L
# TODO FIX very slow - hence only calculate on demand
_pnl_trades = None
# _pnl_trades = tsc.calculate_individual_trade_gains(signal_df, _pnl)
self._pnl_trades = _pnl_trades
self._tsd_pnl = TimeSeriesDesc()
self._tsd_pnl.calculate_ret_stats(self._pnl, br.ann_factor)
self._portfolio.columns = ['Port']
self._tsd_portfolio = TimeSeriesDesc()
self._tsd_portfolio.calculate_ret_stats(self._portfolio, br.ann_factor)
self._cumpnl = tsc.create_mult_index(self._pnl) # individual signals cumulative P&L
self._cumpnl.columns = pnl_cols
self._cumportfolio = tsc.create_mult_index(self._portfolio) # portfolio cumulative P&L
self._cumportfolio.columns = ['Port']
示例8: get_intraday_moves_over_custom_event
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
def get_intraday_moves_over_custom_event(self, data_frame_rets, ef_time_frame, vol=False,
minute_start = 5, mins = 3 * 60, min_offset = 0 , create_index = False,
resample = False, freq = 'minutes'):
tsf = TimeSeriesFilter()
ef_time_frame = tsf.filter_time_series_by_date(data_frame_rets.index[0], data_frame_rets.index[-1], ef_time_frame)
ef_time = ef_time_frame.index
if freq == 'minutes':
ef_time_start = ef_time - timedelta(minutes = minute_start)
ef_time_end = ef_time + timedelta(minutes = mins)
ann_factor = 252 * 1440
elif freq == 'days':
ef_time = ef_time_frame.index.normalize()
ef_time_start = ef_time - timedelta(days = minute_start)
ef_time_end = ef_time + timedelta(days = mins)
ann_factor = 252
ords = range(-minute_start + min_offset, mins + min_offset)
# all data needs to be equally spaced
if resample:
tsf = TimeSeriesFilter()
# make sure time series is properly sampled at 1 min intervals
data_frame_rets = data_frame_rets.resample('1min')
data_frame_rets = data_frame_rets.fillna(value = 0)
data_frame_rets = tsf.remove_out_FX_out_of_hours(data_frame_rets)
data_frame_rets['Ind'] = numpy.nan
start_index = data_frame_rets.index.searchsorted(ef_time_start)
finish_index = data_frame_rets.index.searchsorted(ef_time_end)
# not all observation windows will be same length (eg. last one?)
# fill the indices which represent minutes
# TODO vectorise this!
for i in range(0, len(ef_time_frame.index)):
try:
data_frame_rets.ix[start_index[i]:finish_index[i], 'Ind'] = ords
except:
data_frame_rets.ix[start_index[i]:finish_index[i], 'Ind'] = ords[0:(finish_index[i] - start_index[i])]
# set the release dates
data_frame_rets.ix[start_index,'Rel'] = ef_time # set entry points
data_frame_rets.ix[finish_index + 1,'Rel'] = numpy.zeros(len(start_index)) # set exit points
data_frame_rets['Rel'] = data_frame_rets['Rel'].fillna(method = 'pad') # fill down signals
data_frame_rets = data_frame_rets[pandas.notnull(data_frame_rets['Ind'])] # get rid of other
data_frame = data_frame_rets.pivot(index='Ind',
columns='Rel', values=data_frame_rets.columns[0])
data_frame.index.names = [None]
if create_index:
tsc = TimeSeriesCalcs()
data_frame.ix[-minute_start + min_offset,:] = numpy.nan
data_frame = tsc.create_mult_index(data_frame)
else:
if vol is True:
# annualise (if vol)
data_frame = pandas.rolling_std(data_frame, window=5) * math.sqrt(ann_factor)
else:
data_frame = data_frame.cumsum()
return data_frame
示例9: get_fx_cross
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
#.........这里部分代码省略.........
time_series_request.cut = cut # NYC/BGN ticker
time_series_request.fields = 'close' # close field only
time_series_request.cache_algo = cache_algo # cache_algo_only, cache_algo_return, internet_load
time_series_request.environment = 'backtest'
time_series_request.start_date = start
time_series_request.finish_date = end
time_series_request.data_source = source
for cr in cross:
base = cr[0:3]
terms = cr[3:6]
if (type == 'spot'):
# non-USD crosses
if base != 'USD' and terms != 'USD':
base_USD = self.fxconv.correct_notation('USD' + base)
terms_USD = self.fxconv.correct_notation('USD' + terms)
# TODO check if the cross exists in the database
# download base USD cross
time_series_request.tickers = base_USD
time_series_request.category = self.fxconv.em_or_g10(base, freq)
base_vals = time_series_factory.harvest_time_series(time_series_request)
# download terms USD cross
time_series_request.tickers = terms_USD
time_series_request.category = self.fxconv.em_or_g10(terms, freq)
terms_vals = time_series_factory.harvest_time_series(time_series_request)
if (base_USD[0:3] == 'USD'):
base_vals = 1 / base_vals
if (terms_USD[0:3] == 'USD'):
terms_vals = 1 / terms_vals
base_vals.columns = ['temp']
terms_vals.columns = ['temp']
cross_vals = base_vals.div(terms_vals, axis = 'index')
cross_vals.columns = [cr + '.close']
else:
if base == 'USD': non_USD = terms
if terms == 'USD': non_USD = base
correct_cr = self.fxconv.correct_notation(cr)
time_series_request.tickers = correct_cr
time_series_request.category = self.fxconv.em_or_g10(non_USD, freq)
cross_vals = time_series_factory.harvest_time_series(time_series_request)
# flip if not convention
if(correct_cr != cr):
cross_vals = 1 / cross_vals
cross_vals.columns.names = [cr + '.close']
elif type[0:3] == "tot":
if freq == 'daily':
# download base USD cross
time_series_request.tickers = base + 'USD'
time_series_request.category = self.fxconv.em_or_g10(base, freq) + '-tot'
if type == "tot":
base_vals = time_series_factory.harvest_time_series(time_series_request)
else:
x = 0
# download terms USD cross
time_series_request.tickers = terms + 'USD'
time_series_request.category = self.fxconv.em_or_g10(terms, freq) + '-tot'
if type == "tot":
terms_vals = time_series_factory.harvest_time_series(time_series_request)
else:
x = 0
base_rets = time_series_calcs.calculate_returns(base_vals)
terms_rets = time_series_calcs.calculate_returns(terms_vals)
cross_rets = base_rets.sub(terms_rets.iloc[:,0],axis=0)
# first returns of a time series will by NaN, given we don't know previous point
cross_rets.iloc[0] = 0
cross_vals = time_series_calcs.create_mult_index(cross_rets)
cross_vals.columns = [cr + '-tot.close']
elif freq == 'intraday':
self.logger.info('Total calculated returns for intraday not implemented yet')
return None
if data_frame_agg is None:
data_frame_agg = cross_vals
else:
data_frame_agg = data_frame_agg.join(cross_vals, how='outer')
# strip the nan elements
data_frame_agg = data_frame_agg.dropna()
return data_frame_agg
示例10: calculate_trading_PnL
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import create_mult_index [as 别名]
def calculate_trading_PnL(self, br, asset_a_df, signal_df):
"""
calculate_trading_PnL - Calculates P&L of a trading strategy and statistics to be retrieved later
Parameters
----------
br : BacktestRequest
Parameters for the backtest specifying start date, finish data, transaction costs etc.
asset_a_df : pandas.DataFrame
Asset prices to be traded
signal_df : pandas.DataFrame
Signals for the trading strategy
"""
tsc = TimeSeriesCalcs()
# make sure the dates of both traded asset and signal are aligned properly
asset_df, signal_df = asset_a_df.align(signal_df, join='left', axis = 0)
# only allow signals to change on the days when we can trade assets
signal_df = signal_df.mask(numpy.isnan(asset_df.values)) # fill asset holidays with NaN signals
signal_df = signal_df.fillna(method='ffill') # fill these down
asset_df = asset_df.fillna(method='ffill') # fill down asset holidays
returns_df = tsc.calculate_returns(asset_df)
tc = br.spot_tc_bp
signal_cols = signal_df.columns.values
returns_cols = returns_df.columns.values
pnl_cols = []
for i in range(0, len(returns_cols)):
pnl_cols.append(returns_cols[i] + " / " + signal_cols[i])
if hasattr(br, 'signal_vol_adjust'):
if br.signal_vol_adjust is True:
leverage_df = self.calculate_leverage_factor(returns_df, br.signal_vol_target, br.signal_vol_max_leverage,
br.signal_vol_periods, br.signal_vol_obs_in_year,
br.signal_vol_rebalance_freq)
signal_df = pandas.DataFrame(
signal_df.values * leverage_df.values, index = signal_df.index, columns = signal_df.columns)
self._individual_leverage = leverage_df
_pnl = tsc.calculate_signal_returns_with_tc_matrix(signal_df, returns_df, tc = tc)
_pnl.columns = pnl_cols
# portfolio is average of the underlying signals
interim_portfolio = pandas.DataFrame(data = _pnl.mean(axis = 1), index = _pnl.index, columns = ['Portfolio'])
portfolio_leverage_df = pandas.DataFrame(data = numpy.ones(len(_pnl.index)), index = _pnl.index, columns = ['Portfolio'])
if hasattr(br, 'portfolio_vol_adjust'):
if br.portfolio_vol_adjust is True:
interim_portfolio, portfolio_leverage_df = self.calculate_vol_adjusted_returns(interim_portfolio, br = br)
self._portfolio = interim_portfolio
self._signal = signal_df
self._portfolio_leverage = portfolio_leverage_df
# multiply portfolio leverage * individual signals to get final position signals
length_cols = len(signal_df.columns)
leverage_matrix = numpy.repeat(portfolio_leverage_df.values.flatten()[numpy.newaxis,:], length_cols, 0)
self._portfolio_signal = pandas.DataFrame(
data = numpy.multiply(numpy.transpose(leverage_matrix), signal_df.values),
index = signal_df.index, columns = signal_df.columns) / float(length_cols)
self._pnl = _pnl
self._tsd_pnl = TimeSeriesDesc()
self._tsd_pnl.calculate_ret_stats(self._pnl, br.ann_factor)
self._portfolio.columns = ['Port']
self._tsd_portfolio = TimeSeriesDesc()
self._tsd_portfolio.calculate_ret_stats(self._portfolio, br.ann_factor)
self._cumpnl = tsc.create_mult_index(self._pnl)
self._cumpnl.columns = pnl_cols
self._cumportfolio = tsc.create_mult_index(self._portfolio)
self._cumportfolio.columns = ['Port']