Overall Statistics
Total Trades
48
Average Win
4.82%
Average Loss
-1.40%
Compounding Annual Return
3.571%
Drawdown
13.100%
Expectancy
0.735
Net Profit
14.742%
Sharpe Ratio
0.442
Loss Rate
61%
Win Rate
39%
Profit-Loss Ratio
3.43
Alpha
-0.029
Beta
0.492
Annual Standard Deviation
0.087
Annual Variance
0.008
Information Ratio
-1.104
Tracking Error
0.089
Treynor Ratio
0.078
Total Fees
$48.00
namespace QuantConnect 
{   
    public class ThreeWayOrderAlgorithm : QCAlgorithm
    {
    	private Symbol _symbol;
    	private int _entryOrderId;
    	
        public override void Initialize() 
        {
		    SetStartDate(2013, 1, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            SetCash(25000);
            
            var security = AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            _symbol = security.Symbol;
        }

        public void OnData(TradeBars data) 
        {   
            if (!Portfolio.HoldStock) 
            {
            	var quantity = 100;
            	var currentPrice = data[_symbol].Price;
            	var stopPrice = currentPrice * 0.98m;
                var takeProfitPrice = currentPrice * 1.10m;
            	
                var entryOrder = MarketOrder(_symbol, quantity);
                
                // Using the Entry order Id number to relate orders
                _entryOrderId = entryOrder.OrderId;
                StopMarketOrder(_symbol, -quantity, stopPrice, _entryOrderId.ToString());
                LimitOrder(_symbol, -quantity, takeProfitPrice, _entryOrderId.ToString());
            }
        }
        
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
        	if (!orderEvent.Status.IsFill())
        	{
        		return;
        	}
        	
        	if (orderEvent.OrderId != _entryOrderId)
        	{
        		// Get orders that we tagged with the entry order number
        		var tickets = Transactions
        		    .GetOrderTickets(x => x.Tag == _entryOrderId.ToString());
        		
        		foreach (var ticket in tickets)
        		{
        			// Cancel the open orders (should be just one)
        			if (ticket.Status.IsOpen())
                	{
                    	ticket.Cancel();
                	}
        		}
        	}
        	
        	Log(orderEvent.ToString());
        }
    }
}