![JAR search and dependency download from the Maven repository](/logo.png)
com.opengamma.strata.product.bond.CapitalIndexedBondPosition Maven / Gradle / Ivy
Show all versions of strata-product Show documentation
/*
* Copyright (C) 2018 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.product.bond;
import java.io.Serializable;
import java.util.Map;
import java.util.NoSuchElementException;
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.DerivedProperty;
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.basics.Resolvable;
import com.opengamma.strata.basics.currency.Currency;
import com.opengamma.strata.collect.ArgChecker;
import com.opengamma.strata.product.PortfolioItemInfo;
import com.opengamma.strata.product.PortfolioItemSummary;
import com.opengamma.strata.product.PositionInfo;
import com.opengamma.strata.product.ProductType;
import com.opengamma.strata.product.SecuritizedProductPosition;
import com.opengamma.strata.product.SecurityId;
import com.opengamma.strata.product.common.SummarizerUtils;
/**
* A position in a capital indexed bond.
*
* A position in an underlying {@link CapitalIndexedBond}.
*
* The net quantity of the position is stored using two fields - {@code longQuantity} and {@code shortQuantity}.
* These two fields must not be negative.
* In many cases, only a long quantity or short quantity will be present with the other set to zero.
* However it is also possible for both to be non-zero, allowing long and short positions to be treated separately.
* The net quantity is available via {@link #getQuantity()}.
*
*
Price
* Strata uses decimal prices for bonds in the trade model, pricers and market data.
* For example, a price of 99.32% is represented in Strata by 0.9932.
*/
@BeanDefinition(constructorScope = "package")
public final class CapitalIndexedBondPosition
implements
SecuritizedProductPosition, Resolvable, ImmutableBean, Serializable {
/**
* The additional position information, defaulted to an empty instance.
*
* This allows additional information to be attached to the position.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final PositionInfo info;
/**
* The bond that was traded.
*
* The product captures the contracted financial details.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final CapitalIndexedBond product;
/**
* The long quantity of the security.
*
* This is the quantity of the underlying security that is held.
* The quantity cannot be negative, as that would imply short selling.
*/
@PropertyDefinition(validate = "ArgChecker.notNegative")
private final double longQuantity;
/**
* The short quantity of the security.
*
* This is the quantity of the underlying security that has been short sold.
* The quantity cannot be negative, as that would imply the position is long.
*/
@PropertyDefinition(validate = "ArgChecker.notNegative")
private final double shortQuantity;
//-------------------------------------------------------------------------
/**
* Obtains an instance from position information, product and net quantity.
*
* The net quantity is the long quantity minus the short quantity, which may be negative.
* If the quantity is positive it is treated as a long quantity.
* Otherwise it is treated as a short quantity.
*
* @param positionInfo the position information
* @param product the underlying product
* @param netQuantity the net quantity of the underlying security
* @return the position
*/
public static CapitalIndexedBondPosition ofNet(PositionInfo positionInfo, CapitalIndexedBond product, double netQuantity) {
double longQuantity = netQuantity >= 0 ? netQuantity : 0;
double shortQuantity = netQuantity >= 0 ? 0 : -netQuantity;
return new CapitalIndexedBondPosition(positionInfo, product, longQuantity, shortQuantity);
}
/**
* Obtains an instance from position information, product, long quantity and short quantity.
*
* The long quantity and short quantity must be zero or positive, not negative.
* In many cases, only a long quantity or short quantity will be present with the other set to zero.
* However it is also possible for both to be non-zero, allowing long and short positions to be treated separately.
*
* @param positionInfo the position information
* @param product the underlying product
* @param longQuantity the long quantity of the underlying security
* @param shortQuantity the short quantity of the underlying security
* @return the position
*/
public static CapitalIndexedBondPosition ofLongShort(
PositionInfo positionInfo,
CapitalIndexedBond product,
double longQuantity,
double shortQuantity) {
return new CapitalIndexedBondPosition(positionInfo, product, longQuantity, shortQuantity);
}
@ImmutableDefaults
private static void applyDefaults(Builder builder) {
builder.info = PositionInfo.empty();
}
//-------------------------------------------------------------------------
@Override
public SecurityId getSecurityId() {
return product.getSecurityId();
}
@Override
public Currency getCurrency() {
return product.getCurrency();
}
@Override
@DerivedProperty
public double getQuantity() {
return longQuantity - shortQuantity;
}
//-------------------------------------------------------------------------
@Override
public CapitalIndexedBondPosition withInfo(PortfolioItemInfo info) {
return new CapitalIndexedBondPosition(PositionInfo.from(info), product, longQuantity, shortQuantity);
}
@Override
public CapitalIndexedBondPosition withQuantity(double quantity) {
return CapitalIndexedBondPosition.ofNet(info, product, quantity);
}
//-------------------------------------------------------------------------
@Override
public PortfolioItemSummary summarize() {
// ID x 200
String description = getSecurityId().getStandardId().getValue() + " x " + SummarizerUtils.value(getQuantity());
return SummarizerUtils.summary(this, ProductType.BOND, description, getCurrency());
}
@Override
public ResolvedCapitalIndexedBondTrade resolve(ReferenceData refData) {
ResolvedCapitalIndexedBond resolved = product.resolve(refData);
return new ResolvedCapitalIndexedBondTrade(info, resolved, getQuantity(), null);
}
//------------------------- AUTOGENERATED START -------------------------
/**
* The meta-bean for {@code CapitalIndexedBondPosition}.
* @return the meta-bean, not null
*/
public static CapitalIndexedBondPosition.Meta meta() {
return CapitalIndexedBondPosition.Meta.INSTANCE;
}
static {
MetaBean.register(CapitalIndexedBondPosition.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 CapitalIndexedBondPosition.Builder builder() {
return new CapitalIndexedBondPosition.Builder();
}
/**
* Creates an instance.
* @param info the value of the property, not null
* @param product the value of the property, not null
* @param longQuantity the value of the property
* @param shortQuantity the value of the property
*/
CapitalIndexedBondPosition(
PositionInfo info,
CapitalIndexedBond product,
double longQuantity,
double shortQuantity) {
JodaBeanUtils.notNull(info, "info");
JodaBeanUtils.notNull(product, "product");
ArgChecker.notNegative(longQuantity, "longQuantity");
ArgChecker.notNegative(shortQuantity, "shortQuantity");
this.info = info;
this.product = product;
this.longQuantity = longQuantity;
this.shortQuantity = shortQuantity;
}
@Override
public CapitalIndexedBondPosition.Meta metaBean() {
return CapitalIndexedBondPosition.Meta.INSTANCE;
}
//-----------------------------------------------------------------------
/**
* Gets the additional position information, defaulted to an empty instance.
*
* This allows additional information to be attached to the position.
* @return the value of the property, not null
*/
@Override
public PositionInfo getInfo() {
return info;
}
//-----------------------------------------------------------------------
/**
* Gets the bond that was traded.
*
* The product captures the contracted financial details.
* @return the value of the property, not null
*/
@Override
public CapitalIndexedBond getProduct() {
return product;
}
//-----------------------------------------------------------------------
/**
* Gets the long quantity of the security.
*
* This is the quantity of the underlying security that is held.
* The quantity cannot be negative, as that would imply short selling.
* @return the value of the property
*/
public double getLongQuantity() {
return longQuantity;
}
//-----------------------------------------------------------------------
/**
* Gets the short quantity of the security.
*
* This is the quantity of the underlying security that has been short sold.
* The quantity cannot be negative, as that would imply the position is long.
* @return the value of the property
*/
public double getShortQuantity() {
return shortQuantity;
}
//-----------------------------------------------------------------------
/**
* 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()) {
CapitalIndexedBondPosition other = (CapitalIndexedBondPosition) obj;
return JodaBeanUtils.equal(info, other.info) &&
JodaBeanUtils.equal(product, other.product) &&
JodaBeanUtils.equal(longQuantity, other.longQuantity) &&
JodaBeanUtils.equal(shortQuantity, other.shortQuantity);
}
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(longQuantity);
hash = hash * 31 + JodaBeanUtils.hashCode(shortQuantity);
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(192);
buf.append("CapitalIndexedBondPosition{");
buf.append("info").append('=').append(JodaBeanUtils.toString(info)).append(',').append(' ');
buf.append("product").append('=').append(JodaBeanUtils.toString(product)).append(',').append(' ');
buf.append("longQuantity").append('=').append(JodaBeanUtils.toString(longQuantity)).append(',').append(' ');
buf.append("shortQuantity").append('=').append(JodaBeanUtils.toString(shortQuantity)).append(',').append(' ');
buf.append("quantity").append('=').append(JodaBeanUtils.toString(getQuantity()));
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code CapitalIndexedBondPosition}.
*/
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", CapitalIndexedBondPosition.class, PositionInfo.class);
/**
* The meta-property for the {@code product} property.
*/
private final MetaProperty product = DirectMetaProperty.ofImmutable(
this, "product", CapitalIndexedBondPosition.class, CapitalIndexedBond.class);
/**
* The meta-property for the {@code longQuantity} property.
*/
private final MetaProperty longQuantity = DirectMetaProperty.ofImmutable(
this, "longQuantity", CapitalIndexedBondPosition.class, Double.TYPE);
/**
* The meta-property for the {@code shortQuantity} property.
*/
private final MetaProperty shortQuantity = DirectMetaProperty.ofImmutable(
this, "shortQuantity", CapitalIndexedBondPosition.class, Double.TYPE);
/**
* The meta-property for the {@code quantity} property.
*/
private final MetaProperty quantity = DirectMetaProperty.ofDerived(
this, "quantity", CapitalIndexedBondPosition.class, Double.TYPE);
/**
* The meta-properties.
*/
private final Map> metaPropertyMap$ = new DirectMetaPropertyMap(
this, null,
"info",
"product",
"longQuantity",
"shortQuantity",
"quantity");
/**
* Restricted constructor.
*/
private Meta() {
}
@Override
protected MetaProperty> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case 3237038: // info
return info;
case -309474065: // product
return product;
case 611668775: // longQuantity
return longQuantity;
case -2094395097: // shortQuantity
return shortQuantity;
case -1285004149: // quantity
return quantity;
}
return super.metaPropertyGet(propertyName);
}
@Override
public CapitalIndexedBondPosition.Builder builder() {
return new CapitalIndexedBondPosition.Builder();
}
@Override
public Class extends CapitalIndexedBondPosition> beanType() {
return CapitalIndexedBondPosition.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;
}
/**
* The meta-property for the {@code longQuantity} property.
* @return the meta-property, not null
*/
public MetaProperty longQuantity() {
return longQuantity;
}
/**
* The meta-property for the {@code shortQuantity} property.
* @return the meta-property, not null
*/
public MetaProperty shortQuantity() {
return shortQuantity;
}
/**
* The meta-property for the {@code quantity} property.
* @return the meta-property, not null
*/
public MetaProperty quantity() {
return quantity;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case 3237038: // info
return ((CapitalIndexedBondPosition) bean).getInfo();
case -309474065: // product
return ((CapitalIndexedBondPosition) bean).getProduct();
case 611668775: // longQuantity
return ((CapitalIndexedBondPosition) bean).getLongQuantity();
case -2094395097: // shortQuantity
return ((CapitalIndexedBondPosition) bean).getShortQuantity();
case -1285004149: // quantity
return ((CapitalIndexedBondPosition) bean).getQuantity();
}
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 CapitalIndexedBondPosition}.
*/
public static final class Builder extends DirectFieldsBeanBuilder {
private PositionInfo info;
private CapitalIndexedBond product;
private double longQuantity;
private double shortQuantity;
/**
* Restricted constructor.
*/
private Builder() {
applyDefaults(this);
}
/**
* Restricted copy constructor.
* @param beanToCopy the bean to copy from, not null
*/
private Builder(CapitalIndexedBondPosition beanToCopy) {
this.info = beanToCopy.getInfo();
this.product = beanToCopy.getProduct();
this.longQuantity = beanToCopy.getLongQuantity();
this.shortQuantity = beanToCopy.getShortQuantity();
}
//-----------------------------------------------------------------------
@Override
public Object get(String propertyName) {
switch (propertyName.hashCode()) {
case 3237038: // info
return info;
case -309474065: // product
return product;
case 611668775: // longQuantity
return longQuantity;
case -2094395097: // shortQuantity
return shortQuantity;
default:
throw new NoSuchElementException("Unknown property: " + propertyName);
}
}
@Override
public Builder set(String propertyName, Object newValue) {
switch (propertyName.hashCode()) {
case 3237038: // info
this.info = (PositionInfo) newValue;
break;
case -309474065: // product
this.product = (CapitalIndexedBond) newValue;
break;
case 611668775: // longQuantity
this.longQuantity = (Double) newValue;
break;
case -2094395097: // shortQuantity
this.shortQuantity = (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 CapitalIndexedBondPosition build() {
return new CapitalIndexedBondPosition(
info,
product,
longQuantity,
shortQuantity);
}
//-----------------------------------------------------------------------
/**
* Sets the additional position information, defaulted to an empty instance.
*
* This allows additional information to be attached to the position.
* @param info the new value, not null
* @return this, for chaining, not null
*/
public Builder info(PositionInfo info) {
JodaBeanUtils.notNull(info, "info");
this.info = info;
return this;
}
/**
* Sets the bond that was traded.
*
* The product captures the contracted financial details.
* @param product the new value, not null
* @return this, for chaining, not null
*/
public Builder product(CapitalIndexedBond product) {
JodaBeanUtils.notNull(product, "product");
this.product = product;
return this;
}
/**
* Sets the long quantity of the security.
*
* This is the quantity of the underlying security that is held.
* The quantity cannot be negative, as that would imply short selling.
* @param longQuantity the new value
* @return this, for chaining, not null
*/
public Builder longQuantity(double longQuantity) {
ArgChecker.notNegative(longQuantity, "longQuantity");
this.longQuantity = longQuantity;
return this;
}
/**
* Sets the short quantity of the security.
*
* This is the quantity of the underlying security that has been short sold.
* The quantity cannot be negative, as that would imply the position is long.
* @param shortQuantity the new value
* @return this, for chaining, not null
*/
public Builder shortQuantity(double shortQuantity) {
ArgChecker.notNegative(shortQuantity, "shortQuantity");
this.shortQuantity = shortQuantity;
return this;
}
//-----------------------------------------------------------------------
@Override
public String toString() {
StringBuilder buf = new StringBuilder(192);
buf.append("CapitalIndexedBondPosition.Builder{");
buf.append("info").append('=').append(JodaBeanUtils.toString(info)).append(',').append(' ');
buf.append("product").append('=').append(JodaBeanUtils.toString(product)).append(',').append(' ');
buf.append("longQuantity").append('=').append(JodaBeanUtils.toString(longQuantity)).append(',').append(' ');
buf.append("shortQuantity").append('=').append(JodaBeanUtils.toString(shortQuantity)).append(',').append(' ');
buf.append("quantity").append('=').append(JodaBeanUtils.toString(null));
buf.append('}');
return buf.toString();
}
}
//-------------------------- AUTOGENERATED END --------------------------
}