本文整理汇总了Python中pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs类的典型用法代码示例。如果您正苦于以下问题:Python TimeSeriesCalcs类的具体用法?Python TimeSeriesCalcs怎么用?Python TimeSeriesCalcs使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TimeSeriesCalcs类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_strategy_returns_stats
def run_strategy_returns_stats(self, strategy):
"""
run_strategy_returns_stats - Plots useful statistics for the trading strategy (using PyFolio)
Parameters
----------
strategy : StrategyTemplate
defining trading strategy
"""
pnl = strategy.get_strategy_pnl()
tz = TimeSeriesTimezone()
tsc = TimeSeriesCalcs()
# PyFolio assumes UTC time based DataFrames (so force this localisation)
try:
pnl = tz.localise_index_as_UTC(pnl)
except: pass
# TODO for intraday strategy make daily
# convert DataFrame (assumed to have only one column) to Series
pnl = tsc.calculate_returns(pnl)
pnl = pnl[pnl.columns[0]]
fig = pf.create_returns_tear_sheet(pnl, return_fig=True)
try:
plt.savefig (strategy.DUMP_PATH + "stats.png")
except: pass
plt.show()
示例2: calculate_ret_stats
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
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
示例4: calculate_vol_adjusted_returns
def calculate_vol_adjusted_returns(self, returns_df, br, returns = True):
"""
calculate_vol_adjusted_returns - Adjusts returns for a vol target
Parameters
----------
br : BacktestRequest
Parameters for the backtest specifying start date, finish data, transaction costs etc.
returns_a_df : pandas.DataFrame
Asset returns to be traded
Returns
-------
pandas.DataFrame
"""
tsc = TimeSeriesCalcs()
if not returns: returns_df = tsc.calculate_returns(returns_df)
if not(hasattr(br, 'portfolio_vol_resample_type')):
br.portfolio_vol_resample_type = 'mean'
leverage_df = self.calculate_leverage_factor(returns_df,
br.portfolio_vol_target, br.portfolio_vol_max_leverage,
br.portfolio_vol_periods, br.portfolio_vol_obs_in_year,
br.portfolio_vol_rebalance_freq, br.portfolio_vol_resample_freq,
br.portfolio_vol_resample_type)
vol_returns_df = tsc.calculate_signal_returns_with_tc_matrix(leverage_df, returns_df, tc = br.spot_tc_bp)
vol_returns_df.columns = returns_df.columns
return vol_returns_df, leverage_df
示例5: bus_day_of_month_seasonality
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
示例6: time_of_day_seasonality
def time_of_day_seasonality(self, data_frame, years=False):
tsc = TimeSeriesCalcs()
if years is False:
return tsc.average_by_hour_min_of_day_pretty_output(data_frame)
set_year = set(data_frame.index.year)
year = sorted(list(set_year))
intraday_seasonality = None
commonman = CommonMan()
for i in year:
temp_seasonality = tsc.average_by_hour_min_of_day_pretty_output(data_frame[data_frame.index.year == i])
temp_seasonality.columns = commonman.postfix_list(temp_seasonality.columns.values, " " + str(i))
if intraday_seasonality is None:
intraday_seasonality = temp_seasonality
else:
intraday_seasonality = intraday_seasonality.join(temp_seasonality)
return intraday_seasonality
示例7: compare_strategy_vs_benchmark
def compare_strategy_vs_benchmark(self, br, strategy_df, benchmark_df):
"""
compare_strategy_vs_benchmark - Compares the trading strategy we are backtesting against a benchmark
Parameters
----------
br : BacktestRequest
Parameters for backtest such as start and finish dates
strategy_df : pandas.DataFrame
Strategy time series
benchmark_df : pandas.DataFrame
Benchmark time series
"""
include_benchmark = False
calc_stats = False
if hasattr(br, 'include_benchmark'): include_benchmark = br.include_benchmark
if hasattr(br, 'calc_stats'): calc_stats = br.calc_stats
if include_benchmark:
tsd = TimeSeriesDesc()
cash_backtest = CashBacktest()
ts_filter = TimeSeriesFilter()
ts_calcs = TimeSeriesCalcs()
# align strategy time series with that of benchmark
strategy_df, benchmark_df = strategy_df.align(benchmark_df, join='left', axis = 0)
# if necessary apply vol target to benchmark (to make it comparable with strategy)
if hasattr(br, 'portfolio_vol_adjust'):
if br.portfolio_vol_adjust is True:
benchmark_df = cash_backtest.calculate_vol_adjusted_index_from_prices(benchmark_df, br = br)
# only calculate return statistics if this has been specified (note when different frequencies of data
# might underrepresent vol
if calc_stats:
benchmark_df = benchmark_df.fillna(method='ffill')
tsd.calculate_ret_stats_from_prices(benchmark_df, br.ann_factor)
benchmark_df.columns = tsd.summary()
# realign strategy & benchmark
strategy_benchmark_df = strategy_df.join(benchmark_df, how='inner')
strategy_benchmark_df = strategy_benchmark_df.fillna(method='ffill')
strategy_benchmark_df = ts_filter.filter_time_series_by_date(br.plot_start, br.finish_date, strategy_benchmark_df)
strategy_benchmark_df = ts_calcs.create_mult_index_from_prices(strategy_benchmark_df)
self._benchmark_pnl = benchmark_df
self._benchmark_tsd = tsd
return strategy_benchmark_df
return strategy_df
示例8: calculate_leverage_factor
def calculate_leverage_factor(self, returns_df, vol_target, vol_max_leverage, vol_periods = 60, vol_obs_in_year = 252,
vol_rebalance_freq = 'BM', returns = True, period_shift = 0):
"""
calculate_leverage_factor - Calculates the time series of leverage for a specified vol target
Parameters
----------
returns_df : DataFrame
Asset returns
vol_target : float
vol target for assets
vol_max_leverage : float
maximum leverage allowed
vol_periods : int
number of periods to calculate volatility
vol_obs_in_year : int
number of observations in the year
vol_rebalance_freq : str
how often to rebalance
returns : boolean
is this returns time series or prices?
period_shift : int
should we delay the signal by a number of periods?
Returns
-------
pandas.Dataframe
"""
tsc = TimeSeriesCalcs()
if not returns: returns_df = tsc.calculate_returns(returns_df)
roll_vol_df = tsc.rolling_volatility(returns_df,
periods = vol_periods, obs_in_year = vol_obs_in_year).shift(period_shift)
# calculate the leverage as function of vol target (with max lev constraint)
lev_df = vol_target / roll_vol_df
lev_df[lev_df > vol_max_leverage] = vol_max_leverage
# only allow the leverage change at resampling frequency (eg. monthly 'BM')
lev_df = lev_df.resample(vol_rebalance_freq)
returns_df, lev_df = returns_df.align(lev_df, join='left', axis = 0)
lev_df = lev_df.fillna(method='ffill')
return lev_df
示例9: calculate_ret_stats
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()
示例10: get_pnl_trades
def get_pnl_trades(self):
"""
get_pnl_trades - Gets P&L of each individual trade per signal
Returns
-------
pandas.Dataframe
"""
if self._pnl_trades is None:
tsc = TimeSeriesCalcs()
self._pnl_trades = tsc.calculate_individual_trade_gains(self._signal, self._pnl)
return self._pnl_trades
示例11: run_day_of_month_analysis
def run_day_of_month_analysis(self, strat):
from pythalesians.economics.seasonality.seasonality import Seasonality
from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs
tsc = TimeSeriesCalcs()
seas = Seasonality()
strat.construct_strategy()
pnl = strat.get_strategy_pnl()
# get seasonality by day of the month
pnl = pnl.resample('B').mean()
rets = tsc.calculate_returns(pnl)
bus_day = seas.bus_day_of_month_seasonality(rets, add_average = True)
# get seasonality by month
pnl = pnl.resample('BM').mean()
rets = tsc.calculate_returns(pnl)
month = seas.monthly_seasonality(rets)
self.logger.info("About to plot seasonality...")
gp = GraphProperties()
pf = PlotFactory()
# Plotting spot over day of month/month of year
gp.color = 'Blues'
gp.scale_factor = self.SCALE_FACTOR
gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality day of month.png'
gp.html_file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality day of month.html'
gp.title = strat.FINAL_STRATEGY + ' day of month seasonality'
gp.display_legend = False
gp.color_2_series = [bus_day.columns[-1]]
gp.color_2 = ['red'] # red, pink
gp.linewidth_2 = 4
gp.linewidth_2_series = [bus_day.columns[-1]]
gp.y_axis_2_series = [bus_day.columns[-1]]
pf.plot_line_graph(bus_day, adapter = self.DEFAULT_PLOT_ENGINE, gp = gp)
gp = GraphProperties()
gp.scale_factor = self.SCALE_FACTOR
gp.file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality month of year.png'
gp.html_file_output = self.DUMP_PATH + strat.FINAL_STRATEGY + ' seasonality month of year.html'
gp.title = strat.FINAL_STRATEGY + ' month of year seasonality'
pf.plot_line_graph(month, adapter = self.DEFAULT_PLOT_ENGINE, gp = gp)
return month
示例12: calculate_ret_stats_from_prices
def calculate_ret_stats_from_prices(self, prices_df, ann_factor):
"""
calculate_ret_stats_from_prices - Calculates return statistics for an asset's price
Parameters
----------
prices_df : DataFrame
asset prices
ann_factor : int
annualisation factor to use on return statistics
Returns
-------
DataFrame
"""
tsc = TimeSeriesCalcs()
self.calculate_ret_stats(tsc.calculate_returns(prices_df), ann_factor)
示例13: run_strategy_returns_stats
def run_strategy_returns_stats(self, strategy):
"""
run_strategy_returns_stats - Plots useful statistics for the trading strategy (using PyFolio)
Parameters
----------
strategy : StrategyTemplate
defining trading strategy
"""
pnl = strategy.get_strategy_pnl()
tz = TimeSeriesTimezone()
tsc = TimeSeriesCalcs()
# PyFolio assumes UTC time based DataFrames (so force this localisation)
try:
pnl = tz.localise_index_as_UTC(pnl)
except: pass
# set the matplotlib style sheet & defaults
# at present this only works in Matplotlib engine
try:
matplotlib.rcdefaults()
plt.style.use(GraphicsConstants().plotfactory_pythalesians_style_sheet['pythalesians-pyfolio'])
except: pass
# TODO for intraday strategies, make daily
# convert DataFrame (assumed to have only one column) to Series
pnl = tsc.calculate_returns(pnl)
pnl = pnl.dropna()
pnl = pnl[pnl.columns[0]]
fig = pf.create_returns_tear_sheet(pnl, return_fig=True)
try:
plt.savefig (strategy.DUMP_PATH + "stats.png")
except: pass
plt.show()
示例14: calculate_vol_adjusted_index_from_prices
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)
示例15: g10_line_plot_gdp
def g10_line_plot_gdp(self, start_date, finish_date):
today_root = datetime.date.today().strftime("%Y%m%d") + " "
country_group = 'g10-ez'
gdp = self.get_GDP_QoQ(start_date, finish_date, country_group)
from pythalesians_graphics.graphs import PlotFactory
from pythalesians_graphics.graphs.graphproperties import GraphProperties
gp = GraphProperties()
pf = PlotFactory()
gp.title = "G10 GDP"
gp.units = 'Rebased'
gp.scale_factor = Constants.plotfactory_scale_factor
gp.file_output = today_root + 'G10 UNE ' + str(gp.scale_factor) + '.png'
gdp.columns = [x.split('-')[0] for x in gdp.columns]
gp.linewidth_2 = 3
gp.linewidth_2_series = ['United Kingdom']
from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs
tsc = TimeSeriesCalcs()
gdp = gdp / 100
gdp = tsc.create_mult_index_from_prices(gdp)
pf.plot_generic_graph(gdp, type = 'line', adapter = 'pythalesians', gp = gp)