Overall Statistics
Total Trades
42
Average Win
47.30%
Average Loss
0%
Compounding Annual Return
46.504%
Drawdown
32.400%
Expectancy
0
Net Profit
46.913%
Sharpe Ratio
1.404
Probabilistic Sharpe Ratio
53.640%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.101
Beta
1.24
Annual Standard Deviation
0.44
Annual Variance
0.194
Information Ratio
0.503
Tracking Error
0.4
Treynor Ratio
0.498
Total Fees
$42.00
Estimated Strategy Capacity
$73000.00
Lowest Capacity Asset
IBM R735QTJ8XC9X
using System;
using System.Drawing;

namespace QuantConnect.Algorithm.CSharp
{
    public class CasualYellowGreenLemur : QCAlgorithm
    {
        string Symbol = "IBM";
        int limitPrice = 0;
        int stopMarketTicket = 0;
        
        decimal st = 0.01m;
    	decimal tp = 0.0m;
    	decimal close = 0.0m;
    	string type = "Buy";
    	int numberOfOrders =0;
        
        private ExponentialMovingAverage slow;
        private ExponentialMovingAverage fast;
        private ExponentialMovingAverage ema;
        public List<OrderTicket> Orders = new List<OrderTicket>();
        
        private OrderTicket _limitTicket;
    	private OrderTicket _stopLimitTicket;
    	decimal TP2;
    	 
    	private RollingWindow<TradeBar> _window;

        public override void Initialize() 
        {         
            SetStartDate(2020, 7, 4);
            SetEndDate(2021, 7, 6);
            SetCash(10000);
            
            AddSecurity(SecurityType.Equity, Symbol, Resolution.Minute);
            
            _window = new RollingWindow<TradeBar>(3);    // For other security types, use QuoteBar
            
            fast = EMA(Symbol, 72, Resolution.Minute); 
            slow = EMA(Symbol, 89, Resolution.Minute);
            ema = EMA(Symbol, 9, Resolution.Minute);
            tp = st*2;
            Schedule.On(DateRules.On(EndDate),  
                         TimeRules.At(15, 0),  
                         SpecificTime);
        }
          
    public void SpecificTime()
    {
    	Liquidate(Symbol);
        Debug("liquidated on the last day");
    	
    }
       
        public override void OnData(Slice data)
        {
        	
        	// Add SPY TradeBar in rollling window
            _window.Add(data[Symbol]);

            // Wait for windows to be ready.
            if (!_window.IsReady) return;
            
        	var chart = Portfolio[Symbol];
        	
        	if(_window[1] != null)
        		close = _window[1].Price;
        	
        	if (chart.Price >= slow && numberOfOrders ==0 && _window[1] != null)
            {
            	type= "BUY";
            	
                Log("BUY  >> " + Securities[Symbol].Price);
               
                //SetHoldings(Symbol, .50);
                //var stopMarketTicket = StopMarketOrder(Symbol, -1, close * 0.99m);
                //var limitTicket = LimitOrder(Symbol, 10, close * .99m);
                 
                var close1 = Securities[Symbol].Close; 
                //var stopMarketTicket = StopMarketOrder(Symbol, 10, close-close * st);
                var stopMarketTicket = StopMarketOrder(Symbol, 10,close);
               
                //MarketOrder(Symbol, 100);
                var limitTicket = LimitOrder(Symbol, 10, close+close * tp);
               
                 
                Orders.Add(limitTicket);
                
                numberOfOrders = 1;
                Log("1st Stoploss >>" + stopMarketTicket);
                Log("TP1 >>" + limitTicket);
               
            }
        	
        	if (chart.Price <= fast && numberOfOrders ==0)
            {
            	type= "SELL";
                Log("SELL >> " + Securities[Symbol].Price);                                                    
                
                // var close = Securities[Symbol].Close; 
                var stopMarketTicket = StopMarketOrder(Symbol, 10, close+close * st);
               
                var limitTicket = LimitOrder(Symbol, 10, close-close * tp);
                
                //MarketOrder(Symbol, 100);
                Orders.Add(limitTicket);
               
                numberOfOrders =1;
                Log("Stoploss >>" + stopMarketTicket);
                 Log("TP1 >>" + limitTicket);
                
                
            }
            if (!Portfolio.Invested)
            {
	        	//var close = Securities[Symbol].Close;
            	
                //MarketOrder(Symbol, 100);
                //Debug("Purchased Stock");
               
            }
        }
        
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
        	if (orderEvent.Status == OrderStatus.Filled && numberOfOrders == 0){
        			return;
        	}
        	
        	///TP2
	        if(numberOfOrders == 1)
            {
           		if(Orders[0].OrderId == orderEvent.OrderId && orderEvent.Status != OrderStatus.CancelPending)
	            {
	            	var e = ema * 1;
	            	var currentprice  = Securities[Symbol].Price;
	            	if(currentprice < e)
	            	
	                {
			            Log(type + "  >> " + Securities[Symbol].Price);
			          
		                var qty = orderEvent.FillQuantity * 0.5m;
		                var stopMarketTicket = StopMarketOrder(Symbol, qty, orderEvent.FillPrice); //SL
		                var limitTicket2 = LimitOrder(Symbol, qty, _window[1].Price);
		                
		                if(limitTicket2.OrderId ==-10)
		                	Log("OrderId - 10");
		                
		                Orders.Add(limitTicket2);
		                numberOfOrders =2;
		                Log("Status" + orderEvent.Status);
		                Log("STOP >>" + stopMarketTicket);
		                Log("TP2 >>" + limitTicket2);
	                }
	            }
	            else
	            {
	            	Log(" **Hit stop loss: Resetting the Orders **");
	            	Log("No of orders >>" + Orders.Count);
	            	var order = Orders[Orders.Count-1];
	                order.Cancel();
	            	numberOfOrders=0;
	            	Orders = new List<OrderTicket>();
	            	//Liquidate(Symbol); 
	            
	            }
        	}
        	else if(numberOfOrders == 2)
        	{
        		//TP3 goes here
        		if(Orders[0].OrderId == orderEvent.OrderId && orderEvent.Status != OrderStatus.CancelPending)
	            {
	            	Log(" ** Hit TP2: Resetting the Orders **");
		            Orders = new List<OrderTicket>();
		            numberOfOrders=0;
		            //Liquidate(Symbol); 
	            }
	            else if(Orders[0].OrderId != -3)
	            {
	            	var r =Orders[0];
	                Orders[0].Cancel();
	                
	            	Log(" **Hit stop loss for TP2: Resetting the Orders **");
	            	Orders = new List<OrderTicket>();
	            	numberOfOrders=0;
	            	//Liquidate(Symbol); 
	            
	            }
	            else
	            {
	            	Log(" ** Hit Order 2: **"); 
	            	Orders = new List<OrderTicket>();
	            	numberOfOrders =0;
	            	//Liquidate(Symbol); 
	            }
        	}
        	else if(Orders.Count>=3)
            {
                Orders[Orders.Count-1].Cancel();
            	Log(" **Orders >=3 **");
            	Orders = new List<OrderTicket>();
            	numberOfOrders=0;
            	//Liquidate(Symbol); 
            }
        	
    	} 
	
	}
}