📜  程序寻找简单的移动平均线

📅  最后修改于: 2021-05-04 14:20:37             🧑  作者: Mango

简单移动平均线是在一段时间内从数据中获得的平均值。通常,它的值会随着数据的变化而变化,但在这种类型的均值中,它也会随着时间间隔而变化。我们获得了一段时间t的均值,然后删除了一些先前的数据。再次,我们得到了新的平均值,这个过程继续进行。这就是为什么它是移动平均线。这在金融市场上有很大的应用。

例子:

Input : { 1, 3, 5, 6, 8 } 
        Period = 3
Output :New number added is 1.0, SMA = 0.3333333333333333
        New number added is 3.0, SMA = 1.3333333333333333
        New number added is 5.0, SMA = 3.0
        New number added is 6.0, SMA = 4.666666666666667
        New number added is 8.0, SMA = 6.333333333333333

方法
给定一系列数字和固定的子集大小,移动平均值的第一个元素是通过获取数字序列的初始固定子集的平均值而获得的。然后,通过“向前移动”修改子集,即排除序列的第一个数字,并在子集中包括下一个值。

这是一个用于计算简单移动平均线的Java程序。

Java
// Java program to calculate
// Simple Moving Average
import java.util.*;
  
public class SimpleMovingAverage {
      
    // queue used to store list so that we get the average
    private final Queue Dataset = new LinkedList();
    private final int period;
    private double sum;
  
    // constructor to initialize period
    public SimpleMovingAverage(int period)
    {
        this.period = period;
    }
  
    // function to add new data in the
    // list and update the sum so that
    // we get the new mean
    public void addData(double num)
    {
        sum += num;
        Dataset.add(num);
  
        // Updating size so that length
        // of data set should be equal
        // to period as a normal mean has
        if (Dataset.size() > period)
        {
            sum -= Dataset.remove();
        }
    }
  
    // function to calculate mean
    public double getMean()
    {
        return sum / period;
    }
  
    public static void main(String[] args)
    {
        double[] input_data = { 1, 3, 5, 6, 8,
                                12, 18, 21, 22, 25 };
        int per = 3;
        SimpleMovingAverage obj = new SimpleMovingAverage(per);
        for (double x : input_data) {
            obj.addData(x);
            System.out.println("New number added is " +
                                x + ", SMA = " + obj.getMean());
        }
    }
}


C#
// C# program to calculate
// Simple Moving Average
using System;
using System.Collections.Generic; 
  
public class SimpleMovingAverage 
{
      
    // queue used to store list so that we get the average
    private Queue Dataset = new Queue();
    private int period;
    private double sum;
  
    // constructor to initialize period
    public SimpleMovingAverage(int period)
    {
        this.period = period;
    }
  
    // function to add new data in the
    // list and update the sum so that
    // we get the new mean
    public void addData(double num)
    {
        sum += num;
        Dataset.Enqueue(num);
  
        // Updating size so that length
        // of data set should be equal
        // to period as a normal mean has
        if (Dataset.Count > period)
        {
            sum -= Dataset.Dequeue();
        }
    }
  
    // function to calculate mean
    public double getMean()
    {
        return sum / period;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        double[] input_data = { 1, 3, 5, 6, 8,
                                12, 18, 21, 22, 25 };
        int per = 3;
        SimpleMovingAverage obj = new SimpleMovingAverage(per);
        foreach (double x in input_data) {
            obj.addData(x);
            Console.WriteLine("New number added is " +
                                x + ", SMA = " + obj.getMean());
        }
    }
}
  
// This code contributed by Rajput-Ji


输出:

New number added is 1.0, SMA = 0.3333333333333333
New number added is 3.0, SMA = 1.3333333333333333
New number added is 5.0, SMA = 3.0
New number added is 6.0, SMA = 4.666666666666667
New number added is 8.0, SMA = 6.333333333333333
New number added is 12.0, SMA = 8.666666666666666
New number added is 18.0, SMA = 12.666666666666666
New number added is 21.0, SMA = 17.0
New number added is 22.0, SMA = 20.333333333333332
New number added is 25.0, SMA = 22.666666666666668

参考 :
维基