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(2004, 1, 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);            
            
            _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.
        public void OnData(TradeBars data) 
        {
            if (!_momSPY.IsReady) return;
            
            //One data point per m:
            if (sampledToday.Month == data.Time.Month) return;
            
            //Only take one data point per day (opening price)
            sampledToday = data.Time;
            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);
            }
        }
    }
}