
com.opengamma.strata.product.fra.FraTrade Maven / Gradle / Ivy
/*
* Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.product.fra;
import static java.time.temporal.ChronoUnit.MONTHS;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.joda.beans.Bean;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.MetaProperty;
import org.joda.beans.gen.BeanDefinition;
import org.joda.beans.gen.ImmutableDefaults;
import org.joda.beans.gen.PropertyDefinition;
import org.joda.beans.impl.direct.DirectFieldsBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaBean;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;
import com.opengamma.strata.basics.ReferenceData;
import com.opengamma.strata.product.PortfolioItemInfo;
import com.opengamma.strata.product.PortfolioItemSummary;
import com.opengamma.strata.product.ProductTrade;
import com.opengamma.strata.product.ProductType;
import com.opengamma.strata.product.ResolvableTrade;
import com.opengamma.strata.product.TradeInfo;
import com.opengamma.strata.product.common.SummarizerUtils;
/**
* A trade in a forward rate agreement (FRA).
*
* An Over-The-Counter (OTC) trade in a {@link Fra}.
*
* For example, a FRA trade might involve an agreement to exchange the difference between
* the fixed rate of 1% and the 'GBP-LIBOR-3M' rate in 2 months time.
*/
@BeanDefinition
public final class FraTrade
implements ProductTrade, ResolvableTrade, ImmutableBean, Serializable {
/**
* The additional trade information, defaulted to an empty instance.
*
* This allows additional information to be attached to the trade.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final TradeInfo info;
/**
* The FRA product that was agreed when the trade occurred.
*
* The product captures the contracted financial details of the trade.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final Fra product;
//-------------------------------------------------------------------------
/**
* Obtains an instance of a FRA trade.
*
* @param info the trade info
* @param product the product
* @return the trade
*/
public static FraTrade of(TradeInfo info, Fra product) {
return new FraTrade(info, product);
}
@ImmutableDefaults
private static void applyDefaults(Builder builder) {
builder.info = TradeInfo.empty();
}
//-------------------------------------------------------------------------
@Override
public FraTrade withInfo(PortfolioItemInfo info) {
return new FraTrade(TradeInfo.from(info), product);
}
//-------------------------------------------------------------------------
@Override
public PortfolioItemSummary summarize() {
// 3x6 USD 1mm Rec GBP-LIBOR / Pay 2.5% : 21Jan18-21Apr18
StringBuilder buf = new StringBuilder(64);
Optional tradeDate = info.getTradeDate();
if (tradeDate.isPresent()) {
// use a three day fudge to avoid most holiday and end of month issues when calculating months
buf.append(MONTHS.between(tradeDate.get(), product.getStartDate().plusDays(3)));
buf.append("x");
buf.append(MONTHS.between(tradeDate.get(), product.getEndDate().plusDays(3)));
} else {
buf.append(product.getIndex().getTenor());
}
buf.append(' ');
String floatingRate = product.getIndex().getFloatingRateName().normalized().toString();
String fixedRate = SummarizerUtils.percent(product.getFixedRate());
buf.append(SummarizerUtils.amount(product.getCurrency(), product.getNotional()));
buf.append(" Rec ");
buf.append(product.getBuySell().isBuy() ? floatingRate : fixedRate);
buf.append(" / Pay ");
buf.append(product.getBuySell().isBuy() ? fixedRate : floatingRate);
buf.append(" : ");
buf.append(SummarizerUtils.dateRange(product.getStartDate(), product.getEndDate()));
return SummarizerUtils.summary(this, ProductType.FRA, buf.toString(), product.getCurrency());
}
@Override
public ResolvedFraTrade resolve(ReferenceData refData) {
return ResolvedFraTrade.builder()
.info(info)
.product(product.resolve(refData))
.build();
}
//------------------------- AUTOGENERATED START -------------------------
/**
* The meta-bean for {@code FraTrade}.
* @return the meta-bean, not null
*/
public static FraTrade.Meta meta() {
return FraTrade.Meta.INSTANCE;
}
static {
MetaBean.register(FraTrade.Meta.INSTANCE);
}
/**
* The serialization version id.
*/
private static final long serialVersionUID = 1L;
/**
* Returns a builder used to create an instance of the bean.
* @return the builder, not null
*/
public static FraTrade.Builder builder() {
return new FraTrade.Builder();
}
private FraTrade(
TradeInfo info,
Fra product) {
JodaBeanUtils.notNull(info, "info");
JodaBeanUtils.notNull(product, "product");
this.info = info;
this.product = product;
}
@Override
public FraTrade.Meta metaBean() {
return FraTrade.Meta.INSTANCE;
}
//-----------------------------------------------------------------------
/**
* Gets the additional trade information, defaulted to an empty instance.
*
* This allows additional information to be attached to the trade.
* @return the value of the property, not null
*/
@Override
public TradeInfo getInfo() {
return info;
}
//-----------------------------------------------------------------------
/**
* Gets the FRA product that was agreed when the trade occurred.
*
* The product captures the contracted financial details of the trade.
* @return the value of the property, not null
*/
@Override
public Fra getProduct() {
return product;
}
//-----------------------------------------------------------------------
/**
* Returns a builder that allows this bean to be mutated.
* @return the mutable builder, not null
*/
public Builder toBuilder() {
return new Builder(this);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null && obj.getClass() == this.getClass()) {
FraTrade other = (FraTrade) obj;
return JodaBeanUtils.equal(info, other.info) &&
JodaBeanUtils.equal(product, other.product);
}
return false;
}
@Override
public int hashCode() {
int hash = getClass().hashCode();
hash = hash * 31 + JodaBeanUtils.hashCode(info);
hash = hash * 31 + JodaBeanUtils.hashCode(product);
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(96);
buf.append("FraTrade{");
buf.append("info").append('=').append(JodaBeanUtils.toString(info)).append(',').append(' ');
buf.append("product").append('=').append(JodaBeanUtils.toString(product));
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code FraTrade}.
*/
public static final class Meta extends DirectMetaBean {
/**
* The singleton instance of the meta-bean.
*/
static final Meta INSTANCE = new Meta();
/**
* The meta-property for the {@code info} property.
*/
private final MetaProperty info = DirectMetaProperty.ofImmutable(
this, "info", FraTrade.class, TradeInfo.class);
/**
* The meta-property for the {@code product} property.
*/
private final MetaProperty product = DirectMetaProperty.ofImmutable(
this, "product", FraTrade.class, Fra.class);
/**
* The meta-properties.
*/
private final Map> metaPropertyMap$ = new DirectMetaPropertyMap(
this, null,
"info",
"product");
/**
* Restricted constructor.
*/
private Meta() {
}
@Override
protected MetaProperty> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case 3237038: // info
return info;
case -309474065: // product
return product;
}
return super.metaPropertyGet(propertyName);
}
@Override
public FraTrade.Builder builder() {
return new FraTrade.Builder();
}
@Override
public Class extends FraTrade> beanType() {
return FraTrade.class;
}
@Override
public Map> metaPropertyMap() {
return metaPropertyMap$;
}
//-----------------------------------------------------------------------
/**
* The meta-property for the {@code info} property.
* @return the meta-property, not null
*/
public MetaProperty info() {
return info;
}
/**
* The meta-property for the {@code product} property.
* @return the meta-property, not null
*/
public MetaProperty product() {
return product;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case 3237038: // info
return ((FraTrade) bean).getInfo();
case -309474065: // product
return ((FraTrade) bean).getProduct();
}
return super.propertyGet(bean, propertyName, quiet);
}
@Override
protected void propertySet(Bean bean, String propertyName, Object newValue, boolean quiet) {
metaProperty(propertyName);
if (quiet) {
return;
}
throw new UnsupportedOperationException("Property cannot be written: " + propertyName);
}
}
//-----------------------------------------------------------------------
/**
* The bean-builder for {@code FraTrade}.
*/
public static final class Builder extends DirectFieldsBeanBuilder {
private TradeInfo info;
private Fra product;
/**
* Restricted constructor.
*/
private Builder() {
applyDefaults(this);
}
/**
* Restricted copy constructor.
* @param beanToCopy the bean to copy from, not null
*/
private Builder(FraTrade beanToCopy) {
this.info = beanToCopy.getInfo();
this.product = beanToCopy.getProduct();
}
//-----------------------------------------------------------------------
@Override
public Object get(String propertyName) {
switch (propertyName.hashCode()) {
case 3237038: // info
return info;
case -309474065: // product
return product;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
}
@Override
public Builder set(String propertyName, Object newValue) {
switch (propertyName.hashCode()) {
case 3237038: // info
this.info = (TradeInfo) newValue;
break;
case -309474065: // product
this.product = (Fra) newValue;
break;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
return this;
}
@Override
public Builder set(MetaProperty> property, Object value) {
super.set(property, value);
return this;
}
@Override
public FraTrade build() {
return new FraTrade(
info,
product);
}
//-----------------------------------------------------------------------
/**
* Sets the additional trade information, defaulted to an empty instance.
*
* This allows additional information to be attached to the trade.
* @param info the new value, not null
* @return this, for chaining, not null
*/
public Builder info(TradeInfo info) {
JodaBeanUtils.notNull(info, "info");
this.info = info;
return this;
}
/**
* Sets the FRA product that was agreed when the trade occurred.
*
* The product captures the contracted financial details of the trade.
* @param product the new value, not null
* @return this, for chaining, not null
*/
public Builder product(Fra product) {
JodaBeanUtils.notNull(product, "product");
this.product = product;
return this;
}
//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(96);
buf.append("FraTrade.Builder{");
buf.append("info").append('=').append(JodaBeanUtils.toString(info)).append(',').append(' ');
buf.append("product").append('=').append(JodaBeanUtils.toString(product));
buf.append('}');
return buf.toString();
}
}
//-------------------------- AUTOGENERATED END --------------------------
}