我想到,可以用一个类来表示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; } }