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;
609 InitializeIndicator(symbol, detrendedPriceOscillator, resolution, selector);
611 return detrendedPriceOscillator;
644 InitializeIndicator(symbol, exponentialMovingAverage, resolution, selector);
646 return exponentialMovingAverage;
664 InitializeIndicator(symbol, easeOfMovementValue, resolution, selector);
666 return easeOfMovementValue;
681 var resolution = GetSubscription(symbol).
Resolution;
700 RegisterIndicator<IBaseData>(symbol, filteredIdentity, resolution, selector);
701 return filteredIdentity;
717 var name = Invariant($
"{symbol}({fieldName ?? "close
"}_{resolution})");
719 RegisterIndicator<IBaseData>(symbol, filteredIdentity,
ResolveConsolidator(symbol, resolution), selector);
720 return filteredIdentity;
737 InitializeIndicator(symbol, fisherTransform, resolution, selector);
739 return fisherTransform;
757 InitializeIndicator(symbol, fractalAdaptiveMovingAverage, resolution, selector);
759 return fractalAdaptiveMovingAverage;
778 var name = InitializeOptionIndicator<Gamma>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
780 var gamma =
new Gamma(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
783 if (mirrorOption !=
null)
806 return G(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
821 InitializeIndicator(symbol, heikinAshi, resolution, selector);
841 public HilbertTransform HT(
Symbol symbol,
int length, decimal inPhaseMultiplicationFactor, decimal quadratureMultiplicationFactor,
Resolution? resolution =
null, Func<IBaseData, decimal> selector =
null)
843 var name =
CreateIndicatorName(symbol, $
"HT({length}, {inPhaseMultiplicationFactor}, {quadratureMultiplicationFactor})", resolution);
844 var hilbertTransform =
new HilbertTransform(length, inPhaseMultiplicationFactor, quadratureMultiplicationFactor);
845 InitializeIndicator(symbol, hilbertTransform, resolution, selector);
847 return hilbertTransform;
863 InitializeIndicator(symbol, hullMovingAverage, resolution, selector);
865 return hullMovingAverage;
884 int senkouADelayPeriod,
int senkouBDelayPeriod,
Resolution? resolution =
null, Func<IBaseData, TradeBar> selector =
null)
886 var name =
CreateIndicatorName(symbol, $
"ICHIMOKU({tenkanPeriod},{kijunPeriod},{senkouAPeriod},{senkouBPeriod},{senkouADelayPeriod},{senkouBDelayPeriod})", resolution);
887 var ichimokuKinkoHyo =
new IchimokuKinkoHyo(name, tenkanPeriod, kijunPeriod, senkouAPeriod, senkouBPeriod, senkouADelayPeriod, senkouBDelayPeriod);
888 InitializeIndicator(symbol, ichimokuKinkoHyo, resolution, selector);
890 return ichimokuKinkoHyo;
904 var resolution = GetSubscription(symbol).
Resolution;
905 return Identity(symbol, resolution, selector, fieldName);
936 public Identity Identity(
Symbol symbol, TimeSpan resolution, Func<IBaseData, decimal> selector =
null,
string fieldName =
null)
938 var name = Invariant($
"{symbol}({fieldName ?? "close
"},{resolution})");
960 var name = InitializeOptionIndicator<ImpliedVolatility>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
962 var iv =
new ImpliedVolatility(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel);
965 if (mirrorOption !=
null)
983 return KAMA(symbol, period, 2, 30, resolution, selector);
999 var name =
CreateIndicatorName(symbol, $
"KAMA({period},{fastEmaPeriod},{slowEmaPeriod})", resolution);
1001 InitializeIndicator(symbol, kaufmanAdaptiveMovingAverage, resolution, selector);
1003 return kaufmanAdaptiveMovingAverage;
1020 InitializeIndicator(symbol, kaufmanEfficiencyRatio, resolution, selector);
1022 return kaufmanEfficiencyRatio;
1040 var keltnerChannels =
new KeltnerChannels(name, period, k, movingAverageType);
1041 InitializeIndicator(symbol, keltnerChannels, resolution, selector);
1043 return keltnerChannels;
1058 var logReturn =
new LogReturn(name, period);
1059 InitializeIndicator(symbol, logReturn, resolution, selector);
1077 InitializeIndicator(symbol, leastSquaresMovingAverage, resolution, selector);
1079 return leastSquaresMovingAverage;
1096 InitializeIndicator(symbol, linearWeightedMovingAverage, resolution, selector);
1098 return linearWeightedMovingAverage;
1115 var name =
CreateIndicatorName(symbol, $
"MACD({fastPeriod},{slowPeriod},{signalPeriod})", resolution);
1117 InitializeIndicator(symbol, movingAverageConvergenceDivergence, resolution, selector);
1119 return movingAverageConvergenceDivergence;
1135 InitializeIndicator(symbol, meanAbsoluteDeviation, resolution, selector);
1137 return meanAbsoluteDeviation;
1155 var marketProfile =
new VolumeProfile(name, period, valueAreaVolumePercentage, priceRangeRoundOff);
1156 InitializeIndicator(symbol, marketProfile, resolution, selector);
1158 return marketProfile;
1173 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)
1176 var marketProfile =
new TimeProfile(name, period, valueAreaVolumePercentage, priceRangeRoundOff);
1177 InitializeIndicator(symbol, marketProfile, resolution, selector);
1179 return marketProfile;
1195 InitializeIndicator(symbol, timeSeriesForecast, resolution, selector);
1197 return timeSeriesForecast;
1213 var maximum =
new Maximum(name, period);
1216 if (selector ==
null)
1218 var subscription = GetSubscription(symbol);
1219 if (typeof(
TradeBar).IsAssignableFrom(subscription.Type))
1222 selector = x => ((
TradeBar)x).High;
1250 InitializeIndicator(symbol, moneyFlowIndex, resolution, selector);
1252 return moneyFlowIndex;
1269 var massIndex =
new MassIndex(name, emaPeriod, sumPeriod);
1270 InitializeIndicator(symbol, massIndex, resolution, selector);
1287 var midPoint =
new MidPoint(name, period);
1288 InitializeIndicator(symbol, midPoint, resolution, selector);
1305 var midPrice =
new MidPrice(name, period);
1306 InitializeIndicator(symbol, midPrice, resolution, selector);
1324 var minimum =
new Minimum(name, period);
1327 if (selector ==
null)
1329 var subscription = GetSubscription(symbol);
1330 if (typeof(
TradeBar).IsAssignableFrom(subscription.Type))
1360 var momentum =
new Momentum(name, period);
1361 InitializeIndicator(symbol, momentum, resolution, selector);
1378 var name =
CreateIndicatorName(symbol, $
"MOMERSION({minPeriod},{fullPeriod})", resolution);
1380 InitializeIndicator(symbol, momersion, resolution, selector);
1399 InitializeIndicator(symbol, momentumPercent, resolution, selector);
1401 return momentumPercent;
1417 InitializeIndicator(symbol, normalizedAverageTrueRange, resolution, selector);
1419 return normalizedAverageTrueRange;
1436 InitializeIndicator(symbol, onBalanceVolume, resolution, selector);
1438 return onBalanceVolume;
1455 var pivotPointsHighLow =
new PivotPointsHighLow(name, lengthHigh, lengthLow, lastStoredValues);
1456 InitializeIndicator(symbol, pivotPointsHighLow, resolution, selector);
1458 return pivotPointsHighLow;
1476 InitializeIndicator(symbol, percentagePriceOscillator, resolution, selector);
1478 return percentagePriceOscillator;
1494 var name =
CreateIndicatorName(symbol, $
"PSAR({afStart},{afIncrement},{afMax})", resolution);
1496 InitializeIndicator(symbol, parabolicStopAndReverse, resolution, selector);
1498 return parabolicStopAndReverse;
1515 InitializeIndicator(symbol, regressionChannel, resolution, selector);
1517 return regressionChannel;
1533 InitializeIndicator(symbol, relativeMovingAverage, resolution, selector);
1535 return relativeMovingAverage;
1553 InitializeIndicator(symbol, rateOfChange, resolution, selector);
1555 return rateOfChange;
1572 InitializeIndicator(symbol, rateOfChangePercent, resolution, selector);
1574 return rateOfChangePercent;
1590 InitializeIndicator(symbol, rateOfChangeRatio, resolution, selector);
1592 return rateOfChangeRatio;
1610 InitializeIndicator(symbol, relativeStrengthIndex, resolution, selector);
1612 return relativeStrengthIndex;
1628 InitializeIndicator(symbol, relativeVigorIndex, resolution, selector);
1630 return relativeVigorIndex;
1649 return relativeDailyVolume;
1668 var name = InitializeOptionIndicator<Rho>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
1670 var rho =
new Rho(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
1673 if (mirrorOption !=
null)
1696 return R(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
1710 Resolution? resolution =
null, Func<IBaseData, IBaseDataBar> selector =
null)
1713 var strend =
new SuperTrend(name, period, multiplier, movingAverageType);
1714 InitializeIndicator(symbol, strend, resolution, selector);
1733 var baseBame = riskFreeRate.HasValue ? $
"SR({sharpePeriod},{riskFreeRate})" : $
"SR({sharpePeriod})";
1739 var sharpeRatio =
new SharpeRatio(name, sharpePeriod, riskFreeRateModel);
1740 InitializeIndicator(symbol, sharpeRatio, resolution, selector);
1757 var name =
CreateIndicatorName(symbol, $
"SORTINO({sortinoPeriod},{minimumAcceptableReturn})", resolution);
1758 var sortinoRatio =
new SortinoRatio(name, sortinoPeriod, minimumAcceptableReturn);
1759 InitializeIndicator(symbol, sortinoRatio, resolution, selector);
1761 return sortinoRatio;
1779 InitializeIndicator(symbol, simpleMovingAverage, resolution, selector);
1781 return simpleMovingAverage;
1798 var name =
CreateIndicatorName(symbol, $
"STC({cyclePeriod},{fastPeriod},{slowPeriod})", resolution);
1799 var schaffTrendCycle =
new SchaffTrendCycle(name, cyclePeriod, fastPeriod, slowPeriod, movingAverageType);
1800 InitializeIndicator(symbol, schaffTrendCycle, resolution, selector);
1802 return schaffTrendCycle;
1818 InitializeIndicator(symbol, standardDeviation, resolution, selector);
1820 return standardDeviation;
1836 var name =
CreateIndicatorName(symbol, $
"TDD({period},{minimumAcceptableReturn})", resolution);
1838 InitializeIndicator(symbol, targetDownsideDeviation, resolution, selector);
1840 return targetDownsideDeviation;
1855 Func<IBaseData, TradeBar> selector =
null)
1858 var stochastic =
new Stochastic(name, period, kPeriod, dPeriod);
1859 InitializeIndicator(symbol, stochastic, resolution, selector);
1875 return STO(symbol, period, period, 3, resolution, selector);
1890 var sum =
new Sum(name, period);
1891 InitializeIndicator(symbol, sum, resolution, selector);
1911 var swissArmyKnife =
new SwissArmyKnife(name, period, delta, tool);
1912 InitializeIndicator(symbol, swissArmyKnife, resolution, selector);
1914 return swissArmyKnife;
1933 var name = InitializeOptionIndicator<Theta>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
1935 var theta =
new Theta(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
1938 if (mirrorOption !=
null)
1961 return T(symbol, mirrorOption, riskFreeRate, dividendYield, optionModel, ivModel, resolution);
1977 var t3MovingAverage =
new T3MovingAverage(name, period, volumeFactor);
1978 InitializeIndicator(symbol, t3MovingAverage, resolution, selector);
1980 return t3MovingAverage;
1996 InitializeIndicator(symbol, tripleExponentialMovingAverage, resolution, selector);
1998 return tripleExponentialMovingAverage;
2017 var name =
CreateIndicatorName(symbol, $
"TSI({longTermPeriod},{shortTermPeriod},{signalPeriod})", resolution);
2018 var trueStrengthIndex =
new TrueStrengthIndex(name, longTermPeriod, shortTermPeriod, signalPeriod, signalType);
2019 InitializeIndicator(symbol, trueStrengthIndex, resolution, selector);
2021 return trueStrengthIndex;
2036 InitializeIndicator(symbol, trueRange, resolution, selector);
2054 InitializeIndicator(symbol, triangularMovingAverage, resolution, selector);
2056 return triangularMovingAverage;
2071 var trix =
new Trix(name, period);
2072 InitializeIndicator(symbol, trix, resolution, selector);
2090 var name =
CreateIndicatorName(symbol, $
"ULTOSC({period1},{period2},{period3})", resolution);
2092 InitializeIndicator(symbol, ultimateOscillator, resolution, selector);
2094 return ultimateOscillator;
2113 var name = InitializeOptionIndicator<Vega>(symbol, out var riskFreeRateModel, out var dividendYieldModel, riskFreeRate, dividendYield, optionModel, resolution);
2115 var vega =
new Vega(name, symbol, riskFreeRateModel, dividendYieldModel, mirrorOption, optionModel, ivModel);
2118 if (mirrorOption !=
null)
2138 InitializeIndicator(symbol, variableIndexDynamicAverage, resolution, selector);
2140 return variableIndexDynamicAverage;
2152 [Obsolete(
"'VAR' is obsolete please use 'V' instead")]
2155 return V(symbol, period, resolution, selector);
2170 var variance =
new Variance(name, period);
2171 InitializeIndicator(symbol, variance, resolution, selector);
2188 var valueAtRisk =
new ValueAtRisk(name, period, confidenceLevel);
2189 InitializeIndicator(symbol, valueAtRisk, resolution, selector);
2208 InitializeIndicator(symbol, volumeWeightedAveragePriceIndicator, resolution, selector);
2210 return volumeWeightedAveragePriceIndicator;
2225 return intradayVwap;
2243 InitializeIndicator(symbol, williamsPercentR, resolution, selector);
2245 return williamsPercentR;
2263 InitializeIndicator(symbol, wilderMovingAverage, resolution, selector);
2265 return wilderMovingAverage;
2280 Func<IBaseData, TradeBar> selector =
null)
2284 InitializeIndicator(symbol, si, resolution, selector);
2301 Func<IBaseData, TradeBar> selector =
null)
2305 InitializeIndicator(symbol, asi, resolution, selector);
2320 return TRIN(symbols.ToArray(), resolution, selector);
2335 foreach (var symbol
in symbols)
2338 InitializeIndicator(symbol, trin, resolution, selector);
2356 foreach (var symbol
in symbols)
2359 InitializeIndicator(symbol, adr, resolution, selector);
2377 foreach (var symbol
in symbols)
2380 InitializeIndicator(symbol, advr, resolution, selector);
2398 foreach (var symbol
in symbols)
2401 InitializeIndicator(symbol, adDiff, resolution, selector);
2419 return MOSC(symbols.ToArray(), fastPeriod, slowPeriod, resolution, selector);
2436 foreach (var symbol
in symbols)
2439 InitializeIndicator(symbol, mosc, resolution, selector);
2457 return MSI(symbols.ToArray(), fastPeriod, slowPeriod, resolution, selector);
2474 foreach (var symbol
in symbols)
2477 InitializeIndicator(symbol, msi, resolution, selector);
2508 if (!resolution.HasValue && symbolIsNotEmpty)
2510 resolution = GetSubscription(symbol).
Resolution;
2513 var res =
string.Empty;
2540 throw new ArgumentOutOfRangeException(nameof(resolution), resolution,
"resolution parameter is out of range.");
2543 var parts =
new List<string>();
2545 if (symbolIsNotEmpty)
2551 return Invariant($
"{type}({string.Join("_
", parts)})").Replace(
")(",
",");
2568 .GetSubscriptionDataConfigs(symbol)
2571 .ThenBy(x => x.TickType)
2575 subscription = subscriptions.FirstOrDefault(x => tickType ==
null || tickType == x.TickType);
2576 if (subscription ==
null)
2579 subscription = subscriptions.First();
2582 catch (InvalidOperationException)
2585 throw new Exception($
"Please register to receive data for symbol \'{symbol}\' using the AddSecurity() function.");
2587 return subscription;
2598 [DocumentationAttribute(ConsolidatingData)]
2599 [DocumentationAttribute(Indicators)]
2633 selector = selector ?? (x => x.Value);
2635 RegisterConsolidator(indicator, symbol, consolidator);
2640 var value = selector(consolidated);
2641 indicator.Update(
new IndicatorDataPoint(consolidated.Symbol, consolidated.EndTime, value));
2706 var selectorToUse = selector ?? (x => (
T)x);
2708 RegisterConsolidator(indicator, symbol, consolidator);
2711 var type = typeof(
T);
2712 if (!type.IsAssignableFrom(consolidator.
OutputType))
2718 selectorToUse = consolidated => (
T)(
object)
new IndicatorDataPoint(consolidated.Symbol, consolidated.EndTime, consolidated.Value);
2722 throw new ArgumentException($
"Type mismatch found between consolidator and indicator for symbol: {symbol}." +
2723 $
"Consolidator outputs type {consolidator.OutputType.Name} but indicator expects input type {type.Name}"
2731 var value = selectorToUse(consolidated);
2732 indicator.Update(value);
2755 foreach (var consolidator
in indicator.Consolidators)
2760 indicator.Consolidators.Clear();
2774 resolution = GetResolution(symbol, resolution,
null);
2775 var period = resolution.Value.ToTimeSpan();
2790 var history = GetIndicatorWarmUpHistory(symbol, indicator, period, out var identityConsolidator);
2791 if (history == Enumerable.Empty<
Slice>())
return;
2794 selector ??= (x => x.Value);
2796 Action<IBaseData> onDataConsolidated = bar =>
2799 indicator.Update(input);
2802 WarmUpIndicatorImpl(symbol, period, onDataConsolidated, history, identityConsolidator);
2817 resolution = GetResolution(symbol, resolution, typeof(
T));
2818 var period = resolution.Value.ToTimeSpan();
2834 var history = GetIndicatorWarmUpHistory(symbol, indicator, period, out var identityConsolidator);
2835 if (history == Enumerable.Empty<
Slice>())
return;
2838 selector ??= (x => (
T)x);
2841 Action<T> onDataConsolidated = bar =>
2843 indicator.Update(selector(bar));
2846 WarmUpIndicatorImpl(symbol, period, onDataConsolidated, history, identityConsolidator);
2849 private IEnumerable<Slice> GetIndicatorWarmUpHistory(
Symbol symbol,
IIndicator indicator, TimeSpan timeSpan, out
bool identityConsolidator)
2851 identityConsolidator =
false;
2854 if (periods.HasValue && periods != 0)
2856 var resolution = timeSpan.ToHigherResolutionEquivalent(
false);
2858 identityConsolidator = resolution.ToTimeSpan() == timeSpan;
2859 var resolutionTicks = resolution.ToTimeSpan().Ticks;
2860 if (resolutionTicks != 0)
2862 periods *= (int)(timeSpan.Ticks / resolutionTicks);
2867 return History(
new[] { symbol }, periods.
Value, resolution);
2869 catch (ArgumentException e)
2871 Debug($
"{indicator.Name} could not be warmed up. Reason: {e.Message}");
2874 else if (!_isEmitWarmupInsightWarningSent)
2876 Debug($
"Warning: the 'WarmUpIndicator' feature only works with indicators which inherit from '{nameof(IIndicatorWarmUpPeriodProvider)}'" +
2877 $
" and define a warm up period, setting property 'WarmUpPeriod' with a value > 0." +
2878 $
" The provided indicator of type '{indicator.GetType().Name}' will not be warmed up.");
2879 _isEmitWarmupInsightWarningSent =
true;
2882 return Enumerable.Empty<
Slice>();
2885 private void WarmUpIndicatorImpl<T>(
Symbol symbol, TimeSpan period, Action<T> handler, IEnumerable<Slice> history,
bool identityConsolidator)
2889 if (identityConsolidator)
2891 period = TimeSpan.Zero;
2895 consolidator =
Consolidate(symbol, period, handler);
2899 var providedType = typeof(
T);
2900 if (providedType.IsAbstract)
2909 .ThenBy(tuple => tuple.Item2).First();
2922 var consolidatorInputType = consolidator.
InputType;
2924 foreach (var slice
in history)
2926 if (slice.TryGet(consolidatorInputType, symbol, out var data))
2929 consolidator.
Update(lastBar);
2934 if (lastBar !=
null)
2949 [DocumentationAttribute(ConsolidatingData)]
2950 [DocumentationAttribute(Indicators)]
2953 TimeSpan? timeSpan =
null;
2954 if (resolution.HasValue)
2956 timeSpan = resolution.Value.ToTimeSpan();
2973 var subscription = GetSubscription(symbol, tickType);
2974 var subscriptionTimeSpan = subscription.Resolution.ToTimeSpan();
2977 if (!timeSpan.HasValue)
2979 timeSpan = subscriptionTimeSpan;
2984 if (timeSpan.Value < subscriptionTimeSpan)
2986 throw new ArgumentException($
"Unable to create {symbol} consolidator because {symbol} is registered for " +
2987 Invariant($
"{subscription.Resolution.ToStringInvariant()} data. Consolidators require higher resolution data to produce lower resolution data.")
2990 else if (timeSpan.Value == subscriptionTimeSpan)
2993 timeSpan = TimeSpan.Zero;
3011 if (typeof(
TradeBar).IsAssignableFrom(consolidatorInputType))
3014 if (period.Ticks == 0)
3023 if (typeof(
QuoteBar).IsAssignableFrom(consolidatorInputType))
3026 if (period.Ticks == 0)
3035 if (typeof(
Tick).IsAssignableFrom(consolidatorInputType))
3038 if (period.Ticks == 0)
3057 if (typeof(
DynamicData).IsAssignableFrom(consolidatorInputType))
3060 if (period.Ticks == 0)
3068 if (period.Ticks == 0)
3144 return Consolidate(symbol, period, tickType, handler);
3160 return Consolidate(symbol, period.ToTimeSpan(), tickType, handler);
3177 var subscription = GetSubscription(symbol, tickType);
3180 var consolidator =
CreateConsolidator(period, subscription.Type, subscription.TickType);
3182 AddConsolidator(symbol, consolidator, handler, tickType);
3183 return consolidator;
3228 return Consolidate(symbol, calendar, tickType, handler);
3245 var subscription = GetSubscription(symbol, tickType);
3248 var consolidator =
CreateConsolidator(calendar, subscription.Type, subscription.TickType);
3250 AddConsolidator(symbol, consolidator, handler);
3251 return consolidator;
3261 if (!typeof(
T).IsAssignableFrom(consolidator.
OutputType))
3270 throw new ArgumentException(
3271 $
"Unable to consolidate with the specified handler because the consolidator's output type " +
3272 $
"is {consolidator.OutputType.Name} but the handler's input type is {typeof(T).Name}.");
3276 consolidator.
DataConsolidated += (sender, consolidated) => handler((
T)consolidated);
3282 [DocumentationAttribute(ConsolidatingData)]
3287 if (typeof(
TradeBar).IsAssignableFrom(consolidatorInputType))
3294 if (typeof(
QuoteBar).IsAssignableFrom(consolidatorInputType))
3301 if (typeof(
Tick).IsAssignableFrom(consolidatorInputType))
3311 if (typeof(
DynamicData).IsAssignableFrom(consolidatorInputType))
3324 Func<IBaseData, decimal> selector =
null)
3335 Func<IBaseData, T> selector =
null)
3350 var name =
CreateIndicatorName(symbol, $
"{typeof(T).Name}({riskFreeRate},{dividendYield},{optionModel})", resolution);
3352 riskFreeRateModel = riskFreeRate.HasValue
3357 if (dividendYield.HasValue)
3361 else if (symbol.ID.SecurityType ==
SecurityType.FutureOption || symbol.ID.SecurityType ==
SecurityType.IndexOption)
3376 indicatorBase.Consolidators.Add(consolidator);