| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.common.base.Splitter; |
| | | import com.google.common.collect.Lists; |
| | | import com.yami.trading.bean.data.domain.*; |
| | | import com.yami.trading.bean.data.respDto.TradeTickResp; |
| | | import com.yami.trading.bean.item.domain.Item; |
| | | import com.yami.trading.common.util.Arith; |
| | | import com.yami.trading.common.util.StringUtils; |
| | |
| | | import com.yami.trading.common.util.UTCDateUtils; |
| | | import com.yami.trading.huobi.data.internal.KlineConstant; |
| | | import com.yami.trading.huobi.data.internal.KlineService; |
| | | import com.yami.trading.huobi.data.model.AllticktradeResult; |
| | | import com.yami.trading.huobi.data.model.TimeseriesResult; |
| | | import com.yami.trading.huobi.data.websocket.model.market.MarketDepthEvent; |
| | | import com.yami.trading.huobi.data.websocket.model.market.MarketTrade; |
| | |
| | | import com.yami.trading.huobi.data.websocket.model.market.PriceLevel; |
| | | import com.yami.trading.huobi.hobi.Config; |
| | | import com.yami.trading.huobi.hobi.HobiDataService; |
| | | import com.yami.trading.huobi.hobi.constant.AllticktradeMadeOptions; |
| | | import com.yami.trading.huobi.hobi.constant.TraderMadeOptions; |
| | | import com.yami.trading.huobi.hobi.http.HttpHelper; |
| | | import com.yami.trading.huobi.hobi.http.HttpMethodType; |
| | | import com.yami.trading.service.item.ItemService; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.net.URLEncoder; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | |
| | | private ItemService itemService; |
| | | @Autowired |
| | | private KlineService klineService; |
| | | |
| | | @Value("${alltick.trade-kline}") |
| | | private String tradeKline; |
| | | /** |
| | | * 接口调用间隔(毫秒) |
| | | */ |
| | |
| | | boolean current_lock = false; |
| | | if (lock || (new Date().getTime() - last_time.getTime()) < interval) { |
| | | ThreadUtils.sleep(sleep); |
| | | if (maximum >= 100) { |
| | | if (maximum >= 500) { |
| | | return list; |
| | | } else { |
| | | return this.kline(symbol, period, num, ++maximum); |
| | |
| | | public List<Realtime> realtimeXinLang(String symbols) { |
| | | return xinLangDataService.realtimeSingle(symbols); |
| | | } |
| | | @Override |
| | | public List<Realtime> realtimeNewPrice(String symbols,List<Realtime> realtimes) { |
| | | return xinLangDataService.realtimeNewPrice(symbols,realtimes); |
| | | } |
| | | |
| | | /** |
| | | * 1day 历史数据 : 周期 1年 |
| | |
| | | * 请求 350次 |
| | | */ |
| | | public Map<String, List<Kline>> getDailyWeekMonthHistory(String symbol) { |
| | | if (isXueQiu(symbol)) { |
| | | return xueQiuDataService.getDailyWeekMonthHistory(symbol); |
| | | }else if(isXinlang(symbol)){ |
| | | return xinLangDataService.getDailyWeekMonthHistory(symbol); |
| | | |
| | | } |
| | | Map<String, List<Kline>> map = new HashMap<>(); |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); |
| | | f.setTimeZone(TimeZone.getTimeZone(UTCDateUtils.GMT_TIME_ZONE)); |
| | | Date now = new Date(); |
| | | // 每次只能取一年的数据,需要遍历5年 |
| | | for (int i = 0; i < TraderMadeOptions.weekAndMonthPeriod; i++) { |
| | | String startDate = UTCDateUtils.addYear(now, -1); |
| | | String endDate = f.format(now); |
| | | now = UTCDateUtils.toDate(startDate, "yyyy-MM-dd"); |
| | | |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("currency", symbol); |
| | | param.put("start_date", startDate); |
| | | param.put("end_date", endDate); |
| | | param.put("format", "records"); |
| | | param.put("api_key", TraderMadeOptions.apiKey); |
| | | String json = null; |
| | | for (int i = 0; i < AllticktradeMadeOptions.dayKlineCountPeriod; i++) { |
| | | String resultStr = null; |
| | | try { |
| | | json = HttpHelper.getJSONFromHttpNew(TraderMadeOptions.timeseries, param, HttpMethodType.GET); |
| | | resultStr = getTradeKline(symbol,AllticktradeMadeOptions.dayPeriod,AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", param); |
| | | logger.error("采集外汇k线图失败:{} " , e); |
| | | continue; |
| | | } |
| | | |
| | | JSONObject resultJson = JSON.parseObject(json); |
| | | JSONArray dataArray = resultJson.getJSONArray("quotes"); |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if(200 != resultJson.getInteger("ret")){ |
| | | logger.error("采集外汇k线图失败,返回:{} ", resultStr); |
| | | } |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<TimeseriesResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), TimeseriesResult.class); |
| | | Map<String, List<TimeseriesResult>> monthMap = new LinkedHashMap<>(); |
| | | Map<String, List<TimeseriesResult>> weekMap = new LinkedHashMap<>(); |
| | | for (TimeseriesResult result : list) { |
| | | String month = result.getDate().substring(0, 7); |
| | | if (monthMap.containsKey(month)) { |
| | | monthMap.get(month).add(result); |
| | | } else { |
| | | List<TimeseriesResult> monthList = new ArrayList<>(); |
| | | monthList.add(result); |
| | | monthMap.put(month, monthList); |
| | | } |
| | | |
| | | String firstDateOfWeek = UTCDateUtils.getFirstDateOfWeek(UTCDateUtils.toDate(result.getDate(), "yyyy-MM-dd")); |
| | | if (weekMap.containsKey(firstDateOfWeek)) { |
| | | weekMap.get(firstDateOfWeek).add(result); |
| | | } else { |
| | | List<TimeseriesResult> weekList = new ArrayList<>(); |
| | | weekList.add(result); |
| | | weekMap.put(firstDateOfWeek, weekList); |
| | | } |
| | | } |
| | | |
| | | map.put(Kline.PERIOD_1WEEK, buildOneWeekPeriod(weekMap, symbol)); |
| | | map.put(Kline.PERIOD_1MON, buildOneMonthPeriod(monthMap, symbol)); |
| | | |
| | | // 1day历史数据 : 周期 1年 |
| | | if (0 == i) { |
| | | map.put(Kline.PERIOD_1DAY, buildOneDayPeriod(list, symbol)); |
| | | } |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | map.put(Kline.PERIOD_1DAY, buildOneDayPeriod(list, symbol)); |
| | | } |
| | | } |
| | | try { |
| | |
| | | public Map<String, List<Kline>> getHourlyAndMinuteHistory(String symbol) { |
| | | Map<String, List<Kline>> map = new HashMap<>(); |
| | | |
| | | List<Kline> fourHourlyList = getTimeseriesForFourHourly(symbol); |
| | | map.put(KlineConstant.PERIOD_4HOUR, fourHourlyList); |
| | | List<Kline> monthList = getTimeseries(symbol,KlineConstant.PERIOD_1MON,AllticktradeMadeOptions.monthPeriod); |
| | | map.put(KlineConstant.PERIOD_1MON, monthList); |
| | | |
| | | List<Kline> oneHourlyList = getTimeseriesForOneHourly(symbol); |
| | | List<Kline> weekList = getTimeseries(symbol,KlineConstant.PERIOD_1WEEK,AllticktradeMadeOptions.weekPeriod); |
| | | map.put(KlineConstant.PERIOD_1WEEK, weekList); |
| | | |
| | | List<Kline> dayList = getTimeseries(symbol,KlineConstant.PERIOD_1DAY,AllticktradeMadeOptions.dayPeriod); |
| | | map.put(KlineConstant.PERIOD_1DAY, dayList); |
| | | |
| | | List<Kline> towHourlyList = getTimeseries(symbol,KlineConstant.PERIOD_2HOUR,AllticktradeMadeOptions.towHourPeriod); |
| | | map.put(KlineConstant.PERIOD_2HOUR, towHourlyList); |
| | | |
| | | List<Kline> oneHourlyList = getTimeseries(symbol,KlineConstant.PERIOD_60MIN,AllticktradeMadeOptions.hourPeriod); |
| | | map.put(KlineConstant.PERIOD_60MIN, oneHourlyList); |
| | | |
| | | List<Kline> twoHourlyList = getTimeseriesForTwoHourly(symbol); |
| | | if (CollectionUtil.isEmpty(twoHourlyList)) { |
| | | try { |
| | | List<Kline> hourList = map.get(Kline.PERIOD_60MIN); |
| | | if (CollectionUtil.isNotEmpty(hourList)) { |
| | | map.put(Kline.PERIOD_2HOUR, klineService.calculateKline(symbol, 2, Kline.PERIOD_2HOUR, hourList)); |
| | | } |
| | | } catch (Exception e) { |
| | | logger.error("外汇计算 120mink线图失败", e); |
| | | } |
| | | } else { |
| | | map.put(KlineConstant.PERIOD_2HOUR, twoHourlyList); |
| | | |
| | | } |
| | | |
| | | |
| | | List<Kline> thirtyMinuteList = getTimeseriesThirtyMinute(symbol); |
| | | List<Kline> thirtyMinuteList = getTimeseries(symbol,KlineConstant.PERIOD_30MIN,AllticktradeMadeOptions.thirtyPeriod); |
| | | map.put(KlineConstant.PERIOD_30MIN, thirtyMinuteList); |
| | | |
| | | List<Kline> fifteenMinuteList = getTimeseriesFifteenMinute(symbol); |
| | | List<Kline> fifteenMinuteList = getTimeseries(symbol,KlineConstant.PERIOD_15MIN,AllticktradeMadeOptions.fifteenPeriod); |
| | | map.put(KlineConstant.PERIOD_15MIN, fifteenMinuteList); |
| | | |
| | | List<Kline> fiveMinuteList = getTimeseriesFiveMinute(symbol); |
| | | List<Kline> fiveMinuteList = getTimeseries(symbol,KlineConstant.PERIOD_5MIN,AllticktradeMadeOptions.fivePeriod); |
| | | map.put(KlineConstant.PERIOD_5MIN, fiveMinuteList); |
| | | |
| | | List<Kline> oneMinuteList = getTimeseriesOneMinute(symbol); |
| | | List<Kline> oneMinuteList = getTimeseries(symbol,KlineConstant.PERIOD_1MIN,AllticktradeMadeOptions.minutePeriod); |
| | | map.put(KlineConstant.PERIOD_1MIN, oneMinuteList); |
| | | |
| | | //昨日收盘价 |
| | | BigDecimal yesterdayLastPrice = realtimeSingleZdf(symbol); |
| | | Item item = itemService.findBySymbol(symbol); |
| | | item.setYesterdayLastPrice(yesterdayLastPrice); |
| | | itemService.updateById(item); |
| | | return map; |
| | | } |
| | | |
| | | public List<Kline> buildOneDayPeriod(List<TimeseriesResult> list, String currency) { |
| | | if (isXueQiu(currency)) { |
| | | return xueQiuDataService.buildOneDayPeriod(currency); |
| | | }if (isXinlang(currency)) { |
| | | return xinLangDataService.buildOneDayPeriod(currency); |
| | | public BigDecimal realtimeSingleZdf(String symbol){ |
| | | //计算24小时涨跌幅 |
| | | BigDecimal yesterdayLastPrice = BigDecimal.ZERO; |
| | | List<Kline> dayklines = getTimeseries(symbol,KlineConstant.PERIOD_OTHER,AllticktradeMadeOptions.dayPeriod); |
| | | int decimal = itemService.getDecimal(symbol); |
| | | long minTimestamp = Long.MAX_VALUE; |
| | | for (Kline kline : dayklines) { |
| | | long currentTimestamp = kline.getTs(); |
| | | if (currentTimestamp < minTimestamp) { |
| | | minTimestamp = currentTimestamp; |
| | | yesterdayLastPrice = kline.getClose().setScale(decimal, RoundingMode.HALF_UP); |
| | | } |
| | | } |
| | | return yesterdayLastPrice; |
| | | } |
| | | |
| | | @Override |
| | | public List<Kline> getTimeseries(String symbol,String period,int periodType){ |
| | | List<Kline> resList = new ArrayList<>(); |
| | | for (TimeseriesResult result : list) { |
| | | int klineCount; |
| | | if (KlineConstant.PERIOD_1MIN.equals(period)) { |
| | | // klineCount = 1440; // 当天的1分钟K线 |
| | | klineCount = 1000; |
| | | } else if (KlineConstant.PERIOD_5MIN.equals(period)) { |
| | | klineCount = 288; // 当天的5分钟K线 |
| | | } else if (KlineConstant.PERIOD_15MIN.equals(period)) { |
| | | klineCount = 192; // 2天的15分钟K线,15分钟间隔 |
| | | } else if (KlineConstant.PERIOD_30MIN.equals(period)) { |
| | | klineCount = 480; // 10天的30分钟K线 |
| | | } else if (KlineConstant.PERIOD_60MIN.equals(period)) { |
| | | klineCount = 720; // 1个月的60分钟K线 |
| | | } else if (KlineConstant.PERIOD_2HOUR.equals(period)) { |
| | | klineCount = 12; // 2个月的120分钟K线 |
| | | } else if (KlineConstant.PERIOD_4HOUR.equals(period)) { |
| | | klineCount = 540; // 3个月的4小时K线 |
| | | } else if (KlineConstant.PERIOD_1DAY.equals(period)) { |
| | | klineCount = 1 * 365; // 1年的日K线,1天 * 12个月 = 540 |
| | | } else if (KlineConstant.PERIOD_1WEEK.equals(period)) { |
| | | klineCount = 52 * 5; // 5年的1周K线 (1年52周,5年共260周) |
| | | } else if (KlineConstant.PERIOD_1MON.equals(period)) { |
| | | klineCount = 12 * 5; // 5年的1个月K线,12个月每年,乘以5年 = 60 |
| | | }else if(KlineConstant.PERIOD_ONE.equals(period)){ |
| | | klineCount = 1; |
| | | }else{ |
| | | klineCount = 2; |
| | | } |
| | | String resultStr = null; |
| | | try { |
| | | resultStr = getTradeKline(symbol, periodType, klineCount); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", e); |
| | | } |
| | | |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if (null == resultJson || 200 != resultJson.getInteger("ret")) { |
| | | logger.error("采集外汇k线图失败,--"+period+"--返回:{} ", resultStr); |
| | | } |
| | | |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(symbol); |
| | | kline.setPeriod(period); |
| | | // 毫秒 |
| | | kline.setTs(Long.valueOf(result.getTimestamp() + "000")); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getTurnover()); |
| | | kline.setAmount(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | } |
| | | try { |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | logger.error("延时中断: {}", e); |
| | | } |
| | | return resList; |
| | | } |
| | | |
| | | @Override |
| | | public List<Realtime> realtime24HoruData(String symbols, List<Realtime> realtimes) { |
| | | List<String> strings = Arrays.asList(symbols.split(",")); |
| | | for (int i = 0; i < strings.size(); i++) { |
| | | List<Kline> dayList = getTimeseries(strings.get(i), KlineConstant.PERIOD_ONE, AllticktradeMadeOptions.dayPeriod); |
| | | if(dayList.size()>0){ |
| | | Kline kline = dayList.get(0); |
| | | for (Realtime realtime : realtimes){ |
| | | if(realtime.getSymbol().equals(strings.get(i))){ |
| | | int decimal = itemService.getDecimal(realtime.getSymbol()); |
| | | realtime.setOpen(kline.getOpen().setScale(decimal, RoundingMode.HALF_UP)); |
| | | realtime.setHigh(kline.getHigh().setScale(decimal, RoundingMode.HALF_UP)); |
| | | realtime.setLow(kline.getLow().setScale(decimal, RoundingMode.HALF_UP)); |
| | | realtime.setAmount(kline.getAmount().setScale(decimal, RoundingMode.HALF_UP)); |
| | | realtime.setVolume(kline.getVolume().setScale(decimal, RoundingMode.HALF_UP)); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return realtimes; |
| | | } |
| | | |
| | | public List<Kline> buildOneDayPeriod(List<AllticktradeResult> list, String currency) { |
| | | List<Kline> resList = new ArrayList<>(); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_1DAY); |
| | | kline.setTs(UTCDateUtils.toDate(result.getDate(), "yyyy-MM-dd").getTime()); |
| | | kline.setOpen(result.getOpen()); |
| | | kline.setClose(result.getClose()); |
| | | kline.setHigh(result.getHigh()); |
| | | kline.setLow(result.getLow()); |
| | | kline.setVolume(BigDecimal.ZERO); |
| | | kline.setTs(Long.parseLong(result.getTimestamp())); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | return resList; |
| | |
| | | * 4hourly 3月 |
| | | */ |
| | | public List<Kline> getTimeseriesForFourHourly(String currency) { |
| | | if (isXueQiu(currency)) { |
| | | return xueQiuDataService.getTimeseriesForFourHourly(currency); |
| | | } else if (isXinlang(currency)) { |
| | | return xinLangDataService.getTimeseriesForFourHourly(currency); |
| | | |
| | | } |
| | | List<Kline> resList = new ArrayList<>(); |
| | | // 每次只能取一个月的数据,需要遍历3个月 |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH:mm"); |
| | | f.setTimeZone(TimeZone.getTimeZone(UTCDateUtils.GMT_TIME_ZONE)); |
| | | // Date now = UTCDateUtils.toDate(UTCDateUtils.addHour(new Date(), -4), "yyyy-MM-dd-HH:mm"); |
| | | Date now = new Date(); |
| | | for (int i = 0; i < TraderMadeOptions.fourHourlyPeriod; i++) { |
| | | String startDate = UTCDateUtils.addDay(now, -30); |
| | | String endDate = f.format(now); |
| | | now = UTCDateUtils.toDate(startDate, "yyyy-MM-dd-HH:mm"); |
| | | |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("currency", currency); |
| | | param.put("start_date", startDate); |
| | | param.put("end_date", endDate); |
| | | param.put("interval", "hourly"); |
| | | param.put("period", "4"); |
| | | param.put("format", "records"); |
| | | param.put("api_key", TraderMadeOptions.apiKey); |
| | | String json = HttpHelper.getJSONFromHttpNew(TraderMadeOptions.timeseries, param, HttpMethodType.GET); |
| | | JSONObject resultJson = JSON.parseObject(json); |
| | | JSONArray dataArray = resultJson.getJSONArray("quotes"); |
| | | String resultStr = null; |
| | | try { |
| | | resultStr = getTradeKline(currency,AllticktradeMadeOptions.fourHourPeriod,AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", e); |
| | | continue; |
| | | } |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if(null == resultJson || 200 != resultJson.getInteger("ret")){ |
| | | logger.error("采集外汇k线图失败,返回:{} ", resultStr); |
| | | } |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<TimeseriesResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), TimeseriesResult.class); |
| | | for (TimeseriesResult result : list) { |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_4HOUR); |
| | | // 毫秒 |
| | | kline.setTs(UTCDateUtils.toDate(result.getDate(), "yyyy-MM-dd HH:mm").getTime()); |
| | | kline.setOpen(result.getOpen()); |
| | | kline.setClose(result.getClose()); |
| | | kline.setHigh(result.getHigh()); |
| | | kline.setLow(result.getLow()); |
| | | kline.setVolume(BigDecimal.ZERO); |
| | | kline.setTs(Long.parseLong(result.getTimestamp())); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | } |
| | |
| | | * 1hourly 1月 |
| | | */ |
| | | public List<Kline> getTimeseriesForOneHourly(String currency) { |
| | | if (isXueQiu(currency)) { |
| | | return xueQiuDataService.getTimeseriesForOneHourly(currency); |
| | | } |
| | | if (isXinlang(currency)) { |
| | | return xinLangDataService.getTimeseriesForOneHourly(currency); |
| | | } |
| | | List<Kline> resList = new ArrayList<>(); |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH:mm"); |
| | | f.setTimeZone(TimeZone.getTimeZone(UTCDateUtils.GMT_TIME_ZONE)); |
| | | // Date now = UTCDateUtils.toDate(UTCDateUtils.addHour(new Date(), -1), "yyyy-MM-dd-HH:mm"); |
| | | Date now = new Date(); |
| | | String startDate = UTCDateUtils.addDay(now, -30); |
| | | String endDate = f.format(now); |
| | | |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("currency", currency); |
| | | param.put("start_date", startDate); |
| | | param.put("end_date", endDate); |
| | | param.put("interval", "hourly"); |
| | | param.put("period", "1"); |
| | | param.put("format", "records"); |
| | | param.put("api_key", TraderMadeOptions.apiKey); |
| | | String json = HttpHelper.getJSONFromHttpNew(TraderMadeOptions.timeseries, param, HttpMethodType.GET); |
| | | JSONObject resultJson = JSON.parseObject(json); |
| | | JSONArray dataArray = resultJson.getJSONArray("quotes"); |
| | | String resultStr = null; |
| | | try { |
| | | resultStr = getTradeKline(currency,AllticktradeMadeOptions.hourPeriod,AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", e); |
| | | } |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if(null == resultJson || 200 != resultJson.getInteger("ret")){ |
| | | logger.error("采集外汇k线图失败,返回:{} ", resultStr); |
| | | } |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<TimeseriesResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), TimeseriesResult.class); |
| | | for (TimeseriesResult result : list) { |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_60MIN); |
| | | // 毫秒 |
| | | kline.setTs(UTCDateUtils.toDate(result.getDate(), "yyyy-MM-dd HH:mm").getTime()); |
| | | kline.setOpen(result.getOpen()); |
| | | kline.setClose(result.getClose()); |
| | | kline.setHigh(result.getHigh()); |
| | | kline.setLow(result.getLow()); |
| | | kline.setVolume(BigDecimal.ZERO); |
| | | kline.setTs(Long.parseLong(result.getTimestamp())); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | } |
| | |
| | | |
| | | @Override |
| | | public List<Kline> getTimeseriesForTwoHourly(String currency) { |
| | | if (isXueQiu(currency)) { |
| | | return xueQiuDataService.getTimeseriesForTwoHourly(currency); |
| | | List<Kline> resList = new ArrayList<>(); |
| | | String resultStr = null; |
| | | for (int i = 0; i < TraderMadeOptions.fifteenMinutePeriod; i++) { |
| | | try { |
| | | resultStr = getTradeKline(currency, AllticktradeMadeOptions.towHourPeriod, AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", e); |
| | | } |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if (null == resultJson || 200 != resultJson.getInteger("ret")) { |
| | | logger.error("采集外汇k线图失败,返回:{} ", resultStr); |
| | | } |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_2HOUR); |
| | | // 毫秒 |
| | | kline.setTs(Long.parseLong(result.getTimestamp())); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | } |
| | | } |
| | | if (isXinlang(currency)) { |
| | | return xinLangDataService.getTimeseriesForTwoHourly(currency); |
| | | } |
| | | return Lists.newArrayList(); |
| | | return resList; |
| | | } |
| | | |
| | | /** |
| | |
| | | * 1minute 1天 |
| | | */ |
| | | public List<Kline> getTimeseriesOneMinute(String currency) { |
| | | if (isXueQiu(currency)) { |
| | | return xueQiuDataService.getTimeseriesOneMinute(currency); |
| | | } |
| | | if (isXinlang(currency)) { |
| | | return xinLangDataService.getTimeseriesOneMinute(currency); |
| | | } |
| | | List<Kline> resList = new ArrayList<>(); |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH:mm"); |
| | | f.setTimeZone(TimeZone.getTimeZone(UTCDateUtils.GMT_TIME_ZONE)); |
| | | Date now = new Date(); |
| | | String startDate = UTCDateUtils.addDay(now, -1); |
| | | String endDate = f.format(now); |
| | | |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("currency", currency); |
| | | param.put("start_date", startDate); |
| | | param.put("end_date", endDate); |
| | | param.put("interval", "minute"); |
| | | param.put("period", "1"); |
| | | param.put("format", "records"); |
| | | param.put("api_key", TraderMadeOptions.apiKey); |
| | | String json = HttpHelper.getJSONFromHttpNew(TraderMadeOptions.timeseries, param, HttpMethodType.GET); |
| | | JSONObject resultJson = JSON.parseObject(json); |
| | | JSONArray dataArray = resultJson.getJSONArray("quotes"); |
| | | if (dataArray.size() > 0) { |
| | | List<TimeseriesResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), TimeseriesResult.class); |
| | | for (TimeseriesResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_1MIN); |
| | | // 毫秒 |
| | | kline.setTs(UTCDateUtils.toDate(result.getDate(), "yyyy-MM-dd HH:mm").getTime()); |
| | | kline.setOpen(result.getOpen()); |
| | | kline.setClose(result.getClose()); |
| | | kline.setHigh(result.getHigh()); |
| | | kline.setLow(result.getLow()); |
| | | kline.setVolume(BigDecimal.ZERO); |
| | | resList.add(kline); |
| | | String resultStr = null; |
| | | for (int i = 0; i < TraderMadeOptions.fifteenMinutePeriod; i++) { |
| | | try { |
| | | resultStr = getTradeKline(currency, AllticktradeMadeOptions.minutePeriod, AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", e); |
| | | } |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if (null == resultJson || 200 != resultJson.getInteger("ret")) { |
| | | logger.error("采集外汇k线图失败,返回:{} ", resultStr); |
| | | } |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_1MIN); |
| | | // 毫秒 |
| | | kline.setTs(Long.parseLong(result.getTimestamp())); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | } |
| | | } |
| | | return resList; |
| | |
| | | * 1minute 1天 |
| | | */ |
| | | public List<Kline> getTimeseriesFiveMinute(String currency) { |
| | | if (isXueQiu(currency)) { |
| | | return xueQiuDataService.getTimeseriesFiveMinute(currency); |
| | | } |
| | | if (isXinlang(currency)) { |
| | | return xinLangDataService.getTimeseriesFiveMinute(currency); |
| | | } |
| | | List<Kline> resList = new ArrayList<>(); |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH:mm"); |
| | | f.setTimeZone(TimeZone.getTimeZone(UTCDateUtils.GMT_TIME_ZONE)); |
| | | Date now = new Date(); |
| | | String startDate = UTCDateUtils.addDay(now, -2); |
| | | String endDate = f.format(now); |
| | | |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("currency", currency); |
| | | param.put("start_date", startDate); |
| | | param.put("end_date", endDate); |
| | | param.put("interval", "minute"); |
| | | param.put("period", "5"); |
| | | param.put("format", "records"); |
| | | param.put("api_key", TraderMadeOptions.apiKey); |
| | | String json = HttpHelper.getJSONFromHttpNew(TraderMadeOptions.timeseries, param, HttpMethodType.GET); |
| | | JSONObject resultJson = JSON.parseObject(json); |
| | | JSONArray dataArray = resultJson.getJSONArray("quotes"); |
| | | if (dataArray.size() > 0) { |
| | | List<TimeseriesResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), TimeseriesResult.class); |
| | | for (TimeseriesResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_5MIN); |
| | | // 毫秒 |
| | | kline.setTs(UTCDateUtils.toDate(result.getDate(), "yyyy-MM-dd HH:mm").getTime()); |
| | | kline.setOpen(result.getOpen()); |
| | | kline.setClose(result.getClose()); |
| | | kline.setHigh(result.getHigh()); |
| | | kline.setLow(result.getLow()); |
| | | kline.setVolume(BigDecimal.ZERO); |
| | | resList.add(kline); |
| | | String resultStr = null; |
| | | for (int i = 0; i < TraderMadeOptions.fifteenMinutePeriod; i++) { |
| | | try { |
| | | resultStr = getTradeKline(currency, AllticktradeMadeOptions.fivePeriod, AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", e); |
| | | } |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if (null == resultJson || 200 != resultJson.getInteger("ret")) { |
| | | logger.error("采集外汇k线图失败,返回:{} ", resultStr); |
| | | } |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_5MIN); |
| | | // 毫秒 |
| | | kline.setTs(Long.parseLong(result.getTimestamp())); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | } |
| | | } |
| | | return resList; |
| | |
| | | * 1minute 1天 |
| | | */ |
| | | public List<Kline> getTimeseriesFifteenMinute(String currency) { |
| | | if (isXueQiu(currency)) { |
| | | return xueQiuDataService.getTimeseriesFifteenMinute(currency); |
| | | } |
| | | if (isXinlang(currency)) { |
| | | return xinLangDataService.getTimeseriesFifteenMinute(currency); |
| | | } |
| | | List<Kline> resList = new ArrayList<>(); |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH:mm"); |
| | | f.setTimeZone(TimeZone.getTimeZone(UTCDateUtils.GMT_TIME_ZONE)); |
| | | Date now = new Date(); |
| | | String resultStr = null; |
| | | for (int i = 0; i < TraderMadeOptions.fifteenMinutePeriod; i++) { |
| | | String startDate = UTCDateUtils.addDay(now, -2); |
| | | String endDate = f.format(now); |
| | | now = UTCDateUtils.toDate(startDate, "yyyy-MM-dd-HH:mm"); |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("currency", currency); |
| | | param.put("start_date", startDate); |
| | | param.put("end_date", endDate); |
| | | param.put("interval", "minute"); |
| | | param.put("period", "15"); |
| | | param.put("format", "records"); |
| | | param.put("api_key", TraderMadeOptions.apiKey); |
| | | String json = HttpHelper.getJSONFromHttpNew(TraderMadeOptions.timeseries, param, HttpMethodType.GET); |
| | | JSONObject resultJson = JSON.parseObject(json); |
| | | JSONArray dataArray = resultJson.getJSONArray("quotes"); |
| | | try { |
| | | resultStr = getTradeKline(currency, AllticktradeMadeOptions.fifteenPeriod, AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", e); |
| | | } |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if (null == resultJson || 200 != resultJson.getInteger("ret")) { |
| | | logger.error("采集外汇k线图失败,返回:{} ", resultStr); |
| | | } |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<TimeseriesResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), TimeseriesResult.class); |
| | | for (TimeseriesResult result : list) { |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_15MIN); |
| | | // 毫秒 |
| | | kline.setTs(UTCDateUtils.toDate(result.getDate(), "yyyy-MM-dd HH:mm").getTime()); |
| | | kline.setOpen(result.getOpen()); |
| | | kline.setClose(result.getClose()); |
| | | kline.setHigh(result.getHigh()); |
| | | kline.setLow(result.getLow()); |
| | | kline.setVolume(BigDecimal.ZERO); |
| | | kline.setTs(Long.parseLong(result.getTimestamp())); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | } |
| | |
| | | * 1minute 1天 |
| | | */ |
| | | public List<Kline> getTimeseriesThirtyMinute(String currency) { |
| | | if (isXueQiu(currency)) { |
| | | return xueQiuDataService.getTimeseriesThirtyMinute(currency); |
| | | } |
| | | if (isXinlang(currency)) { |
| | | return xinLangDataService.getTimeseriesThirtyMinute(currency); |
| | | } |
| | | List<Kline> resList = new ArrayList<>(); |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH:mm"); |
| | | f.setTimeZone(TimeZone.getTimeZone(UTCDateUtils.GMT_TIME_ZONE)); |
| | | Date now = new Date(); |
| | | String resultStr = null; |
| | | for (int i = 0; i < TraderMadeOptions.thirtyMinutePeriod; i++) { |
| | | String startDate = UTCDateUtils.addDay(now, -2); |
| | | String endDate = f.format(now); |
| | | now = UTCDateUtils.toDate(startDate, "yyyy-MM-dd-HH:mm"); |
| | | Map<String, String> param = new HashMap<>(); |
| | | param.put("currency", currency); |
| | | param.put("start_date", startDate); |
| | | param.put("end_date", endDate); |
| | | param.put("interval", "minute"); |
| | | param.put("period", "15"); |
| | | param.put("format", "records"); |
| | | param.put("api_key", TraderMadeOptions.apiKey); |
| | | String json = HttpHelper.getJSONFromHttpNew(TraderMadeOptions.timeseries, param, HttpMethodType.GET); |
| | | JSONObject resultJson = JSON.parseObject(json); |
| | | JSONArray dataArray = resultJson.getJSONArray("quotes"); |
| | | try { |
| | | resultStr = getTradeKline(currency,AllticktradeMadeOptions.thirtyPeriod,AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:{} ", e); |
| | | } |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if(null == resultJson || 200 != resultJson.getInteger("ret")){ |
| | | logger.error("采集外汇k线图失败,返回:{} ", resultStr); |
| | | } |
| | | JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | if (dataArray.size() > 0) { |
| | | List<TimeseriesResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), TimeseriesResult.class); |
| | | for (TimeseriesResult result : list) { |
| | | List<AllticktradeResult> list = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AllticktradeResult.class); |
| | | for (AllticktradeResult result : list) { |
| | | Kline kline = new Kline(); |
| | | kline.setSymbol(currency); |
| | | kline.setPeriod(Kline.PERIOD_30MIN); |
| | | // 毫秒 |
| | | kline.setTs(UTCDateUtils.toDate(result.getDate(), "yyyy-MM-dd HH:mm").getTime()); |
| | | kline.setOpen(result.getOpen()); |
| | | kline.setClose(result.getClose()); |
| | | kline.setHigh(result.getHigh()); |
| | | kline.setLow(result.getLow()); |
| | | kline.setVolume(BigDecimal.ZERO); |
| | | kline.setTs(Long.parseLong(result.getTimestamp())); |
| | | kline.setOpen(result.getOpen_price()); |
| | | kline.setClose(result.getClose_price()); |
| | | kline.setHigh(result.getHigh_price()); |
| | | kline.setLow(result.getLow_price()); |
| | | kline.setVolume(result.getVolume()); |
| | | resList.add(kline); |
| | | } |
| | | } |
| | | } |
| | | return resList; |
| | | } |
| | | |
| | | public String getTradeKline(String symbol,int type,int num){ |
| | | JSONObject json = new JSONObject(); |
| | | json.put("trace", UUID.randomUUID().toString()); |
| | | JSONObject data = new JSONObject(); |
| | | data.put("code", symbol); |
| | | data.put("kline_type", type); |
| | | data.put("kline_timestamp_end", 0); |
| | | data.put("query_kline_num", num); |
| | | json.put("data", data); |
| | | try { |
| | | String url = tradeKline.replace("{1}", URLEncoder.encode(json.toString(), "UTF-8")); |
| | | HttpGet request = new HttpGet(url); |
| | | HttpResponse response = HttpHelper.getHttpclient().execute(request); |
| | | return HttpHelper.responseProc(response); |
| | | } catch (Exception e) { |
| | | logger.error("采集外汇k线图失败:==={} ", symbol); |
| | | } |
| | | return null; |
| | | } |
| | | } |