Overall Statistics
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;

namespace QuantConnect {

    //
    //	Make sure to change "BasicTemplateAlgorithm" to your algorithm class name, and that all
    //	files use "public partial class" if you want to split up your algorithm namespace into multiple files.
    //

    public class Handler : QCAlgorithm, IAlgorithm {
        
         string symbol="SPY";
         decimal sevenPoint=1000;
         Resolution myResolution=Resolution.Minute;
         bool myDataFill = true;
         int myLeverage = 1;
         bool extraHours = false;
         
        public override void Initialize() { 
            SetCash(20000);
            SetStartDate(2014, 01, 01);
            SetEndDate(2014, 10, 20);  
            AddSecurity(SecurityType.Equity, symbol, myResolution,myDataFill,myLeverage,extraHours);
        }
        
        public void OnData(TradeBars data) {
            sevenPoint=(3*data[symbol].Close+2*data[symbol].Open+data[symbol].Low+data[symbol].High)/7;
            SimpleMovingAverage sma5 = new SimpleMovingAverage(15);
            decimal sma = sma5.AddSample(sevenPoint);
            
            if (sma < data[symbol].Close) {
                SetHoldings(symbol, 0.5);
            }
            if (sma > data[symbol].Close) {
                SetHoldings(symbol, -0.9);
            }
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic; 
using System.Collections.Concurrent;
using System.Text;
using System.Linq;
using QuantConnect.Models;

namespace QuantConnect {
    
    /*
    *   SMA Indicator: Online calculator for faster backtesting.
    *
    *   To use this indicator: 
    *   1. Create an instance of it in your algorithm:
    *   SMA sma10 = new SMA(10);
    *   
    *   2. Push in data with AddSample:
    *   decimal sma = sma10.AddSample(data["spy"].Close);
    *
    *   3. If you're sensitive to the precise SMA values you push wait until the indicator is Ready.
    */
    
    public class SimpleMovingAverage 
    {
        //Class Variables:
        private int period, samples;
		private decimal sma, sum, divisor;
		private FixedSizedQueue<decimal> sampleQueue;
		
		// Initialise the Simple Moving Average
        public SimpleMovingAverage(int period) {
            this.period = period;
            this.samples = 0;
            this.sampleQueue = new FixedSizedQueue<decimal>(period);
        }
		
		//Public Result Access: Current value of the SMA.
		public decimal SMA {
			get{ return sma;}
		}
		
		//Public Result Access: Track the number of samples:
		public int Samples {
		    get { return samples; }
		}
		
		//Public Result Access: We've got sufficient data samples to know its the SMA.
		public bool Ready {
		    get { return samples >= period; }
		}
		
		// Online implementation of simple moving average 
        public decimal AddSample(decimal quote)
        {
            samples++;
            sum += quote;
		  
            //Add this sample to the SMA, subtract the previous
            sampleQueue.Enqueue(quote);
            if (sampleQueue.Size == period) {
                //"last" is a dequeued item: minus it from the SMA.
                sum -= sampleQueue.LastDequeued;
            }
            
            //When less than period samples, only divide by the number of samples.
            if (samples < period) {
                divisor = samples;
            } else {
                divisor = period;
            }
            
            sma = sum / divisor;
            
		    return sma;
        }
        
        
        //Fixed length queue that dumps things off when no more space in queue.
        private class FixedSizedQueue<T> : ConcurrentQueue<T> {
            public int Size { get; private set; }
            public T LastDequeued { get; private set; }
            public FixedSizedQueue(int size) { Size = size; }
            public new void Enqueue(T obj) {
                base.Enqueue(obj);
                lock (this) {
                    if (base.Count > Size) {
                        T outObj;
                        base.TryDequeue(out outObj);
                        LastDequeued = outObj;
                    }
                }
            }
        }
        
    }
}