Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to add current price to USEquityPricing.close ?

I want to calculate a custom factor with closing price along with current intraday prices.

Is there a way to append current day prices to USEquityPricing.close before or after calling the custom factor ?

def compute(self, today, assets, out, close ...  

or does this have to be done using data.history() ? The problem I see with data.history is that you have to pass the sid of all 8000+ US Equities .. how would I do that?

Thank you

3 responses

Pipeline only has access to yesterdays pricing and data (as of 'before_trading_start'), so no, you cannot incorporate the current price directly into a CustomFactor.

However, you can easily get the current prices of all securities returned by your pipe by using the 'data.current' method. It accepts a list as well as a single security. The index of the pipeline output is just such a list of securities. No need to get 'all 8000+' securities. I like to then store those current prices in the same dataframe as the other pipeline outputs so everything is in one neat place. Something like this...

def before_trading_start(context, data):  
    # Get a dataframe of our pipe data.  
    context.output = pipeline_output('my_pipe')  


def enter_buy_sell_orders(context, data):  
    # Get the current prices for all stocks returned by our pipe (ie context.output.index)  
    # Add those prices as a new column named 'current_price' in 'context.data'  
    context.output['current_price'] = data.current(context.output.index, 'price')

    # Do any logic you wish on any of the columns in the pipe dataframe output  
     stocks_to_buy = context.output.query('current_price < yesterday_close').head(TARGET_STOCKS).index  

See attached algorithm. Good luck.

Hi Dan,

Thanks for your response. That's too bad we cannot incorporate current price directly in CustomFactor. This sort of handicaps the pipeline/filters in live trading scenarios when needing to calculate real-time technical indicators?
What about doing lookups of current pricing and appending it to USEquityPricing.close before passing it as input to a CustomFactor ?

Pipelines are a great tool, but yes, they can't do everything. The single biggest reason for using pipeline is that it's optimized to actually 'pre-fetch' the data asynchronous to the algorithm execution. This GREATLY enhances speed. It's actually grabbing data for multiple days into the future each time it's called (to reduce the database calls). Therefore, you can't 'append' any 'realtime' data to a custom factor. It would also be nice to change a filter based upon current holdings, but no can do. The pipeline has already pre-fetched and pre-calculated the data. Maybe look at this post for some more insight https://www.quantopian.com/posts/custom-factor-calculation-over-iterating-help .

You can always use the data.current and data.history functions along with any of the TaLib functions for intraday calcs. Then simply add them to the pipeline output as in the above algorithm. Above I simply added a column for 'current_price'. This could just as easily been a column for '10_bar_rsi' gotten from the output of a TaLib function.