Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
List sort

Hi pros,

Just want to get an index pipeline in the same order that i put on the StaticAssets() list symbols.

I have been trying with sort(), sorted() and sort_index() but no luck.

What im looking for, is to grab these results in the same order in order to include them in a spreadsheet which has data in that order.

Encosed an example notebook.

Any clue?

I appreciate your help.

Thanks in advance.

Ramon.-

2 responses

The ubiquitous loc method to the rescue. The loc method is typically used to simply select specific rows of a dataframe. However, it also has the feature that it returns those rows in the same order they were selected. So, something like this should work.

# First create a list of stock tickers we want.  
# This list can be in any order.  
my_stock_list = ['APD','ILMN','WMT','MCD','GIS','DLTR','KHC','ALB','ETFC','ABBV','AMGN','SEE']

# Fetch the asset objects associated with those tickers.  
# The assets will stay in the same order as the list.  
my_stocks = symbols(my_stock_list)

# Define a pipeline  
def make_pipeline():  
    close_price = USEquityPricing.close.latest

    # Create a universe based on the stock list  
    universe = StaticAssets(my_stocks)  
    return Pipeline(columns={'Close': close_price}, screen=universe)

# Run the pipeline  
pipeline_output = run_pipeline(make_pipeline(), start_date='2020-07-24', end_date='2020-07-24')

# Probably just want the single date? Use the xs method.  
# To sort them in the same order as the original stock list use the `loc` method.  
# The `loc` method selects specific stocks and also returns them in the same order they are selected.  
sorted_output = pipeline_output.xs('2020-07-24').loc[my_stocks]

See the attached notebook. Hope that's what you were looking for.

Disclaimer

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.

Hi Dan, thanks again for your very very very kind support even on Sunday!. You are the man! Thanks again!.