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.

 

  1. namespace QuantConnect
  2. {
  3. public class ADXData
  4. {
  5. private QCAlgorithm _bot;
  6. private Symbol _symbol;
  7. public AverageDirectionalIndex DailyAdx { get; private set; }
  8. public AverageDirectionalIndex WeeklyAdx { get; private set; }
  9. private TradeBarConsolidator _weeklyConsolidator;
  10. public ADXData(OptionsTradingBot algorithm, Symbol symbol)
  11. {
  12. _bot = algorithm;
  13. _symbol = symbol;
  14. // Initialize daily ADX with a 14-period
  15. DailyAdx = _bot.ADX(symbol, 14, Resolution.Daily);
  16. // Initialize the weekly ADX with a 14-period (adjust period if necessary)
  17. WeeklyAdx = new AverageDirectionalIndex(14);
  18. // Use a consolidator to manually aggregate daily bars into weekly bars (5 trading days)
  19. _weeklyConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(7));
  20. _weeklyConsolidator.DataConsolidated += OnWeeklyConsolidatorBar;
  21. // Add the consolidator for daily data
  22. algorithm.SubscriptionManager.AddConsolidator(symbol, _weeklyConsolidator);
  23. // Warm up the indicators with historical data
  24. WarmUpIndicators(algorithm, symbol);
  25. }
  26. private void WarmUpIndicators(OptionsTradingBot algorithm, Symbol symbol)
  27. {
  28. // Get daily history for warming up the weekly bars
  29. var dailyHistory = algorithm.History<TradeBar>(symbol, 100, Resolution.Daily);
  30. foreach (var bar in dailyHistory)
  31. {
  32. // Warm up the daily ADX
  33. DailyAdx.Update(bar);
  34. // Feed the historical data into the weekly consolidator to build weekly bars
  35. _weeklyConsolidator.Update(bar);
  36. }
  37. }
  38. // Simplified: Removed RollingWindow and adjusted to just use the ADX directly
  39. private void OnWeeklyConsolidatorBar(object sender, TradeBar consolidatedWeeklyBar)
  40. {
  41. // Ensure the weekly bar is not created on Sundays or intraday times
  42. if (consolidatedWeeklyBar.Time.DayOfWeek != DayOfWeek.Sunday)
  43. {
  44. // Log the consolidated bar details for debugging
  45. LogHelper.LogInfo(_bot, $"Consolidated Weekly Bar: O={consolidatedWeeklyBar.Open}, H={consolidatedWeeklyBar.High}, L={consolidatedWeeklyBar.Low}, C={consolidatedWeeklyBar.Close}");
  46. // Update the weekly ADX using the consolidated weekly bar
  47. WeeklyAdx.Update(consolidatedWeeklyBar);
  48. // Log ADX and directional movement
  49. var diPlus = WeeklyAdx.PositiveDirectionalIndex.Current.Value;
  50. var diMinus = WeeklyAdx.NegativeDirectionalIndex.Current.Value;
  51. var adxValue = WeeklyAdx.Current.Value;
  52. LogHelper.LogInfo(_bot, $"DI+ = {diPlus}, DI- = {diMinus}, ADX = {adxValue}");
  53. }
  54. }
  55. public void OnData(Slice slice)
  56. {
  57. if (slice.Bars.TryGetValue(_symbol, out var bar))
  58. {
  59. // Update daily ADX with the latest bar
  60. DailyAdx.Update(bar);
  61. }
  62. }
  63. }
+ Expand

Author

Dee Znut

September 2024