Overall Statistics
Total Trades
157
Average Win
5.71%
Average Loss
-2.14%
Compounding Annual Return
11.774%
Drawdown
19.900%
Expectancy
0.413
Net Profit
99.933%
Sharpe Ratio
0.775
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
2.67
Alpha
0.128
Beta
-0.041
Annual Standard Deviation
0.159
Annual Variance
0.025
Information Ratio
-0.013
Tracking Error
0.229
Treynor Ratio
-3.032
Total Fees
$153.44
namespace QuantConnect 
{   

    public class StopLossExample : QCAlgorithm
    {
    	
    	const decimal StopLossPercent = 0.02m;
    	const string Symbol = "SPY";
    	
    	private decimal highestPrice = 0.0m;
    	
    	private OrderTicket CurrentOrder;
    	private OrderTicket StopLoss;
    	
        public override void Initialize() 
        {
            SetStartDate(2010, 1, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            
            SetCash(25000);
            
            AddSecurity(SecurityType.Equity, Symbol, Resolution.Minute);
        }

        public void OnData(TradeBars data) 
        {   
            var currentPrice = data[Symbol].Close;
            // If no stock, enter position & set StopLoss
            if (!Portfolio.HoldStock) 
            {
                var quantity = (int)Math.Floor(Portfolio.Cash / currentPrice);
                CurrentOrder = Order(Symbol,  quantity);
                StopLoss = StopMarketOrder(Symbol, -quantity, currentPrice * (1m - StopLossPercent));
            }
            // If has stock, update StopLoss if necessary 
            else if (currentPrice > highestPrice)
            {
            	highestPrice = currentPrice;
            	StopLoss.Update(new UpdateOrderFields{ StopPrice = currentPrice * (1m - StopLossPercent) });
            }
        }
    }
}