Overall Statistics
Total Orders
5
Average Win
7.96%
Average Loss
-11.26%
Compounding Annual Return
-1.570%
Drawdown
38.300%
Expectancy
-0.146
Start Equity
100000
End Equity
71425.38
Net Profit
-28.575%
Sharpe Ratio
-0.631
Sortino Ratio
-0.42
Probabilistic Sharpe Ratio
0.000%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
0.71
Alpha
-0.023
Beta
-0.159
Annual Standard Deviation
0.047
Annual Variance
0.002
Information Ratio
-0.385
Tracking Error
0.191
Treynor Ratio
0.188
Total Fees
$12.68
Estimated Strategy Capacity
$130000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
0.03%
from AlgorithmImports import *
from QuantConnect.DataSource import *

class USTreasuryDataAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2000, 3, 1)
        self.set_end_date(2021, 6, 1)
        self.set_cash(100000)

        self.spy_symbol = self.add_equity("SPY", Resolution.HOUR).symbol

        # Requesting data
        self.yield_curve_symbol = self.add_data(USTreasuryYieldCurveRate, "USTYCR").symbol

        # Historical data
        history = self.history(USTreasuryYieldCurveRate, self.yield_curve_symbol, 60, Resolution.DAILY)
        self.debug(f"We got {len(history)} items from our history request")
        
        self.last_inversion = datetime.min

    def on_data(self, slice: Slice) -> None:
        if not slice.contains_key(self.yield_curve_symbol):
            return
        
        rates = slice[self.yield_curve_symbol]
        
        # Check for null before using the values
        if not (rates.ten_year is not None and rates.two_year is not None):
            return
        
        # Only advance if a year has gone by
        if (self.time - self.last_inversion < timedelta(days=365)):
            return
        
        # if there is a yield curve inversion after not having one for a year, short sell SPY for two years
        if (not self.portfolio.invested and rates.two_year > rates.ten_year):
            self.debug(f"{self.time} - Yield curve inversion! Shorting the market for two years")
            self.set_holdings(self.spy_symbol, -0.5)
            self.last_inversion = self.time
            return
        
        # If two years have passed, liquidate our position in SPY
        if (self.time - self.last_inversion >= timedelta(days=365 * 2)):
            self.liquidate(self.spy_symbol)