Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Buy and selling a list

Hey Quantopians,

I am building an algo, and I'm having 2 issues.

  1. In line 99 and 106, my value says that it is too close to zero and there is a runtime error, can someone show me how to fix that.

  2. I want to buy/sell/short a list of stocks, how do i set that up. Trying to buy/sell/short this list is included in the last two definitions of code.

Thanks!!

4 responses

The first problem (1) actually starts in line 89.

    # Don't use assign. This creates a dataframe. Just create a plain old scaler variable.  
    #context.output_spy = context.output.assign(spy_percent_change = spy_percent)  
    context.output_spy = spy_percent

The second issue (2). The easiest way to open and/or close a list of securities at one time is to use the 'order_optimal_portfolio' method (see the docs https://www.quantopian.com/help#optimize-api ).

I've made these changes to the algo and attached.

Good luck.

Awesome, thanks Dan!

And just to confirm, to short a stock, you would just have to make the weight negative not positive correct!

Yes, to short a stock give it a negative weight. I missed that in the algorithm above. Line 112 should be

weights = pd.Series(data=-weight, index=context.short_list)

Notice the negative sign before 'weight'. Good catch.

great!

Thank you