Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
A Little Python Help?

So, I am new to quant analysis and I have been busy going through algo's that users have posted. One such algo sets up the securities that will be traded in a static fashion. "context.sids = { sid(19662): 0.1, sid(0000): 0.2 }" etc. The algorithm then uses this dict to trade. I have been trying to change this to a dynamic stock selection. However, when I set up my pipeline and want to use the data within, this statement returns a list
"context.longs = pipeline_output('my_pipeline') " "context.sids = context.longs[context.longs['longs']]" I now have to convert the rest of the algorith to deal with a list and because I don't have a deep enough understanding of Python, I'm failing, miserably. My question is, how can I turn this list into a dict?

2 responses

I believe you will need one more piece of information. The original statement creates a dictionary where the stock ID (ie the sid) is the index and then associates a lookup value with each sid. In this case 0.1 and 0.2. Those lookup values maybe are weights?


context.sids = { sid(19662): 0.1, sid(0000): 0.2 }

In any case a dictionary needs an index and then an associated value. However, from what I can see all you have is the pipe index and a column named 'longs'. So, suppose you DO have a column in the pipeline which has the weights (or whatever the dictionary lookup needs to be). The syntax is easy

context.sids =context.longs['weight'].to_dict()

Replace 'weight' with a column name in your pipe. The dictionary will have the sids as the index and the values of the column as the lookup value.

You are correct, they are weights. I will have to add them to the pipe after the pipe has the dynamically selected securities. Thank you