Overall Statistics
Total Trades
8
Average Win
26.53%
Average Loss
-20.51%
Compounding Annual Return
-0.184%
Drawdown
58.500%
Expectancy
0.147
Net Profit
-3.045%
Sharpe Ratio
0.043
Probabilistic Sharpe Ratio
0.000%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
1.29
Alpha
-0.038
Beta
0.551
Annual Standard Deviation
0.109
Annual Variance
0.012
Information Ratio
-0.735
Tracking Error
0.099
Treynor Ratio
0.009
Total Fees
$34.37
Estimated Strategy Capacity
$580000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
from AlgorithmImports import *
 
class FredAlternativeDataAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2003, 1, 1)
        self.SetEndDate(2019, 10, 11)
        self.SetCash(100000)

        self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
        
        # Requesting data
        self.fred_peak_to_trough = self.AddData(Fred, Fred.OECDRecessionIndicators.UnitedStatesFromPeakThroughTheTrough, Resolution.Daily).Symbol
        
        # Historical data
        history = self.History(self.fred_peak_to_trough, 60, Resolution.Daily)
        self.Debug(f"We got {len(history)} items from our history request")
        
    def OnData(self, data):
        if data.ContainsKey(self.fred_peak_to_trough) and data.ContainsKey(self.spy):
            peak_to_trough = data.Get(Fred, self.fred_peak_to_trough).Value
            
            # Buy SPY if peak to trough value is 1
            if peak_to_trough == 1 and not self.Portfolio.Invested:
                self.SetHoldings(self.spy, 1)
                
            # Liquidate holdings if peak to trough value is 0
            elif peak_to_trough == 0 and self.Portfolio.Invested:
                self.Liquidate(self.spy)