I am trying to create a weekly consolidator which gives me ADX values per week. Here is my code. The problem is that I always get 0 values for DI+ and DI- and 50 for ADX itself. Can someone help please?
public ADXData(OptionsTradingBot algorithm, Symbol symbol)
{
_bot = algorithm;
_symbol = symbol;
// Initialize the RollingWindow to hold 5 instances of AverageDirectionalIndex
_weeklyAdxWindow = new RollingWindow<AverageDirectionalIndex>(14);
// Initialize daily ADX with a 14-period
DailyAdx = _bot.ADX(symbol, 14, Resolution.Daily);
// 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)
{
DailyAdx.Update(bar);
// Feed the historical data into the weekly consolidator to build weekly bars
_weeklyConsolidator.Update(bar);
}
}
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)
{
var weeklyAdx = new AverageDirectionalIndex(14); // ADX with a 14-period (adjust as necessary)
// Update the ADX directly using the consolidated weekly bar
weeklyAdx.Update(consolidatedWeeklyBar);
/*********************************
THE WEEKLYADX ABOVE is ALWAYS 50 and DI- and DI+ are 0
***********************************/
// 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}");
// Check if ADX is updating correctly and log values
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}");
// Add the calculated weekly ADX to the RollingWindow
_weeklyAdxWindow.Add(weeklyAdx);
// Optionally log the rolling window contents
if (_bot._loggingVerbosity >= 3)
{
LogHelper.LogInfo(_bot, $"Rolling Window (weekly ADX objects): {_weeklyAdxWindow.Count} ADX values at {consolidatedWeeklyBar.Time}");
}
}
}
Mia Alissi
Sources: Data Consolidation Algorithm, Time Period Consolidators - QuantConnect.com, Custom Resolutions - QuantConnect.com, Multiple Symbol Consolidation Algorithm, 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!