예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
 /// <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);
 }
예제 #4
0
        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);
        }
예제 #5
0
        protected override decimal ComputeNextValue(IndicatorDataPoint input)
        {
            double ifish = 0d;
            double normalized;

            mean.Update(input);
            sd.Update(input);

            if (mean.IsReady &&
                sd.IsReady &&
                sd != 0)
            {
                normalized = (double)(4 * (input - mean) / sd);
                ifish      = (Math.Exp(2 * normalized) - 1) / (Math.Exp(2 * normalized) + 1);
            }
            return((decimal)ifish);
        }
예제 #6
0
        /// <summary>
        /// Computes the next value for this indicator from the given state.
        /// </summary>
        /// <param name="input">The input value to this indicator on this time step</param>
        /// <returns>A a value for this indicator</returns>
        protected override decimal ComputeNextValue(IndicatorDataPoint input)
        {
            _rollingData.Add(input.Value);
            if (_rollingData.Count < 3)
            {
                return(0m);
            }

            var previousPoint  = _rollingData[1];
            var previousPoint2 = _rollingData[2];

            var logPoint = 0.0;

            if (previousPoint2 != 0)
            {
                logPoint = Math.Log((double)(previousPoint / previousPoint2));
            }

            _standardDeviation.Update(input.Time, (decimal)logPoint);

            if (!_rollingData.IsReady)
            {
                return(0m);
            }
            if (!_standardDeviation.IsReady)
            {
                return(0m);
            }

            var m = _standardDeviation.Current.Value * previousPoint;

            if (m == 0)
            {
                return(0);
            }

            var spikeValue = (input.Value - previousPoint) / m;

            return(spikeValue);
        }
        /// <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());
        }