Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Multi Stock Equity Long Short Trailing Stop Loss

Hello! I am new to Quantopian and am having trouble trying to add a trailing stop loss to a multi stock equity long short portfolio, like the sample provided. I have tried a couple of things but I can't get anything to work! Any help would be much appreciated. Thank you

3 responses
   from collections import defaultdict  
    context.stops = defaultdict(lambda:0)  
   #call whenever  
    for s in context.portfolio.positions:  
                price = data.current(s, 'price')  
                context.stops[s] = max(context.stops[s], stop * price)  

this has worked for me, I think I got it from Anthony FJ Garner.
I hope this is what you are asking

Excellent thank you! I have attached the code, and I believe it works partially. I think it stops the Long stocks, but not the short.

Also does anyone know how to record or out put to a csv file the stocks bought/sold and the price and then the stocks that have been stopped out?

Thanks

Does anyone know how I can edit this to close out long positions? Thanks