All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opengamma.strata.loader.csv.QuotesCsvLoader Maven / Gradle / Ivy

There is a newer version: 2.12.46
Show newest version
/*
 * Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.strata.loader.csv;

import static java.util.stream.Collectors.toList;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Predicate;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.io.CharSource;
import com.opengamma.strata.basics.StandardId;
import com.opengamma.strata.collect.io.CharSources;
import com.opengamma.strata.collect.io.CsvFile;
import com.opengamma.strata.collect.io.CsvRow;
import com.opengamma.strata.collect.io.ResourceLocator;
import com.opengamma.strata.collect.io.UnicodeBom;
import com.opengamma.strata.collect.result.ParseFailureException;
import com.opengamma.strata.data.FieldName;
import com.opengamma.strata.loader.LoaderUtils;
import com.opengamma.strata.market.observable.QuoteId;

/**
 * Loads a set of quotes into memory from CSV resources.
 * 

* The quotes are expected to be in a CSV format, with the following header row:
* {@code Valuation Date, Symbology, Ticker, Value}. *

    *
  • The 'Valuation Date' column provides the valuation date, allowing data from different * days to be stored in the same file *
  • The 'Symbology' column is the symbology scheme applicable to the ticker. *
  • The 'Ticker' column is the identifier within the symbology. *
  • The 'Field Name' column is the field name, defaulted to 'MarketValue', allowing * fields such as 'Bid' or 'Ask' to be specified. *
  • The 'Value' column is the value of the ticker. *
*

* Each quotes file may contain entries for many different dates. *

* For example: *

 * Valuation Date, Symbology, Ticker, Field Name, Value
 * 2014-01-22, OG-Future, Eurex-FGBL-Mar14, MarketValue, 150.43
 * 2014-01-22, OG-FutOpt, Eurex-OGBL-Mar14-C150, MarketValue, 1.5
 * 2014-01-22, OG-Future, CME-ED-Mar14, MarketValue, 99.620
 * 
* Note that Microsoft Excel prefers the CSV file to have no space after the comma. *

* CSV files sometimes contain a Unicode Byte Order Mark. * Callers are responsible for handling this, such as by using {@link UnicodeBom}. */ public final class QuotesCsvLoader { // CSV column headers private static final String DATE_FIELD = "Valuation Date"; private static final String SYMBOLOGY_FIELD = "Symbology"; private static final String TICKER_FIELD = "Ticker"; private static final String FIELD_NAME_FIELD = "Field Name"; private static final String VALUE_FIELD = "Value"; //------------------------------------------------------------------------- /** * Loads one or more CSV format quote files for a specific date. *

* Only those quotes that match the specified date will be loaded. *

* If the files contain a duplicate entry an exception will be thrown. * * @param marketDataDate the date to load * @param resources the CSV resources * @return the loaded quotes, mapped by {@linkplain QuoteId quote ID} * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableMap load(LocalDate marketDataDate, ResourceLocator... resources) { return load(marketDataDate, Arrays.asList(resources)); } /** * Loads one or more CSV format quote files for a specific date. *

* Only those quotes that match the specified date will be loaded. *

* If the files contain a duplicate entry an exception will be thrown. * * @param marketDataDate the date to load * @param resources the CSV resources * @return the loaded quotes, mapped by {@linkplain QuoteId quote ID} * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableMap load(LocalDate marketDataDate, Collection resources) { Collection charSources = resources.stream().map(r -> r.getCharSource()).collect(toList()); return parse(d -> marketDataDate.equals(d), charSources).getOrDefault(marketDataDate, ImmutableMap.of()); } //------------------------------------------------------------------------- /** * Loads one or more CSV format quote files for a set of dates. *

* Only those quotes that match one of the specified dates will be loaded. *

* If the files contain a duplicate entry an exception will be thrown. * * @param marketDataDates the set of dates to load * @param resources the CSV resources * @return the loaded quotes, mapped by {@link LocalDate} and {@linkplain QuoteId quote ID} * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableMap> load( Set marketDataDates, ResourceLocator... resources) { return load(marketDataDates, Arrays.asList(resources)); } /** * Loads one or more CSV format quote files for a set of dates. *

* Only those quotes that match one of the specified dates will be loaded. *

* If the files contain a duplicate entry an exception will be thrown. * * @param marketDataDates the dates to load * @param resources the CSV resources * @return the loaded quotes, mapped by {@link LocalDate} and {@linkplain QuoteId quote ID} * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableMap> load( Set marketDataDates, Collection resources) { Collection charSources = resources.stream().map(r -> r.getCharSource()).collect(toList()); return parse(d -> marketDataDates.contains(d), charSources); } //------------------------------------------------------------------------- /** * Loads one or more CSV format quote files. *

* All dates that are found will be returned. *

* If the files contain a duplicate entry an exception will be thrown. * * @param resources the CSV resources * @return the loaded quotes, mapped by {@link LocalDate} and {@linkplain QuoteId quote ID} * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableMap> loadAllDates(ResourceLocator... resources) { return loadAllDates(Arrays.asList(resources)); } /** * Loads one or more CSV format quote files. *

* All dates that are found will be returned. *

* If the files contain a duplicate entry an exception will be thrown. * * @param resources the CSV resources * @return the loaded quotes, mapped by {@link LocalDate} and {@linkplain QuoteId quote ID} * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableMap> loadAllDates( Collection resources) { Collection charSources = resources.stream().map(r -> r.getCharSource()).collect(toList()); return parse(d -> true, charSources); } //------------------------------------------------------------------------- /** * Parses one or more CSV format quote files. *

* A predicate is specified that is used to filter the dates that are returned. * This could match a single date, a set of dates or all dates. *

* If the files contain a duplicate entry an exception will be thrown. * * @param datePredicate the predicate used to select the dates * @param charSources the CSV character sources * @return the loaded quotes, mapped by {@link LocalDate} and {@linkplain QuoteId quote ID} * @throws IllegalArgumentException if the files contain a duplicate entry */ public static ImmutableMap> parse( Predicate datePredicate, Collection charSources) { try { // builder ensures keys can only be seen once Map> mutableMap = new HashMap<>(); for (CharSource charSource : charSources) { parseSingle(datePredicate, charSource, mutableMap); } ImmutableMap.Builder> builder = ImmutableMap.builder(); for (Entry> entry : mutableMap.entrySet()) { builder.put(entry.getKey(), entry.getValue().build()); } return builder.build(); } catch (ParseFailureException ex) { throw ex; } catch (RuntimeException ex) { throw new ParseFailureException(ex, "Error parsing quotes CSV files: {exceptionMessage}", ex.getMessage()); } } // loads a single CSV file, filtering by date private static void parseSingle( Predicate datePredicate, CharSource resource, Map> mutableMap) { try { CsvFile csv = CsvFile.of(resource, true); for (CsvRow row : csv.rows()) { String dateText = row.getField(DATE_FIELD); LocalDate date = LoaderUtils.parseDate(dateText); if (datePredicate.test(date)) { String symbologyStr = row.getField(SYMBOLOGY_FIELD); String tickerStr = row.getField(TICKER_FIELD); String fieldNameStr = row.getField(FIELD_NAME_FIELD); String valueStr = row.getField(VALUE_FIELD); double value = Double.valueOf(valueStr); StandardId id = StandardId.of(symbologyStr, tickerStr); FieldName fieldName = fieldNameStr.isEmpty() ? FieldName.MARKET_VALUE : FieldName.of(fieldNameStr); ImmutableMap.Builder builderForDate = mutableMap.computeIfAbsent(date, k -> ImmutableMap.builder()); builderForDate.put(QuoteId.of(id, fieldName), value); } } } catch (RuntimeException ex) { throw new ParseFailureException( ex, "Error parsing CSV file '{fileName}': {exceptionMessage}", CharSources.extractFileName(resource), ex.getMessage()); } } //------------------------------------------------------------------------- /** * Restricted constructor. */ private QuotesCsvLoader() { } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy