How do I exclude leveraged ETFs for any ETFs for that matter and ensure that pipeline returns common stock only?
How do I exclude leveraged ETFs for any ETFs for that matter and ensure that pipeline returns common stock only?
I think the best way is to filter on some fundamentals value (market cap or exchange?) and exclude NaNs, since ETFs don't have any fundamentals fields.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.
I have not looked into the matter yet (must do so - geared ETFs are a disaster in my testing elsewhere) but what about that list of geared ETFs in Zipline? Or is that not accessible in Quantopian?
(security_lists.leveraged_etf_list) Ah, so it is accessible. In which case you could assign leveraged ETFs a raking such that they will not feature in the top X.....presumably. Haven't tried it yet.
I saw someone else use this:
if (stock.symbol == 'SPY') or (stock.symbol in context.flagged_stocks) or (stock in security_lists.leveraged_etf_list):
continue
Is there a way to access stock symbol in the pipeline? I want to filter the leveraged ETF/ETN even before computing the percentage gain. Thanks!
def compute(self, today, assets, out, close):
out[:] = ((close[-1] - close[0]) / close[0]) * 100
Is there any answer to this? There already is security_lists.leveraged_etf_list so it should be easy to create a filter for leverage etfs, I just have no idea in what format the list is or how do I create a filter that forbids using these equities [at pipeline].
Any comments from Quantopian staff?
Never mind, I coded a custom factor that can be used as a filter. Here is the code in case someone else wants to exclude leveraged etfs
class not_in_leveraged_etf_list(CustomFactor):
inputs = []
window_length = 1
def compute(self, today, assets, out):
not_in_etf_list = []
for asset in assets:
if asset not in security_lists.leveraged_etf_list:
not_in_etf_list.append(1)
else:
not_in_etf_list.append(0)
out[:] = not_in_etf_list
You can use it as a filter with screen like this:
pipe.set_screen((not_in_leveraged_etf_list() != 0))
IMHO there should be a default filter for this as everyone who wants to control leverage needs this filter.
This is cool. I had been doing
pipeline_output('some_filter').drop(security_lists.leveraged_etf_list, errors='ignore')
I'm assuming pipeline should be faster (?), though I don't know the internals.
I found that there are other securities that I often want to exclude. Here is a more generic and maybe faster way (no loops) based off of @Mikko's.
def IsInSecList(sec_list):
'''Returns a factor indicating membership (=1) in the given iterable of securities'''
class IsInSecListFactor(CustomFactor):
inputs = []; window_length = 1
def compute(self, today, asset_ids, out): out[:] = asset_ids.isin(sec_list)
return IsInSecListFactor()
Example:
ave_dollar_volume = AverageDollarVolume(window_length=30)
not_lev = IsInSecList(security_lists.leveraged_etf_list) != 1
not_other_bad_securities = IsInSecList(custom_bad_list) != 1
pipe.set_screen(ave_dollar_volume.percentile_between(70, 100, mask=not_lev & not_other_bad_securities))
There is a CustomFilter class in zipline which will make this neater, but it doesn't seem to have made it to Quantopian yet.