Overall Statistics
Total Trades
16
Average Win
14.27%
Average Loss
-1.85%
Compounding Annual Return
2.853%
Drawdown
52.400%
Expectancy
4.449
Net Profit
78.550%
Sharpe Ratio
0.218
Loss Rate
38%
Win Rate
62%
Profit-Loss Ratio
7.72
Alpha
0.001
Beta
1.924
Annual Standard Deviation
0.125
Annual Variance
0.016
Information Ratio
0.108
Tracking Error
0.125
Treynor Ratio
0.014
Total Fees
$79.14
from QuantConnect.Python import PythonQuandl

class BasicTemplateAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(1998,1, 2)  #Set Start Date
        self.SetEndDate(2018,8,5)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        self.AddEquity("SPY", Resolution.Daily)
        self.qGDP = "FRED/GDP"
        self.qNCBCEL = "FRED/NCBCEL"
        Quandl.SetAuthCode("YjQrjz6zF8mVWdGTz3fa")
        self.AddData(Fred, self.qGDP, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Fred, self.qNCBCEL, Resolution.Daily, TimeZones.NewYork, True)
        self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.At(23, 0), self.rebalance)
        self.months = -1
        self.quarterly_rebalance = False

    def rebalance(self):
        #quarterly rebalance
        self.months+=1
        if self.months%3 == 0:
            self.quarterly_rebalance = True
        if self.quarterly_rebalance:
            indicator = float(self.Securities[self.qNCBCEL].Price)/float(self.Securities[self.qGDP].Price) 
            self.Plot("Buffet Indicator","Indicator", indicator)
            if indicator <= 1:
                self.SetHoldings("SPY",1)
            else:
                if self.Portfolio["SPY"].Invested:
                        self.Liquidate()
            self.quarterly_rebalance = False
            self.prev_value = indicator
    
    def OnData(self, data):
        pass

class Fred(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = 'value'