Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
-1.04%
Compounding Annual Return
-4.066%
Drawdown
5.300%
Expectancy
-1
Net Profit
-1.039%
Sharpe Ratio
-0.347
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.009
Beta
-0.162
Annual Standard Deviation
0.102
Annual Variance
0.01
Information Ratio
-1.116
Tracking Error
0.178
Treynor Ratio
0.219
Total Fees
$2.00
namespace QuantConnect 
{
    public class TrailingStop : QCAlgorithm
    {
    	bool first = true;
    	OrderTicket _stopLoss;
    	const decimal _tailingPercent = 0.05m;
    	const decimal _stopLossFactor = 1 - _tailingPercent;
    	public override void Initialize() 
        {
        	SetStartDate(2014, 8, 1);         
            SetEndDate(2014, 11, 1);
            SetCash(25000);
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Hour);
            
            // plot SPY price
            PlotIndicator("SPY", Identity("SPY"));
        }
        public void OnData(TradeBars data) 
        {
        	if (first)
        	{
        		first = false;
        		var quantity = CalculateOrderQuantity("SPY", 1m);
        		MarketOrder("SPY", quantity);
        		_stopLoss = StopMarketOrder("SPY", -quantity, Securities["SPY"].Price*_stopLossFactor);
        		return;
        	}
        	
        	if (_stopLoss != null && !_stopLoss.Status.IsFill())
        	{
        		var currentStopLoss = _stopLoss.Get(OrderField.StopPrice);
        		var newStopLoss = Securities["SPY"].Price*_stopLossFactor;
        		if (newStopLoss > currentStopLoss)
        		{
        			_stopLoss.Update(new UpdateOrderFields {
        				StopPrice = newStopLoss	
        			});
        		}
				Plot("SPY", "Stop", _stopLoss.Get(OrderField.StopPrice));
        	}
        }
    }
}