Overall Statistics
Total Trades
49
Average Win
0.06%
Average Loss
-0.03%
Compounding Annual Return
0.273%
Drawdown
2.900%
Expectancy
1.791
Net Profit
0.981%
Sharpe Ratio
0.161
Probabilistic Sharpe Ratio
5.729%
Loss Rate
19%
Win Rate
81%
Profit-Loss Ratio
2.43
Alpha
-0.008
Beta
0.057
Annual Standard Deviation
0.018
Annual Variance
0
Information Ratio
-0.933
Tracking Error
0.204
Treynor Ratio
0.05
Total Fees
$49.00
Estimated Strategy Capacity
$2500000.00
Lowest Capacity Asset
IBM R735QTJ8XC9X
/*
In this algo we place a market order on IBM if we have not invested 
after placing the market order we put a limit order to take 1% of profit(TP1) for half our holding 
and another limit order with 2% profit(TP2) if TP1 is reached before TP2 we place a stop market order a
*/
using System.Collections.Generic;
using QuantConnect.Data;
using QuantConnect.Interfaces;

namespace QuantConnect
{
    public class VirtualApricotGalago : QCAlgorithm
    {
		private Symbol _symbol;
    	private OrderTicket _limitTicket1;
    	private OrderTicket _limitTicket2;
    	private OrderTicket _stopOrderTicket;

        public override void Initialize()
        {
            SetStartDate(2017, 12, 1);
            SetCash(100000);
            // Use Minute resolution because of 1% are common in low resolution 
            _symbol = AddEquity("IBM", Resolution.Minute).Symbol;
        }
        
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
        	if (orderEvent.Status != OrderStatus.Filled) return;
        	
        	// If limit order 1 is filled, create stop market
        	if (_limitTicket1 != null && orderEvent.OrderId == _limitTicket1.OrderId)
        	{
        		// Set the stop price to the current bar low to avoid being filled immediately
        		var bar = Securities[orderEvent.Symbol].GetLastData() as TradeBar;
        		var stopPrice = Math.Min(orderEvent.FillPrice, bar.Low) - 0.01m;
	        	_stopOrderTicket = StopMarketOrder(_symbol, -50, stopPrice);
	        	_limitTicket1 = null;
	        	// Comment out to avoid consuming log quota
	        	//Debug($"1nd limit order filled. Place stop loss order at {stopPrice}");
        	}
        	
        	// If limit order 2 is filled, cancel stop market order
        	if (_limitTicket2 != null && orderEvent.OrderId == _limitTicket2.OrderId)
        	{ 
	        	_stopOrderTicket.Cancel();
	        	_limitTicket2 = null;
	        	_stopOrderTicket = null;
	        	// Comment out to avoid consuming log quota
	        	//Debug("2nd limit order filled. Cancel stop loss order.");
        	}

        	// If stop market order is filled, cancel limit order 2
        	if(_stopOrderTicket != null && orderEvent.OrderId == _stopOrderTicket.OrderId)
        	{
        		_limitTicket2.Cancel();
        		// Comment out to avoid consuming log quota
	        	//Debug("Stop loss filled. Cancel the 2nd limit order");
        	}
        }

        public override void OnData(Slice data)
        {
            if (!Portfolio.Invested)
            {
	        	var close = Securities[_symbol].Close;
            	// Place market order and limit orders at the same time
                MarketOrder(_symbol, 100);
                _limitTicket1 = LimitOrder(_symbol, -50, close * 1.01m); // TP1 which 1% profit
                _limitTicket2 = LimitOrder(_symbol, -50, close * 1.02m); // TP2 which 2% profit
                // Comment out to avoid consuming log quota
	        	//Debug("");
            }
        }
    }
}