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
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
using System;
using System.Collections.Generic;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using System.Linq;
using QuantConnect.Interfaces;
using QuantConnect.Indicators;
using QuantConnect.Securities;
using QuantConnect.Orders;
using QuantConnect.Data.Consolidators;

namespace Quant 
{
	public static class Contract_ES
    {
        public static FuturesContract GetFuturesContract_ES(this Slice slice, QCAlgorithm Algorithm)
        {
            foreach (var chain in slice.FutureChains)
            {
                if (chain.Value.Symbol.Value.StartsWith("ES"))
                {
                    return (from futuresContract in chain.Value.OrderBy(x => x.Expiry)
                            where futuresContract.Expiry > Algorithm.Time.Date.AddDays(1)
                            select futuresContract).FirstOrDefault();
                }
            }
            return null;
        }
    }
    
	    public class TestAlgo : QCAlgorithm
	    {

    	public RollingWindow<decimal> Volume ;
    	public RollingWindow<decimal> Vol ;
    	
    	public RollingWindow<decimal> Ask_Open ;
    	public RollingWindow<decimal> AskOpen ;
    	
    	public RollingWindow<decimal> Bid_Open ;
    	public RollingWindow<decimal> BidOpen ;
    	
    	public RollingWindow<decimal> Ask_High ;
    	public RollingWindow<decimal> AskHigh ;
    	
    	public RollingWindow<decimal> Bid_High ;
    	public RollingWindow<decimal> BidHigh ;
    	
    	public RollingWindow<decimal> Ask_Low ;
    	public RollingWindow<decimal> AskLow ;
    	
    	public RollingWindow<decimal> Bid_Low ;
    	public RollingWindow<decimal> BidLow ;
    	
    	public RollingWindow<decimal> Ask_Close ;
    	public RollingWindow<decimal> AskClose ;
    	
    	public RollingWindow<decimal> Bid_Close ;
    	public RollingWindow<decimal> BidClose ;
    	
    	public OrderEvent Entryorder {get; set;}
    	public List<int> Entryorder_Id = new List<int>() ;
    	

        public OrderTicket EntryOrder { get; set; }
        public Func<QCAlgorithm, string, decimal, OneCancelsOtherTicketSet> OnOrderFilledEvent { get; set; }
        public OneCancelsOtherTicketSet ProfitLossOrders { get; set; }

        public override void Initialize() 
        {
        	
		SetStartDate(2013, 01, 01);
        SetEndDate(2013, 01, 5);
        SetCash(1000000);

        var contract = AddFuture(Futures.Indices.SP500EMini, Resolution.Minute);
        contract.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));

		SetSecurityInitializer(x => x.SetSlippageModel(new CustomSlippageModel(this)));
		SetBrokerageModel(BrokerageName.AlphaStreams);
	    SetExecution(new VolumeWeightedAveragePriceExecutionModel());
	    
	    AddRiskManagement(new MaximumUnrealizedProfitPercentPerSecurity(0.09m));
	    AddRiskManagement(new MaximumDrawdownPercentPerSecurity(0.09m));
	    
	    Volume = new RollingWindow<decimal>(2);
	    Vol = new RollingWindow<decimal>(2);
	    
		Ask_Open = new RollingWindow<decimal>(2);
		AskOpen = new RollingWindow<decimal>(2);
		
		Bid_Open = new RollingWindow<decimal>(2);
		BidOpen = new RollingWindow<decimal>(2);
		
		Ask_High = new RollingWindow<decimal>(2);
		AskHigh = new RollingWindow<decimal>(2);
		
		Bid_High = new RollingWindow<decimal>(2);
		BidHigh = new RollingWindow<decimal>(2);
		
		Ask_Low = new RollingWindow<decimal>(2);
		AskLow = new RollingWindow<decimal>(2);
		
		Bid_Low = new RollingWindow<decimal>(2);
		BidLow = new RollingWindow<decimal>(2);
		
		Ask_Close = new RollingWindow<decimal>(2);
		AskClose = new RollingWindow<decimal>(2);
		
		Bid_Close = new RollingWindow<decimal>(2);
		BidClose = new RollingWindow<decimal>(2);
		

        }

        public void OnData(Slice slice)
        {	
        	var accountCurrencyCash = Portfolio.TotalPortfolioValue;
        	
            var ES = slice.GetFuturesContract_ES(new QCAlgorithm());
            
            var consolidator_Quote = new QuoteBarConsolidator(TimeSpan.FromMinutes(60));
			consolidator_Quote.DataConsolidated += DataConsolidate_Quote;
			SubscriptionManager.AddConsolidator(ES.Symbol, consolidator_Quote);
			
			var consolidator_Trade = new TradeBarConsolidator(TimeSpan.FromMinutes(60));
			consolidator_Trade.DataConsolidated += DataConsolidate_Trade;
			SubscriptionManager.AddConsolidator(ES.Symbol, consolidator_Trade);
			
			if(!Portfolio.Invested)
			{
            if (ES == null) {return ;}

			if (AskOpen.IsReady && AskClose.IsReady && BidOpen.IsReady && BidClose.IsReady && Vol.IsReady)
			{

			var Quantity = 1;
			var _minSize = 0.25m;

			if (Vol[1] < Vol[0]) 
			{

			
		

				OnOrderFilledEvent = (algoo_ES, symbol, FillPrice) =>
				{
				return new OneCancelsOtherTicketSet(
				algoo_ES.LimitOrder(ES.Symbol, -Quantity, FillPrice * Var.TPLong, "Profit Long _Target"),
				algoo_ES.StopMarketOrder(ES.Symbol, -Quantity, FillPrice * Var.SLLong, "Stop Long _Loss"));
				};
				EntryOrder = MarketOrder(ES.Symbol, Quantity, true, "Entry");
				Debug("EntrOrder_Ask_Open[0] : " + Ask_Open[0]);
				Debug("EntrOrder_AskOpen[0] : " + AskOpen[0]);
				Debug("EntrOrder_Volume[0] : " + Volume[0]);
				Debug("EntrOrder_VolumeTime[0] : " + Volume[0]);
				Debug("EntrOrder_Volume[1] : " + Volume[1]);
				Debug("EntrOrder_Vol[0] : " + Vol[0]);
				Debug("EntrOrder_Vol[1] : " + Vol[1]);
				Debug("AverageFillPrice : " + EntryOrder.AverageFillPrice);
				
			
			}
			}
			}
			
        }
        
        public void DataConsolidate_Trade(object sender, TradeBar tradeBar)
		{
			Volume.Add(tradeBar.Volume);
			if (Volume.IsReady)
			{
			if (Volume[0] != Volume[1])
			{
			Vol.Add(tradeBar.Volume);
			}
			}
		}
		
		public void DataConsolidate_Quote(object sender, QuoteBar quoteBar)
		{
			Ask_Open.Add(quoteBar.Ask.Open);
			if (Ask_Open.IsReady)
			{
			if (Ask_Open[0] != Ask_Open[1])
			{
			AskOpen.Add(quoteBar.Ask.Open);
			}
			}
			
			Bid_Open.Add(quoteBar.Bid.Open);
			if (Bid_Open.IsReady)
			{
			if (Bid_Open[0] != Bid_Open[1])
			{
			BidOpen.Add(quoteBar.Bid.Open);
			}
			}
			
			Ask_High.Add(quoteBar.Ask.High);
			if (Ask_High.IsReady)
			{
			if (Ask_High[0] != Ask_High[1])
			{
			AskHigh.Add(quoteBar.Ask.High);
			}
			}
			
			Bid_High.Add(quoteBar.Bid.High);
			if (Bid_High.IsReady)
			{
			if (Bid_High[0] != Bid_High[1])
			{
			BidHigh.Add(quoteBar.Bid.High);
			}
			}
			
			Ask_Low.Add(quoteBar.Ask.Low);
			if (Ask_Low.IsReady)
			{
			if (Ask_Low[0] != Ask_Low[1])
			{
			AskLow.Add(quoteBar.Ask.Low);
			}
			}
			
			Bid_Low.Add(quoteBar.Bid.Low);
			if (Bid_Low.IsReady)
			{
			if (Bid_Low[0] != Bid_Low[1])
			{
			BidLow.Add(quoteBar.Bid.Low);
			}
			}
			
			Ask_Close.Add(quoteBar.Ask.Close);
			if (Ask_Close.IsReady)
			{
			if (Ask_Close[0] != Ask_Close[1])
			{
			AskClose.Add(quoteBar.Bid.Close);
			}
			}
			
			Bid_Close.Add(quoteBar.Bid.Close);
			if (Bid_Close.IsReady)
			{
			if (Bid_Close[0] != Bid_Close[1])
			{
			BidClose.Add(quoteBar.Bid.Close);
			}
			}
			
		}

        public override void OnOrderEvent(OrderEvent orderEvent)
       	{
        	if (EntryOrder != null)
            {
                this.EntryOrder = null;
            }

            if (orderEvent.Status == OrderStatus.Filled || orderEvent.Status == OrderStatus.PartiallyFilled)
            {
                if (this.OnOrderFilledEvent != null)
                {
                    this.ProfitLossOrders = OnOrderFilledEvent(this, orderEvent.Symbol, orderEvent.FillPrice);
                    OnOrderFilledEvent = null;
                } 
                else if (this.ProfitLossOrders != null)
                {
                    this.ProfitLossOrders.Filled();
                    this.ProfitLossOrders = null;
                }
            }
       	}
       	
       	public class CustomSlippageModel : ISlippageModel
        {
            private readonly QCAlgorithm _algorithm;
            
            public CustomSlippageModel(QCAlgorithm algorithm)
            {
				_algorithm = algorithm;
			}

            public decimal GetSlippageApproximation(Security asset, Order order)
            {	

    			var orderVolume = order.Quantity;
    			var slippage = ((asset.AskSize - asset.BidSize) * 0.0001m / (asset.Volume/orderVolume));

    			
    			if (asset.Symbol.StartsWith("ES"))
    			{
    			if (((asset.AskSize - asset.BidSize) * 0.0001m / (asset.Volume/orderVolume)) < Var.minprizefluct)
    			{
    				return 0.25m ;
    			}
    			}

				return slippage;
            }
        
        }
        
        

        /// <summary>
       	/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
       	/// </summary>
       	public bool CanRunLocally { get; } = true;

       	/// <summary>
       	/// This is used by the regression test system to indicate which languages this algorithm is written in.
       	/// </summary>
       	public Language[] Languages { get; } = { Language.CSharp };

    }
}
namespace Quant
{
    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.Symbol.StartsWith("ES"))
                {
                    orderTicket.Cancel();
                }
                
                if (orderTicket.Status == OrderStatus.Submitted && orderTicket.Symbol.StartsWith("YM"))
                {
                    orderTicket.Cancel();
                }
            }
        }
        
}

}
namespace Quant
{
public static class Var
{
	public const decimal MaxQuantity = 50m ;
	public const decimal CentageLoss = 0.1m ;
	public const decimal valpoint = 12.5m ;
	public const decimal minprizefluct = 0.25m ;
	
 	public const decimal coefVolume = 1.1m ;
	
	public const decimal TPLong = 1.001m ;
	public const decimal SLLong = 0.972m ;
	public const decimal TPShort = 1.001m ;
	public const decimal SLShort = 0.972m ;
}
}