| | |
| | | import com.yami.trading.bean.item.domain.Item; |
| | | 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.hobi.constant.AllticktradeMadeOptions; |
| | | import com.yami.trading.huobi.hobi.constant.TraderMadeOptions; |
| | | import com.yami.trading.huobi.hobi.http.HttpHelper; |
| | | import com.yami.trading.service.etf.EtfMinuteKLineService; |
| | | import com.yami.trading.service.item.ItemService; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.checkerframework.checker.units.qual.K; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.net.URLEncoder; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | private KlineService klineService; |
| | | @Autowired |
| | | private EtfMinuteKLineService etfMinuteKLineService; |
| | | |
| | | @Value("${alltick.trade-kline}") |
| | | private String tradeKline; |
| | | |
| | | public final void saveInit(String symbol) { |
| | | Item bySymbol = itemService.findBySymbol(symbol); |
| | |
| | | kline.setPeriod(Kline.PERIOD_1MIN); |
| | | return kline; |
| | | }).collect(Collectors.toList()); |
| | | Map<String, List<Kline>> dailyWeekMonthHistoryMap = getDailyWeekMonthHistory(symbol, klineList); |
| | | // Map<String, List<Kline>> dailyWeekMonthHistoryMap = getDailyWeekMonthHistory(symbol, klineList); |
| | | Map<String, List<Kline>> hourlyAndMinuteHistoryMap = getHourlyAndMinuteHistory(symbol, klineList); |
| | | klineService.saveInit(symbol, dailyWeekMonthHistoryMap, hourlyAndMinuteHistoryMap); |
| | | klineService.saveInit(symbol, null, hourlyAndMinuteHistoryMap); |
| | | } |
| | | |
| | | @Autowired |
| | |
| | | * 获取分钟数据 |
| | | */ |
| | | public Map<String, List<Kline>> getHourlyAndMinuteHistory(String symbol , List<Kline> klineList) { |
| | | // Map<String, List<Kline>> map = new HashMap<>(); |
| | | // |
| | | // List<Kline> fourHourlyList = getTimeseriesForFourHourly(symbol, klineList); |
| | | // map.put(KlineConstant.PERIOD_4HOUR, fourHourlyList); |
| | | // |
| | | // List<Kline> twoHourlyList = getTimeseriesForTwoHourly(symbol, klineList); |
| | | // map.put(KlineConstant.PERIOD_2HOUR, twoHourlyList); |
| | | // |
| | | // List<Kline> oneHourlyList = getTimeseriesForOneHourly(symbol, klineList); |
| | | // map.put(KlineConstant.PERIOD_60MIN, oneHourlyList); |
| | | // |
| | | // List<Kline> thirtyMinuteList = getTimeseriesThirtyMinute(symbol, klineList); |
| | | // map.put(KlineConstant.PERIOD_30MIN, thirtyMinuteList); |
| | | // |
| | | // List<Kline> fifteenMinuteList = getTimeseriesFifteenMinute(symbol, klineList); |
| | | // map.put(KlineConstant.PERIOD_15MIN, fifteenMinuteList); |
| | | // |
| | | // List<Kline> fiveMinuteList = getTimeseriesFiveMinute(symbol, klineList); |
| | | // map.put(KlineConstant.PERIOD_5MIN, fiveMinuteList); |
| | | // |
| | | // List<Kline> oneMinuteList = klineList; |
| | | // map.put(KlineConstant.PERIOD_1MIN, oneMinuteList); |
| | | // return map; |
| | | Map<String, List<Kline>> map = new HashMap<>(); |
| | | |
| | | List<Kline> fourHourlyList = getTimeseriesForFourHourly(symbol, klineList); |
| | | map.put(KlineConstant.PERIOD_4HOUR, fourHourlyList); |
| | | List<Kline> monthList = getTimeseries(symbol,KlineConstant.PERIOD_1MON, AllticktradeMadeOptions.monthPeriod); |
| | | map.put(KlineConstant.PERIOD_1MON, monthList); |
| | | |
| | | List<Kline> twoHourlyList = getTimeseriesForTwoHourly(symbol, klineList); |
| | | map.put(KlineConstant.PERIOD_2HOUR, twoHourlyList); |
| | | List<Kline> weekList = getTimeseries(symbol,KlineConstant.PERIOD_1WEEK,AllticktradeMadeOptions.weekPeriod); |
| | | map.put(KlineConstant.PERIOD_1WEEK, weekList); |
| | | |
| | | List<Kline> oneHourlyList = getTimeseriesForOneHourly(symbol, klineList); |
| | | 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> thirtyMinuteList = getTimeseriesThirtyMinute(symbol, klineList); |
| | | List<Kline> thirtyMinuteList = getTimeseries(symbol,KlineConstant.PERIOD_30MIN,AllticktradeMadeOptions.thirtyPeriod); |
| | | map.put(KlineConstant.PERIOD_30MIN, thirtyMinuteList); |
| | | |
| | | List<Kline> fifteenMinuteList = getTimeseriesFifteenMinute(symbol, klineList); |
| | | List<Kline> fifteenMinuteList = getTimeseries(symbol,KlineConstant.PERIOD_15MIN,AllticktradeMadeOptions.fifteenPeriod); |
| | | map.put(KlineConstant.PERIOD_15MIN, fifteenMinuteList); |
| | | |
| | | List<Kline> fiveMinuteList = getTimeseriesFiveMinute(symbol, klineList); |
| | | List<Kline> fiveMinuteList = getTimeseries(symbol,KlineConstant.PERIOD_5MIN,AllticktradeMadeOptions.fivePeriod); |
| | | map.put(KlineConstant.PERIOD_5MIN, fiveMinuteList); |
| | | |
| | | List<Kline> oneMinuteList = klineList; |
| | | List<Kline> oneMinuteList = getTimeseries(symbol,KlineConstant.PERIOD_1MIN,AllticktradeMadeOptions.minutePeriod); |
| | | map.put(KlineConstant.PERIOD_1MIN, oneMinuteList); |
| | | return map; |
| | | } |
| | | |
| | | public List<Kline> getTimeseries(String symbol,String period,int periodType){ |
| | | List<Kline> resList = new ArrayList<>(); |
| | | // 每次只能取一个月的数据,需要遍历3个月 |
| | | for (int i = 0; i < TraderMadeOptions.thirtyMinutePeriod; i++) { |
| | | String resultStr = null; |
| | | try { |
| | | resultStr = getTradeKline(symbol,periodType,AllticktradeMadeOptions.dayKlineNumPeriod); |
| | | } catch (Exception e) { |
| | | continue; |
| | | } |
| | | JSONObject resultJson = JSON.parseObject(resultStr); |
| | | if(null == resultJson || 200 != resultJson.getInteger("ret")){ |
| | | } |
| | | 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.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) { |
| | | System.out.println(e.getMessage()); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 1day 历史数据 : 周期 1年 |