Overall Statistics
Total Orders
11
Average Win
31.21%
Average Loss
0%
Compounding Annual Return
7.318%
Drawdown
31.100%
Expectancy
0
Start Equity
100000
End Equity
545010.48
Net Profit
445.010%
Sharpe Ratio
0.32
Sortino Ratio
0.262
Probabilistic Sharpe Ratio
0.172%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.016
Beta
0.476
Annual Standard Deviation
0.111
Annual Variance
0.012
Information Ratio
-0.039
Tracking Error
0.116
Treynor Ratio
0.075
Total Fees
$93.78
Estimated Strategy Capacity
$1700000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
0.13%
#region imports
    using Newtonsoft.Json;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Globalization;
    using System.Drawing;
    using QuantConnect;
    using QuantConnect.Algorithm.Framework;
    using QuantConnect.Algorithm.Framework.Selection;
    using QuantConnect.Algorithm.Framework.Alphas;
    using QuantConnect.Algorithm.Framework.Portfolio;
    using QuantConnect.Algorithm.Framework.Portfolio.SignalExports;
    using QuantConnect.Algorithm.Framework.Execution;
    using QuantConnect.Algorithm.Framework.Risk;
    using QuantConnect.Algorithm.Selection;
    using QuantConnect.Api;
    using QuantConnect.Parameters;
    using QuantConnect.Benchmarks;
    using QuantConnect.Brokerages;
    using QuantConnect.Configuration;
    using QuantConnect.Util;
    using QuantConnect.Interfaces;
    using QuantConnect.Algorithm;
    using QuantConnect.Indicators;
    using QuantConnect.Data;
    using QuantConnect.Data.Auxiliary;
    using QuantConnect.Data.Consolidators;
    using QuantConnect.Data.Custom;
    using QuantConnect.Data.Custom.IconicTypes;
    using QuantConnect.DataSource;
    using QuantConnect.Data.Fundamental;
    using QuantConnect.Data.Market;
    using QuantConnect.Data.Shortable;
    using QuantConnect.Data.UniverseSelection;
    using QuantConnect.Notifications;
    using QuantConnect.Orders;
    using QuantConnect.Orders.Fees;
    using QuantConnect.Orders.Fills;
    using QuantConnect.Orders.OptionExercise;
    using QuantConnect.Orders.Slippage;
    using QuantConnect.Orders.TimeInForces;
    using QuantConnect.Python;
    using QuantConnect.Scheduling;
    using QuantConnect.Securities;
    using QuantConnect.Securities.Equity;
    using QuantConnect.Securities.Future;
    using QuantConnect.Securities.Option;
    using QuantConnect.Securities.Positions;
    using QuantConnect.Securities.Forex;
    using QuantConnect.Securities.Crypto;
    using QuantConnect.Securities.CryptoFuture;
    using QuantConnect.Securities.Interfaces;
    using QuantConnect.Securities.Volatility;
    using QuantConnect.Storage;
    using QuantConnect.Statistics;
    using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
    using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion

namespace QuantConnect
{
    public class FredAlternativeDataAlgorithm : QCAlgorithm
    {
        private Symbol _fredPeakToTrough;
        private Symbol _spy;
        
        public override void Initialize()
        {
            SetStartDate(2000, 1, 1);
            SetEndDate(2023, 12, 31);
            SetCash(100000);
            
            _spy = AddEquity("SPY", Resolution.Daily).Symbol;
            
            // Requesting FED US peak-to-trough OECD recession indicators for trade signal generation
            _fredPeakToTrough = AddData<Fred>(Fred.OECDRecessionIndicators.UnitedStatesFromPeakThroughTheTrough).Symbol;
            
            // Historical data
            var history = History<Fred>(_fredPeakToTrough, 60, Resolution.Daily);
            Debug($"We got {history.Count()} items from our history request");
        }

        public override void OnData(Slice slice)
        {
            // Trade with updated FED peak-to-trough indicator
            if (slice.ContainsKey(_fredPeakToTrough) && slice.ContainsKey(_spy))
            {
                var peakToTrough = slice.Get<Fred>(_fredPeakToTrough).Value;
                
                // Buy SPY if peak to trough value is 0, which is the expansionary period
                if (peakToTrough == 0m && !Portfolio.Invested)
                {
                    SetHoldings(_spy, 1);
                }
                
                // Liquidate holdings if peak to trough value is 1, which is recessionary period
                else if (peakToTrough == 1m && Portfolio.Invested)
                {
                    Liquidate(_spy);
                }
            }
        }
    }
}