无法遍历 CSV 列

我正在构建一个股票筛选器,它通过 csv 文件的每一列应用计算。但是,当我运行 for 循环时,我只得到一个结果。


    String path = "C:/Users/0/Desktop/Git/Finance/Data/NQ100.csv";

    Reader buf = Files.newBufferedReader(Paths.get(path));

    CSVParser parsed = new CSVParser(buf, CSVFormat.DEFAULT.withFirstRecordAsHeader()

            .withIgnoreHeaderCase().withTrim());


    // Parse tickers

    Map<String, Integer> header = parsed.getHeaderMap();

    List<String> tickerList = new ArrayList<>(header.keySet());


    for (int x=1; x < tickerList.size(); x++) { <----------------------- PROBLEM


        // Accessing closing price by Header names

        List<Double> closeList = new ArrayList<>();

        for (CSVRecord record : parsed) {

            String stringClose = record.get(x);

            Double close = Double.valueOf(stringClose);

            closeList.add(close);

        }


        // Percentage Change

        List<Double> pctList = new ArrayList<>();

        for (int i=1; i < closeList.size(); i++) {

            Double pct = closeList.get(i) / closeList.get(i-1) - 1;

            pctList.add(pct);

        }


        // Statistics

        Double sum = 0.0, var = 0.0, mean, sd, rfr, sr;

        // Mean

        for (Double num : pctList) sum += num;

        mean = sum/pctList.size();

        // Standard Deviation

        for (Double num: pctList) var += Math.pow(num - mean, 2);

        sd = Math.sqrt(var/pctList.size());

        // Risk Free Rate

        rfr = Math.pow((1+0.03),(1/252.0))-1;

        // Sharpe Ratio

        sr = Math.sqrt(252) * ((mean-rfr)/sd);


        System.out.println(tickerList.get(x) + " " + sr);

    }

我的数据如下所示:


,AAL,AAPL,ADBE

2007-10-25,26.311651,23.141403,47.200001

2007-10-26,26.273216,23.384495,47.0

2007-10-29,26.004248,23.43387,47.0

所以我期待:


AAL XXX

AAPL XXX

ADBE XXX

但我得到的只是:


AAL 0.3604941921663456

如果你们能帮我找到问题,将不胜感激!


守着星空守着你
浏览 189回答 2
2回答

温温酱

您只能Iterable在Java 中迭代一次,在您的情况下CSVParser parsed实现Iterable<CSVRecord>.因此,您仅在第一次计算AAL 的统计数据时对其进行迭代,在分析AAPL和ADBE 的数据期间,它将作为空数据处理。您可以通过引入 helper list init 来处理这个问题,parsed在for循环之前添加下一个代码(当然,这是一个单行解决方案,例如在 Java 8 中,但此选项也适用于早期版本):&nbsp; &nbsp; List<CSVRecord> records = new ArrayList<>();&nbsp; &nbsp; for (CSVRecord record : parsed) {&nbsp; &nbsp; &nbsp; &nbsp; records.add(record);&nbsp; &nbsp; }并更改下一行:for (CSVRecord record : records) {和:for (CSVRecord record : parsed) {对于您提供的 CSV,您将获得下一个输出:AAL -21.583101145880306AAPL 23.417753561072438ADBE -16.75343297000953

阿波罗的战车

所以这里有一段对我有用的代码,如果我理解你的问题,你只想“读取”csv文件中的每一列和每一行,希望有所帮助。&nbsp; &nbsp; &nbsp; &nbsp; br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo), "UTF8"));&nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a!=0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] datos = line.split(cvsSplitBy);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(datos[0] + " - " + datos[1] + " - " + datos[2]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java