Overall Statistics
Total Trades
7619
Average Win
112.80%
Average Loss
0%
Compounding Annual Return
37.762%
Drawdown
68.700%
Expectancy
0
Net Profit
2369.988%
Sharpe Ratio
0.71
Probabilistic Sharpe Ratio
11.432%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.586
Beta
-0.009
Annual Standard Deviation
0.826
Annual Variance
0.682
Information Ratio
0.671
Tracking Error
0.854
Treynor Ratio
-67.458
Total Fees
$34126.95
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Indicators;
using QuantConnect.Securities;
using QuantConnect.Orders;


namespace QuantConnect.Algorithm.Framework.Alphas
{
   
public class Algorithm_YM : NullAlphaModel
{
	
	public RollingWindow<decimal> BidPriceYM = new RollingWindow<decimal>(3);
    public RollingWindow<decimal> AskPriceYM = new RollingWindow<decimal>(3);
    public RollingWindow<decimal> VolumeYM = new RollingWindow<decimal>(3);

   
	private readonly Symbol _symbol;
	
    public Algorithm_YM(Symbol symbol)
    {
        _symbol = symbol;
    }
    
    public OrderTicket EntryOrder_Long_YY_YM { get; set; }
    public OrderTicket Stop_Long_YY_YM { get; set; }
    public OrderTicket Limit_Long_YY_YM { get; set; }
    
    public OrderTicket EntryOrder_Long_Y_YM { get; set; }
    public OrderTicket Stop_Long_Y_YM { get; set; }
    public OrderTicket Limit_Long_Y_YM { get; set; }
    
    public OrderTicket EntryOrder_Short_YY_YM { get; set; }
    public OrderTicket Stop_Short_YY_YM { get; set; }
    public OrderTicket Limit_Short_YY_YM { get; set; }
    
    public OrderTicket EntryOrder_Short_Y_YM { get; set; }
    public OrderTicket Stop_Short_Y_YM { get; set; }
    public OrderTicket Limit_Short_Y_YM { get; set; }
    
    public QCAlgorithm algorithm {get; set;}

public void OnOrderEvent(OrderEvent orderEvent)
 {
	if (!orderEvent.Status.IsFill()) return;
	
	if (EntryOrder_Long_YY_YM != null || 
		EntryOrder_Long_Y_YM != null ||
		EntryOrder_Short_YY_YM != null ||
		EntryOrder_Short_Y_YM != null)
	{
	
	var orderId = orderEvent.OrderId;
	
	var orderId_EntryOrder_Long_YY_YM = EntryOrder_Long_YY_YM.OrderId;
	var orderId_EntryOrder_Long_Y_YM = EntryOrder_Long_Y_YM.OrderId;
	var orderId_EntryOrder_Short_YY_YM = EntryOrder_Short_YY_YM.OrderId;
	var orderId_EntryOrder_Short_Y_YM = EntryOrder_Short_Y_YM.OrderId;
	
	var order_ES = orderEvent.Symbol.StartsWith("YM");
	
	var orderFill = orderEvent.FillPrice;
	
	if (orderId == orderId_EntryOrder_Long_YY_YM)
	{
		var FillPrice_EntryOrder_Long_YY_YM = orderEvent.FillPrice;
		var Symbol = orderEvent.Symbol;
		var Quantity = 100;
		
		OrderTicket Limit_Long_YY_YM = algorithm.MarketOrder(Symbol, Quantity, true, "Entry OnOrderEvent YM Test: ");
		
	}
	
	}

 }

public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
{
	var accountCurrencyCash = algorithm.Portfolio.TotalPortfolioValue;

    foreach(var chain in data.FutureChains)
    {

    // remplacer les lettre entre guillemet par l'abreviation correspondant au contract analysé. 
	if (chain.Value.Symbol.StartsWith("YM"))
    {
	var YM = (from futuresContract in chain.Value.OrderBy(x => x.Expiry)
	where futuresContract.Expiry > algorithm.Time.Date.AddDays(1) 
	select futuresContract).FirstOrDefault();
    
    if (YM != null)
	{
		
	BidPriceYM.Add(YM.BidPrice);
	AskPriceYM.Add(YM.AskPrice);
	VolumeYM.Add(YM.Volume);
	
	if (!BidPriceYM.IsReady || !AskPriceYM.IsReady || !VolumeYM.IsReady)
	continue;

	if (YM.BidPrice != 0 && YM.AskPrice != 0)
	{

	var _quantityy = (decimal)(VolumeYM[0]+VolumeYM[1]) * 0.001m;
	
	if (BidPriceYM[0]>AskPriceYM[1]) 
	{

	EntryOrder_Long_YY_YM = algorithm.MarketOrder(YM.Symbol, _quantityy, false, "Entry");
	break;
	}
	
	if (AskPriceYM[0]<BidPriceYM[1]) 
	{

	EntryOrder_Long_Y_YM = algorithm.MarketOrder(YM.Symbol, _quantityy, false, "Entry");
	break;	
	}

	}}}
}
return Enumerable.Empty<Insight>();
}



}
}
namespace QuantConnect.Algorithm.Framework.Alphas
{
public static class VarES
{
	public const decimal minprizefluct = 0.25m ;
	
	public const decimal TPLong = 1.001m ;
	public const decimal SLLong = 0.972m ;
	
	public const decimal TPShort = 1.001m ;
	public const decimal SLShort = 0.972m ;
}
}
namespace QuantConnect.Algorithm.Framework.Alphas
{
public static class VarYM
{
	public const decimal minprizefluct = 1m ;

	public const decimal TPLong = 1.0005m ;
	public const decimal SLLong = 0.94m ;
	
	public const decimal TPShort = 1.0005m ;
	public const decimal SLShort = 0.94m ;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Indicators;
using QuantConnect.Securities;
using QuantConnect.Orders;


namespace QuantConnect.Algorithm.Framework.Alphas
{

public class Algorithm_ES : NullAlphaModel
{

	public RollingWindow<decimal> BidPriceES = new RollingWindow<decimal>(3);
    public RollingWindow<decimal> AskPriceES = new RollingWindow<decimal>(3);
    public RollingWindow<decimal> VolumeES = new RollingWindow<decimal>(3);

    private readonly Symbol _symbol;
    
    public Algorithm_ES(Symbol symbol)
    {
        _symbol = symbol;
    }
    

public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data)
{
	var accountCurrencyCash = algorithm.Portfolio.TotalPortfolioValue;
	
    foreach(var chain in data.FutureChains)
    {

    // remplacer les lettre entre guillemet par l'abreviation correspondant au contract analysé. 
	if (chain.Value.Symbol.StartsWith("ES"))
    {
	var ES = (from futuresContract in chain.Value.OrderBy(x => x.Expiry)
	where futuresContract.Expiry > algorithm.Time.Date.AddDays(1) 
	select futuresContract).FirstOrDefault();
    
    if (ES != null)
	{
		
	BidPriceES.Add(ES.BidPrice);
	AskPriceES.Add(ES.AskPrice);
	VolumeES.Add(ES.Volume);
	
	if (!BidPriceES.IsReady || !AskPriceES.IsReady || !VolumeES.IsReady)
	continue;

	if (ES.BidPrice != 0 && ES.AskPrice != 0)
	{

	var _quantityy = (decimal)(VolumeES[0]+VolumeES[1]) * 0.00001m;
	
	if (BidPriceES[0]>AskPriceES[1]) 
	{

	algorithm.MarketOrder(ES.Symbol, _quantityy, false, "Entry");
	break;
	}

	if (AskPriceES[0]<BidPriceES[1]) 
	{

	algorithm.MarketOrder(ES.Symbol, _quantityy, false, "Entry");
	break;	
	}

	}}}
    
}

return Enumerable.Empty<Insight>();
}



}
}
using System;
using System.Collections.Generic;
using QuantConnect.Data.Market;
using QuantConnect.Securities.Future;
using QuantConnect.Indicators;
using QuantConnect.Securities;
using QuantConnect.Algorithm.Framework.Execution ;
using QuantConnect.Orders;
using QuantConnect.Algorithm.Framework.Risk;
using QuantConnect.Brokerages;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Orders.Slippage;
using QuantConnect.Interfaces;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Algorithm.Framework.Execution;

namespace QuantConnect.Algorithm.Framework.Alphas
{
public partial class Algorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
	private String[] futureSymbols = new[] {"ES", "YM"};
	private Dictionary<Symbol, FuturesChain> contract_chains = new Dictionary<Symbol, FuturesChain>();
	private Dictionary<Symbol, Future> contracts = new Dictionary<Symbol, Future>();

	private Symbol _YM;
	private Symbol _ES;
	
public override void Initialize()
{
	SetStartDate(2000, 01, 01);
    SetEndDate(2010, 01, 01);
    SetCash(1000000);

foreach (var futureSymbol in futureSymbols) 
{
	Debug($"Registering {futureSymbol}");
	Future fut = AddFuture(futureSymbol, Resolution.Minute);
	fut.SetFilter(universe => universe.FrontMonth());
}

_ES = QuantConnect.Symbol.Create("ES", SecurityType.Future, Market.USA);
_YM = QuantConnect.Symbol.Create("YM", SecurityType.Future, Market.USA);

SetSecurityInitializer(x => x.SetSlippageModel(new CustomSlippageModel(this)));
SetBrokerageModel(BrokerageName.AlphaStreams);
SetExecution(new VolumeWeightedAveragePriceExecutionModel());


AddRiskManagement(new MaximumUnrealizedProfitPercentPerSecurity(0.1m));
AddRiskManagement(new MaximumDrawdownPercentPerSecurity(0.1m));

AddAlpha(new Algorithm_ES(_ES));
AddAlpha(new Algorithm_YM(_YM));

}


public class FuturesUniverseSelectionModel : FutureUniverseSelectionModel
{
	public FuturesUniverseSelectionModel(Func<DateTime, IEnumerable<Symbol>> futureChainSymbolSelector)
	:base(TimeSpan.FromDays(1), futureChainSymbolSelector){}
		
	public FutureFilterUniverse filter(FutureFilterUniverse filter){
	return filter.Expiration(TimeSpan.Zero, TimeSpan.FromDays(182)).OnlyApplyFilterAtMarketOpen();}
}

public class CustomSlippageModel : ISlippageModel
{
            private readonly QCAlgorithm _algorithm;
            
            public RollingWindow<decimal> High = new RollingWindow<decimal>(2);
            public RollingWindow<decimal> Low = new RollingWindow<decimal>(2);
            public RollingWindow<decimal> Volume = new RollingWindow<decimal>(2);
			
            public CustomSlippageModel(QCAlgorithm algorithm)
            {
				_algorithm = algorithm;
			}

            public decimal GetSlippageApproximation(Security asset, Order order)
            {			
    			High.Add(asset.High);
    			Low.Add(asset.Low);
    			Volume.Add(asset.Volume);
    			
    			var orderVolume = order.AbsoluteQuantity;
    			var slippage = (High[0]-Low[0]) * 0.01m / (Volume[0]/orderVolume);
    			
    			if (asset.Symbol.Value.StartsWith("ES"))
    			{
    			if (((High[0]-Low[0]) * 0.01m / (Volume[0]/orderVolume)) < VarES.minprizefluct)
    			{
    				return VarES.minprizefluct ;
    			}
    			}
    			
    			if (asset.Symbol.Value.StartsWith("YM"))
    			{
    			if (((High[0]-Low[0]) * 0.01m / (Volume[0]/orderVolume)) < VarES.minprizefluct)
    			{
    				return VarYM.minprizefluct ;
    			}
    			}
    			
				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, Language.Python };

        /// <summary>
        /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
        /// </summary>
        public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
        {
            {"Total Trades", "2"},
            {"Average Win", "0%"},
            {"Average Loss", "0%"},
            {"Compounding Annual Return", "-91.448%"},
            {"Drawdown", "5.000%"},
            {"Expectancy", "0"},
            {"Net Profit", "-3.312%"},
            {"Sharpe Ratio", "-14.22"},
            {"Loss Rate", "0%"},
            {"Win Rate", "0%"},
            {"Profit-Loss Ratio", "0"},
            {"Alpha", "-1.479"},
            {"Beta", "0.204"},
            {"Annual Standard Deviation", "0.099"},
            {"Annual Variance", "0.01"},
            {"Information Ratio", "-10.043"},
            {"Tracking Error", "0.176"},
            {"Treynor Ratio", "-6.907"},
            {"Total Fees", "$3.70"},
            {"Total Insights Generated", "6"},
            {"Total Insights Closed", "5"},
            {"Total Insights Analysis Completed", "5"},
            {"Long Insight Count", "6"},
            {"Short Insight Count", "0"},
            {"Long/Short Ratio", "100%"},
            {"Estimated Monthly Alpha Value", "$-90.70258"},
            {"Total Accumulated Estimated Alpha Value", "$-15.621"},
            {"Mean Population Estimated Insight Value", "$-3.1242"},
            {"Mean Population Direction", "0%"},
            {"Mean Population Magnitude", "0%"},
            {"Rolling Averaged Population Direction", "0%"},
            {"Rolling Averaged Population Magnitude", "0%"}
        };
}
}