Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-20.823%
Drawdown
12.600%
Expectancy
0
Net Profit
0%
Sharpe Ratio
-1.268
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.179
Beta
0.15
Annual Standard Deviation
0.164
Annual Variance
0.027
Information Ratio
-0.056
Tracking Error
0.215
Treynor Ratio
-1.386
Total Fees
$1.00
using System;
using System.Collections;
using System.Collections.Generic; 
using QuantConnect.Securities;  
using QuantConnect.Models;   
using QuantConnect.Data.Consolidators;

namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Full Basic Template:
    *
    *   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full algorithm can be found at:
    *   https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
    */
    
    public class Candle {
            
        public decimal Open = 0;
        public decimal High = Decimal.MinValue;
        public decimal Low = Decimal.MaxValue;
        public decimal Close = 0;
        public int Samples = 0;
        
        public void Update(TradeBar bar) {
            if (Open == 0) Open = bar.Open;
            if (High < bar.High) High = bar.High;
            if (Low > bar.Low) Low = bar.Low;
            Close = bar.Close;
            Samples++;
        }
    }
        
        
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        //Initialize the data and resolution you require for your strategy:
        
        int curQty;
        
        string ticker = "SPY";
                
        TradeBarConsolidator _consolidator;
        
        public override void Initialize() 
        {
			
            //Start and End Date range for the backtest:
            SetStartDate(2015, 6, 7);         
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            
            //Cash allocation
            SetCash(10000);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, ticker, Resolution.Minute);
            
            _consolidator = new TradeBarConsolidator( TimeSpan.FromHours(8) );
            _consolidator.DataConsolidated += EightHourBars;
            SubscriptionManager.AddConsolidator(ticker,_consolidator);
			
			
			
        	// schedules the 'OnFiveMinutesAfterMarketOpen' function to run 5 minutes after the Symbol's market open
        	Schedule.On(DateRules.EveryDay(ticker), TimeRules.AfterMarketOpen(ticker, 5/*minutes after open*/), OnFiveMinutesAfterMarketOpen);
        	
        	// schedules the 'OnFiveMinutesBeforeMarketClose' function to run 5 minutes before the Symbol's market close
        	Schedule.On(DateRules.EveryDay(ticker), TimeRules.BeforeMarketClose(ticker, 5/*minutes before close*/), OnFiveMinutesBeforeMarketClose);
        }
        
        public void OnFiveMinutesAfterMarketOpen()
        {
        	Security selectedSecurity= Securities[ticker];
        	// updates using the price at 9:35am
        	Debug("Open " + Time.ToString("u") + selectedSecurity.Close);
        }
        
        public void OnFiveMinutesBeforeMarketClose()
        {
        	Security selectedSecurity= Securities[ticker];
        	// updates using the price at 3:55pm
        	Debug("Close " + Time.ToString("u") + selectedSecurity.Close);
        }
        
        public void EightHourBars(Object o, TradeBar bar) 
        {
            Debug(Time.ToString("u") + " Close Price: " + bar.Close);
        }

        //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) 
        {   
            // "TradeBars" object holds many "TradeBar" objects: it is a dictionary indexed by the symbol:
            // 
            //  e.g.  data["MSFT"] data["GOOG"]
            
            
            if (!Portfolio.HoldStock) 
            {
            
                curQty = (int)Math.Floor(Portfolio.Cash / data[ticker].Open);
                
                //Order function places trades: enter the string symbol and the quantity you want:
                Order(ticker,  curQty);
                
                //Debug sends messages to the user console: "Time" is the algorithm time keeper object 
                Debug("Purchased " + ticker + " on " + Time.ToShortDateString());
                
                //You can also use log to send longer messages to a file. You are capped to 10kb
                //Log("This is a longer message send to log.");
            }
            if (Portfolio.HoldStock)
            {
            	//Order(ticker, curQty * -1);
            }
        }
    }
}