zoukankan      html  css  js  c++  java
  • EMA指标和MACD指标的JAVA语言实现

    转:http://blog.csdn.net/byronm/article/details/8062307

    最近做的项目需要实现EMA和MACD,但苦于网上没有具体的实现算法。所以自己尝试着编写了一套。

    已经和通达信等主流股票分析软件核对过结果,并将其开源放在GitHub上,含Junit 测试用例。

    GitHub地址:https://github.com/wizardbyron/finance-indicators

    1.       /** 
    2.  * Calculate EMA, 
    3.  *  
    4.  * @param list 
    5.  *            :Price list to calculate,the first at head, the last at tail. 
    6.  * @return 
    7.  */  
    8. public static final Double getEXPMA(final List<Double> list, final int number) {  
    9.     // 开始计算EMA值,  
    10.     Double k = 2.0 / (number + 1.0);// 计算出序数  
    11.     Double ema = list.get(0);// 第一天ema等于当天收盘价  
    12.     for (int i = 1; i < list.size(); i++) {  
    13.         // 第二天以后,当天收盘 收盘价乘以系数再加上昨天EMA乘以系数-1  
    14.         ema = list.get(i) * k + ema * (1 - k);  
    15.     }  
    16.     return ema;  
    17. }  
    18.   
    19. /** 
    20.  * calculate MACD values 
    21.  *  
    22.  * @param list 
    23.  *            :Price list to calculate,the first at head, the last at tail. 
    24.  * @param shortPeriod 
    25.  *            :the short period value. 
    26.  * @param longPeriod 
    27.  *            :the long period value. 
    28.  * @param midPeriod 
    29.  *            :the mid period value. 
    30.  * @return 
    31.  */  
    32. public static final HashMap<String, Double> getMACD(final List<Double> list, final int shortPeriod, final int longPeriod, int midPeriod) {  
    33.     HashMap<String, Double> macdData = new HashMap<String, Double>();  
    34.     List<Double> diffList = new ArrayList<Double>();  
    35.     Double shortEMA = 0.0;  
    36.     Double longEMA = 0.0;  
    37.     Double dif = 0.0;  
    38.     Double dea = 0.0;  
    39.   
    40.     for (int i = list.size() - 1; i >= 0; i--) {  
    41.         List<Double> sublist = list.subList(0, list.size() - i);  
    42.         shortEMA = Indicators.getEXPMA(sublist, shortPeriod);  
    43.         longEMA = Indicators.getEXPMA(sublist, longPeriod);  
    44.         dif = shortEMA - longEMA;  
    45.         diffList.add(dif);  
    46.     }  
    47.     dea = Indicators.getEXPMA(diffList, midPeriod);  
    48.     macdData.put("DIF", dif);  
    49.     macdData.put("DEA", dea);  
    50.     macdData.put("MACD", (dif - dea) * 2);  
    51.     return macdData;  
    52. }  
  • 相关阅读:
    mysql导入sql文件
    35. Search Insert Position(python)
    21. Merge Two Sorted Lists (python)
    20.Valid Parentheses (python)
    1.the sum (python)
    Zabbix4.0--客户端的安装与添加监控(windows跟linux)
    Linux系统优化04-Centos关闭selinux与防火墙
    SQL SERVER(使用SSMS)备份数据库文件
    SQL Server Management Studio (SSMS)单独安装,仅安装连接工具
    linux ``与"" ''区别
  • 原文地址:https://www.cnblogs.com/xhqgogogo/p/3386426.html
Copyright © 2011-2022 走看看