Back

Indicator Examples (BB, MACD, SMA, EMA, RSI, ATR)

By popular request I've developed an example project with the common indicators, including: Bollinger Bands, Simple Moving Average, Exponential Moving Average, Relative Strength Index, Average True Range and MACD. Down the bottom of the algorithm we plot them together with price. I've also set it up to use Daily consolidators - this way the plots look like characteristic ones you'll see on other stock charting websites.

The engine updates the indicators automatically and feeds the value through to the algorithms output for charting! Its a pretty cool system under the hood. I'd recommend looking at the Github project.
Update Backtest






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.



By default any single dimension indicator (SMA, EMA, ect..) uses the closing of the bar. If you'd like to use something else, you can specify a function that selects what value to use:fast = EMA(Symbol, 25, Resolution.Minute, baseData => ((TradeBar)baseData).High);
There are some helpers to make this easier, so the above could be written as:fast = EMA(Symbol, 25, Resolution.Minute, Field.High);
Here's a link to the Field class.
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.


I'm getting this following error when I tried to use the consolidator in the backtest. What is the proper way to register the indicator?

"Backtest Error: Error initializing algorithm: Type mismatch found between consolidator and indicator for symbol: DIA.Consolidator outputs type TradeBar but indicator expects input type IndicatorDataPoint"

public SymbolData(string symbol, QCAlgorithm algorithm)
{
Symbol = symbol;
_bb = new BollingerBands(100, 3, MovingAverageType.Simple);
_atr = new AverageTrueRange(100, MovingAverageType.Simple);
algorithm.RegisterIndicator(Symbol, _bb, TimeSpan.FromMinutes(15));
algorithm.RegisterIndicator(Symbol, _atr, TimeSpan.FromMinutes(30));
}
0

Hey Johnny, you'll need to include some of your Initialize code. From the error message the symbol with issue is 'DIA'. Is this custom data? The error message is saying you're trying to register an indicator that accepts a single value (such as close, or high) but passing a full TradeBar. You can try appending 'Field.Close' to the RegisterIndicator call for '_bb'. If you still have issues please share the full algorithm and we can be of further assistance!
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 Michael,

Thanks for taking a look at my code. DIA is an ETF that tracks the Dow 30. It is a standard symbol in the quantconnect database. If I try to append the Field.Close then I must use the quantconnection resolution class instead of time span. There is currently a method that converts resolution to timespan. Do you think it would be possible to implement a feature that convert timespan to resolution allowing us to create our own custom resolutions.

Ex. TimeSpan.FromMinutes(15).ToResolution();

Also, the feature using strings for symbol is now deprecated. Where do I find a symbol's sid? That information is not displayed in the data manager.

using System;
using QuantConnect.Data.Market;
using QuantConnect.Securities;
using QuantConnect.Indicators;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Algorithm.CSharp.MyTradingStrategies;

namespace QuantConnect.Algorithm
{
public class BollingerBandsAlgorithm : QCAlgorithm
{
class SymbolData
{
public readonly string Symbol;
public readonly BollingerBands _bb;
public readonly AverageTrueRange _atr;
public bool isReady { get { return _bb.IsReady && _atr.IsReady; } }

public SymbolData(string symbol, QCAlgorithm algorithm)
{
Symbol = symbol;
_bb = new BollingerBands(100, 3, MovingAverageType.Simple);
_atr = new AverageTrueRange(100, MovingAverageType.Simple);
algorithm.RegisterIndicator(Symbol, _bb, TimeSpan.FromMinutes(15));
algorithm.RegisterIndicator(Symbol, _atr, TimeSpan.FromMinutes(15));
}
}
private List symbols = new List()
{ "DIA", "DXJ", "EEM", "EFA", "EWZ",
"FXI", "GDX","IWM", "IYR", "QQQ",
"SPY", "XLB", "XLE","XLF", "XLI",
"XLK", "XLP", "XLU", "XLV", "XLY" };

private Dictionary Symbols = new Dictionary();

//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
//Initialize
SetStartDate(2007, 1, 1);
SetEndDate(DateTime.Now.AddDays(-1));
SetCash(100000);

foreach (var symbol in symbols)
{
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
Symbols.Add(symbol, new SymbolData(symbol, this));
}
}

public void OnData(TradeBars data)
{
foreach (var symbolData in Symbols.Values)
{
if (!symbolData.isReady || data.ContainsKey(symbolData.Symbol))
return;

if (!Portfolio.HoldStock)
{
var _buyPrice = symbolData._bb.LowerBand;
var _buySignal = data[symbolData.Symbol].Close > _buyPrice;
if (_buySignal) { SetHoldings(symbolData.Symbol, 1); }
}
else
{
var _sellPrice = symbolData._bb.UpperBand;
var _sellSignal = data[symbolData.Symbol].Close > _sellPrice;
if (_sellSignal) { Liquidate(symbolData.Symbol); }
}
}
}
}
}
0

Hey Johnny, I've added the requested overload. It will be available in the next deploy. Just a head up, it won't work with the ATR since the field selector required for ATR requires a TradeBar.

The ToResolution method is a great idea, but may require a little thought on our end since it is a widely used enumeration.

An alternate solution is to have your SymbolData class accept a TradeBarConsolidator which you could create from your Initialize method:var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(15));
Then there are overloads available that accept the Field parameter.
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.


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