Overall Statistics
Total Trades
5
Average Win
7.97%
Average Loss
-11.27%
Compounding Annual Return
-0.353%
Drawdown
19.500%
Expectancy
-0.146
Net Profit
-6.677%
Sharpe Ratio
-0.059
Probabilistic Sharpe Ratio
0.000%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
0.71
Alpha
-0.002
Beta
0.001
Annual Standard Deviation
0.037
Annual Variance
0.001
Information Ratio
-0.375
Tracking Error
0.174
Treynor Ratio
-1.883
Total Fees
$11.89
using QuantConnect.Data.Custom.USTreasury;

namespace QuantConnect.Algorithm.CSharp
{
    public class USTreasuryYieldCurveAlgorithm : QCAlgorithm
    {
		private Symbol _yieldCurve;
		private Symbol _spy;
		private DateTime _lastInversion = DateTime.MinValue;

        public override void Initialize()
        {
            SetStartDate(2000, 3, 1);
            SetEndDate(2019, 9, 15);
            SetCash(100000);
            
            _spy = AddEquity("SPY", Resolution.Hour).Symbol;
			_yieldCurve = AddData<USTreasuryYieldCurveRate>("USTYCR").Symbol;
			
			// Request 60 days of history with the USTreasuryYieldCurveRate custom data Symbol.
			var history = History<USTreasuryYieldCurveRate>(_yieldCurve, 60, Resolution.Daily);
			
			// Count the number of items we get from our history request
            Debug($"We got {history.Count()} items from our history request");
        }

        public override void OnData(Slice data)
        {
            if (!data.ContainsKey(_yieldCurve))
            {
            	return;
            }
            
            // Preserve null values by getting the data with `slice.Get<T>`
            // Accessing the data using `data[_yieldCurve]` results in null
            // values becoming `default(decimal)` which is equal to 0
            var rates = data.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 SPY for two years
            if (!Portfolio.Invested && rates.TwoYear > rates.TenYear)
            {
            	Debug($"{Time} - Yield curve inversion! Shorting the market for two years");
            	SetHoldings(_spy, -0.5);
            	
            	_lastInversion = Time;

            	return;
            }

            // If two years have passed, liquidate our position in SPY
            if (Time - _lastInversion >= TimeSpan.FromDays(365 * 2)) 
            {
            	Liquidate(_spy);
            }
        }
    }
}