Hi QuantConnect Community! I would like to ask your help.
Into Research Notebook, I want to calculate ADX indicator using consolidated data. I created a TradeBar object and add parameters in order to Update indicator. However, the results of
adx.Update(trade_bar)
Is always False. I haven't been able to solve this problem.
Could you help me to find what is the mistake here? Below is the block of code:
# DF is DataFrame with consolidated data (OHLCV). The index is a DateTime object.
# Previously, I dropped NaN values from DataFrame.
# Define instance for ADX -> period=14 is the default value
adx = AverageDirectionalIndex(period=14)
# Dictionary to hold consolidated ADX values.
adx_values = {'time': [], 'positiveDI': [], 'negativeDI': []}
# DI+ and DI- requires a TradeBar object.
trade_bar = TradeBar()
# Iterate through consolidated dataframe.
for item in DF.itertuples():
time = item.Index
# Add parameters to "trade_bar" object
trade_bar.Open = item.Open
trade_bar.High = item.High
trade_bar.Low = item.Low
trade_bar.Close = item.Close
trade_bar.EndTime = item.Index
trade_bar.Volume = item.Volume
# Update indicator with consolidated data
adx.Update(trade_bar)
# If indicator values are ready, append data to dictionary previosly defined
if adx.IsReady:
adx_values['time'].append(time)
adx_values['positiveDI'].append(adx.PositiveDirectionalIndex.Current.Value)
adx_values['negativeDI'].append(adx.NegativeDirectionalIndex.Current.Value)
# Create indicator dataframe from dictionary
consolidated_adx = pd.DataFrame(adx_values, columns=['time', 'positiveDI', 'negativeDI'])
# Set index to time
consolidated_adx = consolidated_adx.set_index('time')
Regards.
Derek Melchin
Hi Edinson,
We were unable to reproduce the issue. See the attached backtest for reference. For further assistance, please attach the full notebook so we can reproduce the problem.
Best,
Derek Melchin
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.
Edinson Leandro Medina Alfonzo
Hi Derek Melchin!
I have contrasted your code with mine. I could see that I defined the TradeBar object only once before start the for loop. While in your code, this object is defined in each iteration.
I have understood the issue in my code. In order to replace and use the new Time-OHLCV data, it is also necessary replace this definition in each iteration.
Thanks a lot for your help! I had stuck during a lot of time with this.
Regards
Edinson Leandro Medina Alfonzo
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!