Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Shorts happening on long_only() strategy

I am working on a long only algorithm and I'm running into a problem where it shorts a company by an insignificant amount of shares (although it shouldn't be shorting at all). I'm not sure why it is shorting when it should be ordering rather than selling. Below is the coportion of my code that keeps shorting. Where it errors at is the order_target_percent(stock, wgt) when a stock IS NOT IN my portfolio. Any help would be appreciated.

for stock in long_secs:  
        if (stock not in open_orders) & (stock in context.price_avg):  
            if (stock not in context.portfolio.positions):  
                order_target_percent(stock, wgt)  
            else:  
                if (data.current(stock, 'price') < float(context.price_avg[stock])):  
                    order_target_percent(stock, wgt)  
5 responses

two likely culprits..
1. Is wgt ever negative?
2. Are you ever ordering before a previous order has been completely filled?

It's easier to see what's going wrong of you post the entire script.

I will post the rebalance portion of my script but 1) No. 2) Shouldn't since I'm checking to see if stock isn't in open orders

def my_rebalance(context,data):  
    long_secs = context.output[context.output['rank']].index  
    if len(long_secs) > 0:  
        wgt = float(1) / float(len(long_secs))  
    else:  
        wgt = 0  
    open_orders = get_open_orders()  
    for stock in context.portfolio.positions:  
        if (stock not in long_secs) & (stock not in open_orders) & (float(context.price_avg[stock]) > context.portfolio.positions[stock].cost_basis * 1.15):  
                order_target_percent(stock, float(0))  
    for stock in long_secs:  
        if (stock not in open_orders) & (stock in context.price_avg):  
            if (stock not in context.portfolio.positions) & ((stock in context.price_avg) & (data.current(stock, 'price') < float(context.price_avg[stock]))):  
                if wgt > 0:  
                    order_target_percent(stock, wgt)  

With track_orders you'll be able to see what's happening. Otherwise some more info could be good here, how often does that function run, for example? What are you using to find out it is being shorted? Logging perhaps. I made some changes to be able to think about the code, also generally I think this is quite a bit easier to make changes and so forth and would ask whether functionally this is still the same. You could set a breakpoint on the ordering lines, right click, set a condition like s.symbol == 'XYZ' for that stock, and when it breaks in check the position .amount compared to the order etc.

def trade(context,data):  
    long_secs = context.output[context.output['rank']].index  
    if len(long_secs): wgt = 1.0 / len(long_secs)  
    else:              wgt = 0  
    pos = context.portfolio.positions  
    open_orders = get_open_orders()

    for s in pos:  
        if s in long_secs:   continue  
        if s in open_orders: continue  
        if context.price_avg[s] <= pos[s].cost_basis * 1.15: continue  
        order_target_percent(s, 0)

    for s in long_secs:  
        if wgt <= 0:                   continue  
        if s in pos:                   continue  
        if s in open_orders:           continue  
        if s not in context.price_avg: continue  
        if data.current(s, 'price') >= context.price_avg[s]: continue  
        order_target_percent(s, wgt)  

Here's a way to see the extent of any shorting and when any jumps occur by substituting shrts_value in the maximum intraday leverage code, and will slow things down however is temporary.

def handle_data(context, data):  
    if 'mx_shrt' not in context:  
        context.mx_shrt = 0               # Init this instead in initialize() for better efficiency  
    # flipping this negative to positive with minus sign  
    shrts_value = -sum([pos.amount * pos.last_sale_price for s, pos in context.portfolio.positions.items() if pos.amount < 0])  
    if shrts_value > context.mx_shrt:  
        context.mx_shrt = shrts_value  
        record(MxShrt = context.mx_shrt)    # Record maximum shorting value encountered