股票MA算法(Java实现)

2021-01-18 21:25:38 浏览数 (1)

代码语言:javascript复制
public class MAUtils {
    private void test(int dayCount) {
        //算法公式:MA(C,X)=(X1 X2 … XX)/X.
        //MA(C,1)=C/1=1(这里C=1,X=1);
        //MA(C,2)=(1 2)/2=1.5;
        //MA(C,3)=(1 2 3)/3=2;
        //MA(C,4)=(1 2 3 4)/4=2.5;
        //MA(C,5)=(1 2 3 4 5)/5=3.
        List<Data> dataList = new ArrayList<>();
        for (int i = 0; i < dayCount; i  ) {
            Data data = new Data();
            data.setClosePrice(i   1);
            data.setOpenPrice(i   1);
            dataList.add(data);
        }

        List<Double> maResults = new ArrayList<>();
        for (int days = 0; days < dayCount; days  ) {
            double sum = 0;
            for (int dataIdx = 0; dataIdx < dataList.size(); dataIdx  ) {
                if (dataIdx > days) {
                    break;
                }
                sum  = dataList.get(dataIdx).getOpenPrice();
            }
            maResults.add(sum / (days   1));
        }
        System.out.println("=============");
        System.out.println(maResults);
    }

    public static void main(String[] args) {
        new MAUtils().test(5);
    }

    class Data {
        Data() {

        }

        double closePrice;
        double openPrice;

        public double getClosePrice() {
            return closePrice;
        }

        public void setClosePrice(double closePrice) {
            this.closePrice = closePrice;
        }

        public double getOpenPrice() {
            return openPrice;
        }

        public void setOpenPrice(double openPrice) {
            this.openPrice = openPrice;
        }

        @Override
        public String toString() {
            return "Data{"  
                    "closePrice="   closePrice  
                    ", openPrice="   openPrice  
                    '}';
        }
    }

注:自行BigDecimal处理

0 人点赞