本文整理汇总了Python中pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs类的典型用法代码示例。如果您正苦于以下问题:Python TimeSeriesCalcs类的具体用法?Python TimeSeriesCalcs怎么用?Python TimeSeriesCalcs使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TimeSeriesCalcs类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
开发者ID:poeticcapybara,项目名称:pythalesians,代码行数:33,代码来源:tradeanalysis.py
示例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()
开发者ID:DDDDavid,项目名称:pythalesians,代码行数:27,代码来源:timeseriesdesc.py
示例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
开发者ID:humdings,项目名称:pythalesians,代码行数:31,代码来源:seasonality.py
示例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
开发者ID:Sahanduiuc,项目名称:pythalesians,代码行数:34,代码来源:cashbacktest.py
示例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
开发者ID:BryanFletcher,项目名称:pythalesians,代码行数:32,代码来源:seasonality.py
示例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
开发者ID:humdings,项目名称:pythalesians,代码行数:25,代码来源:seasonality.py
示例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
开发者ID:hedgefair,项目名称:pythalesians,代码行数:56,代码来源:strategytemplate.py
示例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
开发者ID:humdings,项目名称:pythalesians,代码行数:55,代码来源:cashbacktest.py
示例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()
开发者ID:quantcruncher,项目名称:pythalesians,代码行数:12,代码来源:timeseriesdesc.py
示例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
开发者ID:Sahanduiuc,项目名称:pythalesians,代码行数:14,代码来源:cashbacktest.py
示例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
开发者ID:neverspill,项目名称:pythalesians,代码行数:48,代码来源:tradeanalysis.py
示例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)
开发者ID:DDDDavid,项目名称:pythalesians,代码行数:18,代码来源:timeseriesdesc.py
示例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()
开发者ID:neverspill,项目名称:pythalesians,代码行数:40,代码来源:tradeanalysis.py
示例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)
开发者ID:Sahanduiuc,项目名称:pythalesians,代码行数:22,代码来源:cashbacktest.py
示例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)
开发者ID:NunoEdgarGub1,项目名称:pythalesians,代码行数:24,代码来源:commonecondatafactory.py
示例16: monthly_seasonality
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
开发者ID:BryanFletcher,项目名称:pythalesians,代码行数:24,代码来源:seasonality.py
示例17: Seasonality
# process data
from pythalesians.economics.seasonality.seasonality import Seasonality
from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs
# displaying data
from pythalesians.graphics.graphs.plotfactory import PlotFactory
from pythalesians.graphics.graphs.graphproperties import GraphProperties
# logging
from pythalesians.util.loggermanager import LoggerManager
import datetime
seasonality = Seasonality()
tsc = TimeSeriesCalcs()
logger = LoggerManager().getLogger(__name__)
pf = PlotFactory()
###### calculate seasonal moves in EUR/USD and GBP/USD (using Quandl data)
if True:
time_series_request = TimeSeriesRequest(
start_date = "01 Jan 1970", # start date
finish_date = datetime.date.today(), # finish date
freq = 'daily', # daily data
data_source = 'quandl', # use Quandl as data source
tickers = ['EURUSD', # ticker (Thalesians)
'GBPUSD'],
fields = ['close'], # which fields to download
vendor_tickers = ['FRED/DEXUSEU', 'FRED/DEXUSUK'], # ticker (Quandl)
开发者ID:humdings,项目名称:pythalesians,代码行数:30,代码来源:seasonality_examples.py
示例18: fetch_group_time_series
def fetch_group_time_series(self, time_series_request_list):
data_frame_agg = None
time_series_calcs = TimeSeriesCalcs()
# depends on the nature of operation as to whether we should use threading or multiprocessing library
if Constants().time_series_factory_thread_technique is "thread":
from multiprocessing.dummy import Pool
else:
# most of the time is spend waiting for Bloomberg to return, so can use threads rather than multiprocessing
# must use the multiprocessing_on_dill library otherwise can't pickle objects correctly
# note: currently not very stable
from multiprocessing_on_dill import Pool
thread_no = Constants().time_series_factory_thread_no['other']
if time_series_request_list[0].data_source in Constants().time_series_factory_thread_no:
thread_no = Constants().time_series_factory_thread_no[time_series_request_list[0].data_source]
pool = Pool(thread_no)
# open the market data downloads in their own threads and return the results
result = pool.map_async(self.fetch_single_time_series, time_series_request_list)
data_frame_group = result.get()
pool.close()
pool.join()
# data_frame_group = results.get()
# data_frame_group = results
# data_frame_group = None
# import multiprocessing as multiprocessing
# close the pool and wait for the work to finish
# processes = []
# for x in range(0, len(time_series_request_list)):
# time_series_request = time_series_request_list[x]
# processes = [multiprocessing.Process(target = self.fetch_single_time_series,
# args = (x)) for x in time_series_request_list]
# pool.apply_async(tsf.harvest_category, args = (category_desc, environment, freq,
# exclude_freq_cat, force_new_download_freq_cat, include_freq_cat))
# Run processes
# for p in processes: p.start()
# Exit the completed processes
# for p in processes: p.join()
# collect together all the time series
if data_frame_group is not None:
data_frame_group = [i for i in data_frame_group if i is not None]
if data_frame_group is not None:
data_frame_agg = time_series_calcs.pandas_outer_join(data_frame_group)
# for data_frame_single in data_frame_group:
# # if you call for returning multiple tickers, be careful with memory considerations!
# if data_frame_single is not None:
# if data_frame_agg is not None:
# data_frame_agg = data_frame_agg.join(data_frame_single, how='outer')
# else:
# data_frame_agg = data_frame_single
return data_frame_agg
开发者ID:BryanFletcher,项目名称:pythalesians,代码行数:68,代码来源:lighttimeseriesfactory.py
示例19: calculate_trading_PnL
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()
# 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
#.........这里部分代码省略.........
开发者ID:Sahanduiuc,项目名称:pythalesians,代码行数:101,代码来源:cashbacktest.py
示例20: create_tech_ind
def create_tech_ind(self, data_frame_non_nan, name, tech_params):
self._signal = None
data_frame = data_frame_non_nan.fillna(method="ffill")
if name == "SMA":
self._techind = pandas.rolling_mean(data_frame, tech_params.sma_period)
narray = numpy.where(data_frame > self._techind, 1, -1)
self._signal = pandas.DataFrame(index = data_frame.index, data = narray)
self._signal.columns = [x + " SMA Signal" for x in data_frame.columns.values]
self._techind.columns = [x + " SMA" for x in data_frame.columns.values]
elif name == "ROC":
tsc = TimeSeriesCalcs()
data_frame = tsc.calculate_returns(data_frame)
self._techind = pandas.rolling_mean(data_frame, tech_params.roc_period)
narray = numpy.where(self._techind > 0, 1, -1)
self._signal = pandas.DataFrame(index = data_frame.index, data = narray)
self._signal.columns = [x + " ROC Signal" for x in data_frame.columns.values]
self._techind.columns = [x + " ROC" for x in data_frame.columns.values]
elif name == "SMA2":
sma = pandas.rolling_mean(data_frame, tech_params.sma_period)
sma2 = pandas.rolling_mean(data_frame, tech_params.sma2_period)
narray = numpy.where(sma > sma2, 1, -1)
self._signal = pandas.DataFrame(index = data_frame.index, data = narray)
self._signal.columns = [x + " SMA2 Signal" for x in data_frame.columns.values]
sma.columns = [x + " SMA" for x in data_frame.columns.values]
sma2.columns = [x + " SMA2" for x in data_frame.columns.values]
self._techind = pandas.concat([sma, sma2], axis = 1)
elif name in ['RSI']:
# delta = data_frame.diff()
#
# dUp, dDown = delta.copy(), delta.copy()
# dUp[dUp < 0] = 0
# dDown[dDown > 0] = 0
#
# rolUp = pandas.rolling_mean(dUp, tech_params.rsi_period)
# rolDown = pandas.rolling_mean(dDown, tech_params.rsi_period).abs()
#
# rsi = rolUp / rolDown
# Get the difference in price from previous step
delta = data_frame.diff()
# Get rid of the first row, which is NaN since it did not have a previous
# row to calculate the differences
delta = delta[1:]
# Make the positive gains (up) and negative gains (down) Series
up, down = delta.copy(), delta.copy()
up[up < 0] = 0
down[down > 0] = 0
# Calculate the EWMA
roll_up1 = pandas.stats.moments.ewma(up, tech_params.rsi_period)
roll_down1 = pandas.stats.moments.ewma(down.abs(), tech_params.rsi_period)
# Calculate the RSI based on EWMA
RS1 = roll_up1 / roll_down1
RSI1 = 100.0 - (100.0 / (1.0 + RS1))
# Calculate the SMA
roll_up2 = pandas.rolling_mean(up, tech_params.rsi_period)
roll_down2 = pandas.rolling_mean(down.abs(), tech_params.rsi_period)
# Calculate the RSI based on SMA
RS2 = roll_up2 / roll_down2
RSI2 = 100.0 - (100.0 / (1.0 + RS2))
self._techind = RSI2
self._techind.columns = [x + " RSI" for x in data_frame.columns.values]
signal = data_frame.copy()
sells = (signal.shift(-1) < tech_params.rsi_lower) & (signal > tech_params.rsi_lower)
buys = (signal.shift(-1) > tech_params.rsi_upper) & (signal < tech_params.rsi_upper)
# print (buys[buys == True])
# buys
signal[buys] = 1
signal[sells] = -1
signal[~(buys | sells)] = numpy.nan
signal = signal.fillna(method = 'ffill')
self._signal = signal
self._signal.columns = [x + " RSI Signal" for x in data_frame.columns.values]
elif name in ["BB"]:
#.........这里部分代码省略.........
开发者ID:CARLOSMANZUETA2000,项目名称:pythalesians,代码行数:101,代码来源:techindicator.py
注:本文中的pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论