Back to Community
Custom Factor including intraday data

Hello all,

I am trying to include both end of day and intraday data in a CustomFactor. Is there an example of this?

Even more simply, I am not clear on how any intraday data can get into the calculation. The sample below shows that creating a variable based on price_close = USEquityPricing.close.latest up in the make_pipeline() method does not get updated throughout the day.

Thanks,

Ted

Clone Algorithm
6
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
 
def initialize(context):
    """
    Called once at the start of the algorithm.
    """   
    # Rebalance every day, 1 hour after market open.
    schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=1))
     
    # Record tracking variables at the end of each day.
    schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())
     
    # Create our dynamic stock selector.
    attach_pipeline(make_pipeline(), 'my_pipeline')
         
def make_pipeline():
    """
    A function to create our dynamic stock selector (pipeline). Documentation on
    pipeline can be found here: https://www.quantopian.com/help#pipeline-title
    """
    
     
    # Create a dollar volume factor.
    dollar_volume = AverageDollarVolume(window_length=1)
 
    # Pick the top 1% of stocks ranked by dollar volume.
    high_dollar_volume = dollar_volume.percentile_between(99, 100)
     
    pipe = Pipeline(
        screen = high_dollar_volume,
        columns = {
            'dollar_volume': dollar_volume
        }
    )
    return pipe
 
def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = pipeline_output('my_pipeline')
  
    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index
     
def my_assign_weights(context, data):
    """
    Assign weights to securities that we want to order.
    """
    pass
 
def my_rebalance(context,data):
    """
    Execute orders according to our schedule_function() timing. 
    """
    pass
 
def my_record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    pass
 
def handle_data(context,data):
    """
    Called every minute.
    """
    pass
There was a runtime error.
6 responses

USEquityPricing.close.latest refers to the close price of the previous day - so it won't change throughout the current day.

If you want to get current pricing on a minute basis you should use data.current as referenced in this tutorial

Pipelines are meant to be computed on the order of once a day for security selection (the values in pipeline only uses daily data as recent as the previous trading day)

Hopes this helps!
Matt

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.

Matthew,

Thank you for your response. So Pipeline is designed to calculate factors once a day using historical daily data? OK. How do I calculate a factor using intraday data? Is there an example?

Best regards,

Ted

Yes, the pipeline is designed for calculating things once per day using historical daily data.

So - if you scheduled a function to run every 30 minutes and wanted to trade based on some information, you should use the data object.
A quick example of getting the most recent price of APPL as shown in the tutorial would be

data.current(sid(24), 'price')

Besides price, what other kinds of intraday data are you interested in computing?

Data would be average price performance and average volume over a window of several days. But I wanted to include the intraday price performance and volume within the factor.

I had hoped that something like:

price_history = data.history(context.securities, "price", bar_count=20, frequency="1d")  
volume_history = data.history(context.securities, "volume", bar_count=20, frequency="1d")

would get me access to the data I needed, for both historical days and current day. I was looking for an example of how to use that data in a screen at 3:50 pm ET.

You could generate some data within the pipeline, then do other filtering outside of the pipeline.

For example - you could do all your regular filtering based off historical data in the pipeline.

Then, when you order stocks (at a higher frequency) you could check a condition like

if data.current(sid(24), 'price') > x:  
    do something  

in the scheduled function

Is there an algorithm implementing that strategy -- either in the Quantopian examples or forum posts -- that you could point me towards?