/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using QuantConnect.Data; using QuantConnect.Data.Consolidators; using QuantConnect.Data.Market; using QuantConnect.Indicators; using System; using System.Linq; namespace QuantConnect.Algorithm { public partial class QCAlgorithm { /// /// Creates a new Acceleration Bands indicator. /// /// The symbol whose Acceleration Bands we want. /// The period of the three moving average (middle, upper and lower band). /// A coefficient specifying the distance between the middle band and upper or lower bands. /// Type of the moving average. /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar. /// public AccelerationBands ABANDS(Symbol symbol, int period, decimal width = 4, MovingAverageType movingAverageType = MovingAverageType.Simple, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("ABANDS_{0}_{1}", period, width), resolution); var abands = new AccelerationBands(name, period, width, movingAverageType); RegisterIndicator(symbol, abands, resolution, selector); return abands; } /// /// Creates a new AccumulationDistribution indicator. /// /// The symbol whose AD we want /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The AccumulationDistribution indicator for the requested symbol over the speified period public AccumulationDistribution AD(Symbol symbol, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "AD", resolution); var ad = new AccumulationDistribution(name); RegisterIndicator(symbol, ad, resolution, selector); return ad; } /// /// Creates a new AccumulationDistributionOscillator indicator. /// /// The symbol whose ADOSC we want /// The fast moving average period /// The slow moving average period /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The AccumulationDistributionOscillator indicator for the requested symbol over the speified period public AccumulationDistributionOscillator ADOSC(Symbol symbol, int fastPeriod, int slowPeriod, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("ADOSC({0},{1})", fastPeriod, slowPeriod), resolution); var adOsc = new AccumulationDistributionOscillator(name, fastPeriod, slowPeriod); RegisterIndicator(symbol, adOsc, resolution, selector); return adOsc; } /// /// Creates a new Average Directional Index indicator. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose Average Directional Index we seek /// The resolution. /// The period over which to compute the Average Directional Index /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// The Average Directional Index indicator for the requested symbol. public AverageDirectionalIndex ADX(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "ADX", resolution); var averageDirectionalIndex = new AverageDirectionalIndex(name, period); RegisterIndicator(symbol, averageDirectionalIndex, resolution, selector); return averageDirectionalIndex; } /// /// Creates a new AverageDirectionalMovementIndexRating indicator. /// /// The symbol whose ADXR we want /// The period over which to compute the ADXR /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The AverageDirectionalMovementIndexRating indicator for the requested symbol over the specified period public AverageDirectionalMovementIndexRating ADXR(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "ADXR" + period, resolution); var adxr = new AverageDirectionalMovementIndexRating(name, period); RegisterIndicator(symbol, adxr, resolution, selector); return adxr; } /// /// Creates a new ArnaudLegouxMovingAverage indicator. /// /// The symbol whose ALMA we want /// int - the number of periods to calculate the ALMA /// int - this parameter is responsible for the shape of the curve coefficients. /// /// /// decimal - This parameter allows regulating the smoothness and high sensitivity of the /// Moving Average. The range for this parameter is [0, 1]. /// /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The ArnaudLegouxMovingAverage indicator for the requested symbol over the specified period public ArnaudLegouxMovingAverage ALMA(Symbol symbol, int period, int sigma = 6, decimal offset = 0.85m, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("ALMA_{0}_{1}_{2}", period, sigma, offset), resolution); var alma = new ArnaudLegouxMovingAverage(name, period, sigma, offset); RegisterIndicator(symbol, alma, resolution, selector); return alma; } /// /// Creates a new AbsolutePriceOscillator indicator. /// /// The symbol whose APO we want /// The fast moving average period /// The slow moving average period /// The type of moving average to use /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The AbsolutePriceOscillator indicator for the requested symbol over the specified period public AbsolutePriceOscillator APO(Symbol symbol, int fastPeriod, int slowPeriod, MovingAverageType movingAverageType, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("APO({0},{1})", fastPeriod, slowPeriod), resolution); var apo = new AbsolutePriceOscillator(name, fastPeriod, slowPeriod, movingAverageType); RegisterIndicator(symbol, apo, resolution, selector); return apo; } /// /// Creates a new AroonOscillator indicator which will compute the AroonUp and AroonDown (as well as the delta) /// /// The symbol whose Aroon we seek /// The look back period for computing number of periods since maximum and minimum /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// An AroonOscillator configured with the specied periods public AroonOscillator AROON(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { return AROON(symbol, period, period, resolution, selector); } /// /// Creates a new AroonOscillator indicator which will compute the AroonUp and AroonDown (as well as the delta) /// /// The symbol whose Aroon we seek /// The look back period for computing number of periods since maximum /// The look back period for computing number of periods since minimum /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// An AroonOscillator configured with the specified periods public AroonOscillator AROON(Symbol symbol, int upPeriod, int downPeriod, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("AROON({0},{1})", upPeriod, downPeriod), resolution); var aroon = new AroonOscillator(name, upPeriod, downPeriod); RegisterIndicator(symbol, aroon, resolution, selector); return aroon; } /// /// Creates a new AverageTrueRange indicator for the symbol. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose ATR we want /// The smoothing period used to smooth the computed TrueRange values /// The type of smoothing to use /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// A new AverageTrueRange indicator with the specified smoothing type and period public AverageTrueRange ATR(Symbol symbol, int period, MovingAverageType type = MovingAverageType.Simple, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "ATR" + period, resolution); var atr = new AverageTrueRange(name, period, type); RegisterIndicator(symbol, atr, resolution, selector); return atr; } /// /// Creates a new BollingerBands indicator which will compute the MiddleBand, UpperBand, LowerBand, and StandardDeviation /// /// The symbol whose BollingerBands we seek /// The period of the standard deviation and moving average (middle band) /// The number of standard deviations specifying the distance between the middle band and upper or lower bands /// The type of moving average to be used /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// A BollingerBands configured with the specied period public BollingerBands BB(Symbol symbol, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("BB({0},{1})", period, k), resolution); var bb = new BollingerBands(name, period, k, movingAverageType); RegisterIndicator(symbol, bb, resolution, selector); return bb; } /// /// Creates a new Balance Of Power indicator. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose Balance Of Power we seek /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// The Balance Of Power indicator for the requested symbol. public BalanceOfPower BOP(Symbol symbol, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "BOP", resolution); var bop = new BalanceOfPower(name); RegisterIndicator(symbol, bop, resolution, selector); return bop; } /// /// Creates a new CommodityChannelIndex indicator. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose CCI we want /// The period over which to compute the CCI /// The type of moving average to use in computing the typical price averge /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// The CommodityChannelIndex indicator for the requested symbol over the specified period public CommodityChannelIndex CCI(Symbol symbol, int period, MovingAverageType movingAverageType = MovingAverageType.Simple, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "CCI" + period, resolution); var cci = new CommodityChannelIndex(name, period, movingAverageType); RegisterIndicator(symbol, cci, resolution, selector); return cci; } /// /// Creates a new ChandeMomentumOscillator indicator. /// /// The symbol whose CMO we want /// The period over which to compute the CMO /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The ChandeMomentumOscillator indicator for the requested symbol over the specified period public ChandeMomentumOscillator CMO(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "CMO" + period, resolution); var cmo = new ChandeMomentumOscillator(name, period); RegisterIndicator(symbol, cmo, resolution, selector); return cmo; } /// /// Creates a new Donchian Channel indicator which will compute the Upper Band and Lower Band. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose Donchian Channel we seek. /// The period over which to compute the upper Donchian Channel. /// The period over which to compute the lower Donchian Channel. /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// The Donchian Channel indicator for the requested symbol. public DonchianChannel DCH(Symbol symbol, int upperPeriod, int lowerPeriod, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "DCH", resolution); var donchianChannel = new DonchianChannel(name, upperPeriod, lowerPeriod); RegisterIndicator(symbol, donchianChannel, resolution, selector); return donchianChannel; } /// /// Overload shorthand to create a new symmetric Donchian Channel indicator which /// has the upper and lower channels set to the same period length. /// /// The symbol whose Donchian Channel we seek. /// The period over which to compute the Donchian Channel. /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// The Donchian Channel indicator for the requested symbol. public DonchianChannel DCH(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { return DCH(symbol, period, period, resolution, selector); } /// /// Creates a new DoubleExponentialMovingAverage indicator. /// /// The symbol whose DEMA we want /// The period over which to compute the DEMA /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The DoubleExponentialMovingAverage indicator for the requested symbol over the specified period public DoubleExponentialMovingAverage DEMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "DEMA" + period, resolution); var dema = new DoubleExponentialMovingAverage(name, period); RegisterIndicator(symbol, dema, resolution, selector); return dema; } /// /// Creates a new indicator. /// /// The symbol whose DPO we want /// The period over which to compute the DPO /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// A new registered DetrendedPriceOscillator indicator for the requested symbol over the specified period public DetrendedPriceOscillator DPO(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "DPO" + period, resolution); var dpo = new DetrendedPriceOscillator(name, period); RegisterIndicator(symbol, dpo, resolution, selector); return dpo; } /// /// Creates an ExponentialMovingAverage indicator for the symbol. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose EMA we want /// The period of the EMA /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The ExponentialMovingAverage for the given parameters public ExponentialMovingAverage EMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "EMA" + period, resolution); var ema = new ExponentialMovingAverage(name, period); RegisterIndicator(symbol, ema, resolution, selector); return ema; } /// /// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically /// updated on the symbol's subscription resolution /// /// The symbol whose values we want as an indicator /// Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value) /// Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter /// The name of the field being selected /// A new FilteredIdentity indicator for the specified symbol and selector public FilteredIdentity FilteredIdentity(Symbol symbol, Func selector = null, Func filter = null, string fieldName = null) { var resolution = GetSubscription(symbol).Resolution; return FilteredIdentity(symbol, resolution, selector, filter, fieldName); } /// /// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically /// updated on the symbol's subscription resolution /// /// The symbol whose values we want as an indicator /// The desired resolution of the data /// Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value) /// Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter /// The name of the field being selected /// A new FilteredIdentity indicator for the specified symbol and selector public FilteredIdentity FilteredIdentity(Symbol symbol, Resolution resolution, Func selector = null, Func filter = null, string fieldName = null) { string name = CreateIndicatorName(symbol, fieldName ?? "close", resolution); var filteredIdentity = new FilteredIdentity(name, filter); RegisterIndicator(symbol, filteredIdentity, resolution, selector); return filteredIdentity; } /// /// Creates a new FilteredIdentity indicator for the symbol The indicator will be automatically /// updated on the symbol's subscription resolution /// /// The symbol whose values we want as an indicator /// The desired resolution of the data /// Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value) /// Filters the IBaseData send into the indicator, if null defaults to true (x => true) which means no filter /// The name of the field being selected /// A new FilteredIdentity indicator for the specified symbol and selector public FilteredIdentity FilteredIdentity(Symbol symbol, TimeSpan resolution, Func selector = null, Func filter = null, string fieldName = null) { string name = string.Format("{0}({1}_{2})", symbol, fieldName ?? "close", resolution); var filteredIdentity = new FilteredIdentity(name, filter); RegisterIndicator(symbol, filteredIdentity, ResolveConsolidator(symbol, resolution), selector); return filteredIdentity; } /// /// Creates an FractalAdaptiveMovingAverage (FRAMA) indicator for the symbol. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose FRAMA we want /// The period of the FRAMA /// The long period of the FRAMA /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The FRAMA for the given parameters public FractalAdaptiveMovingAverage FRAMA(Symbol symbol, int period, int longPeriod = 198, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "FRAMA" + period, resolution); var frama = new FractalAdaptiveMovingAverage(name, period, longPeriod); RegisterIndicator(symbol, frama, resolution, selector); return frama; } /// /// Creates a new Heikin-Ashi indicator. /// /// The symbol whose Heikin-Ashi we want /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The Heikin-Ashi indicator for the requested symbol over the specified period public HeikinAshi HeikinAshi(Symbol symbol, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "HA", resolution); var ha = new HeikinAshi(name); RegisterIndicator(symbol, ha, resolution, selector); return ha; } /// /// Creates a new HullMovingAverage indicator. The Hull moving average is a series of nested weighted moving averages, is fast and smooth. /// /// The symbol whose Hull moving average we want /// The period over which to compute the Hull moving average /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// public HullMovingAverage HMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "HMA" + period, resolution); var hma = new HullMovingAverage(name, period); RegisterIndicator(symbol, hma, resolution, selector); return hma; } /// /// Creates a new IchimokuKinkoHyo indicator for the symbol. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose ICHIMOKU we want /// The period to calculate the Tenkan-sen period /// The period to calculate the Kijun-sen period /// The period to calculate the Tenkan-sen period /// The period to calculate the Tenkan-sen period /// The period to calculate the Tenkan-sen period /// The period to calculate the Tenkan-sen period /// The resolution /// A new IchimokuKinkoHyo indicator with the specified periods and delays public IchimokuKinkoHyo ICHIMOKU(Symbol symbol, int tenkanPeriod, int kijunPeriod, int senkouAPeriod, int senkouBPeriod, int senkouADelayPeriod, int senkouBDelayPeriod, Resolution? resolution = null) { var name = CreateIndicatorName(symbol, string.Format("ICHIMOKU({0},{1})", tenkanPeriod, kijunPeriod), resolution); var ichimoku = new IchimokuKinkoHyo(name, tenkanPeriod, kijunPeriod, senkouAPeriod, senkouBPeriod, senkouADelayPeriod, senkouBDelayPeriod); RegisterIndicator(symbol, ichimoku, resolution); return ichimoku; } /// /// Creates a new Identity indicator for the symbol The indicator will be automatically /// updated on the symbol's subscription resolution /// /// The symbol whose values we want as an indicator /// Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value) /// The name of the field being selected /// A new Identity indicator for the specified symbol and selector public Identity Identity(Symbol symbol, Func selector = null, string fieldName = null) { var resolution = GetSubscription(symbol).Resolution; return Identity(symbol, resolution, selector, fieldName); } /// /// Creates a new Identity indicator for the symbol The indicator will be automatically /// updated on the symbol's subscription resolution /// /// The symbol whose values we want as an indicator /// The desired resolution of the data /// Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value) /// The name of the field being selected /// A new Identity indicator for the specified symbol and selector public Identity Identity(Symbol symbol, Resolution resolution, Func selector = null, string fieldName = null) { string name = CreateIndicatorName(symbol, fieldName ?? "close", resolution); var identity = new Identity(name); RegisterIndicator(symbol, identity, resolution, selector); return identity; } /// /// Creates a new Identity indicator for the symbol The indicator will be automatically /// updated on the symbol's subscription resolution /// /// The symbol whose values we want as an indicator /// The desired resolution of the data /// Selects a value from the BaseData, if null defaults to the .Value property (x => x.Value) /// The name of the field being selected /// A new Identity indicator for the specified symbol and selector public Identity Identity(Symbol symbol, TimeSpan resolution, Func selector = null, string fieldName = null) { string name = string.Format("{0}({1}_{2})", symbol, fieldName ?? "close", resolution); var identity = new Identity(name); RegisterIndicator(symbol, identity, ResolveConsolidator(symbol, resolution), selector); return identity; } /// /// Creates a new KaufmanAdaptiveMovingAverage indicator. /// /// The symbol whose KAMA we want /// The period over which to compute the KAMA /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The KaufmanAdaptiveMovingAverage indicator for the requested symbol over the specified period public KaufmanAdaptiveMovingAverage KAMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "KAMA" + period, resolution); var kama = new KaufmanAdaptiveMovingAverage(name, period); RegisterIndicator(symbol, kama, resolution, selector); return kama; } /// /// Creates a new Keltner Channels indicator. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose Keltner Channel we seek /// The period over which to compute the Keltner Channels /// The number of multiples of the from the middle band of the Keltner Channels /// Specifies the type of moving average to be used as the middle line of the Keltner Channel /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// The Keltner Channel indicator for the requested symbol. public KeltnerChannels KCH(Symbol symbol, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "KCH", resolution); var keltnerChannels = new KeltnerChannels(name, period, k, movingAverageType); RegisterIndicator(symbol, keltnerChannels, resolution, selector); return keltnerChannels; } /// /// Creates a new LogReturn indicator. /// /// The symbol whose log return we seek /// The period of the log return. /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar. /// log return indicator for the requested symbol. public LogReturn LOGR(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "LOGR", resolution); var logr = new LogReturn(name, period); RegisterIndicator(symbol, logr, resolution, selector); return logr; } /// /// Creates and registers a new Least Squares Moving Average instance. /// /// The symbol whose LSMA we seek. /// The LSMA period. Normally 14. /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar. /// A LeastSquaredMovingAverage configured with the specified period public LeastSquaresMovingAverage LSMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "LSMA" + period, resolution); var lsma = new LeastSquaresMovingAverage(name, period); RegisterIndicator(symbol, lsma, resolution, selector); return lsma; } /// /// Creates a new LinearWeightedMovingAverage indicator. This indicator will linearly distribute /// the weights across the periods. /// /// The symbol whose LWMA we want /// The period over which to compute the LWMA /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// public LinearWeightedMovingAverage LWMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "LWMA" + period, resolution); var lwma = new LinearWeightedMovingAverage(name, period); RegisterIndicator(symbol, lwma, resolution, selector); return lwma; } /// /// Creates a MACD indicator for the symbol. The indicator will be automatically updated on the given resolution. /// /// The symbol whose MACD we want /// The period for the fast moving average /// The period for the slow moving average /// The period for the signal moving average /// The type of moving average to use for the MACD /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The moving average convergence divergence between the fast and slow averages public MovingAverageConvergenceDivergence MACD(Symbol symbol, int fastPeriod, int slowPeriod, int signalPeriod, MovingAverageType type = MovingAverageType.Simple, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("MACD({0},{1})", fastPeriod, slowPeriod), resolution); var macd = new MovingAverageConvergenceDivergence(name, fastPeriod, slowPeriod, signalPeriod, type); RegisterIndicator(symbol, macd, resolution, selector); return macd; } /// /// Creates a new MeanAbsoluteDeviation indicator. /// /// The symbol whose MeanAbsoluteDeviation we want /// The period over which to compute the MeanAbsoluteDeviation /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The MeanAbsoluteDeviation indicator for the requested symbol over the specified period public MeanAbsoluteDeviation MAD(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "MAD" + period, resolution); var mad = new MeanAbsoluteDeviation(name, period); RegisterIndicator(symbol, mad, resolution, selector); return mad; } /// /// Creates a new Maximum indicator to compute the maximum value /// /// The symbol whose max we want /// The look back period over which to compute the max value /// The resolution /// Selects a value from the BaseData to send into the indicator, if null and the symbol is of type TradeBar defaults to the High property, /// otherwise it defaults to Value property of BaseData (x => x.Value) /// A Maximum indicator that compute the max value and the periods since the max value public Maximum MAX(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "MAX" + period, resolution); var max = new Maximum(name, period); // assign a default value for the selector function if (selector == null) { var subscription = GetSubscription(symbol); if (typeof(TradeBar).IsAssignableFrom(subscription.Type)) { // if we have trade bar data we'll use the High property, if not x => x.Value will be set in RegisterIndicator selector = x => ((TradeBar)x).High; } } RegisterIndicator(symbol, max, ResolveConsolidator(symbol, resolution), selector); return max; } /// /// Creates a new MoneyFlowIndex indicator. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose MFI we want /// The period over which to compute the MFI /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The MoneyFlowIndex indicator for the requested symbol over the specified period public MoneyFlowIndex MFI(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "MFI" + period, resolution); var mfi = new MoneyFlowIndex(name, period); RegisterIndicator(symbol, mfi, resolution, selector); return mfi; } /// /// Creates a new Mass Index indicator. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose Mass Index we want. /// The period used by both EMA. /// The sum period. /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The Mass Index indicator for the requested symbol over the specified period public MassIndex MASS(Symbol symbol, int emaPeriod = 9, int sumPeriod = 25, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "MII" + emaPeriod + sumPeriod, resolution); var mi = new MassIndex(name, emaPeriod, sumPeriod); RegisterIndicator(symbol, mi, resolution, selector); return mi; } /// /// Creates a new MidPoint indicator. /// /// The symbol whose MIDPOINT we want /// The period over which to compute the MIDPOINT /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The MidPoint indicator for the requested symbol over the specified period public MidPoint MIDPOINT(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "MIDPOINT" + period, resolution); var midpoint = new MidPoint(name, period); RegisterIndicator(symbol, midpoint, resolution, selector); return midpoint; } /// /// Creates a new MidPrice indicator. /// /// The symbol whose MIDPRICE we want /// The period over which to compute the MIDPRICE /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The MidPrice indicator for the requested symbol over the specified period public MidPrice MIDPRICE(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "MIDPRICE" + period, resolution); var midprice = new MidPrice(name, period); RegisterIndicator(symbol, midprice, resolution, selector); return midprice; } /// /// Creates a new Minimum indicator to compute the minimum value /// /// The symbol whose min we want /// The look back period over which to compute the min value /// The resolution /// Selects a value from the BaseData to send into the indicator, if null and the symbol is of type TradeBar defaults to the Low property, /// otherwise it defaults to Value property of BaseData (x => x.Value) /// A Minimum indicator that compute the in value and the periods since the min value public Minimum MIN(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "MIN" + period, resolution); var min = new Minimum(name, period); // assign a default value for the selector function if (selector == null) { var subscription = GetSubscription(symbol); if (typeof(TradeBar).IsAssignableFrom(subscription.Type)) { // if we have trade bar data we'll use the Low property, if not x => x.Value will be set in RegisterIndicator selector = x => ((TradeBar)x).Low; } } RegisterIndicator(symbol, min, ResolveConsolidator(symbol, resolution), selector); return min; } /// /// Creates a new Momentum indicator. This will compute the absolute n-period change in the security. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose momentum we want /// The period over which to compute the momentum /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The momentum indicator for the requested symbol over the specified period public Momentum MOM(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "MOM" + period, resolution); var momentum = new Momentum(name, period); RegisterIndicator(symbol, momentum, resolution, selector); return momentum; } /// /// Creates a new Momersion indicator. /// /// The symbol whose Momersion we want /// The minimum period over which to compute the Momersion /// The full period over which to compute the Momersion /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The Momersion indicator for the requested symbol over the specified period public MomersionIndicator MOMERSION(Symbol symbol, int minPeriod, int fullPeriod, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("MOMERSION({0},{1})", minPeriod, fullPeriod), resolution); var momersion = new MomersionIndicator(name, minPeriod, fullPeriod); RegisterIndicator(symbol, momersion, resolution, selector); return momersion; } /// /// Creates a new MomentumPercent indicator. This will compute the n-period percent change in the security. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose momentum we want /// The period over which to compute the momentum /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The momentum indicator for the requested symbol over the specified period public MomentumPercent MOMP(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "MOMP" + period, resolution); var momentum = new MomentumPercent(name, period); RegisterIndicator(symbol, momentum, resolution, selector); return momentum; } /// /// Creates a new NormalizedAverageTrueRange indicator. /// /// The symbol whose NATR we want /// The period over which to compute the NATR /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The NormalizedAverageTrueRange indicator for the requested symbol over the specified period public NormalizedAverageTrueRange NATR(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "NATR" + period, resolution); var natr = new NormalizedAverageTrueRange(name, period); RegisterIndicator(symbol, natr, resolution, selector); return natr; } /// /// Creates a new On Balance Volume indicator. This will compute the cumulative total volume /// based on whether the close price being higher or lower than the previous period. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose On Balance Volume we seek /// The resolution. /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// The On Balance Volume indicator for the requested symbol. public OnBalanceVolume OBV(Symbol symbol, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "OBV", resolution); var onBalanceVolume = new OnBalanceVolume(name); RegisterIndicator(symbol, onBalanceVolume, resolution, selector); return onBalanceVolume; } /// /// Creates a new PercentagePriceOscillator indicator. /// /// The symbol whose PPO we want /// The fast moving average period /// The slow moving average period /// The type of moving average to use /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The PercentagePriceOscillator indicator for the requested symbol over the specified period public PercentagePriceOscillator PPO(Symbol symbol, int fastPeriod, int slowPeriod, MovingAverageType movingAverageType, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("PPO({0},{1})", fastPeriod, slowPeriod), resolution); var ppo = new PercentagePriceOscillator(name, fastPeriod, slowPeriod, movingAverageType); RegisterIndicator(symbol, ppo, resolution, selector); return ppo; } /// /// Creates a new Parabolic SAR indicator /// /// The symbol whose PSAR we seek /// Acceleration factor start value. Normally 0.02 /// Acceleration factor increment value. Normally 0.02 /// Acceleration factor max value. Normally 0.2 /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to casting the input value to a TradeBar /// A ParabolicStopAndReverse configured with the specified periods public ParabolicStopAndReverse PSAR(Symbol symbol, decimal afStart = 0.02m, decimal afIncrement = 0.02m, decimal afMax = 0.2m, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("PSAR({0},{1},{2})", afStart, afIncrement, afMax), resolution); var psar = new ParabolicStopAndReverse(name, afStart, afIncrement, afMax); RegisterIndicator(symbol, psar, resolution, selector); return psar; } /// /// Creates a new RegressionChannel indicator which will compute the LinearRegression, UpperChannel and LowerChannel lines, the intercept and slope /// /// The symbol whose RegressionChannel we seek /// The period of the standard deviation and least square moving average (linear regression line) /// The number of standard deviations specifying the distance between the linear regression and upper or lower channel lines /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// A Regression Channel configured with the specied period and number of standard deviation public RegressionChannel RC(Symbol symbol, int period, decimal k, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("RC({0},{1})", period, k), resolution); var rc = new RegressionChannel(name, period, k); RegisterIndicator(symbol, rc, resolution, selector); return rc; } /// /// Creates a new RateOfChange indicator. This will compute the n-period rate of change in the security. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose RateOfChange we want /// The period over which to compute the RateOfChange /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The RateOfChange indicator for the requested symbol over the specified period public RateOfChange ROC(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "ROC" + period, resolution); var rateofchange = new RateOfChange(name, period); RegisterIndicator(symbol, rateofchange, resolution, selector); return rateofchange; } /// /// Creates a new RateOfChangePercent indicator. This will compute the n-period percentage rate of change in the security. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose RateOfChangePercent we want /// The period over which to compute the RateOfChangePercent /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The RateOfChangePercent indicator for the requested symbol over the specified period public RateOfChangePercent ROCP(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "ROCP" + period, resolution); var rateofchangepercent = new RateOfChangePercent(name, period); RegisterIndicator(symbol, rateofchangepercent, resolution, selector); return rateofchangepercent; } /// /// Creates a new RateOfChangeRatio indicator. /// /// The symbol whose ROCR we want /// The period over which to compute the ROCR /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The RateOfChangeRatio indicator for the requested symbol over the specified period public RateOfChangeRatio ROCR(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "ROCR" + period, resolution); var rocr = new RateOfChangeRatio(name, period); RegisterIndicator(symbol, rocr, resolution, selector); return rocr; } /// /// Creates a new RelativeStrengthIndex indicator. This will produce an oscillator that ranges from 0 to 100 based /// on the ratio of average gains to average losses over the specified period. /// /// The symbol whose RSI we want /// The period over which to compute the RSI /// The type of moving average to use in computing the average gain/loss values /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The RelativeStrengthIndex indicator for the requested symbol over the specified period public RelativeStrengthIndex RSI(Symbol symbol, int period, MovingAverageType movingAverageType = MovingAverageType.Simple, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "RSI" + period, resolution); var rsi = new RelativeStrengthIndex(name, period, movingAverageType); RegisterIndicator(symbol, rsi, resolution, selector); return rsi; } /// /// Creates an SimpleMovingAverage indicator for the symbol. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose SMA we want /// The period of the SMA /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The SimpleMovingAverage for the given parameters public SimpleMovingAverage SMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "SMA" + period, resolution); var sma = new SimpleMovingAverage(name, period); RegisterIndicator(symbol, sma, resolution, selector); return sma; } /// /// Creates a new StandardDeviation indicator. This will return the population standard deviation of samples over the specified period. /// /// The symbol whose STD we want /// The period over which to compute the STD /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The StandardDeviation indicator for the requested symbol over the speified period public StandardDeviation STD(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "STD" + period, resolution); var std = new StandardDeviation(name, period); RegisterIndicator(symbol, std, resolution, selector); return std; } /// /// Creates a new Stochastic indicator. /// /// The symbol whose stochastic we seek /// The resolution. /// The period of the stochastic. Normally 14 /// The sum period of the stochastic. Normally 14 /// The sum period of the stochastic. Normally 3 /// Stochastic indicator for the requested symbol. public Stochastic STO(Symbol symbol, int period, int kPeriod, int dPeriod, Resolution? resolution = null) { string name = CreateIndicatorName(symbol, "STO", resolution); var stoch = new Stochastic(name, period, kPeriod, dPeriod); RegisterIndicator(symbol, stoch, resolution); return stoch; } /// /// Overload short hand to create a new Stochastic indicator; defaulting to the 3 period for dStoch /// /// The symbol whose stochastic we seek /// The resolution. /// The period of the stochastic. Normally 14 /// Stochastic indicator for the requested symbol. public Stochastic STO(Symbol symbol, int period, Resolution? resolution = null) { return STO(symbol, period, period, 3, resolution); } /// /// Creates a new Sum indicator. /// /// The symbol whose Sum we want /// The period over which to compute the Sum /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The Sum indicator for the requested symbol over the specified period public Sum SUM(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "SUM" + period, resolution); var sum = new Sum(name, period); RegisterIndicator(symbol, sum, resolution, selector); return sum; } /// /// Creates Swiss Army Knife transformation for the symbol. The indicator will be automatically /// updated on the given resolution. /// /// The symbol to use for calculations /// The period of the calculation /// The delta scale of the BandStop or BandPass /// The tool os the Swiss Army Knife /// The resolution /// elects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The calculation using the given tool public SwissArmyKnife SWISS(Symbol symbol, int period, double delta, SwissArmyKnifeTool tool, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "SWISS" + period, resolution); var swiss = new SwissArmyKnife(name, period, delta, tool); RegisterIndicator(symbol, swiss, resolution, selector); return swiss; } /// /// Creates a new T3MovingAverage indicator. /// /// The symbol whose T3 we want /// The period over which to compute the T3 /// The volume factor to be used for the T3 (value must be in the [0,1] range, defaults to 0.7) /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The T3MovingAverage indicator for the requested symbol over the specified period public T3MovingAverage T3(Symbol symbol, int period, decimal volumeFactor = 0.7m, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("T3({0},{1})", period, volumeFactor), resolution); var t3 = new T3MovingAverage(name, period, volumeFactor); RegisterIndicator(symbol, t3, resolution, selector); return t3; } /// /// Creates a new TripleExponentialMovingAverage indicator. /// /// The symbol whose TEMA we want /// The period over which to compute the TEMA /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The TripleExponentialMovingAverage indicator for the requested symbol over the specified period public TripleExponentialMovingAverage TEMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "TEMA" + period, resolution); var tema = new TripleExponentialMovingAverage(name, period); RegisterIndicator(symbol, tema, resolution, selector); return tema; } /// /// Creates a new TrueRange indicator. /// /// The symbol whose TR we want /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The TrueRange indicator for the requested symbol. public TrueRange TR(Symbol symbol, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "TR", resolution); var tr = new TrueRange(name); RegisterIndicator(symbol, tr, resolution, selector); return tr; } /// /// Creates a new TriangularMovingAverage indicator. /// /// The symbol whose TRIMA we want /// The period over which to compute the TRIMA /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The TriangularMovingAverage indicator for the requested symbol over the specified period public TriangularMovingAverage TRIMA(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "TRIMA" + period, resolution); var trima = new TriangularMovingAverage(name, period); RegisterIndicator(symbol, trima, resolution, selector); return trima; } /// /// Creates a new Trix indicator. /// /// The symbol whose TRIX we want /// The period over which to compute the TRIX /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The Trix indicator for the requested symbol over the specified period public Trix TRIX(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "TRIX" + period, resolution); var trix = new Trix(name, period); RegisterIndicator(symbol, trix, resolution, selector); return trix; } /// /// Creates a new UltimateOscillator indicator. /// /// The symbol whose ULTOSC we want /// The first period over which to compute the ULTOSC /// The second period over which to compute the ULTOSC /// The third period over which to compute the ULTOSC /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The UltimateOscillator indicator for the requested symbol over the specified period public UltimateOscillator ULTOSC(Symbol symbol, int period1, int period2, int period3, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, string.Format("ULTOSC({0},{1},{2})", period1, period2, period3), resolution); var ultosc = new UltimateOscillator(name, period1, period2, period3); RegisterIndicator(symbol, ultosc, resolution, selector); return ultosc; } /// /// Creates a new Variance indicator. This will return the population variance of samples over the specified period. /// /// The symbol whose VAR we want /// The period over which to compute the VAR /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The Variance indicator for the requested symbol over the speified period public Variance VAR(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "VAR" + period, resolution); var variance = new Variance(name, period); RegisterIndicator(symbol, variance, resolution, selector); return variance; } /// /// Creates an VolumeWeightedAveragePrice (VWAP) indicator for the symbol. The indicator will be automatically /// updated on the given resolution. /// /// The symbol whose VWAP we want /// The period of the VWAP /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The VolumeWeightedAveragePrice for the given parameters public VolumeWeightedAveragePriceIndicator VWAP(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { var name = CreateIndicatorName(symbol, "VWAP" + period, resolution); var vwap = new VolumeWeightedAveragePriceIndicator(name, period); RegisterIndicator(symbol, vwap, resolution, selector); return vwap; } /// /// Creates a new Williams %R indicator. This will compute the percentage change of /// the current closing price in relation to the high and low of the past N periods. /// The indicator will be automatically updated on the given resolution. /// /// The symbol whose Williams %R we want /// The period over which to compute the Williams %R /// The resolution /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) /// The rateofchangepercent indicator for the requested symbol over the specified period public WilliamsPercentR WILR(Symbol symbol, int period, Resolution? resolution = null, Func selector = null) { string name = CreateIndicatorName(symbol, "WILR" + period, resolution); var williamspercentr = new WilliamsPercentR(name, period); RegisterIndicator(symbol, williamspercentr, resolution, selector); return williamspercentr; } /// /// Creates a new name for an indicator created with the convenience functions (SMA, EMA, ect...) /// /// The symbol this indicator is registered to /// The indicator type, for example, 'SMA5' /// The resolution requested /// A unique for the given parameters public string CreateIndicatorName(Symbol symbol, string type, Resolution? resolution) { if (!resolution.HasValue) { resolution = GetSubscription(symbol).Resolution; } string res; switch (resolution) { case Resolution.Tick: res = "_tick"; break; case Resolution.Second: res = "_sec"; break; case Resolution.Minute: res = "_min"; break; case Resolution.Hour: res = "_hr"; break; case Resolution.Daily: res = "_day"; break; case null: res = string.Empty; break; default: throw new ArgumentOutOfRangeException("resolution"); } return string.Format("{0}({1}{2})", type, symbol.ToString(), res); } /// /// Gets the SubscriptionDataConfig for the specified symbol /// /// Thrown if no configuration is found for the requested symbol /// The symbol to retrieve configuration for /// The SubscriptionDataConfig for the specified symbol protected SubscriptionDataConfig GetSubscription(Symbol symbol) { SubscriptionDataConfig subscription; try { // find our subscription to this symbol subscription = SubscriptionManager.Subscriptions.First(x => x.Symbol == symbol); } catch (InvalidOperationException) { // this will happen if we did not find the subscription, let's give the user a decent error message throw new Exception("Please register to receive data for symbol '" + symbol.ToString() + "' using the AddSecurity() function."); } return subscription; } /// /// Creates and registers a new consolidator to receive automatic updates at the specified resolution as well as configures /// the indicator to receive updates from the consolidator. /// /// The symbol to register against /// The indicator to receive data from the consolidator /// The resolution at which to send data to the indicator, null to use the same resolution as the subscription /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) public void RegisterIndicator(Symbol symbol, IndicatorBase indicator, Resolution? resolution = null, Func selector = null) { RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution), selector ?? (x => x.Value)); } /// /// Creates and registers a new consolidator to receive automatic updates at the specified resolution as well as configures /// the indicator to receive updates from the consolidator. /// /// The symbol to register against /// The indicator to receive data from the consolidator /// The resolution at which to send data to the indicator, null to use the same resolution as the subscription /// Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) public void RegisterIndicator(Symbol symbol, IndicatorBase indicator, TimeSpan? resolution = null, Func selector = null) { RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution), selector ?? (x => x.Value)); } /// /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates /// from the consolidator. /// /// The symbol to register against /// The indicator to receive data from the consolidator /// The consolidator to receive raw subscription data /// Selects a value from the BaseData send into the indicator, if null defaults to the Value property of BaseData (x => x.Value) public void RegisterIndicator(Symbol symbol, IndicatorBase indicator, IDataConsolidator consolidator, Func selector = null) { // default our selector to the Value property on BaseData selector = selector ?? (x => x.Value); // register the consolidator for automatic updates via SubscriptionManager SubscriptionManager.AddConsolidator(symbol, consolidator); // attach to the DataConsolidated event so it updates our indicator consolidator.DataConsolidated += (sender, consolidated) => { var value = selector(consolidated); indicator.Update(new IndicatorDataPoint(consolidated.Symbol, consolidated.EndTime, value)); }; } /// /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates /// from the consolidator. /// /// The symbol to register against /// The indicator to receive data from the consolidator /// The resolution at which to send data to the indicator, null to use the same resolution as the subscription public void RegisterIndicator(Symbol symbol, IndicatorBase indicator, Resolution? resolution = null) where T : IBaseData { RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution)); } /// /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates /// from the consolidator. /// /// The symbol to register against /// The indicator to receive data from the consolidator /// The resolution at which to send data to the indicator, null to use the same resolution as the subscription /// Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x) public void RegisterIndicator(Symbol symbol, IndicatorBase indicator, Resolution? resolution, Func selector) where T : IBaseData { RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution), selector); } /// /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates /// from the consolidator. /// /// The symbol to register against /// The indicator to receive data from the consolidator /// The resolution at which to send data to the indicator, null to use the same resolution as the subscription /// Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x) public void RegisterIndicator(Symbol symbol, IndicatorBase indicator, TimeSpan? resolution, Func selector = null) where T : IBaseData { RegisterIndicator(symbol, indicator, ResolveConsolidator(symbol, resolution), selector); } /// /// Registers the consolidator to receive automatic updates as well as configures the indicator to receive updates /// from the consolidator. /// /// The symbol to register against /// The indicator to receive data from the consolidator /// The consolidator to receive raw subscription data /// Selects a value from the BaseData send into the indicator, if null defaults to a cast (x => (T)x) public void RegisterIndicator(Symbol symbol, IndicatorBase indicator, IDataConsolidator consolidator, Func selector = null) where T : IBaseData { // assign default using cast selector = selector ?? (x => (T)x); // register the consolidator for automatic updates via SubscriptionManager SubscriptionManager.AddConsolidator(symbol, consolidator); // check the output type of the consolidator and verify we can assign it to T var type = typeof(T); if (!type.IsAssignableFrom(consolidator.OutputType)) { throw new ArgumentException(string.Format("Type mismatch found between consolidator and indicator for symbol: {0}." + "Consolidator outputs type {1} but indicator expects input type {2}", symbol, consolidator.OutputType.Name, type.Name) ); } // attach to the DataConsolidated event so it updates our indicator consolidator.DataConsolidated += (sender, consolidated) => { var value = selector(consolidated); indicator.Update(value); }; } /// /// Gets the default consolidator for the specified symbol and resolution /// /// The symbo whose data is to be consolidated /// The resolution for the consolidator, if null, uses the resolution from subscription /// The new default consolidator public IDataConsolidator ResolveConsolidator(Symbol symbol, Resolution? resolution) { var subscription = GetSubscription(symbol); // if not specified, default to the subscription's resolution if (!resolution.HasValue) { resolution = subscription.Resolution; } var timeSpan = resolution.Value.ToTimeSpan(); // verify this consolidator will give reasonable results, if someone asks for second consolidation but we have minute // data we won't be able to do anything good, we'll call it second, but it would really just be minute! if (timeSpan < subscription.Resolution.ToTimeSpan()) { throw new ArgumentException(string.Format("Unable to create {0} {1} consolidator because {0} is registered for {2} data. " + "Consolidators require higher resolution data to produce lower resolution data.", symbol, resolution.Value, subscription.Resolution) ); } return ResolveConsolidator(symbol, timeSpan); } /// /// Gets the default consolidator for the specified symbol and resolution /// /// The symbo whose data is to be consolidated /// The requested time span for the consolidator, if null, uses the resolution from subscription /// The new default consolidator public IDataConsolidator ResolveConsolidator(Symbol symbol, TimeSpan? timeSpan) { var subscription = GetSubscription(symbol); // if not specified, default to the subscription resolution if (!timeSpan.HasValue) { timeSpan = subscription.Resolution.ToTimeSpan(); } // verify this consolidator will give reasonable results, if someone asks for second consolidation but we have minute // data we won't be able to do anything good, we'll call it second, but it would really just be minute! if (timeSpan.Value < subscription.Resolution.ToTimeSpan()) { throw new ArgumentException(string.Format("Unable to create {0} consolidator because {0} is registered for {1} data. " + "Consolidators require higher resolution data to produce lower resolution data.", symbol, subscription.Resolution) ); } // if our type can be used as a trade bar, then let's just make one of those // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to TradeBar if (typeof(TradeBar).IsAssignableFrom(subscription.Type)) { return new TradeBarConsolidator(timeSpan.Value); } // if our type can be used as a quote bar, then let's just make one of those // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to QuoteBar if (typeof(QuoteBar).IsAssignableFrom(subscription.Type)) { return new QuoteBarConsolidator(timeSpan.Value); } // if our type can be used as a tick then we'll use a consolidator that keeps the TickType // we use IsAssignableFrom instead of IsSubclassOf so that we can account for types that are able to be cast to Tick if (typeof(Tick).IsAssignableFrom(subscription.Type)) { // Use IdentityDataConsolidator when ticks are not meant to consolidated into bars if (timeSpan.Value.Ticks == 0) { return new IdentityDataConsolidator(); } switch (subscription.TickType) { case TickType.OpenInterest: return new OpenInterestConsolidator(timeSpan.Value); case TickType.Quote: return new TickQuoteBarConsolidator(timeSpan.Value); default: return new TickConsolidator(timeSpan.Value); } } // if our type can be used as a DynamicData then we'll use the DynamicDataConsolidator if (typeof(DynamicData).IsAssignableFrom(subscription.Type)) { return new DynamicDataConsolidator(timeSpan.Value); } // no matter what we can always consolidate based on the time-value pair of BaseData return new BaseDataConsolidator(timeSpan.Value); } } // End Partial Algorithm Template - Indicators. } // End QC Namespace