Overall Statistics
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Futures Example
    *
    *   QuantConnect allows importing generic data sources! This example demonstrates importing a futures
    *   data from the popular open data source Quandl.
    *
    *   QuantConnect has a special deal with Quandl giving you access to Stevens Continuous Futurs (SCF) for free. 
    *   If you'd like to download SCF for local backtesting, you can download it through Quandl.com.
    */
    public class QCUQuandlFutures : QCAlgorithm
    {
        string SPY = "SPY";
        string ACWI = "ACWI";
        string Tbill = "BIL";
        string Bonds = "AGG";

        Momentum _momSPY;
        Momentum _momACWI;
        Momentum _momTbill;
        DateTime sampledToday = DateTime.Now;
        
        

        
        public override void Initialize()
        {
            SetStartDate(2008, 6, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1)); 
            SetCash(25000);
            
            AddSecurity(SecurityType.Equity, SPY, Resolution.Minute);
            Securities[SPY].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);            
            AddSecurity(SecurityType.Equity, ACWI, Resolution.Minute);
            Securities[ACWI].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);            
            AddSecurity(SecurityType.Equity, Tbill, Resolution.Minute);
            Securities[Tbill].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);            
            AddSecurity(SecurityType.Equity, Bonds, Resolution.Minute);
            Securities[Bonds].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);      
            
            // define our daily trade bar consolidator. we can access the daily bar
            // from the DataConsolidated events
            var dailyConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(1));
            
            // attach our event handler. the event handler is a function that will be called each time we produce
            // a new consolidated piece of data.
            dailyConsolidator.DataConsolidated += OnDataDaily;

            // this call adds our daily consolidator to the manager to receive updates from the engine
            SubscriptionManager.AddConsolidator(SPY, dailyConsolidator);
            SubscriptionManager.AddConsolidator(ACWI, dailyConsolidator);
            SubscriptionManager.AddConsolidator(Tbill, dailyConsolidator);
            SubscriptionManager.AddConsolidator(Bonds, dailyConsolidator);
            
            _momSPY = MOM(SPY, 252, Resolution.Daily);
            _momACWI = MOM(ACWI, 252, Resolution.Daily);
            _momTbill = MOM(Tbill, 252, Resolution.Daily);
        }
           
        
     
        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        private void OnDataDaily(object sender, TradeBar consolidated)
        {
            if (!_momSPY.IsReady) return;
            
            //One data point per m:

            //Only take one data point per day (opening price)
            decimal holdingPercent = .5m;
            
            string etf;
            decimal _momStrong;
            
            if (_momSPY >= _momACWI)
            {
                etf = SPY;
                _momStrong = _momSPY;
            } else
            {
                etf = ACWI;
                _momStrong = _momACWI;
            }
           
            if (_momStrong > _momTbill)
            {
                if(Portfolio[etf].Quantity > 0) return;
                
                Liquidate();
                SetHoldings(etf, holdingPercent);
                Log("Set Holdings to " + Portfolio[etf].Quantity + "of " + etf);
            } else
            {
                if(Portfolio[Bonds].Quantity > 0) return;
                
                Liquidate();
                SetHoldings(Bonds, holdingPercent);
                Log("Set Holdings to " + Portfolio[Bonds].Quantity + "of " + Bonds);
            }
        }
    }
}