/**
* Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.product.index;
import java.io.Serializable;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.joda.beans.Bean;
import org.joda.beans.BeanDefinition;
import org.joda.beans.ImmutableBean;
import org.joda.beans.ImmutableDefaults;
import org.joda.beans.ImmutableValidator;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaProperty;
import org.joda.beans.Property;
import org.joda.beans.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.collect.ArgChecker;
import com.opengamma.strata.product.ResolvableTrade;
import com.opengamma.strata.product.SecuritizedProductTrade;
import com.opengamma.strata.product.TradeInfo;
/**
* A trade representing a futures contract based on an Ibor index.
* <p>
* A trade in an underlying {@link IborFuture}.
* <p>
* An Ibor future is also known as a <i>STIR future</i> (Short Term Interest Rate).
* For example, the purchase of 2 contracts of the widely traded "CME Eurodollar futures contract".
*
* <h4>Price</h4>
* The price of an Ibor future is based on the interest rate of the underlying index.
* It is defined as {@code (100 - percentRate)}.
* <p>
* Strata uses <i>decimal prices</i> for Ibor futures in the trade model, pricers and market data.
* The decimal price is based on the decimal rate equivalent to the percentage.
* For example, a price of 99.32 implies an interest rate of 0.68% which is represented in Strata by 0.9932.
*/
@BeanDefinition(constructorScope = "package")
public final class IborFutureTrade
implements SecuritizedProductTrade, ResolvableTrade<ResolvedIborFutureTrade>, ImmutableBean, Serializable {
/**
* The additional trade information, defaulted to an empty instance.
* <p>
* This allows additional information to be attached to the trade.
* The trade date is required when calling {@link IborFutureTrade#resolve(ReferenceData)}.
*/
@PropertyDefinition(overrideGet = true)
private final TradeInfo info;
/**
* The future that was traded.
* <p>
* The product captures the contracted financial details of the trade.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final IborFuture product;
/**
* The quantity that was traded.
* <p>
* This is the number of contracts that were traded.
* This will be positive if buying and negative if selling.
*/
@PropertyDefinition(overrideGet = true)
private final double quantity;
/**
* The price that was traded, in decimal form.
* <p>
* This is the price agreed when the trade occurred.
* <p>
* Strata uses <i>decimal prices</i> for Ibor futures in the trade model, pricers and market data.
* The decimal price is based on the decimal rate equivalent to the percentage.
* For example, a price of 99.32 implies an interest rate of 0.68% which is represented in Strata by 0.9932.
*/
@PropertyDefinition(validate = "ArgChecker.notNegative", overrideGet = true)
private final double price;
//-------------------------------------------------------------------------
@ImmutableDefaults
private static void applyDefaults(Builder builder) {
builder.info = TradeInfo.empty();
}
@ImmutableValidator
private void validate() {
ArgChecker.isTrue(price < 2, "Price must be in decimal form, such as 0.993 for a 0.7% rate, but was: {}", price);
}
//-------------------------------------------------------------------------
@Override
public ResolvedIborFutureTrade resolve(ReferenceData refData) {
ResolvedIborFuture resolved = getProduct().resolve(refData);
return new ResolvedIborFutureTrade(info, resolved, quantity, price);
}
//------------------------- AUTOGENERATED START -------------------------
///CLOVER:OFF
/**
* The meta-bean for {@code IborFutureTrade}.
* @return the meta-bean, not null
*/
public static IborFutureTrade.Meta meta() {
return IborFutureTrade.Meta.INSTANCE;
}
static {
JodaBeanUtils.registerMetaBean(IborFutureTrade.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 IborFutureTrade.Builder builder() {
return new IborFutureTrade.Builder();
}
/**
* Creates an instance.
* @param info the value of the property
* @param product the value of the property, not null
* @param quantity the value of the property
* @param price the value of the property
*/
IborFutureTrade(
TradeInfo info,
IborFuture product,
double quantity,
double price) {
JodaBeanUtils.notNull(product, "product");
ArgChecker.notNegative(price, "price");
this.info = info;
this.product = product;
this.quantity = quantity;
this.price = price;
validate();
}
@Override
public IborFutureTrade.Meta metaBean() {
return IborFutureTrade.Meta.INSTANCE;
}
@Override
public <R> Property<R> property(String propertyName) {
return metaBean().<R>metaProperty(propertyName).createProperty(this);
}
@Override
public Set<String> propertyNames() {
return metaBean().metaPropertyMap().keySet();
}
//-----------------------------------------------------------------------
/**
* Gets the additional trade information, defaulted to an empty instance.
* <p>
* This allows additional information to be attached to the trade.
* The trade date is required when calling {@link IborFutureTrade#resolve(ReferenceData)}.
* @return the value of the property
*/
@Override
public TradeInfo getInfo() {
return info;
}
//-----------------------------------------------------------------------
/**
* Gets the future that was traded.
* <p>
* The product captures the contracted financial details of the trade.
* @return the value of the property, not null
*/
@Override
public IborFuture getProduct() {
return product;
}
//-----------------------------------------------------------------------
/**
* Gets the quantity that was traded.
* <p>
* This is the number of contracts that were traded.
* This will be positive if buying and negative if selling.
* @return the value of the property
*/
@Override
public double getQuantity() {
return quantity;
}
//-----------------------------------------------------------------------
/**
* Gets the price that was traded, in decimal form.
* <p>
* This is the price agreed when the trade occurred.
* <p>
* Strata uses <i>decimal prices</i> for Ibor futures in the trade model, pricers and market data.
* The decimal price is based on the decimal rate equivalent to the percentage.
* For example, a price of 99.32 implies an interest rate of 0.68% which is represented in Strata by 0.9932.
* @return the value of the property
*/
@Override
public double getPrice() {
return price;
}
//-----------------------------------------------------------------------
/**
* 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()) {
IborFutureTrade other = (IborFutureTrade) obj;
return JodaBeanUtils.equal(info, other.info) &&
JodaBeanUtils.equal(product, other.product) &&
JodaBeanUtils.equal(quantity, other.quantity) &&
JodaBeanUtils.equal(price, other.price);
}
return false;
}
@Override
public int hashCode() {
int hash = getClass().hashCode();
hash = hash * 31 + JodaBeanUtils.hashCode(info);
hash = hash * 31 + JodaBeanUtils.hashCode(product);
hash = hash * 31 + JodaBeanUtils.hashCode(quantity);
hash = hash * 31 + JodaBeanUtils.hashCode(price);
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(160);
buf.append("IborFutureTrade{");
buf.append("info").append('=').append(info).append(',').append(' ');
buf.append("product").append('=').append(product).append(',').append(' ');
buf.append("quantity").append('=').append(quantity).append(',').append(' ');
buf.append("price").append('=').append(JodaBeanUtils.toString(price));
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code IborFutureTrade}.
*/
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<TradeInfo> info = DirectMetaProperty.ofImmutable(
this, "info", IborFutureTrade.class, TradeInfo.class);
/**
* The meta-property for the {@code product} property.
*/
private final MetaProperty<IborFuture> product = DirectMetaProperty.ofImmutable(
this, "product", IborFutureTrade.class, IborFuture.class);
/**
* The meta-property for the {@code quantity} property.
*/
private final MetaProperty<Double> quantity = DirectMetaProperty.ofImmutable(
this, "quantity", IborFutureTrade.class, Double.TYPE);
/**
* The meta-property for the {@code price} property.
*/
private final MetaProperty<Double> price = DirectMetaProperty.ofImmutable(
this, "price", IborFutureTrade.class, Double.TYPE);
/**
* The meta-properties.
*/
private final Map<String, MetaProperty<?>> metaPropertyMap$ = new DirectMetaPropertyMap(
this, null,
"info",
"product",
"quantity",
"price");
/**
* Restricted constructor.
*/
private Meta() {
}
@Override
protected MetaProperty<?> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case 3237038: // info
return info;
case -309474065: // product
return product;
case -1285004149: // quantity
return quantity;
case 106934601: // price
return price;
}
return super.metaPropertyGet(propertyName);
}
@Override
public IborFutureTrade.Builder builder() {
return new IborFutureTrade.Builder();
}
@Override
public Class<? extends IborFutureTrade> beanType() {
return IborFutureTrade.class;
}
@Override
public Map<String, MetaProperty<?>> metaPropertyMap() {
return metaPropertyMap$;
}
//-----------------------------------------------------------------------
/**
* The meta-property for the {@code info} property.
* @return the meta-property, not null
*/
public MetaProperty<TradeInfo> info() {
return info;
}
/**
* The meta-property for the {@code product} property.
* @return the meta-property, not null
*/
public MetaProperty<IborFuture> product() {
return product;
}
/**
* The meta-property for the {@code quantity} property.
* @return the meta-property, not null
*/
public MetaProperty<Double> quantity() {
return quantity;
}
/**
* The meta-property for the {@code price} property.
* @return the meta-property, not null
*/
public MetaProperty<Double> price() {
return price;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case 3237038: // info
return ((IborFutureTrade) bean).getInfo();
case -309474065: // product
return ((IborFutureTrade) bean).getProduct();
case -1285004149: // quantity
return ((IborFutureTrade) bean).getQuantity();
case 106934601: // price
return ((IborFutureTrade) bean).getPrice();
}
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 IborFutureTrade}.
*/
public static final class Builder extends DirectFieldsBeanBuilder<IborFutureTrade> {
private TradeInfo info;
private IborFuture product;
private double quantity;
private double price;
/**
* Restricted constructor.
*/
private Builder() {
applyDefaults(this);
}
/**
* Restricted copy constructor.
* @param beanToCopy the bean to copy from, not null
*/
private Builder(IborFutureTrade beanToCopy) {
this.info = beanToCopy.getInfo();
this.product = beanToCopy.getProduct();
this.quantity = beanToCopy.getQuantity();
this.price = beanToCopy.getPrice();
}
//-----------------------------------------------------------------------
@Override
public Object get(String propertyName) {
switch (propertyName.hashCode()) {
case 3237038: // info
return info;
case -309474065: // product
return product;
case -1285004149: // quantity
return quantity;
case 106934601: // price
return price;
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 = (IborFuture) newValue;
break;
case -1285004149: // quantity
this.quantity = (Double) newValue;
break;
case 106934601: // price
this.price = (Double) 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 Builder setString(String propertyName, String value) {
setString(meta().metaProperty(propertyName), value);
return this;
}
@Override
public Builder setString(MetaProperty<?> property, String value) {
super.setString(property, value);
return this;
}
@Override
public Builder setAll(Map<String, ? extends Object> propertyValueMap) {
super.setAll(propertyValueMap);
return this;
}
@Override
public IborFutureTrade build() {
return new IborFutureTrade(
info,
product,
quantity,
price);
}
//-----------------------------------------------------------------------
/**
* Sets the additional trade information, defaulted to an empty instance.
* <p>
* This allows additional information to be attached to the trade.
* The trade date is required when calling {@link IborFutureTrade#resolve(ReferenceData)}.
* @param info the new value
* @return this, for chaining, not null
*/
public Builder info(TradeInfo info) {
this.info = info;
return this;
}
/**
* Sets the future that was traded.
* <p>
* 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(IborFuture product) {
JodaBeanUtils.notNull(product, "product");
this.product = product;
return this;
}
/**
* Sets the quantity that was traded.
* <p>
* This is the number of contracts that were traded.
* This will be positive if buying and negative if selling.
* @param quantity the new value
* @return this, for chaining, not null
*/
public Builder quantity(double quantity) {
this.quantity = quantity;
return this;
}
/**
* Sets the price that was traded, in decimal form.
* <p>
* This is the price agreed when the trade occurred.
* <p>
* Strata uses <i>decimal prices</i> for Ibor futures in the trade model, pricers and market data.
* The decimal price is based on the decimal rate equivalent to the percentage.
* For example, a price of 99.32 implies an interest rate of 0.68% which is represented in Strata by 0.9932.
* @param price the new value
* @return this, for chaining, not null
*/
public Builder price(double price) {
ArgChecker.notNegative(price, "price");
this.price = price;
return this;
}
//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(160);
buf.append("IborFutureTrade.Builder{");
buf.append("info").append('=').append(JodaBeanUtils.toString(info)).append(',').append(' ');
buf.append("product").append('=').append(JodaBeanUtils.toString(product)).append(',').append(' ');
buf.append("quantity").append('=').append(JodaBeanUtils.toString(quantity)).append(',').append(' ');
buf.append("price").append('=').append(JodaBeanUtils.toString(price));
buf.append('}');
return buf.toString();
}
}
///CLOVER:ON
//-------------------------- AUTOGENERATED END --------------------------
}