Overall Statistics
namespace QuantConnect.Algorithm.CSharp
{
    public class DualSellOrderTest : QCAlgorithm
    {
    	
    	private OrderTicket EntryOrder { get; set; }
    	private OneCancelsOtherTicketSet ProfitLossOrders { get; set; }

        public override void Initialize()
        {
            SetStartDate(2018, 8, 1);  //Set Start Date
            SetEndDate(2018, 9, 1);  //Set Start Date
            SetCash(100000);             //Set Strategy Cash
            
            SetCash(100000);             //Set Strategy Cash
            SetCash("BTC", 0);

            SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash);
            var btc = AddCrypto("BTCUSD", Resolution.Hour);
        }

        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// Slice object keyed by symbol containing the stock data
        public override void OnData(Slice data)
        {
			if (EntryOrder == null)
            {
                decimal quantity = (Portfolio.CashBook["USD"].Amount * .25M) / Securities["BTCUSD"].Price;
                quantity = Math.Round(quantity, 8);

                EntryOrder = MarketOrder("BTCUSD", quantity, false, "Entry");

                if (EntryOrder.Status == OrderStatus.Filled || EntryOrder.Status == OrderStatus.PartiallyFilled)
                {
                    var filledPrice = this.EntryOrder.AverageFillPrice;

                    ProfitLossOrders = new OneCancelsOtherTicketSet(
                                StopMarketOrder("BTCUSD", -quantity, Math.Round(filledPrice * (.97m), 2), "Stop Loss"),
                                LimitOrder("BTCUSD", -quantity, Math.Round(filledPrice * (1.03m), 2), "Profit Target")
                                );
                }
                else { EntryOrder = null; }
            }
        }
        
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
            // If we sold, remove stop loss or profit taking
            if (orderEvent.Direction == OrderDirection.Sell && (orderEvent.Status == OrderStatus.Filled || orderEvent.Status == OrderStatus.PartiallyFilled))
            {
                var sellOrder = Transactions.GetOrderTicket(orderEvent.OrderId);

                if (ProfitLossOrders != null)
                {
                    ProfitLossOrders.Filled();
                    ProfitLossOrders = null;
                    EntryOrder = null;
                }
            }
        }

    }
    
    public class OneCancelsOtherTicketSet
        {
            public OneCancelsOtherTicketSet(params OrderTicket[] orderTickets)
            {
                this.OrderTickets = orderTickets;
            }

            private OrderTicket[] OrderTickets { get; set; }

            public void Filled()
            {
                // Cancel all the outstanding tickets.
                foreach (var orderTicket in this.OrderTickets)
                {
                    if (orderTicket.Status == OrderStatus.Submitted)
                    {
                        orderTicket.Cancel();
                    }
                }
            }
        }
}