Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
variable as record series name

Hi,

If I set a universe and would like to plot each stock in the universe, how can I do this?
This doesn't work. Is there any other way?

    for stock in data:  
        record(str(stock)=data[stock].price)  

Thanks!

7 responses

Hi Noam,

Try the code below - it should work to track the universe. It records the 20 day moving average for the 98 - 100% universe of stocks based on trading volume.

def initialize(context):  
   set_universe(universe.DollarVolumeUniverse(98, 100))

def handle_data(context, data):  
    for all_stocks in data:  
        record(position = data[all_stocks].mavg(20))  
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.

Noam, if you want a series per stock in the universe, you'll need to modify your Python syntax a bit, to pass in the name dynamically:

for stock in data:  
    record(**{str(stock): data[stock].price})  

I hope that helps!

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.

Thanks all for the replies!
Still new to python, can you explain what it means? Are you dereferencing like in c?

Thanks all for the replies!
Still new to python, can you explain what it means? Are you dereferencing like in c?

This is keyword argument unpacking, which provides keyword arguments to the function call via the dictionary's contents. So record(**{'stock1': 10.0, 'stock2': 20.0}) has the same effect as record(stock1=10.0, stock2=20.0).

The Python docs just refer to it as the **-operator, and it's defined by a Python syntax rule: "If the syntax **expression appears in the function call, expression must evaluate to a mapping, the contents of which are treated as additional keyword arguments."

Unfortunately, record() is limited to 5 datasets at a time. You'll need to slim down which ones you're graphing.

I learned some Python today!

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.

You can now pass variables as a series name by using positional arguments!

For example, you can now do something like this:

def initialize(context):  
    context.stocks = [sid(24), sid(300), sid(2)] 

def handle_data(context, data):  
    # You can pass a string variable into record().  
    # Here we record the price of all the stocks in our universe.  
    for stock in data:  
      price = data[stock].price  
      record(stock, price)

    # You can also pass in a variable with a string value.  
    # This records the high and low values for Apple.  
    fields = ['high', 'low']  
    for field in fields:  
      record(field, data[sid(24)][field])  

For more information, take a look at the record section of the help doc.