21 using System.Collections.Generic;
28 public partial class QCAlgorithm
33 [DocumentationAttribute(Indicators)]
48 Resolution? resolution =
null, Func<IBaseData, TradeBar> selector =
null)
51 var accelerationBands =
new AccelerationBands(name, period, width, movingAverageType);
52 InitializeIndicator(symbol, accelerationBands, resolution, selector);
54 return accelerationBands;
69 InitializeIndicator(symbol, accumulationDistribution, resolution, selector);
71 return accumulationDistribution;
88 InitializeIndicator(symbol, accumulationDistributionOscillator, resolution, selector);
90 return accumulationDistributionOscillator;
106 public Alpha A(
Symbol target,
Symbol reference,
int alphaPeriod = 1,
int betaPeriod = 252,
Resolution? resolution =
null, decimal? riskFreeRate =
null, Func<IBaseData, IBaseDataBar> selector =
null)
108 var baseBame = riskFreeRate.HasValue ? $
"A({alphaPeriod},{betaPeriod},{riskFreeRate})" : $
"A({alphaPeriod},{betaPeriod})";
116 var alpha =
new Alpha(name, target, reference, alphaPeriod, betaPeriod, riskFreeRateModel);
117 InitializeIndicator(target, alpha, resolution, selector);
118 InitializeIndicator(reference, alpha, resolution, selector);
136 Resolution? resolution =
null, Func<IBaseData, decimal> selector =
null)
138 var name =
CreateIndicatorName(symbol, $
"ARIMA({arOrder},{diffOrder},{maOrder},{period})", resolution);
140 InitializeIndicator(symbol, arimaIndicator, resolution, selector);
142 return arimaIndicator;
159 InitializeIndicator(symbol, averageDirectionalIndex, resolution, selector);
161 return averageDirectionalIndex;
176 var name =
CreateIndicatorName(symbol, $
"AO({fastPeriod},{slowPeriod},{type})", resolution);
177 var awesomeOscillator =
new AwesomeOscillator(name, fastPeriod, slowPeriod, type);
178 InitializeIndicator(symbol, awesomeOscillator, resolution, selector);
180 return awesomeOscillator;
196 InitializeIndicator(symbol, averageDirectionalMovementIndexRating, resolution, selector);
198 return averageDirectionalMovementIndexRating;
220 InitializeIndicator(symbol, arnaudLegouxMovingAverage, resolution, selector);
222 return arnaudLegouxMovingAverage;
240 InitializeIndicator(symbol, absolutePriceOscillator, resolution, selector);
242 return absolutePriceOscillator;
256 return AROON(symbol, period, period, resolution, selector);
273 InitializeIndicator(symbol, aroonOscillator, resolution, selector);
275 return aroonOscillator;
293 InitializeIndicator(symbol, averageTrueRange, resolution, selector);
295 return averageTrueRange;
312 InitializeIndicator(symbol, augenPriceSpike, resolution, selector);
314 return augenPriceSpike;
329 Resolution? resolution =
null, Func<IBaseData, decimal> selector =
null)
332 var bollingerBands =
new BollingerBands(name, period, k, movingAverageType);
333 InitializeIndicator(symbol, bollingerBands, resolution, selector);
335 return bollingerBands;
352 var beta =
new Beta(name, target, reference, period);
353 InitializeIndicator(target, beta, resolution, selector);
354 InitializeIndicator(reference, beta, resolution, selector);
372 InitializeIndicator(symbol, balanceOfPower, resolution, selector);
374 return balanceOfPower;
389 Func<IBaseData, decimal> selector =
null)
391 var name =
CreateIndicatorName(symbol, $
"CC({shortRocPeriod},{longRocPeriod},{lwmaPeriod})", resolution);
392 var coppockCurve =
new CoppockCurve(name, shortRocPeriod, longRocPeriod, lwmaPeriod);
393 InitializeIndicator(symbol, coppockCurve, resolution, selector);
413 var correlation =
new Correlation(name, target, reference, period);
414 InitializeIndicator(target, correlation, resolution, selector);
415 InitializeIndicator(reference, correlation, resolution, selector);
435 InitializeIndicator(symbol, commodityChannelIndex, resolution, selector);
437 return commodityChannelIndex;
453 InitializeIndicator(symbol, chaikinMoneyFlow, resolution, selector);
455 return chaikinMoneyFlow;
472 InitializeIndicator(symbol, chandeMomentumOscillator, resolution, selector);
474 return chandeMomentumOscillator;
492 InitializeIndicator(symbol, deMarkerIndicator, resolution, selector);
493 return deMarkerIndicator;
506 [DocumentationAttribute(Indicators)]
510 var donchianChannel =
new DonchianChannel(name, upperPeriod, lowerPeriod);
511 InitializeIndicator(symbol, donchianChannel, resolution, selector);
513 return donchianChannel;
528 return DCH(symbol, period, period, resolution, selector);
547 var name = InitializeOptionIndicator<Delta>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
549 var delta =
new Delta(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
552 if (mirrorOption !=
null)
575 return D(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
591 InitializeIndicator(symbol, doubleExponentialMovingAverage, resolution, selector);
593 return doubleExponentialMovingAverage;
608 public DerivativeOscillator DO(
Symbol symbol,
int rsiPeriod,
int smoothingRsiPeriod,
int doubleSmoothingRsiPeriod,
int signalLinePeriod,
Resolution? resolution =
null, Func<IBaseData, decimal> selector =
null)
610 var name =
CreateIndicatorName(symbol, $
"DO({rsiPeriod},{smoothingRsiPeriod},{doubleSmoothingRsiPeriod},{signalLinePeriod})", resolution);
611 var derivativeOscillator =
new DerivativeOscillator(name, rsiPeriod, smoothingRsiPeriod, doubleSmoothingRsiPeriod, signalLinePeriod);
612 InitializeIndicator(symbol, derivativeOscillator, resolution, selector);
614 return derivativeOscillator;
630 InitializeIndicator(symbol, detrendedPriceOscillator, resolution, selector);
632 return detrendedPriceOscillator;
665 InitializeIndicator(symbol, exponentialMovingAverage, resolution, selector);
667 return exponentialMovingAverage;
685 InitializeIndicator(symbol, easeOfMovementValue, resolution, selector);
687 return easeOfMovementValue;
702 var resolution = GetSubscription(symbol).
Resolution;
721 RegisterIndicator<IBaseData>(symbol, filteredIdentity, resolution, selector);
722 return filteredIdentity;
738 var name = Invariant($
"{symbol}({fieldName ?? "close
"}_{resolution})");
740 RegisterIndicator<IBaseData>(symbol, filteredIdentity,
ResolveConsolidator(symbol, resolution), selector);
741 return filteredIdentity;
758 InitializeIndicator(symbol, fisherTransform, resolution, selector);
760 return fisherTransform;
778 InitializeIndicator(symbol, fractalAdaptiveMovingAverage, resolution, selector);
780 return fractalAdaptiveMovingAverage;
799 var name = InitializeOptionIndicator<Gamma>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
801 var gamma =
new Gamma(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
804 if (mirrorOption !=
null)
827 return G(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
842 InitializeIndicator(symbol, heikinAshi, resolution, selector);
862 public HilbertTransform HT(
Symbol symbol,
int length, decimal inPhaseMultiplicationFactor, decimal quadratureMultiplicationFactor,
Resolution? resolution =
null, Func<IBaseData, decimal> selector =
null)
864 var name =
CreateIndicatorName(symbol, $
"HT({length}, {inPhaseMultiplicationFactor}, {quadratureMultiplicationFactor})", resolution);
865 var hilbertTransform =
new HilbertTransform(length, inPhaseMultiplicationFactor, quadratureMultiplicationFactor);
866 InitializeIndicator(symbol, hilbertTransform, resolution, selector);
868 return hilbertTransform;
884 InitializeIndicator(symbol, hullMovingAverage, resolution, selector);
886 return hullMovingAverage;
905 int senkouADelayPeriod,
int senkouBDelayPeriod,
Resolution? resolution =
null, Func<IBaseData, TradeBar> selector =
null)
907 var name =
CreateIndicatorName(symbol, $
"ICHIMOKU({tenkanPeriod},{kijunPeriod},{senkouAPeriod},{senkouBPeriod},{senkouADelayPeriod},{senkouBDelayPeriod})", resolution);
908 var ichimokuKinkoHyo =
new IchimokuKinkoHyo(name, tenkanPeriod, kijunPeriod, senkouAPeriod, senkouBPeriod, senkouADelayPeriod, senkouBDelayPeriod);
909 InitializeIndicator(symbol, ichimokuKinkoHyo, resolution, selector);
911 return ichimokuKinkoHyo;
925 var resolution = GetSubscription(symbol).
Resolution;
926 return Identity(symbol, resolution, selector, fieldName);
957 public Identity Identity(
Symbol symbol, TimeSpan resolution, Func<IBaseData, decimal> selector =
null,
string fieldName =
null)
959 var name = Invariant($
"{symbol}({fieldName ?? "close
"},{resolution})");
981 var name = InitializeOptionIndicator<ImpliedVolatility>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
983 var iv =
new ImpliedVolatility(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel);
986 if (mirrorOption !=
null)
1004 return KAMA(symbol, period, 2, 30, resolution, selector);
1020 var name =
CreateIndicatorName(symbol, $
"KAMA({period},{fastEmaPeriod},{slowEmaPeriod})", resolution);
1022 InitializeIndicator(symbol, kaufmanAdaptiveMovingAverage, resolution, selector);
1024 return kaufmanAdaptiveMovingAverage;
1041 InitializeIndicator(symbol, kaufmanEfficiencyRatio, resolution, selector);
1043 return kaufmanEfficiencyRatio;
1061 var keltnerChannels =
new KeltnerChannels(name, period, k, movingAverageType);
1062 InitializeIndicator(symbol, keltnerChannels, resolution, selector);
1064 return keltnerChannels;
1079 var logReturn =
new LogReturn(name, period);
1080 InitializeIndicator(symbol, logReturn, resolution, selector);
1098 InitializeIndicator(symbol, leastSquaresMovingAverage, resolution, selector);
1100 return leastSquaresMovingAverage;
1117 InitializeIndicator(symbol, linearWeightedMovingAverage, resolution, selector);
1119 return linearWeightedMovingAverage;
1136 var name =
CreateIndicatorName(symbol, $
"MACD({fastPeriod},{slowPeriod},{signalPeriod})", resolution);
1138 InitializeIndicator(symbol, movingAverageConvergenceDivergence, resolution, selector);
1140 return movingAverageConvergenceDivergence;
1156 InitializeIndicator(symbol, meanAbsoluteDeviation, resolution, selector);
1158 return meanAbsoluteDeviation;
1176 var marketProfile =
new VolumeProfile(name, period, valueAreaVolumePercentage, priceRangeRoundOff);
1177 InitializeIndicator(symbol, marketProfile, resolution, selector);
1179 return marketProfile;
1194 public TimeProfile TP(
Symbol symbol,
int period = 2, decimal valueAreaVolumePercentage = 0.70m, decimal priceRangeRoundOff = 0.05m,
Resolution resolution =
Resolution.Daily, Func<IBaseData, TradeBar> selector =
null)
1197 var marketProfile =
new TimeProfile(name, period, valueAreaVolumePercentage, priceRangeRoundOff);
1198 InitializeIndicator(symbol, marketProfile, resolution, selector);
1200 return marketProfile;
1216 InitializeIndicator(symbol, timeSeriesForecast, resolution, selector);
1218 return timeSeriesForecast;
1234 var maximum =
new Maximum(name, period);
1237 if (selector ==
null)
1239 var subscription = GetSubscription(symbol);
1240 if (typeof(
TradeBar).IsAssignableFrom(subscription.Type))
1243 selector = x => ((
TradeBar)x).High;
1271 InitializeIndicator(symbol, moneyFlowIndex, resolution, selector);
1273 return moneyFlowIndex;
1290 var massIndex =
new MassIndex(name, emaPeriod, sumPeriod);
1291 InitializeIndicator(symbol, massIndex, resolution, selector);
1308 var midPoint =
new MidPoint(name, period);
1309 InitializeIndicator(symbol, midPoint, resolution, selector);
1326 var midPrice =
new MidPrice(name, period);
1327 InitializeIndicator(symbol, midPrice, resolution, selector);
1345 var minimum =
new Minimum(name, period);
1348 if (selector ==
null)
1350 var subscription = GetSubscription(symbol);
1351 if (typeof(
TradeBar).IsAssignableFrom(subscription.Type))
1381 var momentum =
new Momentum(name, period);
1382 InitializeIndicator(symbol, momentum, resolution, selector);
1399 var name =
CreateIndicatorName(symbol, $
"MOMERSION({minPeriod},{fullPeriod})", resolution);
1401 InitializeIndicator(symbol, momersion, resolution, selector);
1420 InitializeIndicator(symbol, momentumPercent, resolution, selector);
1422 return momentumPercent;
1438 InitializeIndicator(symbol, normalizedAverageTrueRange, resolution, selector);
1440 return normalizedAverageTrueRange;
1457 InitializeIndicator(symbol, onBalanceVolume, resolution, selector);
1459 return onBalanceVolume;
1476 var pivotPointsHighLow =
new PivotPointsHighLow(name, lengthHigh, lengthLow, lastStoredValues);
1477 InitializeIndicator(symbol, pivotPointsHighLow, resolution, selector);
1479 return pivotPointsHighLow;
1497 InitializeIndicator(symbol, percentagePriceOscillator, resolution, selector);
1499 return percentagePriceOscillator;
1515 var name =
CreateIndicatorName(symbol, $
"PSAR({afStart},{afIncrement},{afMax})", resolution);
1517 InitializeIndicator(symbol, parabolicStopAndReverse, resolution, selector);
1519 return parabolicStopAndReverse;
1536 InitializeIndicator(symbol, regressionChannel, resolution, selector);
1538 return regressionChannel;
1554 InitializeIndicator(symbol, relativeMovingAverage, resolution, selector);
1556 return relativeMovingAverage;
1574 InitializeIndicator(symbol, rateOfChange, resolution, selector);
1576 return rateOfChange;
1593 InitializeIndicator(symbol, rateOfChangePercent, resolution, selector);
1595 return rateOfChangePercent;
1611 InitializeIndicator(symbol, rateOfChangeRatio, resolution, selector);
1613 return rateOfChangeRatio;
1631 InitializeIndicator(symbol, relativeStrengthIndex, resolution, selector);
1633 return relativeStrengthIndex;
1650 InitializeIndicator(symbol, relativeVigorIndex, resolution, selector);
1652 return relativeVigorIndex;
1671 return relativeDailyVolume;
1690 var name = InitializeOptionIndicator<Rho>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
1692 var rho =
new Rho(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
1695 if (mirrorOption !=
null)
1718 return R(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
1732 Resolution? resolution =
null, Func<IBaseData, IBaseDataBar> selector =
null)
1735 var strend =
new SuperTrend(name, period, multiplier, movingAverageType);
1736 InitializeIndicator(symbol, strend, resolution, selector);
1755 var baseBame = riskFreeRate.HasValue ? $
"SR({sharpePeriod},{riskFreeRate})" : $
"SR({sharpePeriod})";
1761 var sharpeRatio =
new SharpeRatio(name, sharpePeriod, riskFreeRateModel);
1762 InitializeIndicator(symbol, sharpeRatio, resolution, selector);
1779 var name =
CreateIndicatorName(symbol, $
"SORTINO({sortinoPeriod},{minimumAcceptableReturn})", resolution);
1780 var sortinoRatio =
new SortinoRatio(name, sortinoPeriod, minimumAcceptableReturn);
1781 InitializeIndicator(symbol, sortinoRatio, resolution, selector);
1783 return sortinoRatio;
1801 InitializeIndicator(symbol, simpleMovingAverage, resolution, selector);
1803 return simpleMovingAverage;
1820 var name =
CreateIndicatorName(symbol, $
"STC({cyclePeriod},{fastPeriod},{slowPeriod})", resolution);
1821 var schaffTrendCycle =
new SchaffTrendCycle(name, cyclePeriod, fastPeriod, slowPeriod, movingAverageType);
1822 InitializeIndicator(symbol, schaffTrendCycle, resolution, selector);
1824 return schaffTrendCycle;
1840 InitializeIndicator(symbol, standardDeviation, resolution, selector);
1842 return standardDeviation;
1858 var name =
CreateIndicatorName(symbol, $
"TDD({period},{minimumAcceptableReturn})", resolution);
1860 InitializeIndicator(symbol, targetDownsideDeviation, resolution, selector);
1862 return targetDownsideDeviation;
1877 Func<IBaseData, TradeBar> selector =
null)
1880 var stochastic =
new Stochastic(name, period, kPeriod, dPeriod);
1881 InitializeIndicator(symbol, stochastic, resolution, selector);
1897 return STO(symbol, period, period, 3, resolution, selector);
1912 var sum =
new Sum(name, period);
1913 InitializeIndicator(symbol, sum, resolution, selector);
1933 var swissArmyKnife =
new SwissArmyKnife(name, period, delta, tool);
1934 InitializeIndicator(symbol, swissArmyKnife, resolution, selector);
1936 return swissArmyKnife;
1955 var name = InitializeOptionIndicator<Theta>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
1957 var theta =
new Theta(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
1960 if (mirrorOption !=
null)
1983 return T(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
1999 var t3MovingAverage =
new T3MovingAverage(name, period, volumeFactor);
2000 InitializeIndicator(symbol, t3MovingAverage, resolution, selector);
2002 return t3MovingAverage;
2018 InitializeIndicator(symbol, tripleExponentialMovingAverage, resolution, selector);
2020 return tripleExponentialMovingAverage;
2039 var name =
CreateIndicatorName(symbol, $
"TSI({longTermPeriod},{shortTermPeriod},{signalPeriod})", resolution);
2040 var trueStrengthIndex =
new TrueStrengthIndex(name, longTermPeriod, shortTermPeriod, signalPeriod, signalType);
2041 InitializeIndicator(symbol, trueStrengthIndex, resolution, selector);
2043 return trueStrengthIndex;
2058 InitializeIndicator(symbol, trueRange, resolution, selector);
2076 InitializeIndicator(symbol, triangularMovingAverage, resolution, selector);
2078 return triangularMovingAverage;
2093 var trix =
new Trix(name, period);
2094 InitializeIndicator(symbol, trix, resolution, selector);
2112 var name =
CreateIndicatorName(symbol, $
"ULTOSC({period1},{period2},{period3})", resolution);
2114 InitializeIndicator(symbol, ultimateOscillator, resolution, selector);
2116 return ultimateOscillator;
2135 var name = InitializeOptionIndicator<Vega>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
2137 var vega =
new Vega(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
2140 if (mirrorOption !=
null)
2160 InitializeIndicator(symbol, variableIndexDynamicAverage, resolution, selector);
2162 return variableIndexDynamicAverage;
2174 [Obsolete(
"'VAR' is obsolete please use 'V' instead")]
2177 return V(symbol, period, resolution, selector);
2192 var variance =
new Variance(name, period);
2193 InitializeIndicator(symbol, variance, resolution, selector);
2210 var valueAtRisk =
new ValueAtRisk(name, period, confidenceLevel);
2211 InitializeIndicator(symbol, valueAtRisk, resolution, selector);
2230 InitializeIndicator(symbol, volumeWeightedAveragePriceIndicator, resolution, selector);
2232 return volumeWeightedAveragePriceIndicator;
2247 return intradayVwap;
2265 InitializeIndicator(symbol, williamsPercentR, resolution, selector);
2267 return williamsPercentR;
2285 InitializeIndicator(symbol, wilderMovingAverage, resolution, selector);
2287 return wilderMovingAverage;
2302 Func<IBaseData, TradeBar> selector =
null)
2306 InitializeIndicator(symbol, si, resolution, selector);
2323 Func<IBaseData, TradeBar> selector =
null)
2327 InitializeIndicator(symbol, asi, resolution, selector);
2342 return TRIN(symbols.ToArray(), resolution, selector);
2357 foreach (var symbol
in symbols)
2360 InitializeIndicator(symbol, trin, resolution, selector);
2378 foreach (var symbol
in symbols)
2381 InitializeIndicator(symbol, adr, resolution, selector);
2399 foreach (var symbol
in symbols)
2402 InitializeIndicator(symbol, advr, resolution, selector);
2420 foreach (var symbol
in symbols)
2423 InitializeIndicator(symbol, adDiff, resolution, selector);
2441 return MOSC(symbols.ToArray(), fastPeriod, slowPeriod, resolution, selector);
2458 foreach (var symbol
in symbols)
2461 InitializeIndicator(symbol, mosc, resolution, selector);
2479 return MSI(symbols.ToArray(), fastPeriod, slowPeriod, resolution, selector);
2496 foreach (var symbol
in symbols)
2499 InitializeIndicator(symbol, msi, resolution, selector);
2530 if (!resolution.HasValue && symbolIsNotEmpty)
2532 resolution = GetSubscription(symbol).
Resolution;
2535 var res =
string.Empty;
2562 throw new ArgumentOutOfRangeException(nameof(resolution), resolution,
"resolution parameter is out of range.");
2565 var parts =
new List<string>();
2567 if (symbolIsNotEmpty)
2573 return Invariant($
"{type}({string.Join("_
", parts)})").Replace(
")(",
",");
2590 .GetSubscriptionDataConfigs(symbol)
2593 .ThenBy(x => x.TickType)
2597 subscription = subscriptions.FirstOrDefault(x => tickType ==
null || tickType == x.TickType);
2598 if (subscription ==
null)
2601 subscription = subscriptions.First();
2604 catch (InvalidOperationException)
2607 throw new Exception($
"Please register to receive data for symbol \'{symbol}\' using the AddSecurity() function.");
2609 return subscription;
2620 [DocumentationAttribute(ConsolidatingData)]
2621 [DocumentationAttribute(Indicators)]
2655 selector = selector ?? (x => x.Value);
2657 RegisterConsolidator(indicator, symbol, consolidator);
2662 var value = selector(consolidated);
2663 indicator.Update(
new IndicatorDataPoint(consolidated.Symbol, consolidated.EndTime, value));
2728 var selectorToUse = selector ?? (x => (
T)x);
2730 RegisterConsolidator(indicator, symbol, consolidator);
2733 var type = typeof(
T);
2734 if (!type.IsAssignableFrom(consolidator.
OutputType))
2740 selectorToUse = consolidated => (
T)(
object)
new IndicatorDataPoint(consolidated.Symbol, consolidated.EndTime, consolidated.Value);
2744 throw new ArgumentException($
"Type mismatch found between consolidator and indicator for symbol: {symbol}." +
2745 $
"Consolidator outputs type {consolidator.OutputType.Name} but indicator expects input type {type.Name}"
2753 var value = selectorToUse(consolidated);
2754 indicator.Update(value);
2777 foreach (var consolidator
in indicator.Consolidators)
2782 indicator.Consolidators.Clear();
2796 resolution = GetResolution(symbol, resolution,
null);
2797 var period = resolution.Value.ToTimeSpan();
2812 var history = GetIndicatorWarmUpHistory(symbol, indicator, period, out var identityConsolidator);
2813 if (history == Enumerable.Empty<
Slice>())
return;
2816 selector ??= (x => x.Value);
2818 Action<IBaseData> onDataConsolidated = bar =>
2821 indicator.Update(input);
2824 WarmUpIndicatorImpl(symbol, period, onDataConsolidated, history, identityConsolidator);
2839 resolution = GetResolution(symbol, resolution, typeof(
T));
2840 var period = resolution.Value.ToTimeSpan();
2856 var history = GetIndicatorWarmUpHistory(symbol, indicator, period, out var identityConsolidator);
2857 if (history == Enumerable.Empty<
Slice>())
return;
2860 selector ??= (x => (
T)x);
2863 Action<T> onDataConsolidated = bar =>
2865 indicator.Update(selector(bar));
2868 WarmUpIndicatorImpl(symbol, period, onDataConsolidated, history, identityConsolidator);
2871 private IEnumerable<Slice> GetIndicatorWarmUpHistory(
Symbol symbol,
IIndicator indicator, TimeSpan timeSpan, out
bool identityConsolidator)
2873 identityConsolidator =
false;
2876 if (periods.HasValue && periods != 0)
2878 var resolution = timeSpan.ToHigherResolutionEquivalent(
false);
2880 identityConsolidator = resolution.ToTimeSpan() == timeSpan;
2881 var resolutionTicks = resolution.ToTimeSpan().Ticks;
2882 if (resolutionTicks != 0)
2884 periods *= (int)(timeSpan.Ticks / resolutionTicks);
2889 return History(
new[] { symbol }, periods.
Value, resolution);
2891 catch (ArgumentException e)
2893 Debug($
"{indicator.Name} could not be warmed up. Reason: {e.Message}");
2896 else if (!_isEmitWarmupInsightWarningSent)
2898 Debug($
"Warning: the 'WarmUpIndicator' feature only works with indicators which inherit from '{nameof(IIndicatorWarmUpPeriodProvider)}'" +
2899 $
" and define a warm up period, setting property 'WarmUpPeriod' with a value > 0." +
2900 $
" The provided indicator of type '{indicator.GetType().Name}' will not be warmed up.");
2901 _isEmitWarmupInsightWarningSent =
true;
2904 return Enumerable.Empty<
Slice>();
2907 private void WarmUpIndicatorImpl<T>(
Symbol symbol, TimeSpan period, Action<T> handler, IEnumerable<Slice> history,
bool identityConsolidator)
2911 if (identityConsolidator)
2913 period = TimeSpan.Zero;
2917 consolidator =
Consolidate(symbol, period, handler);
2921 var providedType = typeof(
T);
2922 if (providedType.IsAbstract)
2931 .ThenBy(tuple => tuple.Item2).First();
2944 var consolidatorInputType = consolidator.
InputType;
2946 foreach (var slice
in history)
2948 if (slice.TryGet(consolidatorInputType, symbol, out var data))
2951 consolidator.
Update(lastBar);
2956 if (lastBar !=
null)
2971 [DocumentationAttribute(ConsolidatingData)]
2972 [DocumentationAttribute(Indicators)]
2975 TimeSpan? timeSpan =
null;
2976 if (resolution.HasValue)
2978 timeSpan = resolution.Value.ToTimeSpan();
2995 var subscription = GetSubscription(symbol, tickType);
2996 var subscriptionTimeSpan = subscription.Resolution.ToTimeSpan();
2999 if (!timeSpan.HasValue)
3001 timeSpan = subscriptionTimeSpan;
3006 if (timeSpan.Value < subscriptionTimeSpan)
3008 throw new ArgumentException($
"Unable to create {symbol} consolidator because {symbol} is registered for " +
3009 Invariant($
"{subscription.Resolution.ToStringInvariant()} data. Consolidators require higher resolution data to produce lower resolution data.")
3012 else if (timeSpan.Value == subscriptionTimeSpan)
3015 timeSpan = TimeSpan.Zero;
3033 if (typeof(
TradeBar).IsAssignableFrom(consolidatorInputType))
3036 if (period.Ticks == 0)
3045 if (typeof(
QuoteBar).IsAssignableFrom(consolidatorInputType))
3048 if (period.Ticks == 0)
3057 if (typeof(
Tick).IsAssignableFrom(consolidatorInputType))
3060 if (period.Ticks == 0)
3079 if (typeof(
DynamicData).IsAssignableFrom(consolidatorInputType))
3082 if (period.Ticks == 0)
3090 if (period.Ticks == 0)
3166 return Consolidate(symbol, period, tickType, handler);
3182 return Consolidate(symbol, period.ToTimeSpan(), tickType, handler);
3199 var subscription = GetSubscription(symbol, tickType);
3202 var consolidator =
CreateConsolidator(period, subscription.Type, subscription.TickType);
3204 AddConsolidator(symbol, consolidator, handler, tickType);
3205 return consolidator;
3250 return Consolidate(symbol, calendar, tickType, handler);
3267 var subscription = GetSubscription(symbol, tickType);
3270 var consolidator =
CreateConsolidator(calendar, subscription.Type, subscription.TickType);
3272 AddConsolidator(symbol, consolidator, handler);
3273 return consolidator;
3283 if (!typeof(
T).IsAssignableFrom(consolidator.
OutputType))
3292 throw new ArgumentException(
3293 $
"Unable to consolidate with the specified handler because the consolidator's output type " +
3294 $
"is {consolidator.OutputType.Name} but the handler's input type is {typeof(T).Name}.");
3298 consolidator.
DataConsolidated += (sender, consolidated) => handler((
T)consolidated);
3304 [DocumentationAttribute(ConsolidatingData)]
3309 if (typeof(
TradeBar).IsAssignableFrom(consolidatorInputType))
3316 if (typeof(
QuoteBar).IsAssignableFrom(consolidatorInputType))
3323 if (typeof(
Tick).IsAssignableFrom(consolidatorInputType))
3333 if (typeof(
DynamicData).IsAssignableFrom(consolidatorInputType))
3346 Func<IBaseData, decimal> selector =
null)
3357 Func<IBaseData, T> selector =
null)
3372 var name =
CreateIndicatorName(symbol, $
"{typeof(T).Name}({riskFreeRate},{dividendYield},{optionModel})", resolution);
3374 riskFreeRateModel = riskFreeRate.HasValue
3379 if (dividendYield.HasValue)
3383 else if (symbol.ID.SecurityType ==
SecurityType.FutureOption || symbol.ID.SecurityType ==
SecurityType.IndexOption)
3398 indicatorBase.Consolidators.Add(consolidator);