Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Questions on USEquityPricing and pipeline

Hi All !

I have severall questions on USEquityPricing :

USEquityPricing exist to get close, but is it even for open, high, low, close, volume ?

USEquityPricing allows to get .last(), but is it possible to get for exemple : USEquityPricing.close[-2] (considering if i am right [-1] is the current close]

I had try to create a pipe in this way, but doesn't work, see notebook below.

Thanks

3 responses

Hi Remi,

I worked on a similar topic this week so happy to help. To get price X days earlier you will need to create a CustomFactor. Find an example below:

class Close_Price_2_Days_Ago(CustomFactor):  
    # A factor to return the close price 2 days ago (the day before yesterday)  
    inputs = [USEquityPricing.close]  
    window_length = 2  
    def compute(self, today, assets, out, close_price):  
        out[:] = close_price[0]  

I found the way to call historical prices in output out[:] a bit confusing at start but the logic is the same than in numpy arrays.
close[-1] is close of the latest row (so last data available). close[0] is first close in the selected array, so the oldest close in the array defined by the window_length parameter. If window_length = 20 it means that you have an array of 20 rows (=20 days)*more than 8000 columns (one per stock).
You can find numerous posts explaining this.

Regarding USEquityPricing , it is called a Dataset. Please have a look at the following Help page.

It is very clear. Think of USEquityPricing.close or USEquityPricing .open as numpy arrays with size (dates * assets).

Good luck!

To answer your first question, 'yes' the USEquityPricing dataset has open, high, low, close, and volume attributes (see https://www.quantopian.com/help#importing-datasets ). The answer to your second question regarding how to get data from previous days is one needs to write a simple custom factor.

class N_DaysAgo(CustomFactor):  
    '''  
    Returns the adjusted value of the input data.  
    The number of days to look back should be set using window_length=n  
    where n is the number of days to look back  
    '''  
    def compute(self, today, assets, out, close):  
        out[:] = close[0]

Check out these two posts which are on that topic https://www.quantopian.com/posts/price-of-security-n-days-ago-from-within-pipeline and https://www.quantopian.com/posts/get-lagged-output-of-pipeline-custom-factor . The latter post explains how to get the value of any factor (not just pricing) N days ago.

I've updated the notebook with a custom factor and attached it for reference. Note that using the method in the notebook will yield the adjusted price and volume (as of the pipeline date). Typically this is what one wants. However, the actual unadjusted price/volume can be fetched using the method in the second post above.

Good luck.

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 Cyril, Hi Dan !

Thanks for your reply and sorry to don't find your respective post.
Here my contribution, to make an exemple that you can attach at your post :)

Is it normal that the result is just on five stocks ?
I can answer myself at this question lol
Yes i think it's a restriction in notebook, because in my backtest, orders are not only passed on these showed assets.