如何在C#中计算简单移动平均线?

我只是迷路了,似乎不知道该怎么计算简单的移动平均线?


这是文件中用于计算简单移动平均线的方法之一。


        public async Task<decimal?> UpdateAsync(decimal? value, CancellationToken cancelToken)

        {

            await Task.Run(() =>

            {

                var av = default(decimal?);


                if (_av.Count - 1 >= _p)

                {


                }

                else

                {

                    av = value;

                }


                _av.Add(av);


                return av;

            }, cancelToken).ConfigureAwait(false);


            throw new Exception("major issue");

        }

    }


元芳怎么了
浏览 194回答 2
2回答

慕勒3428872

让我们从问题开始:我只是迷路了,似乎不知道该怎么计算简单的移动平均线?鉴于public static class Extensions{&nbsp; &nbsp;public static IEnumerable<decimal> MovingAvg(this IEnumerable<decimal> source, int period)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; var buffer = new Queue<decimal>();&nbsp; &nbsp; &nbsp; foreach (var value in source)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;buffer.Enqueue(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sume the buffer for the average at any given time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;yield return buffer.Sum() / buffer.Count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Dequeue when needed&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (buffer.Count == period)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer.Dequeue();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}用法static&nbsp; void Main(string[] args){&nbsp; &nbsp;var input = new decimal[] { 1, 2, 2, 4, 5, 6, 6, 6, 9, 10, 11, 12, 13, 14, 15 };&nbsp; &nbsp;var result = input.MovingAvg(2);&nbsp; &nbsp;Console.WriteLine(string.Join(", ",result));}输出1, 1.5, 2, 3, 4.5, 5.5, 6, 6, 7.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5

慕慕森

所以首先什么是移动平均线简单移动平均线是一种计算数字流平均值的方法,它仅对流中的最后一个 P 数字求平均值,其中 P 称为周期。接下来是代码,就像我想象我的移动平均线是如何实现的。可以使用,但是在我们添加数字后的每一步,我都希望我的平均值得到更新。LINQ我的代码被大量注释,所以请阅读注释。public class MovingAverageCalculator {&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Maximum number of numbers this moving average calculator can hold.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; private readonly int maxElementCount;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Numbers which will be used for calculating moving average.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; private readonly int[] currentElements;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// At which index the next number will be added.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; private int currentIndex;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// How many elements are there currently in this moving average calculator.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; private int currentElementCount;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Current total of all the numbers that are being managed by this moving average calculator&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; private double currentTotal;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Current Average of all the numbers.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; private double currentAvg;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// An object which can calcauclate moving average of given numbers.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="period">Maximum number elements</param>&nbsp; &nbsp; public MovingAverageCalculator(int period) {&nbsp; &nbsp; &nbsp; &nbsp; maxElementCount = period;&nbsp; &nbsp; &nbsp; &nbsp; currentElements = new int[maxElementCount];&nbsp; &nbsp; &nbsp; &nbsp; currentIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; currentTotal = 0;&nbsp; &nbsp; &nbsp; &nbsp; currentAvg = 0;&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Adds an item to the moving average series then updates the average.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="number">Number to add.</param>&nbsp; &nbsp; public void AddElement(int number){&nbsp; &nbsp; &nbsp; &nbsp; // You can only have at most maximum elements in your moving average series.&nbsp; &nbsp; &nbsp; &nbsp; currentElementCount = Math.Min(++currentElementCount, maxElementCount);&nbsp; &nbsp; &nbsp; &nbsp; // IF your current index reached the maximum allowable number then you must reset&nbsp; &nbsp; &nbsp; &nbsp; if (currentIndex == maxElementCount){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Copy the current index number to the local variable&nbsp; &nbsp; &nbsp; &nbsp; var temp = currentElements[currentIndex];&nbsp; &nbsp; &nbsp; &nbsp; // Substract the value from the current total because it will be replaced by the added number.&nbsp; &nbsp; &nbsp; &nbsp; currentTotal -= temp;&nbsp; &nbsp; &nbsp; &nbsp; // Add the number to the current index&nbsp; &nbsp; &nbsp; &nbsp; currentElements[currentIndex] = number;&nbsp; &nbsp; &nbsp; &nbsp; // Increase the total by the added number.&nbsp; &nbsp; &nbsp; &nbsp; currentTotal += number;&nbsp; &nbsp; &nbsp; &nbsp; // Increase index&nbsp; &nbsp; &nbsp; &nbsp; currentIndex++;&nbsp; &nbsp; &nbsp; &nbsp; // Calculate average&nbsp; &nbsp; &nbsp; &nbsp; currentAvg = (double)currentTotal / currentElementCount;&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Gets the current average&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <returns>Average</returns>&nbsp; &nbsp; public double GetAverage() => currentAvg;}最后,我检查了你的代码,但它对我来说没有意义
打开App,查看更多内容
随时随地看视频慕课网APP