I'm already aware of how to impliment a stop loss in Quanconnect. But i'd like a better way to impliment a trailing stop loss. 

One way I did it, which I don't prefer is having a function that counts the number of days a security is in my holdings and then using that number to decide how many days to look back in history I want to check to compare if the price has dropped x % less than the maximum price in history. 

 

For example:

 

The fucntion below keeps track of number of days in portfolio.

def position_check(self):
        for stock in self.symbols:
            shares_held = float(self.Portfolio[stock].Quantity)
            if shares_held:
                self.days_in_portfolio[stock]+=1
            else:
                self.days_in_portfolio[stock]=0

 

The funtion below checks to see if the current price is x % less than the price since I bought the security:

 

def stop_loss(self,stock):
        hist = self.History([stock],self.days_in_portfolio[stock],Resolution.Daily)
        curr_price = float(self.Portfolio[stock].Price)
        if 'close' not in hist:
            return 0
        prev_max =hist['close'].max()

trail_stop= ((curr_price - prev_max) /prev_max) <= -self.stop_loss_value

return trail_stop

 

Thanks for any help in advance. :)