zoukankan      html  css  js  c++  java
  • k线蜡烛实体类-多级别-火币java版

    我想到,可以用一个类来表示k线的多级别,

    自动获取当前k线级别的上一级别或下一级别

    例如,15分钟k线实体类min15,它的上一级别是min30,下一级别是min5

    做到如下调用,

    int k_level = min15.getLevel();
    int up_k_level = k_level * K_LEVEL.getMultiple();
    K_LEVEL k_level = K_LEVEL.getK_Level(up_k_level);

    一,这个k线级别类开发基于火币的javaSDK

    huobiJavaSDK中,蜡烛图和k线类如下

    1.蜡烛图java类(getter和setter自动生成):

    public class Candlestick {
    
      private Long id;
    
      private BigDecimal amount;
    
      private BigDecimal count;
    
      private BigDecimal open;
    
      private BigDecimal high;
    
      private BigDecimal low;
    
      private BigDecimal close;
    
      private BigDecimal vol;
    
    }

    2.k线enum(getter自动生成):

    public enum CandlestickIntervalEnum {
    
      MIN1("1min"),
      MIN5("5min"),
      MIN15("15min"),
      MIN30("30min"),
      MIN60("60min"),
      HOUR4("4hour"),
      DAY1("1day"),
      MON1("1mon"),
      WEEK1("1week"),
      YEAR1("1year");
    
      private final String code;
    
      CandlestickIntervalEnum(String code) {
        this.code = code;
      }
    
    }

    二,基本以上两个类编写的k线级别类

    看上去非常好用

    public class K_LEVEL {
        // k线级别倍数
        public static Integer multiple = 2;
    
        public static Integer min1 = 1;
        public static Integer min5 = 2;
        public static Integer min15 = 4;
        public static Integer min30 = 8;
        public static Integer min60 = 16;
        public static Integer hour4 = 32;
        public static Integer day1 = 64;
        public static Integer week1 = 128;
        public static Integer month1 = 256;
        public static Integer year1 = 512;
    
        // 初始级别min1
        private Integer k_level_int = 1;
        private CandlestickIntervalEnum k_level_enum = CandlestickIntervalEnum.MIN1;
    
        // 获取相应级别
        public K_LEVEL(Integer klevel) throws Exception {
            if (klevel < 1 || klevel > 512 || isPowerOfTwo(klevel)) {
                throw new Exception("level error, not in k level int");
            }
    
            this.k_level_int = klevel;
    
            switch (klevel) {
            case 1:
                this.k_level_enum = CandlestickIntervalEnum.MIN1;
                break;
            case 2:
                this.k_level_enum = CandlestickIntervalEnum.MIN5;
                break;
            case 4:
                this.k_level_enum = CandlestickIntervalEnum.MIN15;
                break;
            case 8:
                this.k_level_enum = CandlestickIntervalEnum.MIN30;
                break;
            case 16:
                this.k_level_enum = CandlestickIntervalEnum.MIN60;
                break;
            case 32:
                this.k_level_enum = CandlestickIntervalEnum.HOUR4;
                break;
            case 64:
                this.k_level_enum = CandlestickIntervalEnum.DAY1;
                break;
            case 128:
                this.k_level_enum = CandlestickIntervalEnum.WEEK1;
                break;
            case 256:
                this.k_level_enum = CandlestickIntervalEnum.MON1;
                break;
            case 512:
                this.k_level_enum = CandlestickIntervalEnum.YEAR1;
                break;
            }
        }
    
        // 获取上一级别
        public K_LEVEL getUp_KLevel(Integer klevel) throws Exception {
            int level = klevel * this.multiple;
            return new K_LEVEL(level);
        }
    
        // 获取下一级别
        public K_LEVEL getDown_KLevel(Integer klevel) throws Exception {
            int level = klevel / this.multiple;
            return new K_LEVEL(level);
        }
    
        // 是否是2的指数
        public static boolean isPowerOfTwo(int n) {
            String str = Integer.toBinaryString(n);
            if (n < 1)
                return false;
            else if (str.lastIndexOf("1") == 0)
                return true;
            else
                return false;
        }
    
    }
  • 相关阅读:
    poj2452
    bnuoj16491
    1326: The contest(并查集+分组背包)
    BNUOJ-1065或运算的简单解法
    递推、规律思维题总结
    uva10160(dfs+状态压缩)
    第七章 人工智能,7.1 基于深度强化学习与自适应在线学习的搜索和推荐算法研究(作者:灵培、霹雳、哲予)
    第六章 大数据,6.3 突破传统,4k大屏的沉浸式体验(作者: 彦川、小丛)
    第六章 大数据,6.2 双11背后的大规模数据处理(作者:惠岸 朋春 谦乐)
    第六章 大数据,6.1 双11数据大屏背后的实时计算处理(作者:藏六 黄晓锋 同杰)
  • 原文地址:https://www.cnblogs.com/difs/p/14458281.html
Copyright © 2011-2022 走看看