public void RegressionChannelComputesCorrectly() { var period = 20; var indicator = new RegressionChannel(period, 2); var stdDev = new StandardDeviation(period); var time = DateTime.Now; var prices = LeastSquaresMovingAverageTest.prices; var expected = LeastSquaresMovingAverageTest.expected; var actual = new decimal[prices.Length]; for (int i = 0; i < prices.Length; i++) { indicator.Update(time, prices[i]); stdDev.Update(time, prices[i]); actual[i] = Math.Round(indicator.Current.Value, 4); time = time.AddMinutes(1); } Assert.AreEqual(expected, actual); var expectedUpper = indicator.Current + stdDev.Current * 2; Assert.AreEqual(expectedUpper, indicator.UpperChannel); var expectedLower = indicator.Current - stdDev.Current * 2; Assert.AreEqual(expectedLower, indicator.LowerChannel); }
/// <summary> /// Computes the next value of the following sub-indicators from the given state: /// StandardDeviation, MiddleBand, UpperBand, LowerBand /// </summary> /// <param name="input">The input given to the indicator</param> /// <returns>The input is returned unmodified.</returns> protected override decimal ComputeNextValue(IndicatorDataPoint input) { StandardDeviation.Update(input); MiddleBand.Update(input); UpperBand.Update(input); LowerBand.Update(input); return(input); }
/// <summary> /// Resets this indicator and all sub-indicators (StandardDeviation, LowerBand, MiddleBand, UpperBand) /// </summary> public override void Reset() { StandardDeviation.Reset(); MiddleBand.Reset(); UpperBand.Reset(); LowerBand.Reset(); base.Reset(); }
public void ComparesAgainstExternalDataAfterReset() { var std = new StandardDeviation("STD", 10); RunTestIndicator(std); std.Reset(); RunTestIndicator(std); }
/// <summary> /// Initializes a new instance of the BollingerBands class /// </summary> /// <param name="name">The name of this indicator</param> /// <param name="period">The period of the standard deviation and moving average (middle band)</param> /// <param name="k">The number of standard deviations specifying the distance between the middle band and upper or lower bands</param> /// <param name="movingAverageType">The type of moving average to be used</param> public BollingerBands(String name, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple) : base(name) { MovingAverageType = movingAverageType; StandardDeviation = new StandardDeviation(name + "_StandardDeviation", period); MiddleBand = movingAverageType.AsIndicator(name + "_MiddleBand", period); LowerBand = MiddleBand.Minus(StandardDeviation.Times(k), name + "_LowerBand"); UpperBand = MiddleBand.Plus(StandardDeviation.Times(k), name + "_UpperBand"); }
/// <summary> /// Creates a new AugenPriceSpike indicator with the specified period /// </summary> /// <param name="name">The name of this indicator</param> /// <param name="period">The period of this indicator</param> public AugenPriceSpike(string name, int period) : base(name) { if (period < 3) { throw new ArgumentException("AugenPriceSpike Indicator must have a period of at least 3", nameof(period)); } _standardDeviation = new StandardDeviation(period); _rollingData = new RollingWindow <decimal>(3); WarmUpPeriod = period + 2; }
public void ResetsProperly() { var std = new StandardDeviation(3); std.Update(DateTime.Today, 1m); std.Update(DateTime.Today.AddSeconds(1), 5m); std.Update(DateTime.Today.AddSeconds(2), 1m); Assert.IsTrue(std.IsReady); std.Reset(); TestHelper.AssertIndicatorIsInDefaultState(std); }
public void ComputesCorrectly() { // Indicator output was compared against the following function in Julia // stdpop(v) = sqrt(sum((v - mean(v)).^2) / length(v)) var std = new StandardDeviation(3); var reference = DateTime.MinValue; std.Update(reference.AddDays(1), 1m); Assert.AreEqual(0m, std.Current.Value); std.Update(reference.AddDays(2), -1m); Assert.AreEqual(1m, std.Current.Value); std.Update(reference.AddDays(3), 1m); Assert.AreEqual(0.942809041582063m, std.Current.Value); std.Update(reference.AddDays(4), -2m); Assert.AreEqual(1.24721912892465m, std.Current.Value); std.Update(reference.AddDays(5), 3m); Assert.AreEqual(2.05480466765633m, std.Current.Value); }
/// <summary> /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// </summary> /// <seealso cref="QCAlgorithm.SetStartDate(System.DateTime)"/> /// <seealso cref="QCAlgorithm.SetEndDate(System.DateTime)"/> /// <seealso cref="QCAlgorithm.SetCash(decimal)"/> public override void Initialize() { //mylog.Debug(transheader); mylog.Debug(ondataheader); //Initialize dates SetStartDate(_startDate); SetEndDate(_endDate); SetCash(22000); //Add as many securities as you like. All the data will be passed into the event handler: AddSecurity(SecurityType.Equity, symbol, Resolution.Minute); Price = new RollingWindow<IndicatorDataPoint>(14); cycleSignal = new RollingWindow<IndicatorDataPoint>(14); cycle = new CyberCycle(7); Price = new RollingWindow<IndicatorDataPoint>(14); diff = new RollingWindow<IndicatorDataPoint>(20); standardDeviation = new StandardDeviation(30); fish = new InverseFisherTransform(10); fishHistory = new RollingWindow<IndicatorDataPoint>(7); fishDirectionHistory = new RollingWindow<IndicatorDataPoint>(7); }
public SymbolData(Symbol symbol, IEnumerable<BaseData> history) { Symbol = symbol; SMA = new SimpleMovingAverage(365).Of(ROC); STD = new StandardDeviation(365).Of(ROC); foreach (var data in history) { Update(data); } }
/// <summary> /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// </summary> /// <seealso cref="QCAlgorithm.SetStartDate(System.DateTime)"/> /// <seealso cref="QCAlgorithm.SetEndDate(System.DateTime)"/> /// <seealso cref="QCAlgorithm.SetCash(decimal)"/> public override void Initialize() { //mylog.Debug(transheader); mylog.Debug("Mean Reversion Algorithm"); mylog.Debug(ondataheader); dailylog.Debug("Mean Reversion Algorithm"); dailylog.Debug(dailyheader); //Initialize dates SetStartDate(_startDate); SetEndDate(_endDate); SetCash(22000); //Add as many securities as you like. All the data will be passed into the event handler: AddSecurity(SecurityType.Equity, symbol, Resolution.Minute); Price = new RollingWindow<IndicatorDataPoint>(14); trendHistory = new RollingWindow<IndicatorDataPoint>(14); trend = new InstantaneousTrend(10); ema10 = new ExponentialMovingAverage(10); sma10 = new SimpleMovingAverage(10); madiff = new RollingWindow<IndicatorDataPoint>(390); stddev = new StandardDeviation(390); emaHistory = new RollingWindow<IndicatorDataPoint>(10); smaHistory = new RollingWindow<IndicatorDataPoint>(10); }
public InverseFisherTransform(string name, int period) : base(name) { mean = new SimpleMovingAverage(period); sd = new StandardDeviation(period); }
private static void RunTestIndicator(StandardDeviation std) { TestHelper.TestIndicator(std, "spy_var.txt", "Var", (ind, expected) => Assert.AreEqual(Math.Sqrt(expected), (double)std.Current.Value, 1e-6)); }
public void ComparesAgainstExternalData() { var std = new StandardDeviation("STD", 10); TestHelper.TestIndicator(std, "spy_var.txt", "Var", (ind, expected) => Assert.AreEqual(Math.Sqrt(expected), (double)std.Current.Value, 1e-6)); }
/// <summary> /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// </summary> public override void Initialize() { // initialize algorithm level parameters SetStartDate(2013, 10, 07); SetEndDate(2013, 10, 11); //SetStartDate(2014, 01, 01); //SetEndDate(2014, 06, 01); SetCash(100000); // leverage tradier $1 traders SetBrokerageModel(BrokerageName.TradierBrokerage); // request high resolution equity data AddSecurity(SecurityType.Equity, Symbol, Resolution.Second); // save off our security so we can reference it quickly later Security = Securities[Symbol]; // Set our max leverage Security.SetLeverage(MaximumLeverage); // define our longer term indicators ADX14 = ADX(Symbol, 28, Resolution.Hour); STD14 = STD(Symbol, 14, Resolution.Daily); ATR14 = ATR(Symbol, 14, resolution: Resolution.Daily); PSARMin = new ParabolicStopAndReverse(Symbol, afStart: 0.0001m, afIncrement: 0.0001m); // smooth our ATR over a week, we'll use this to determine if recent volatilty warrants entrance var oneWeekInMarketHours = (int)(5*6.5); SmoothedATR14 = new ExponentialMovingAverage("Smoothed_" + ATR14.Name, oneWeekInMarketHours).Of(ATR14); // smooth our STD over a week as well SmoothedSTD14 = new ExponentialMovingAverage("Smoothed_"+STD14.Name, oneWeekInMarketHours).Of(STD14); // initialize our charts var chart = new Chart(Symbol); chart.AddSeries(new Series(ADX14.Name)); chart.AddSeries(new Series("Enter", SeriesType.Scatter)); chart.AddSeries(new Series("Exit", SeriesType.Scatter)); chart.AddSeries(new Series(PSARMin.Name, SeriesType.Scatter)); AddChart(chart); var history = History(Symbol, 20, Resolution.Daily); foreach (var bar in history) { ADX14.Update(bar); ATR14.Update(bar); STD14.Update(bar.EndTime, bar.Close); } // schedule an event to run every day at five minutes after our Symbol's market open Schedule.Event("MarketOpenSpan") .EveryDay(Symbol) .AfterMarketOpen(Symbol, minutesAfterOpen: OpeningSpanInMinutes) .Run(MarketOpeningSpanHandler); Schedule.Event("MarketOpen") .EveryDay(Symbol) .AfterMarketOpen(Symbol, minutesAfterOpen: -1) .Run(() => PSARMin.Reset()); }