Overall Statistics
Total Orders
5
Average Win
7.96%
Average Loss
-11.25%
Compounding Annual Return
-1.575%
Drawdown
38.300%
Expectancy
-0.146
Net Profit
-28.652%
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.64
Estimated Strategy Capacity
$130000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
0.03%
#region imports
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Globalization;
    using System.Drawing;
    using QuantConnect;
    using System.Text.RegularExpressions;
    using QuantConnect.Algorithm.Framework;
    using QuantConnect.Algorithm.Framework.Selection;
    using QuantConnect.Algorithm.Framework.Alphas;
    using QuantConnect.Algorithm.Framework.Portfolio;
    using QuantConnect.Algorithm.Framework.Execution;
    using QuantConnect.Algorithm.Framework.Risk;
    using QuantConnect.Algorithm.Selection;
    using QuantConnect.Parameters;
    using QuantConnect.Benchmarks;
    using QuantConnect.Brokerages;
    using QuantConnect.Util;
    using QuantConnect.Interfaces;
    using QuantConnect.Algorithm;
    using QuantConnect.Indicators;
    using QuantConnect.Data;
    using QuantConnect.Data.Consolidators;
    using QuantConnect.Data.Custom;
    using QuantConnect.DataSource;
    using QuantConnect.Data.Fundamental;
    using QuantConnect.Data.Market;
    using QuantConnect.Data.UniverseSelection;
    using QuantConnect.Notifications;
    using QuantConnect.Orders;
    using QuantConnect.Orders.Fees;
    using QuantConnect.Orders.Fills;
    using QuantConnect.Orders.Slippage;
    using QuantConnect.Scheduling;
    using QuantConnect.Securities;
    using QuantConnect.Securities.Equity;
    using QuantConnect.Securities.Future;
    using QuantConnect.Securities.Option;
    using QuantConnect.Securities.Forex;
    using QuantConnect.Securities.Crypto;   
    using QuantConnect.Securities.Interfaces;
    using QuantConnect.Storage;
    using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
    using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion

using QuantConnect.DataSource;

namespace QuantConnect.Algorithm.CSharp.AltData
{
    public class USTreasuryDataAlgorithm : QCAlgorithm
    {
        private Symbol _spySymbol;
        private Symbol _yieldCurveSymbol;
        private DateTime _lastInversion = DateTime.MinValue;
        
        public override void Initialize()
        {
            SetStartDate(2000, 3, 1);
            SetEndDate(2021, 6, 1);
            SetCash(100000);

            _spySymbol = AddEquity("SPY", Resolution.Hour).Symbol;

            // Requestion data
            _yieldCurveSymbol = AddData<USTreasuryYieldCurveRate>("USTYCR").Symbol;

            // Historical data
            var history = History<USTreasuryYieldCurveRate>(_yieldCurveSymbol, 60, Resolution.Daily);
            Debug($"We got {history.Count()} items from our history request");
        }
        
        
        public override void OnData(Slice slice)
        {
            if (!slice.ContainsKey(_yieldCurveSymbol))
            {
                return;
            }
            
            // Preserve null values by getting the data with `slice.Get<T>`
            // Accessing the data using `slice[_yieldCurveSymbol]` results in null
            // values becoming `default(decimal)` which is equal to 0
            var rates = slice.Get<USTreasuryYieldCurveRate>().Values.First();
            
            // Check for null before using the values
            if (!rates.TenYear.HasValue || !rates.TwoYear.HasValue)
            {
                return;
            }
            
            // Only advance if a year has gone by
            if (Time - _lastInversion < TimeSpan.FromDays(365))
            {
                return;
            }
            
            // if there is a yield curve inversion after not having one for a year, short sell SPY for two years
            if (!Portfolio.Invested && rates.TwoYear > rates.TenYear)
            {
                Debug($"{Time} - Yield curve inversion! Shorting the market for two years");
                SetHoldings(_spySymbol, -0.5); 
                 _lastInversion = Time;
                return;
            }
            
            
            // If two years have passed, liquidate our position in SPY
            if (Time - _lastInversion >= TimeSpan.FromDays(365 * 2))
            {
                Liquidate(_spySymbol);
            }
        }
    }
}