<commit_before>/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.analytics.financial.instrument.future;

import javax.time.calendar.LocalDate;
import javax.time.calendar.ZonedDateTime;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.Validate;

import com.opengamma.analytics.financial.instrument.InstrumentDefinitionVisitor;
import com.opengamma.analytics.financial.instrument.InstrumentDefinitionWithData;
import com.opengamma.analytics.financial.interestrate.future.derivative.InterestRateFutureOptionMarginSecurity;
import com.opengamma.analytics.financial.interestrate.future.derivative.InterestRateFutureOptionMarginTransaction;

/**
 * Description of transaction on an interest rate future option security with daily margining process (LIFFE and Eurex type).
 */
public class InterestRateFutureOptionMarginTransactionDefinition implements InstrumentDefinitionWithData<InterestRateFutureOptionMarginTransaction, Double> {

  /**
   * The underlying option future security.
   */
  private final InterestRateFutureOptionMarginSecurityDefinition _underlyingOption;
  /**
   * The quantity of the transaction. Can be positive or negative.
   */
  private final int _quantity;
  /**
   * The transaction date.
   */
  private final ZonedDateTime _tradeDate;
  /**
   * The transaction price. The price is in relative number and not in percent. This is the quoted price of the option.
   */
  private final double _tradePrice;

  /**
   * Constructor of the future option transaction from details.
   * @param underlyingOption The underlying option future security.
   * @param quantity The quantity of the transaction. Can be positive or negative.
   * @param tradeDate The transaction date.
   * @param tradePrice The transaction price.
   */
  public InterestRateFutureOptionMarginTransactionDefinition(InterestRateFutureOptionMarginSecurityDefinition underlyingOption, int quantity, ZonedDateTime tradeDate, double tradePrice) {
    Validate.notNull(underlyingOption, "underlying option");
    Validate.notNull(tradeDate, "trade date");
    this._underlyingOption = underlyingOption;
    this._quantity = quantity;
    this._tradeDate = tradeDate;
    this._tradePrice = tradePrice;
  }

  /**
   * Gets the underlying option future security.
   * @return The underlying.
   */
  public InterestRateFutureOptionMarginSecurityDefinition getUnderlyingOption() {
    return _underlyingOption;
  }

  /**
   * Gets the quantity of the transaction. Can be positive or negative.
   * @return The quantity of the transaction. 
   */
  public int getQuantity() {
    return _quantity;
  }

  /**
   * Gets the transaction date.
   * @return The transaction date.
   */
  public ZonedDateTime getTradeDate() {
    return _tradeDate;
  }

  /**
   * Gets the transaction price.
   * @return The transaction price.
   */
  public double getTradePrice() {
    return _tradePrice;
  }

  @Override
  public InterestRateFutureOptionMarginTransaction toDerivative(ZonedDateTime date, String... yieldCurveNames) {
    throw new UnsupportedOperationException("The method toDerivative of InterestRateTransactionDefinition does not support the two argument method (without margin price data).");
  }

  @Override
  /**
   * The lastMarginPrice is the last closing price used for margining. It is usually the official closing price of the previous business day.
   */
  public InterestRateFutureOptionMarginTransaction toDerivative(ZonedDateTime dateTime, Double lastMarginPrice, String... yieldCurveNames) {
    Validate.notNull(dateTime, "date");
    Validate.notNull(yieldCurveNames, "yield curve names");
    LocalDate date = dateTime.toLocalDate();
    Validate.isTrue(!date.isAfter(_underlyingOption.getUnderlyingFuture().getFixingPeriodStartDate().toLocalDate()), "Date is after last margin date");
    LocalDate tradeDateLocal = _tradeDate.toLocalDate();
    Validate.isTrue(!date.isBefore(tradeDateLocal), "Date is after trade date");
    final InterestRateFutureOptionMarginSecurity underlyingOption = _underlyingOption.toDerivative(dateTime, yieldCurveNames);
    double referencePrice;
    if (tradeDateLocal.isBefore(dateTime.toLocalDate())) { // Transaction was before last margining.
      referencePrice = lastMarginPrice;
    } else { // Transaction is today
      referencePrice = _tradePrice;
    }
    InterestRateFutureOptionMarginTransaction optionTransaction = new InterestRateFutureOptionMarginTransaction(underlyingOption, _quantity, referencePrice);
    return optionTransaction;
  }

  @Override
  public <U, V> V accept(InstrumentDefinitionVisitor<U, V> visitor, U data) {
    return visitor.visitInterestRateFutureOptionMarginTransactionDefinition(this, data);
  }

  @Override
  public <V> V accept(InstrumentDefinitionVisitor<?, V> visitor) {
    return visitor.visitInterestRateFutureOptionMarginTransactionDefinition(this);
  }

  @Override<commit_msg>Changed confusing error message when valuation occurs before trade date<commit_after>/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.analytics.financial.instrument.future;

import com.opengamma.analytics.financial.instrument.InstrumentDefinitionVisitor;
import com.opengamma.analytics.financial.instrument.InstrumentDefinitionWithData;
import com.opengamma.analytics.financial.interestrate.future.derivative.InterestRateFutureOptionMarginSecurity;
import com.opengamma.analytics.financial.interestrate.future.derivative.InterestRateFutureOptionMarginTransaction;

import javax.time.calendar.LocalDate;
import javax.time.calendar.ZonedDateTime;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.Validate;

/**
 * Description of transaction on an interest rate future option security with daily margining process (LIFFE and Eurex type).
 */
public class InterestRateFutureOptionMarginTransactionDefinition implements InstrumentDefinitionWithData<InterestRateFutureOptionMarginTransaction, Double> {

  /**
   * The underlying option future security.
   */
  private final InterestRateFutureOptionMarginSecurityDefinition _underlyingOption;
  /**
   * The quantity of the transaction. Can be positive or negative.
   */
  private final int _quantity;
  /**
   * The transaction date.
   */
  private final ZonedDateTime _tradeDate;
  /**
   * The transaction price. The price is in relative number and not in percent. This is the quoted price of the option.
   */
  private final double _tradePrice;

  /**
   * Constructor of the future option transaction from details.
   * @param underlyingOption The underlying option future security.
   * @param quantity The quantity of the transaction. Can be positive or negative.
   * @param tradeDate The transaction date.
   * @param tradePrice The transaction price.
   */
  public InterestRateFutureOptionMarginTransactionDefinition(InterestRateFutureOptionMarginSecurityDefinition underlyingOption, int quantity, ZonedDateTime tradeDate, double tradePrice) {
    Validate.notNull(underlyingOption, "underlying option");
    Validate.notNull(tradeDate, "trade date");
    this._underlyingOption = underlyingOption;
    this._quantity = quantity;
    this._tradeDate = tradeDate;
    this._tradePrice = tradePrice;
  }

  /**
   * Gets the underlying option future security.
   * @return The underlying.
   */
  public InterestRateFutureOptionMarginSecurityDefinition getUnderlyingOption() {
    return _underlyingOption;
  }

  /**
   * Gets the quantity of the transaction. Can be positive or negative.
   * @return The quantity of the transaction. 
   */
  public int getQuantity() {
    return _quantity;
  }

  /**
   * Gets the transaction date.
   * @return The transaction date.
   */
  public ZonedDateTime getTradeDate() {
    return _tradeDate;
  }

  /**
   * Gets the transaction price.
   * @return The transaction price.
   */
  public double getTradePrice() {
    return _tradePrice;
  }

  @Override
  public InterestRateFutureOptionMarginTransaction toDerivative(ZonedDateTime date, String... yieldCurveNames) {
    throw new UnsupportedOperationException("The method toDerivative of InterestRateTransactionDefinition does not support the two argument method (without margin price data).");
  }

  @Override
  /**
   * The lastMarginPrice is the last closing price used for margining. It is usually the official closing price of the previous business day.
   */
  public InterestRateFutureOptionMarginTransaction toDerivative(ZonedDateTime dateTime, Double lastMarginPrice, String... yieldCurveNames) {
    Validate.notNull(dateTime, "date");
    Validate.notNull(yieldCurveNames, "yield curve names");
    LocalDate date = dateTime.toLocalDate();
    Validate.isTrue(!date.isAfter(_underlyingOption.getUnderlyingFuture().getFixingPeriodStartDate().toLocalDate()), "Date is after last margin date");
    LocalDate tradeDateLocal = _tradeDate.toLocalDate();
    Validate.isTrue(!date.isBefore(tradeDateLocal), "Valuation date is before the trade date"); // TODO - Confirm with Quants whether this check is required/desired. Consider, for eg,  HVaR..
    final InterestRateFutureOptionMarginSecurity underlyingOption = _underlyingOption.toDerivative(dateTime, yieldCurveNames);
    double referencePrice;
    if (tradeDateLocal.isBefore(dateTime.toLocalDate())) { // Transaction was before last margining.
      referencePrice = lastMarginPrice;
    } else { // Transaction is today
      referencePrice = _tradePrice;
    }
    InterestRateFutureOptionMarginTransaction optionTransaction = new InterestRateFutureOptionMarginTransaction(underlyingOption, _quantity, referencePrice);
    return optionTransaction;
  }

  @Override
  public <U, V> V accept(InstrumentDefinitionVisitor<U, V> visitor, U data) {
    return visitor.visitInterestRateFutureOptionMarginTransactionDefinition(this, data);
  }

  @Override
  public <V> V accept(InstrumentDefinitionVisitor<?, V> visitor) {
    return visitor.visitInterestRateFutureOptionMarginTransactionDefinition(this);
  }

  @Override
{'fraction_non_alphanumeric': 0.07707412862052038, 'fraction_numerical': 0.0007854688267059401, 'mean_word_length': 4.687325516471245, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '146998', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2607, 'n_words': 767}