-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slippage sweep #170
Closed
Closed
Slippage sweep #170
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
from . import utils | ||
from . import timeseries | ||
from . import pos | ||
from . import txn | ||
|
||
from .utils import APPROX_BDAYS_PER_MONTH | ||
|
||
|
@@ -1064,7 +1065,7 @@ def plot_turnover(returns, transactions, positions, | |
y_axis_formatter = FuncFormatter(utils.one_dec_places) | ||
ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter)) | ||
|
||
df_turnover = pos.get_turnover(transactions, positions) | ||
df_turnover = txn.get_turnover(transactions, positions) | ||
df_turnover_by_month = df_turnover.resample("M") | ||
df_turnover.plot(color='steelblue', alpha=1.0, lw=0.5, ax=ax, **kwargs) | ||
df_turnover_by_month.plot( | ||
|
@@ -1088,6 +1089,106 @@ def plot_turnover(returns, transactions, positions, | |
return ax | ||
|
||
|
||
def plot_slippage_sweep(returns, transactions, positions, | ||
slippage_params=(3, 8, 10, 12, 15, 20, 50), | ||
ax=None, **kwargs): | ||
"""Plots a equity curves at different per-dollar slippage assumptions. | ||
|
||
Parameters | ||
---------- | ||
returns : pd.Series | ||
Timeseries of portfolio returns to be adjusted for various | ||
degrees of slippage. | ||
transactions : pd.DataFrame | ||
Daily transaction volume and dollar ammount. | ||
- See full explanation in tears.create_full_tear_sheet. | ||
positions : pd.DataFrame | ||
Daily net position values. | ||
- See full explanation in tears.create_full_tear_sheet. | ||
slippage_params: tuple | ||
Slippage pameters to apply to the return time series (in | ||
basis points). | ||
ax : matplotlib.Axes, optional | ||
Axes upon which to plot. | ||
**kwargs, optional | ||
Passed to seaborn plotting function. | ||
|
||
Returns | ||
------- | ||
ax : matplotlib.Axes | ||
The axes that were plotted on. | ||
|
||
""" | ||
if ax is None: | ||
ax = plt.gca() | ||
|
||
turnover = txn.get_turnover(transactions, positions, | ||
period=None, average=False) | ||
|
||
slippage_sweep = pd.DataFrame() | ||
for bps in slippage_params: | ||
adj_returns = txn.adjust_returns_for_slippage(returns, turnover, bps) | ||
label = str(bps) + " bps" | ||
slippage_sweep[label] = timeseries.cum_returns(adj_returns, 1) | ||
|
||
slippage_sweep.plot(alpha=1.0, lw=0.5, ax=ax) | ||
|
||
ax.set_title('Cumulative Returns Given Additional Per-Dollar Slippage') | ||
ax.set_ylabel('') | ||
|
||
ax.legend(loc='center left') | ||
|
||
return ax | ||
|
||
|
||
def plot_slippage_sensitivity(returns, transactions, positions, | ||
ax=None, **kwargs): | ||
"""Plots curve relating per-dollar slippage to average annual returns. | ||
|
||
Parameters | ||
---------- | ||
returns : pd.Series | ||
Timeseries of portfolio returns to be adjusted for various | ||
degrees of slippage. | ||
transactions : pd.DataFrame | ||
Daily transaction volume and dollar ammount. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo ammount. |
||
- See full explanation in tears.create_full_tear_sheet. | ||
positions : pd.DataFrame | ||
Daily net position values. | ||
- See full explanation in tears.create_full_tear_sheet. | ||
ax : matplotlib.Axes, optional | ||
Axes upon which to plot. | ||
**kwargs, optional | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't seem to get passed. |
||
Passed to seaborn plotting function. | ||
|
||
Returns | ||
------- | ||
ax : matplotlib.Axes | ||
The axes that were plotted on. | ||
|
||
""" | ||
if ax is None: | ||
ax = plt.gca() | ||
|
||
turnover = txn.get_turnover(transactions, positions, | ||
period=None, average=False) | ||
avg_returns_given_slippage = pd.Series() | ||
for bps in range(1, 100): | ||
adj_returns = txn.adjust_returns_for_slippage(returns, turnover, bps) | ||
avg_returns = timeseries.annual_return( | ||
adj_returns, style='calendar') | ||
avg_returns_given_slippage.loc[bps] = avg_returns | ||
|
||
avg_returns_given_slippage.plot(alpha=1.0, lw=2, ax=ax) | ||
|
||
ax.set(title='Average Annual Returns Given Additional Per-Dollar Slippage', | ||
xticks=np.arange(0, 100, 10), | ||
ylabel='Average Annual Return', | ||
xlabel='Per-Dollar Slippage (bps)') | ||
|
||
return ax | ||
|
||
|
||
def plot_daily_turnover_hist(transactions, positions, | ||
ax=None, **kwargs): | ||
"""Plots a histogram of daily turnover rates. | ||
|
@@ -1114,7 +1215,7 @@ def plot_daily_turnover_hist(transactions, positions, | |
|
||
if ax is None: | ||
ax = plt.gca() | ||
turnover = pos.get_turnover(transactions, positions, period=None) | ||
turnover = txn.get_turnover(transactions, positions, period=None) | ||
sns.distplot(turnover, ax=ax, **kwargs) | ||
ax.set_title('Distribution of Daily Turnover Rates') | ||
ax.set_xlabel('Turnover Rate') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see these being passed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor formatting issue, the
positions
param needs to be unindented.