Overall Statistics
Total Trades
17
Average Win
0%
Average Loss
-3.98%
Compounding Annual Return
-98.156%
Drawdown
39.100%
Expectancy
-1
Net Profit
-29.536%
Sharpe Ratio
-2.931
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.205
Beta
-186.825
Annual Standard Deviation
0.783
Annual Variance
0.613
Information Ratio
-2.948
Tracking Error
0.783
Treynor Ratio
0.012
Total Fees
$749.33
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();
                    }
                }
            }
        }
}