Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to generate different context variables based on a list of SIDs ?

Hi,

I am trying to create different context variables based on SIDs in my context.securities variable. For example:

From context.securities = [sid(16841),sid(24), sid(23709)] I would like to automatically generate 3 variables context.amzn, context.aapl, context.nflx. How exactly can I do that ? I have tried different things but I can't actually generate a variable named context.correspondingsymbol.

Thank you

4 responses

Instead of a list, I suggest looking into a Python dict. Something along the lines of:

context.securities = {'amzn': sid(16841), 'aapl': sid(24), 'nflx': sid(23709)}  

I have tried that, but still with a for-loop I couldn't create the context variables context.amzn, context.aapl, context.nflx automatically. How can I do that ?

Hi Fernando -

I don't understand what you are trying to do. If you create the dictionary, I don't see the need for anything else. For example, you can just do:

context.securities['amzn']  

See:

https://docs.python.org/3/tutorial/datastructures.html

See also:

https://www.quantopian.com/help#ide-manual-asset-lookup