Overall Statistics
Total Trades
102
Average Win
7.55%
Average Loss
-1.83%
Compounding Annual Return
3.575%
Drawdown
34.700%
Expectancy
0.741
Net Profit
57.971%
Sharpe Ratio
0.311
Loss Rate
66%
Win Rate
34%
Profit-Loss Ratio
4.12
Alpha
0.106
Beta
-2.986
Annual Standard Deviation
0.149
Annual Variance
0.022
Information Ratio
0.177
Tracking Error
0.149
Treynor Ratio
-0.016
Total Fees
$669.23
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()