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);
}
}
}
Mia Alissi
To address the issues in your C# code, ensure that you have the necessary using directives and references for the types and namespaces used. Here's the corrected version of your code with the appropriate using directives:
Ensure that your project references the necessary QuantConnect libraries to access these types.
Sources: Data Consolidation Algorithm, Calendar Consolidators - QuantConnect.com, Average Directional Index - QuantConnect.com, Time Period Consolidators - QuantConnect.com, Futures Fast Trend Following, with Trend Strength
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Dee Znut
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!