Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
43.759%
Drawdown
2.300%
Expectancy
0
Net Profit
0%
Sharpe Ratio
2.131
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.265
Beta
0.249
Annual Standard Deviation
0.142
Annual Variance
0.02
Information Ratio
1.045
Tracking Error
0.148
Treynor Ratio
1.211
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic; 
using QuantConnect.Securities;  
using QuantConnect.Models;    

namespace QuantConnect {  
    
    public partial class QCUDataFiltering : QCAlgorithm 
    {
        //Create new bars from ticks list:
        private Consolidator _barCreator = new Consolidator();
        
        public override void Initialize()
        {
            SetCash(25000);
            SetStartDate(2014, 6, 1);          
            SetEndDate(2014, 6, 15); 
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Tick);
            
            //Add our custom data filter.
            Securities["SPY"].DataFilter = new ExchangeDataFilter();
        }
        
        //TradeBars event handler:
        public void OnData (TradeBars data)
        {
            if (!data.ContainsKey("SPY")) return;
            
            // Here:: >> data["SPY"] is a 1 second, filtered tradeBar.
            var spy = data["SPY"];
            //Log(spy.Time.ToShortTimeString() + "," + spy.Open.ToString("C") + "," + spy.High.ToString("C") + "," + spy.Low.ToString("C") + "," + spy.Close.ToString("C"));
        }
        
        // Data arriving here will now be filtered.
        public void OnData( Ticks data )
        {
            //Generate the bars:
            _barCreator.SetTicks(data);
            var bars = _barCreator.GenerateBars();
            
            //Fire the tradebars event handler during market hours
            if (bars["SPY"].Time.TimeOfDay.TotalHours > 9.5 && bars["SPY"].Time.TimeOfDay.TotalHours < 16) {
                OnData(bars);
            }
            
            //foreach (var tick in data["SPY"]) Log(tick.Exchange);
            if (!Portfolio.Invested) SetHoldings("SPY", 1);    
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;

namespace QuantConnect {

    public class ExchangeDataFilter : ISecurityDataFilter
    {
        /// <summary>
        /// Global Market Short Codes and their full versions: (used in tick objects)
        /// https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Common/Global.cs
        /// </summary>
        public static class MarketCodesFilter 
        {
            /// US Market Codes
            public static Dictionary<string, string> US = new Dictionary<string, string>() 
            {
                {"A", "American Stock Exchange"},
                {"B", "Boston Stock Exchange"},
                {"C", "National Stock Exchange"},
                {"D", "FINRA ADF"},
                {"I", "International Securities Exchange"},
                {"J", "Direct Edge A"},
                {"K", "Direct Edge X"},
                {"M", "Chicago Stock Exchange"},
                {"N", "New York Stock Exchange"},
                {"P", "Nyse Arca Exchange"},
                {"Q", "NASDAQ OMX"},
                {"T", "NASDAQ OMX"},
                {"U", "OTC Bulletin Board"},
                {"u", "Over-the-Counter trade in Non-NASDAQ issue"},
                {"W", "Chicago Board Options Exchange"},
                {"X", "Philadelphia Stock Exchange"},
                {"Y", "BATS Y-Exchange, Inc"},
                {"Z", "BATS Exchange, Inc"}
            };
    
            /// Canada Market Short Codes:
            public static Dictionary<string, string> Canada = new Dictionary<string, string>() 
            {
                {"T", "Toronto"},
                {"V", "Venture"}
            };
            
            // Allowed exchanges for this filter: top 4
            public static List<string> AllowedExchanges = new List<string>() { 
                "P",    //NYSE ARCA - SPY PRIMARY EXCHANGE
                        //https://www.google.com/finance?q=NYSEARCA%3ASPY&ei=XcA2VKCSLs228waMhYCIBg
                
                /*
                "N",    //NYSE
                "Z",    //BATS
                "Q",    //NASDAQ
                "T"*/     //NASDAQ
            }; 
        }
        
        
        /// <summary>
        /// Filter out a tick from this vehicle, with this new data:
        /// </summary>
        /// <param name="data">New data packet:</param>
        /// <param name="vehicle">Vehicle of this filter.</param>
        public bool Filter(Security asset, BaseData data)
        {
            // TRUE -->  Accept Tick
            // FALSE --> Reject Tick
            var tick = data as Tick;

            // This is a tick bar
            if (tick != null)
            {
                if (MarketCodesFilter.AllowedExchanges.Contains(tick.Exchange))
                {
                    return true;   
                }
            }
            
            //Only allow those exchanges through.
            return false;
        }
        
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;

namespace QuantConnect {
    
	public class Consolidator {
	    
        private Ticks tickList;
        
        //Initialize the Consolidator
        public Consolidator() {
            // Queue to store the ticks temporarily.
            this.tickList = new Ticks(new DateTime());
        }
        
        // Add a bar to the list, when it totals X bars return a new tradebar.
        public void SetTicks(Ticks tickList) {
            this.tickList = tickList;
        }
        
        //Using the barQueue generate a new "consolidated bar" then return
        // https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Common/Data/Market/TradeBars.cs
        // https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Common/Data/Market/Ticks.cs
        public TradeBars GenerateBars() {
            
            //Return array of tradebars:
            TradeBars bars = new TradeBars();
            
            //Create the new bar for each symbol:
            foreach (string tickSymbol in tickList.Keys)
            {
                string symbol = "";
                long volume = 0;
                DateTime barOpenTime = new DateTime();
                decimal open = Decimal.Zero, high = Decimal.MinValue, low = Decimal.MaxValue, close = Decimal.Zero;
                
                foreach(Tick tick in tickList[tickSymbol]) 
                {
                    if (barOpenTime == new DateTime()) barOpenTime = tick.Time;
                    if (symbol == "") symbol = tick.Symbol;
                    if (open == Decimal.Zero) open = tick.Price;
                    if (high < tick.Price) high = tick.Price;
                    if (low > tick.Price) low = tick.Price;
                    close = tick.Price;
                    volume += tick.Quantity;
                }
                
                if (!bars.ContainsKey(tickSymbol)) {
                    bars.Add(tickSymbol, new TradeBar(barOpenTime, symbol, open, high, low, close, volume));
                }
            }
            
            //Create the new trade bar.
            return bars;
        }
        
    }
}