Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
61.179%
Drawdown
1.000%
Expectancy
0
Net Profit
0%
Sharpe Ratio
7.646
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.131
Beta
0.493
Annual Standard Deviation
0.06
Annual Variance
0.004
Information Ratio
-3.364
Tracking Error
0.061
Treynor Ratio
0.931
Total Fees
$1.00
namespace QuantConnect 
{
    public class ConsolidatorAlgorithm : QCAlgorithm
    {
        public override void Initialize()
        {
            SetStartDate(2013, 1, 1);         
            SetEndDate(2013, 2, 1); 
            SetCash(25000);
            
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            
            // define our 15 minute consolidator
            var fifteenMinuteConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(15));
            
            // if we want to make decisions every 15 minutes as well, we can add an event handler
            // to the DataConsolidated event
            fifteenMinuteConsolidator.DataConsolidated += OnFiftenMinuteSPY;
            
            int fast = 15;
            int slow = 30;
            
            // EDIT:: If we want something other than the closing value of the bar to be pushed into our
            //        indicators, then we can specify that in the RegisterIndicator method as shown b
            
            // define our EMA, we'll manually register this, so we aren't using the helper function 'EMA(...)'
            var fastEmaOnFifteenMinuteBars = new ExponentialMovingAverage("SPY_High_EMA15", fast);
            var slowEmaOnFifteenMinuteBars = new ExponentialMovingAverage("SPY_Low_EMA30", slow);
            
            // we can define complex indicator's using various extension methods.
            // here I use the 'Over' extension method which performs division
            // so this will be fast/slow. This returns a new indicator that represents
            // the division operation between the two
            var ratio = fastEmaOnFifteenMinuteBars.Over(slowEmaOnFifteenMinuteBars, "SPY_Ratio_EMA");
            
            // now we can use the 'Of' extension method to define the ROC on the ratio
            // The 'Of' extension method allows combining multiple indicators together such
            // that the data from one gets sent into the other
            var rocpOfRatio = new RateOfChangePercent("SPY_ROCP_Ratio", fast).Of(ratio);
            
            // we an even define a smoothed version of this indicator
            var smoothedRocpOfRatio = new ExponentialMovingAverage("SPY_Smoothed_ROCP_Ratio", 5).Of(rocpOfRatio);
            
            // register our indicator and consolidator together. this will wire the consolidator up to receive
            // data for the specified symbol, and also set up the indicator to receive its data from the consolidator
            
            // EDIT:: If we want to register these indicators to receive something other than the close
            //        bar value then we can define a 'selector' to pass in to the RegisterIndicator method
            //        The selector has a type of Func<BaseData, close> which is a function that accepts a
            //        BaseData as input and returns a decimal value, here I used the Field.High and Field.Low
            //        helper propertis to return the selector
            
            RegisterIndicator("SPY", fastEmaOnFifteenMinuteBars, fifteenMinuteConsolidator, Field.High);
            RegisterIndicator("SPY", slowEmaOnFifteenMinuteBars, fifteenMinuteConsolidator, Field.Low);
            
            // register the indicator to be plotted along
            PlotIndicator("SPY", fastEmaOnFifteenMinuteBars);
            PlotIndicator("SPY", slowEmaOnFifteenMinuteBars);
            PlotIndicator("SPY_ROCP_Ratio", rocpOfRatio, smoothedRocpOfRatio);
            PlotIndicator("SPY_Ratio_EMA", ratio);
        }
        
        //15 minute events here:
        public void OnFiftenMinuteSPY(object sender, TradeBar data)
        {
            if (!Portfolio.Invested)
            {
                SetHoldings("SPY", 1.0);
            }
        }

        //Traditional 1 minute events here:
        public void OnData(TradeBars data) 
        {
        }
    }
}