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

Hi,

I have a rookie question on the use of the dictionary (for data especially), as I am getting up to speed with Python language.

Let's say I defined my universe of stocks in my algorithm (via set_universe or by adding some stocks manually using sids.

Is there a difference between the following lines of codes?

1)
''' for s in data:
order(s,10)
'''

2)
''' context.sids = data.keys()
for s in context.sids
order(s,10)
'''

In other words, when using a dictionary, do we need to specify that the variable we are using is a key or is it automatic when iterating?

My second question, related to set_universe, is how to exclude some stocks from the set_universe.
For example the 98/100 universe would include VXX etf, which arguably I would not treat like other stocks etf. Can I remove specific side from my universe?

Thanks for your help
Vincent

1 response

Hello Vincent,

Here's some code for you to explore. Looks like #1 & #2 above are equivalent, so you don't need the .keys(). Removing a specific sid is straightforward.

Grant

def initialize(context):  
    context.stocks = [sid(24),sid(8554),sid(33652)]

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    for s in data:  
        order(s,100)  
    for s in data.keys():  
        order(s,100)  
    # http://stackoverflow.com/questions/4915920/how-to-delete-an-item-in-a-list-if-it-exists-python  
    aapl = sid(24)  
    while aapl in context.stocks:  
        context.stocks.remove(aapl)  
    for s in context.stocks:  
        order(s,50)