代码生成k线数据

来源:互联网 发布:fedora 24 php 编辑:程序博客网 时间:2024/06/05 07:41
package com.cgs.test;import com.cgs.test.model.DealHistoryItem;import com.cgs.test.model.KDayItem;import com.cgs.test.model.KMinItem;import com.cgs.test.model.KdataItem;import com.cgs.test.model.MarketValue;import com.zebrafutures.commons.redis.RedisHelper;import java.io.File;import java.io.IOException;import java.math.BigDecimal;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Collections;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Random;import javax.annotation.PostConstruct;import org.apache.commons.io.FileUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class KService {  private String path = "C:\\market_price.csv";  private List<MarketValue> valueList = new ArrayList<>();  private Logger logger = LoggerFactory.getLogger(KService.class);  @Autowired  private RedisHelper redisHelper;  private String[] contractIds = {"300000040","260862601"};  private List<KDayItem> dayItemList = new ArrayList<>();  public void buildMinK(SimpleDateFormat simpleDateFormat){    Date date;    for (MarketValue marketValue : valueList){      for (int i=0; i<contractIds.length; i++){        KMinItem kMinItem = new KMinItem();        try {          date = simpleDateFormat.parse(marketValue.tradingDate + marketValue.tradingTime);          kMinItem.setTime(date.getTime());          kMinItem.setClose(marketValue.close);          kMinItem.setHigh(marketValue.high);          kMinItem.setContractId(contractIds[i]);          kMinItem.setLow(marketValue.low);          kMinItem.setOpen(marketValue.open);          kMinItem.setVolume(marketValue.volume);          redisHelper.lpushString(RedisKeys.K_MIN + contractIds[i],kMinItem.toRedisValue());          System.out.println(kMinItem.toRedisValue() + RedisKeys.K_MIN );        } catch (ParseException e) {          e.printStackTrace();        }      }    }  }  @PostConstruct  public void loadOnStartUp(){    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmm");    try {      buildMarketValue();        //buildTick(simpleDateFormat);        buildMinK(simpleDateFormat);        buildFiveMinK(simpleDateFormat);        buildFiftenMINK(simpleDateFormat);        build30MinK(simpleDateFormat);        buildhourK(simpleDateFormat);        buildDayK(simpleDateFormat);        buildWeekK(simpleDateFormat);        buildMonthK(simpleDateFormat);//      buildTick(simpleDateFormat);    } catch (IOException e) {      e.printStackTrace();    }  }  public void buildFiveMinK(SimpleDateFormat simpleDateFormat){    doBuildMinK(simpleDateFormat,5 * 60 * 1000,RedisKeys.K_FIVEMIN);  }  public void buildFiftenMINK(SimpleDateFormat simpleDateFormat){    doBuildMinK(simpleDateFormat,15 * 60 * 1000,RedisKeys.K_FIFTEENMIN);  }  public void build30MinK(SimpleDateFormat simpleDateFormat){    doBuildMinK(simpleDateFormat,30 * 60 * 1000,RedisKeys.K_THIRTYMIN);  }  public void buildhourK(SimpleDateFormat simpleDateFormat){    doBuildMinK(simpleDateFormat,60 * 60 * 1000,RedisKeys.K_HOUR);  }  public void buildDayK(SimpleDateFormat simpleDateFormat){    doBuildDayK(simpleDateFormat,RedisKeys.K_DAY,"day");  }  public void buildWeekK(SimpleDateFormat simpleDateFormat){    doBuildDayK(simpleDateFormat,RedisKeys.K_WEEK,"week");  }  public void buildMonthK(SimpleDateFormat simpleDateFormat){    doBuildDayK(simpleDateFormat,RedisKeys.K_MONTH,"month");  }//  public void buildYearK(SimpleDateFormat simpleDateFormat){//    doBuildDayK(simpleDateFormat,RedisKeys.,"week");//  }  public void doBuildMinK(SimpleDateFormat simpleDateFormat,long timeperiod,String redisMinKey){    Map<String,KMinItem> minKMap = new HashMap<>();    Date date;    try {      for (MarketValue marketValue : valueList){        for (int i=0; i<contractIds.length; i++){          if (minKMap.get(contractIds[i]) == null){            date = simpleDateFormat.parse(marketValue.tradingDate + marketValue.tradingTime);            KMinItem kMinItem = new KMinItem();            kMinItem.setTime(date.getTime());            kMinItem.setClose(marketValue.close);            kMinItem.setHigh(marketValue.high);            kMinItem.setContractId(contractIds[i]);            kMinItem.setLow(marketValue.low);            kMinItem.setOpen(marketValue.open);            kMinItem.setVolume(marketValue.volume);            minKMap.put(contractIds[i],kMinItem);          }else{            date = simpleDateFormat.parse(marketValue.tradingDate + marketValue.tradingTime);            if((date.getTime() - minKMap.get(contractIds[i]).getTime())< timeperiod){              KMinItem finalItem = minKMap.get(contractIds[i]);              finalItem.setVolume(finalItem.getVolume() + marketValue.volume);              finalItem.setClose(marketValue.close);              finalItem.setHigh(finalItem.getHigh().compareTo(marketValue.high) == 1 ? finalItem.getHigh() : marketValue.high);              finalItem.setLow(finalItem.getLow().compareTo(marketValue.low) == -1 ? finalItem.getLow() : marketValue.low);            }else {              System.out.println(minKMap.get(contractIds[i]).toRedisValue());              redisHelper.lpushString(redisMinKey + contractIds[i],minKMap.get(contractIds[i]).toRedisValue());              minKMap.remove(contractIds[i]);            }          }        }      }    } catch (ParseException e) {      e.printStackTrace();    }  }  public List<KDayItem> doBuildDayK(SimpleDateFormat simpleDateFormat,String redisMinKey,String period){    Map<String,KDayItem> dayItemMap = new HashMap<>();    List<KDayItem> itemList = new ArrayList<>();    String lastKey = "";    Map<String,Long> lastTimeMap = new HashMap<>();    Date date;    try {      for (MarketValue marketValue : valueList){        for (int i=0; i<contractIds.length; i++){          String key = generateKeyByPeriod(period,contractIds[i],marketValue);          if (dayItemMap.get(key) == null){            date = simpleDateFormat.parse(marketValue.tradingDate + marketValue.tradingTime);            KDayItem kDayItem = new KDayItem();            kDayItem.setTime(date.getTime());            if (lastTimeMap.get(key) == null){              kDayItem.setLastTime(date.getTime());            }else if (lastTimeMap.get(key) != null){              kDayItem.setLastTime(lastTimeMap.get(key));            }            kDayItem.setClose(marketValue.close);            kDayItem.setHigh(marketValue.high);            kDayItem.setContractId(contractIds[i]);            kDayItem.setLow(marketValue.low);            kDayItem.setOpen(marketValue.open);            kDayItem.setVolume(marketValue.volume);            kDayItem.setSettlement(marketValue.close);            dayItemMap.put(key,kDayItem);            if (dayItemMap.get(lastKey) != null){              String value = dayItemMap.get(key).toRedisValue();              lastTimeMap.put(key,dayItemMap.get(key).getLastTime());              redisHelper.lpushString(redisMinKey + contractIds[i],value);            }          }else{            if(dayItemMap.containsKey(key)){              lastKey = key;              KDayItem finalItem = dayItemMap.get(key);              finalItem.setVolume(finalItem.getVolume() + marketValue.volume);              finalItem.setClose(marketValue.close);              finalItem.setHigh(finalItem.getHigh().compareTo(marketValue.high) == 1 ? finalItem.getHigh() : marketValue.high);              finalItem.setLow(finalItem.getLow().compareTo(marketValue.low) == -1 ? finalItem.getLow() : marketValue.low);              finalItem.setSettlement(marketValue.close);            }          }        }      }    } catch (ParseException e) {      e.printStackTrace();    }    return itemList;  }  public String generateKeyByPeriod(String period,String contractId,MarketValue marketValue)      throws ParseException {    switch (period){      case "day" :        return  marketValue.tradingDate + "day" +contractId;      case "week":        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");        Date date = sdf.parse(marketValue.tradingDate);        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        return "week" + String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR)) +"week" + contractId;      case "month":        return "month" + marketValue.tradingDate.substring(4,6) + "month" + contractId;      case "year":        return "year" + marketValue.tradingDate.substring(0,3) + contractId;      default:        return "";    }  }  public void doBuildDayK(String redisKey){    for (MarketValue marketValue : valueList){      String tradingDate = marketValue.tradingDate;      String year = tradingDate.substring(0,3);      String month = tradingDate.substring(4,5);      String day = tradingDate.substring(6,7);      for (int i=0; i<contractIds.length; i++){      }    }  }  private void buildMarketValue() throws IOException {    File file = new File(path);    List<String> values = FileUtils.readLines(file);    Collections.reverse(values);    String[] array;    for (String value : values) {      logger.info(value);      array = value.split(",");      if (array[7].equals("0")){        continue;      }      MarketValue marketValue = new MarketValue();      marketValue.open = new BigDecimal(array[1]);      marketValue.high = new BigDecimal(array[2]);      marketValue.low = new BigDecimal(array[3]);      marketValue.close = new BigDecimal(array[4]);      marketValue.volume = Long.parseLong(array[7]);      String time = array[0];      String[] timeArray = time.split(" ");      marketValue.tradingDate = timeArray[0].replace("/","");      marketValue.tradingTime = timeArray[1].replace(":","");      valueList.add(marketValue);    }  }  public void buildTick(SimpleDateFormat simpleDateFormat){    try{      for (MarketValue marketValue : valueList){        for (int i=0; i<contractIds.length; i++) {          Date date = simpleDateFormat.parse(marketValue.tradingDate + marketValue.tradingTime);          DealHistoryItem dealHistoryItem = new DealHistoryItem();          dealHistoryItem.setVolume(marketValue.volume);          dealHistoryItem.setContractId(contractIds[i]);          dealHistoryItem.setPrice(marketValue.open);          dealHistoryItem.setTime(date.getTime());          redisHelper.lpushString(RedisKeys.DEAL_HISTORY + contractIds[i],dealHistoryItem.toRedisValue());        }      }    } catch (ParseException e) {      e.printStackTrace();    }  }  private String getTradeDate(String date) {    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");    return sdf.format(date);  }  private String getTradeTime(String time) {    SimpleDateFormat sdf = new SimpleDateFormat("HHmmss.SSS");    return sdf.format(time);  }  private String  getRandom(){      StringBuilder sb = new StringBuilder();      Random random = new Random();      for (int i=0; i<3; i++){        sb.append(random.nextInt(10));      }      return sb.toString();  }}

0 0
原创粉丝点击