Overall Statistics
Total Trades
19
Average Win
14.19%
Average Loss
-0.92%
Compounding Annual Return
9.183%
Drawdown
16.000%
Expectancy
14.206
Net Profit
293.959%
Sharpe Ratio
0.613
Loss Rate
8%
Win Rate
92%
Profit-Loss Ratio
15.47
Alpha
-0.001
Beta
4.883
Annual Standard Deviation
0.108
Annual Variance
0.012
Information Ratio
0.486
Tracking Error
0.108
Treynor Ratio
0.014
Total Fees
$180.32
from QuantConnect.Python import PythonQuandl

class BasicTemplateAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2003,1, 1)  #Set Start Date
        self.SetEndDate(2018,8,5)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        self.AddEquity("SPY", Resolution.Daily)
        self.AddEquity("QQQ", Resolution.Daily)
        self.AddEquity("AGG", 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
        #to help notice trends
        self.prev_value = 0

    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:
                if self.prev_value < indicator:
                    if self.Portfolio["AGG"].Invested:
                        self.Liquidate()
                    self.SetHoldings("QQQ",1)
            else:
                if self.Portfolio["QQQ"].Invested:
                        self.Liquidate()
                self.SetHoldings("AGG",1)
            self.quarterly_rebalance = False
            self.prev_value = indicator
    
    def OnData(self, data):
        pass

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