I am trying to create a weekly consolidator to calculate ADX for SPY. the problem is that the values are wayyyyyyyy off. The ADX values show up in the 80s whereas in reality the values were in the 20s. DI+ and DI- are also off.

 

namespace QuantConnect
{
    public class ADXData
    {
        private QCAlgorithm _bot;
        private Symbol _symbol;
        public AverageDirectionalIndex DailyAdx { get; private set; }
        public AverageDirectionalIndex WeeklyAdx { get; private set; }
        private TradeBarConsolidator _weeklyConsolidator;

        public ADXData(OptionsTradingBot algorithm, Symbol symbol)
        {
            _bot = algorithm;
            _symbol = symbol;

            // Initialize daily ADX with a 14-period
            DailyAdx = _bot.ADX(symbol, 14, Resolution.Daily);

            // Initialize the weekly ADX with a 14-period (adjust period if necessary)
            WeeklyAdx = new AverageDirectionalIndex(14);

            // Use a consolidator to manually aggregate daily bars into weekly bars (5 trading days)
            _weeklyConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(7));
            _weeklyConsolidator.DataConsolidated += OnWeeklyConsolidatorBar;

            // Add the consolidator for daily data
            algorithm.SubscriptionManager.AddConsolidator(symbol, _weeklyConsolidator);

            // Warm up the indicators with historical data
            WarmUpIndicators(algorithm, symbol);
        }

        private void WarmUpIndicators(OptionsTradingBot algorithm, Symbol symbol)
        {
            // Get daily history for warming up the weekly bars
            var dailyHistory = algorithm.History<TradeBar>(symbol, 100, Resolution.Daily);
            foreach (var bar in dailyHistory)
            {
                // Warm up the daily ADX
                DailyAdx.Update(bar);

                // Feed the historical data into the weekly consolidator to build weekly bars
                _weeklyConsolidator.Update(bar);
            }
        }

        // Simplified: Removed RollingWindow and adjusted to just use the ADX directly
        private void OnWeeklyConsolidatorBar(object sender, TradeBar consolidatedWeeklyBar)
        {
            // Ensure the weekly bar is not created on Sundays or intraday times
            if (consolidatedWeeklyBar.Time.DayOfWeek != DayOfWeek.Sunday)
            {
                // Log the consolidated bar details for debugging
                LogHelper.LogInfo(_bot, $"Consolidated Weekly Bar: O={consolidatedWeeklyBar.Open}, H={consolidatedWeeklyBar.High}, L={consolidatedWeeklyBar.Low}, C={consolidatedWeeklyBar.Close}");

                // Update the weekly ADX using the consolidated weekly bar
                WeeklyAdx.Update(consolidatedWeeklyBar);

                // Log ADX and directional movement
                var diPlus = WeeklyAdx.PositiveDirectionalIndex.Current.Value;
                var diMinus = WeeklyAdx.NegativeDirectionalIndex.Current.Value;
                var adxValue = WeeklyAdx.Current.Value;

                LogHelper.LogInfo(_bot, $"DI+ = {diPlus}, DI- = {diMinus}, ADX = {adxValue}");
            }
        }

        public void OnData(Slice slice)
        {
            if (slice.Bars.TryGetValue(_symbol, out var bar))
            {
                // Update daily ADX with the latest bar
                DailyAdx.Update(bar);
            }
        }
}