Overall Statistics Total Trades 1 Average Win 0% Average Loss 0% Compounding Annual Return 2.109% Drawdown 0.900% Expectancy 0 Net Profit 0.689% Sharpe Ratio 1.145 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0.085 Beta -3.226 Annual Standard Deviation 0.018 Annual Variance 0 Information Ratio 0.057 Tracking Error 0.018 Treynor Ratio -0.007 Total Fees \$1.00
```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))
self.AddChart(IndicatorPlot)

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
currentStopLoss=price*Decimal(1-self.stopLossPct)

# 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:
self.stopLossLevel.append(currentStopLoss)

# 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 = []                        ```