Overall Statistics
import numpy as np
import talib as ta

### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2006,1, 1)  #Set Start Date
        #self.SetEndDate(2013,10,11)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.AddEquity("SPY", Resolution.Minute)
        self.AddEquity('GLD', Resolution.Minute)
        
        self.Schedule.On(self.DateRules.EveryDay('GLD'), self.TimeRules.BeforeMarketClose('GLD', 2), Action(self.trade))
        

    def trade(self):
        
        h = self.History(['GLD'], 250, Resolution.Daily).unstack(level=0).close
        
        avg = ta.EMA(h['GLD'].values, 200)[-1]
        close = h['GLD'].iloc[-1]
        
        
        if close > avg * (1 + 0.001):
            self.SetHoldings('GLD', 1)
        elif close < avg * (1 - 0.001):
            self.Liquidate()