Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-2.379
Tracking Error
0.147
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
using System;
using System.Drawing;

namespace QuantConnect.Algorithm.CSharp
{
    public class CasualYellowGreenLemur : QCAlgorithm
    {
        private DateTime _previous;

    	private Symbol _chainSymbol;
        private Symbol _contractSymbol;
        
         private const decimal StopLossPercent = 0.01m;  
        private	const decimal TakeProfitPercent = 0.04m;
		private const decimal tolerance = 0.00015m;
		private	int quantity = 1000;
    	private decimal price = 0.0m;
    	private OrderTicket CurrentOrder;
    	private List<OrderTicket> StopLoss = new List<OrderTicket>();
    	private List<OrderTicket> ProfitTarget = new List<OrderTicket>();

        private Dictionary<Symbol, ExponentialMovingAverage> fast = new Dictionary<Symbol, ExponentialMovingAverage>();
        private Dictionary<Symbol, ExponentialMovingAverage> slow = new Dictionary<Symbol, ExponentialMovingAverage>();

        public override void Initialize() 
        {         
            SetStartDate(2021, 3, 1);
            SetEndDate(2021, 3, 16);
            SetCash(100000);

        	var future = AddFuture(Futures.Indices.NASDAQ100EMini);
        	future.SetFilter(0, 182);
        	_chainSymbol = future.Symbol;
        }
        
        public void OnData(Slice slice) {
           
           if (_previous.Date == Time.Date) return;
           
            
           
			FuturesChain chain;
			// Find the contracts in the FuturesChain
			// See docs: https://www.quantconnect.com/docs/data-library/futures
			if (slice.FuturesChains.TryGetValue(_chainSymbol, out chain))
			{
    			var underlying = chain.Underlying;
    			foreach (var contract in chain)
    			{
    				// Create indicators for each contract and save them in dictionaries keyed by Symbol
        			var symbol = contract.Symbol;
        			if (!slow.ContainsKey(symbol))
        			{
        				slow[symbol] = EMA(symbol, 89, Resolution.Minute);
        			}
        			
        			if (!fast.ContainsKey(symbol))
        			{
        				fast[symbol] = EMA(symbol, 72, Resolution.Minute);
        			}
    			}
    			
    			// For example, select the contract with the earliest expiry
                _contractSymbol = (
                        from futuresContract in chain.OrderBy(x => x.Expiry)
                        where futuresContract.Expiry > Time.Date.AddDays(90)
                        select futuresContract
                    ).FirstOrDefault()?.Symbol;
			}
           
        	if (_contractSymbol == null)
        	{
        		return;
        	}

        	var holdings = Portfolio[_contractSymbol].Quantity;
            var profitloss = Portfolio[_contractSymbol].UnrealizedProfit;

            // we only want to go long if we're currently short or flat
            // if the fast is greater than the slow, we'll go long
            // the fast for the selected contract is found in the dictionary
            if (holdings == 0 )
            {
            	var f= fast[_contractSymbol] *1;
            	var s = slow[_contractSymbol]* (1 + tolerance);
                // if the slow is greater than the fast, we'll go long
                if (fast[_contractSymbol] > (slow[_contractSymbol] * (1 + tolerance)))
                {
                    var stopLossPrice = Securities[_chainSymbol].Price -  StopLossPercent * Securities[_chainSymbol].Price;
                    
                  	var t =  price;
                  	var q =  quantity;
                  	var y = StopLossPercent * Securities[_chainSymbol].Price;
		            StopLoss.Add(StopMarketOrder(_chainSymbol, -1* quantity, stopLossPrice));
		           // var orderId= StopLoss;
	               // Set Profit Target 
	               var profitTargetPrice = Securities[_chainSymbol].Price +  TakeProfitPercent * Securities[_chainSymbol].Price;
	               ProfitTarget.Add(LimitOrder(_chainSymbol, quantity, profitTargetPrice));
                    Log("BUY  >> " + Securities[_chainSymbol].Price + " Our stopLossPrice: " + stopLossPrice  + " ProfitTarget: " + profitTargetPrice+ " Quantity: " + quantity); 
                    var p = ProfitTarget[ProfitTarget.Count()-1].OrderId;
                    var s1 = StopLoss[StopLoss.Count()-1].OrderId;
                    Log("StopLoss OrderId = "+ StopLoss[StopLoss.Count()-1].OrderId + " ProfitTarget OrderId = "+ ProfitTarget[ProfitTarget.Count()-1].OrderId);
                    SetHoldings(_chainSymbol, 0.50);
                  
                  
                }
            }
            
             if (holdings > 0)//if (holdings > 0 && fast <= slow)
            {
                //Log("SELL >> " + Securities[Symbol].Price + " Our price: " + price + " Quantity: " + quantity);
            	 var stopLossPrice = Securities[_chainSymbol].Price -  StopLossPercent * Securities[_chainSymbol].Price;
                Liquidate(_chainSymbol); 
                
                var close = Securities[_chainSymbol].Close; 
              ///  var stopMarketTicket = StopMarketOrder(Symbol, 10, close * 0.99m);
                
                StopLoss.Add(StopMarketOrder(_chainSymbol, quantity, stopLossPrice));
                // Set Profit Target 
                var profitTargetPrice = Securities[_chainSymbol].Price +  TakeProfitPercent * Securities[_chainSymbol].Price;
                ProfitTarget.Add(LimitOrder(_chainSymbol, quantity, profitTargetPrice));
                Log("SELL  >> " + Securities[_chainSymbol].Price + " Our stopLossPrice: " + stopLossPrice  + " ProfitTarget: " + profitTargetPrice+ " Quantity: " + quantity); 
                Log("StopLoss OrderId = "+ StopLoss[StopLoss.Count()-1].OrderId + " ProfitTarget OrderId = "+ ProfitTarget[ProfitTarget.Count()-1].OrderId);
            }
            Plot(_chainSymbol, "Price", Securities[_chainSymbol].Price);
            
            
            // easily plot indicators, the series name will be the name of the indicator
           // Plot(Symbol, "Slow", slow);
            //Plot(Symbol, "fast", fast);
            
        }
        
         public override void OnOrderEvent(OrderEvent orderEvent)
		{
			// Ignore OrderEvents that are not closed
			if (!orderEvent.Status.IsClosed())
			{
				return;
			}
			var t = ProfitTarget.Count();
			var s = StopLoss;
			// Defensive check
			if (ProfitTarget.Count() == 0 || StopLoss.Count() == 0)
			{
				
				return;
			}
			
			var filledOrderId = orderEvent.OrderId;
			var x=0;
			// If the ProfitTarget order was filled, close the StopLoss order
			foreach(var profitTarget in ProfitTarget)
			{
				if (profitTarget.OrderId == filledOrderId)
				{
					Log("StopLoss.Cancel OrderId = " + StopLoss[x].OrderId);
					StopLoss[x].Cancel();
				}
				++x	;
			}
			
			// If the StopLoss order was filled, close the ProfitTarget
			x=0;
			foreach(var stopLoss in StopLoss)
			{
				if (stopLoss.OrderId == filledOrderId)
				{
					Log("ProfitTarget.Cancel OrderId = " + ProfitTarget[x].OrderId);
					ProfitTarget[x].Cancel();
				}
				++x	;
			}
			
			
		}

        	//Plot(_contractSymbol, "Price", slice[_contractSymbol].Price);
            //Plot(_contractSymbol, _fast[_contractSymbol], _slow[_contractSymbol]);

    
            
            }
        }