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
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.Concurrent;
using System.Linq;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.CSharp
{
    public class EmaCrossUniverseSelectionAlgorithm : QCAlgorithm
    {
        const decimal Tolerance = 0.01m;
        private const int Count = 10;
        private const decimal _targetpercent = 0.1m;
        private SecurityChanges _changes = SecurityChanges.None;
        private readonly ConcurrentDictionary<Symbol, SelectionData> _averages = new ConcurrentDictionary<Symbol, SelectionData>();

        private class SelectionData
        {
            RollingWindow<decimal> ClosingPrices = new RollingWindow<decimal>(1);

            public SelectionData()
            {
            }

            public decimal Gain
            {
                get { return (ClosingPrices[0]-ClosingPrices[1])/(ClosingPrices[1]); }
            }

            public bool Update(DateTime time, decimal value, Symbol symbol, QCAlgorithm algo)
            {
            	algo.Log(String.Format("Symbol: {0}\tTime: {1}\tPrice: {2}", symbol.Value, time.ToString(), value.ToString()));
	        	ClosingPrices.Add(value);
                return ClosingPrices.IsReady;
            }
        }

        public override void Initialize()
        {
            UniverseSettings.Leverage = 1.0m;
            UniverseSettings.Resolution = Resolution.Minute;
        	SetBrokerageModel(BrokerageName.TradierBrokerage, AccountType.Cash);
            SetSecurityInitializer(new CustomSecurityInitializer(BrokerageModel, DataNormalizationMode.Raw));

            SetStartDate(2010, 01, 01);
            SetEndDate(2010, 04, 01);
            SetCash(100*1000);

            AddUniverse(coarse =>
            {
                return (from cf in coarse
                        let avg = _averages.GetOrAdd(cf.Symbol, sym => new SelectionData())
                        where avg.Update(cf.EndTime, cf.Price, cf.Symbol, this)
                        orderby avg.Gain descending 
                        select cf.Symbol).Take(Count);
            });
        }

        public void OnData(TradeBars data)
        {
            if (_changes == SecurityChanges.None) return;

            if (this.Time.Hour==15 && this.Time.Minute == 49)
            {
                Liquidate();
            }

            if (this.Time.Hour==9 && this.Time.Minute == 31)
            {
	            foreach (var security in _changes.AddedSecurities)
	            {
	            	if (security.Price > 0.0m && IsPurchasable(security))
	            	{
		                Buy(security.Symbol, GetQuantity(security));
	            	}
	            }
            }
        }
        
        public bool IsPurchasable(Security security)
        {
        	bool ret = false;
        	
        	double totalcash = Convert.ToDouble(Portfolio.Cash * _targetpercent);
        	double price = Convert.ToDouble(security.Price);
        	int quantity = Convert.ToInt32(Math.Floor(totalcash / price));
        	
        	if (quantity > 0)
        	{
        		ret = true;
        	}
        	
        	return ret;
        }
        
        public int GetQuantity(Security security)
        {
        	int ret = 0;
        	
        	double totalcash = Convert.ToDouble(Portfolio.Cash * _targetpercent);
        	double price = Convert.ToDouble(security.Price);
        	int quantity = Convert.ToInt32(Math.Floor(totalcash / price));
        	
			ret = quantity;
        	
        	return ret;
        }

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            _changes = changes;
        }
    }
}
using QuantConnect.Orders.Slippage;

namespace QuantConnect
{
    class CustomSecurityInitializer : BrokerageModelSecurityInitializer
    {
        private readonly DataNormalizationMode _dataNormalizationMode;

        public CustomSecurityInitializer(IBrokerageModel brokerageModel, DataNormalizationMode dataNormalizationMode)
            : base(brokerageModel)
        {
            _dataNormalizationMode = dataNormalizationMode;
        }

        public override void Initialize(Security security)
        {
            base.Initialize(security);

            security.SetDataNormalizationMode(_dataNormalizationMode);
            security.SlippageModel = new ConstantSlippageModel(0.001m);
        }
    }
}