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

I am having trouble figuring out how to rebalance my portfolio weights
I want it to work so if the portfolio is empty and I buy a stock then I want to use 100% of portfolio to buy the stock
But as soon as another stock is about to be purchased I want the new stock to be 50% of portfolio and the old stock to be the other 50%
How do I make that work??

4 responses

Why don't you try to divide the target percent by the number of positions?
order_target_percent(stock, 1.0 / len(context.XXXXX))

Thanks for the response! I tried that but it leaves my in a position for example if i have 10,000 my first stock holds 10,000 and then my second stock holds 5,000 so i am holding 15,000 worth of stock but only have 10,000

why don't you attach your code so that we can see where is the problem coming from? Are you properly selling?

That could be the problem here is my_rebalance code:

for stock in context.output.index:  
    if stock in context.up_down_to_low_long.index:  
        if not get_open_orders(stock):  
                order_target_percent(stock,.1)  

for stock in context.output.index:  
    if stock in context.up_down_to_low_short.index:  
        if not get_open_orders(stock):  
                order_target_percent(stock,-.1)  

for stock in context.output.index:  
    if stock in context.buy_longs_f.index:  
        if not get_open_orders(stock):  
                order_target_percent(stock,.1)  

for stock in context.portfolio.positions:  
    if context.portfolio.positions[stock].amount > 0:  
        if stock in context.negative_x.index:  
            if not get_open_orders(stock):  
                order_target_percent(stock,0)  

    if context.portfolio.positions[stock].amount < 0:  
        if stock in context.positive_x.index:  
            if not get_open_orders(stock):  
                order_target_percent(stock,0)