Using ATR and other 'complex' indicators with history

Back

I keep getting an error like: 

Runtime Error: IndicatorBase.Update() 'input' expected to be of type QuantConnect.Indicators.IndicatorDataPoint but is of type QuantConnect.Data.Market.TradeBar (Open Stacktrace)

When trying to use history or a consolidator with the Normalized ATR, or other indicators like those, which need more than just the closing price to compute. Is there an example of how to use that properly? For example, if I want 45 minute consolidated ATR data

Update Backtest







 
0

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.


Hey Nabeel,

ATR is a bar indicator, which means it requires a TradeBar to be updated. If we register our indicator to data from a symbol either by using a helper method:

self.atr = self.ATR(symbol, period, resolution)

or manually registering

self.atr = AverageTrueRange(period)
self.RegisterIndicator(symbol, self.atr, resolution)

Then our indicator will be automatically updated with bar data. However, if we don't register our indicator, we will need to manually update it with bar data.

self.atr.Update(TradeBar)

If we are using historical data to update our indicator. We need to convert our rows in the dataframe into TradeBars.

history = self.History(symbol, period, resolution)
for bar in history.itertuples():
tradebar = TradeBar(bar.Index[1], self.symbol, bar.open, bar.high, bar.low, bar.close, bar.volume)
self.atr.Update(tradebar)

Here's an example of an algorithm which uses the CCI indicator, which is another bar indicator. It shows both how historical data is used to update our indicator and how to use tradebars directly from the slice data to update our indicator.

0

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.


Thanks for the reply, Rahul. I still get this error randomly and I'm not sure why:

Runtime Error: IndicatorBase.Update() 'input' expected to be of type QuantConnect.Indicators.IndicatorDataPoint but is of type QuantConnect.Data.Market.TradeBar (Open Stacktrace). I can't attach a broken backtest, but this is the code, in the "ConslidatedBarHandler" method.

using System;
using System.Collections.Generic;
using QuantConnect.Brokerages;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Interfaces;

namespace QuantConnect.Algorithm.CSharp
{
public class NabeelMacd: QCAlgorithm, IRegressionAlgorithmDefinition
{
private MovingAverageConvergenceDivergence _macd;
private NormalizedAverageTrueRange _atr;
private readonly string _symbol = "SPY";
private TradeBar _last;

/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetStartDate(2019, 01, 01);
SetEndDate(2020, 01, 01);
SetCash(10000);

SetBrokerageModel(BrokerageName.Alpaca);
SetWarmUp(TimeSpan.FromMinutes(26 * 60));

AddSecurity(SecurityType.Equity, _symbol, Resolution.Minute);

var barConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(60));
// var barConsolidator = new TradeBarConsolidator(TimeSpan.FromHours(4));
barConsolidator.DataConsolidated += ConslidatedBarHandler;
SubscriptionManager.AddConsolidator(_symbol, barConsolidator);

// define our daily macd(12,26) with a 9 day signal
_macd = MACD(_symbol, 12, 26, 9, MovingAverageType.Exponential, Resolution.Hour);
_atr = new NormalizedAverageTrueRange(10);
}

/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">TradeBars IDictionary object with your stock data</param>
public void OnData(TradeBars data)
{
}

/// <summary>
/// This is our event handler for our 30 minute trade bar defined above in Initialize(). So each time the consolidator
/// produces a new 30 minute bar, this function will be called automatically. The 'sender' parameter will be the
/// instance of the IDataConsolidator that invoked the event, but you'll almost never need that!
/// </summary>
private void ConslidatedBarHandler(object sender, TradeBar consolidated)
{
_macd.Update(consolidated); // < Throws an error
_atr.Update(consolidated);

var holding = Portfolio[_symbol];

var signalDeltaPercent = (_macd - _macd.Signal)/_macd.Fast;
const decimal tolerance = 0.0025m;

if (_atr < 4) {
return;
}

// if our macd is greater than our signal, then let's go long
if (holding.Quantity <= 0 && signalDeltaPercent > tolerance) { // 0.01%
// longterm says buy as well
SetHoldings(_symbol, 1.0);
}
// of our macd is less than our signal, then let's go short
else if (holding.Quantity >= 0 && signalDeltaPercent < -tolerance) {
Liquidate(_symbol);
}

// plot both lines
Plot("MACD", _macd, _macd.Signal);
Plot(_symbol, "Open", consolidated.Open);
Plot(_symbol, _macd.Fast, _macd.Slow);

_last = consolidated;
}
}
}

0

It got answered in the other thread... thanks!

0

Update Backtest





0

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.


Loading...

This discussion is closed