本文整理汇总了Python中pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs.calculate_individual_trade_gains方法的典型用法代码示例。如果您正苦于以下问题:Python TimeSeriesCalcs.calculate_individual_trade_gains方法的具体用法?Python TimeSeriesCalcs.calculate_individual_trade_gains怎么用?Python TimeSeriesCalcs.calculate_individual_trade_gains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs
的用法示例。
在下文中一共展示了TimeSeriesCalcs.calculate_individual_trade_gains方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_pnl_trades
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import calculate_individual_trade_gains [as 别名]
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
示例2: calculate_trading_PnL
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import calculate_individual_trade_gains [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])
# 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
_pnl_trades = tsc.calculate_individual_trade_gains(signal_df, _pnl)
# 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._pnl_trades = _pnl_trades
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
self._tsd_pnl = TimeSeriesDesc()
self._tsd_pnl.calculate_ret_stats(self._pnl, br.ann_factor)
#.........这里部分代码省略.........
示例3: TechIndicator
# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import calculate_individual_trade_gains [as 别名]
logger.info("Running backtest...")
# use technical indicator to create signals
# (we could obviously create whatever function we wanted for generating the signal dataframe)
tech_ind = TechIndicator()
tech_ind.create_tech_ind(spot_df, indicator, tech_params); signal_df = tech_ind.get_signal()
# use the same data for generating signals
cash_backtest.calculate_trading_PnL(br, asset_df, signal_df)
port = cash_backtest.get_cumportfolio()
port.columns = [indicator + ' = ' + str(tech_params.sma_period) + ' ' + str(cash_backtest.get_portfolio_pnl_desc()[0])]
signals = cash_backtest.get_porfolio_signal() # get final signals for each series
returns = cash_backtest.get_pnl() # get P&L for each series
time_series_calcs = TimeSeriesCalcs()
trade_returns = time_series_calcs.calculate_individual_trade_gains(signals, returns)
print(trade_returns)
# print the last positions (we could also save as CSV etc.)
print(signals.tail(1))
pf = PlotFactory()
gp = GraphProperties()
gp.title = "EUR/USD trend model"
gp.source = 'Thalesians/BBG (calc with PyThalesians Python library)'
gp.scale_factor = 1
gp.file_output = 'output_data/eurusd-trend-example.png'
pf.plot_line_graph(port, adapter = 'pythalesians', gp = gp)