Overall Statistics
import numpy as np
from decimal import *

class trailingStopLossHack(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2013,10, 7)  #Set Start Date
        self.SetEndDate(2014,2,3)    #Set End Date
        self.SetCash(10000)           #Set Strategy Cash
        self.AddEquity("SPY", Resolution.Daily)
        self.stopLossLevel = []
        # Set trailing stop percentage
        self.stopLossPct = 0.05
        ### Add plot
        IndicatorPlot = Chart("Trade Plot")
        IndicatorPlot.AddSeries(Series("Price", SeriesType.Line, 0))
        IndicatorPlot.AddSeries(Series("Trailing Stop-Loss", SeriesType.Line, 0))
    def OnData(self, data):
        if not data.ContainsKey("SPY"): return
        price = data["SPY"].Close

        # Buy SPY if not invested
        if not self.Portfolio.Invested:
            self.Buy("SPY", 10)

        if self.Portfolio.Invested:
            # Calculate current stop loss level
            # Check if current stop loss level exceeds all values in stopLossLevel list
            SL = [i for i in self.stopLossLevel if i >= currentStopLoss]
            # If current stop loss level exceeds all values in stopLossLevel list, then add value to list
            if len(SL)==0:
            # If current price is less than current stop loss level value, then liquidate
            if price<self.stopLossLevel[-1]:
                self.Sell("SPY", -10)

        ## Add values to Trade-Plot    
        if len(self.stopLossLevel)>0:
            self.Plot("Trade Plot", "Price", price)
            self.Plot("Trade Plot", "Trailing Stop-Loss", self.stopLossLevel[-1])

    # Reset stopLossLevel list on order fill
    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        if order.Status == OrderStatus.Filled:
                self.stopLossLevel = []