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

How would i substitute Q500US in this code?

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  
from quantopian.pipeline.filters.morningstar import Q500US  
attach_pipeline  
import re  
import datetime as dt   # Surprising that datetime doesn't have timezone  
import pandas as pd  
from pytz import timezone  
from zipline.utils import tradingcalendar  




def initialize(context):  
    schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=1))  
    attach_pipeline(make_pipeline(), 'my_pipeline')  


    context.aapl = sid(24)  
    context.spy = sid(8554)  
    context.googl = sid(46631)  
    context.agn = sid(205)  
    context.infn = sid(33979)  


def handle_data(context,data):  
    stocks = symbols('AAPL','GOOG','AGN','INFN','prx','m', 'a','aa','aac','aan','aat', 'aap','aav','ab','abb','l','lad','ladr')

    for stock in stocks:  
        if get_open_orders(stock): return  
        hist= data.history(stock, 'price', 30, '1d')  
        if  hist[-1] < hist[0:-2].min() and data.can_trade(stock) and context.account.leverage<=2.8:  
            order_target_percent(stock, 0.5)  
        elif hist[-1] >= hist[0:-2].max()  and data.can_trade(stock):  
            order_target(stock, 0)

    record(leverage=context.account.leverage)  




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  
    """  
    # Base universe set to the Q500US  
    base_universe = Q500US()

    # Factor of yesterday's close price.  
    yesterday_close = USEquityPricing.close.latest  
    pipe = Pipeline(  
        screen = base_universe,  
        columns = {  
            'close': yesterday_close,  
        }  
    )  
    return pipe  
def before_trading_start(context, data):  
    results = pipeline_output('my_pipeline')  
    print results.head(5)  
    """  
    Called every day before market open.  
    """  
    #I MAY NEED THIS context.output = pipeline_output('my_pipeline')  
    # These are the securities that we are interested in trading each day.  
    #COULD NEED THIS 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



    """  
    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] )  
13 responses

The next step is to do the computation you do (what I call model) in the Pipeline. now that is where I can't do half of my models because it Im still learning how to do CustomFactors and stuff.

for stock in stocks:
if get_open_orders(stock): return
hist= data.history(stock, 'price', 30, '1d')
if hist[-1] < hist[0:-2].min() and data.can_trade(stock) and context.account.leverage<=2.8:
order_target_percent(stock, 0.5)
elif hist[-1] >= hist[0:-2].max() and data.can_trade(stock):
order_target(stock, 0)

Can anyone else walk me through it?

I've made some changes and documented them in your algorithm. The backtest is attached. Have it using the Q500US rather than a fixed set of securities. Also moved the logic to a scheduled function rather than using the 'handle_data' method.

Note that your ordering logic needs work. I wasn't sure what you were trying to accomplish there.

Hope that helps a bit.

wow that looks really weird, thanks

what about that leverage though ?

heyo, thanks, but i want to keep Q500, can that be integrated?

using Dan's?

Dan,
This is a great example, thank you so much for posting this!

Thank you for sharing, if anybody knows how to pick up historical stocks within the pipeline? like from this example - last_close -1, last_close -2, last_close -3

All the built in pipeline factors return the previous days data (eg yesterdays open or close price). However, one can easily get earlier historical data with a custom factor. Here is an example custom factor for getting historical open prices.

# Import the CustomFactor class  
from quantopian.pipeline import CustomFactor


class PreviousOpen(CustomFactor):  
    # Define inputs  
    inputs = [USEquityPricing.open]  


    # Set window_length to whatever number of days to lookback  
    # The default is 2 days ago.  
    # This can be set/over-ridden as shown below:  
    # my_open_10 = PreviousOpen(window_length = 10)  
    # would return the open 10 days ago  
    window_length = 2  


    def compute(self, today, assets, out, open):  
        out[:] = open[0]

To get further back historical prices simply change the window length. To get different data (eg the close price) then change the input BoundColumn (ie the data). Once defined, use the custom factor just like any other built in factor.

Thank you, Dan! To make sure I am understanding this correctly.
To find historical 5 days I would need to write the following?

# Import the CustomFactor class  
from quantopian.pipeline import CustomFactor


class PreviousOpen(CustomFactor):  
    # Define inputs  
    inputs = [USEquityPricing.close]  


    # Set window_length to whatever number of days to lookback  
    # The default is 2 days ago.  
    # This can be set/over-ridden as shown below:  
    # my_open_10 = PreviousOpen(window_length = 10)  
    # would return the open 10 days ago  
    window_length = 5 

    def compute_1(self, today, assets, out, close):  
        out[:] = close[0]  
   def compute_2(self, today, assets, out, close):  
        out[:] = close[-1]  
def compute_3(self, today, assets, out, close):  
        out[:] = close[-3]  
   def compute_4(self, today, assets, out, close):  
        out[:] = close[-4]  
   def compute_5(self, today, assets, out, close):  
        out[:] = close[-5]

What you have won't exactly work. You are trying to define multiple outputs for the custom factor. This IS possible but the syntax is a little different if so. However, no need to do that. Simply create 5 instances of the same factor giving each one a different window length. Using the custom factor in my post, just instantiate it 5 times in the pipeline definition like this.


# Create an asset that we want to get data for  
my_asset = symbols('SPY')

# Make a filter to get just spy data  
my_filter = StaticAssets([my_asset])

# Make our factors  
# Using a mask limits the computation to only the specified asset.  
close_yesterday = PreviousClose(window_length = 1, mask = my_filter)  
close_2_day_ago = PreviousClose(window_length = 2, mask = my_filter)  
close_3_day_ago = PreviousClose(window_length = 3, mask = my_filter)  
close_4_day_ago = PreviousClose(window_length = 4, mask = my_filter)  
close_5_day_ago = PreviousClose(window_length = 5, mask = my_filter) 


# Now create our pipeline object which does the heavy lifting to get the data we specify  
# Add a screen to just return specific assets. Otherwise all assets in the Q database are returned  
# Defining the pipeline is really just defining the columns we want in the returned dataframe and  
# limiting the rows (if desired) to a particular subset of assets.  
my_pipe = Pipeline(  
        screen = my_filter,  
        columns = {  
            'close_yesterday': close_yesterday,  
            'close_2_day_ago': close_2_day_ago,  
            'close_3_day_ago': close_3_day_ago,  
            'close_4_day_ago': close_4_day_ago,  
            'close_5_day_ago': close_5_day_ago,

        }  
    )

This works in both an algorithm and in a research notebook . Take a look at the attached notebook to see how the returned data matches up with data from a 'get_pricing fetch.

Thank you, Dan, it is exactly what I am looking for, just need to add several more conditions. I will loop back with results. Thank you!