
com.opengamma.strata.product.etd.EtdOptionPosition Maven / Gradle / Ivy
Show all versions of strata-product Show documentation
/*
* Copyright (C) 2017 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.product.etd;
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.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.ResolvableSecurityPosition;
import com.opengamma.strata.product.SecuritizedProductPosition;
import com.opengamma.strata.product.SecurityId;
import com.opengamma.strata.product.common.SummarizerUtils;
/**
* A position in an ETD option, where the security is embedded ready for mark-to-market pricing.
*
* This represents a position in a option, defined by long and short quantity.
* The option security is embedded directly, however the underlying product model is not available.
*
* 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()}.
*/
@BeanDefinition
public final class EtdOptionPosition
implements EtdPosition, SecuritizedProductPosition, ResolvableSecurityPosition, 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 underlying security.
*/
@PropertyDefinition(validate = "notNull", overrideGet = true)
private final EtdOptionSecurity security;
/**
* 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", overrideGet = true)
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", overrideGet = true)
private final double shortQuantity;
//-------------------------------------------------------------------------
/**
* Obtains an instance from the security 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 security the underlying security
* @param netQuantity the net quantity of the underlying security
* @return the position
*/
public static EtdOptionPosition ofNet(EtdOptionSecurity security, double netQuantity) {
return ofNet(PositionInfo.empty(), security, netQuantity);
}
/**
* Obtains an instance from position information, security 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 security the underlying security
* @param netQuantity the net quantity of the underlying security
* @return the position
*/
public static EtdOptionPosition ofNet(PositionInfo positionInfo, EtdOptionSecurity security, double netQuantity) {
double longQuantity = netQuantity >= 0 ? netQuantity : 0;
double shortQuantity = netQuantity >= 0 ? 0 : -netQuantity;
return new EtdOptionPosition(positionInfo, security, longQuantity, shortQuantity);
}
/**
* Obtains an instance from the security, 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 security the underlying security
* @param longQuantity the long quantity of the underlying security
* @param shortQuantity the short quantity of the underlying security
* @return the position
*/
public static EtdOptionPosition ofLongShort(EtdOptionSecurity security, double longQuantity, double shortQuantity) {
return ofLongShort(PositionInfo.empty(), security, longQuantity, shortQuantity);
}
/**
* Obtains an instance from position information, security, 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 security the underlying security
* @param longQuantity the long quantity of the underlying security
* @param shortQuantity the short quantity of the underlying security
* @return the position
*/
public static EtdOptionPosition ofLongShort(
PositionInfo positionInfo,
EtdOptionSecurity security,
double longQuantity,
double shortQuantity) {
return new EtdOptionPosition(positionInfo, security, longQuantity, shortQuantity);
}
@ImmutableDefaults
private static void applyDefaults(Builder builder) {
builder.info = PositionInfo.empty();
}
//-------------------------------------------------------------------------
@Override
public EtdOptionSecurity getProduct() {
return security;
}
//-------------------------------------------------------------------------
@Override
public EtdOptionPosition withInfo(PortfolioItemInfo info) {
return new EtdOptionPosition(PositionInfo.from(info), security, longQuantity, shortQuantity);
}
@Override
public EtdOptionPosition withQuantity(double quantity) {
return EtdOptionPosition.ofNet(info, security, quantity);
}
@Override
public EtdOptionPosition withQuantities(double longQuantity, double shortQuantity) {
return EtdOptionPosition.ofLongShort(info, security, longQuantity, shortQuantity);
}
//-------------------------------------------------------------------------
@Override
public PortfolioItemSummary summarize() {
// O-ECAG-OGBS-201706-P1.50 x 200, Jun17 P1.50
String option = security.summaryDescription();
String description =
getSecurityId().getStandardId().getValue() + " x " + SummarizerUtils.value(getQuantity()) + ", " + option;
return SummarizerUtils.summary(this, ProductType.ETD_OPTION, description, getCurrency());
}
/**
* Gets the net quantity of the security.
*
* This returns the net quantity of the underlying security.
* The result is positive if the net position is long and negative
* if the net position is short.
*
* This is calculated by subtracting the short quantity from the long quantity.
*
* @return the net quantity of the underlying security
*/
@Override
@DerivedProperty
public double getQuantity() {
return longQuantity - shortQuantity;
}
@Override
@DerivedProperty
public Currency getCurrency() {
return EtdPosition.super.getCurrency();
}
@Override
@DerivedProperty
public SecurityId getSecurityId() {
return EtdPosition.super.getSecurityId();
}
@Override
public SecuritizedProductPosition> resolveTarget(ReferenceData refData) {
return this;
}
//------------------------- AUTOGENERATED START -------------------------
/**
* The meta-bean for {@code EtdOptionPosition}.
* @return the meta-bean, not null
*/
public static EtdOptionPosition.Meta meta() {
return EtdOptionPosition.Meta.INSTANCE;
}
static {
MetaBean.register(EtdOptionPosition.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 EtdOptionPosition.Builder builder() {
return new EtdOptionPosition.Builder();
}
private EtdOptionPosition(
PositionInfo info,
EtdOptionSecurity security,
double longQuantity,
double shortQuantity) {
JodaBeanUtils.notNull(info, "info");
JodaBeanUtils.notNull(security, "security");
ArgChecker.notNegative(longQuantity, "longQuantity");
ArgChecker.notNegative(shortQuantity, "shortQuantity");
this.info = info;
this.security = security;
this.longQuantity = longQuantity;
this.shortQuantity = shortQuantity;
}
@Override
public EtdOptionPosition.Meta metaBean() {
return EtdOptionPosition.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 underlying security.
* @return the value of the property, not null
*/
@Override
public EtdOptionSecurity getSecurity() {
return security;
}
//-----------------------------------------------------------------------
/**
* 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
*/
@Override
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
*/
@Override
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()) {
EtdOptionPosition other = (EtdOptionPosition) obj;
return JodaBeanUtils.equal(info, other.info) &&
JodaBeanUtils.equal(security, other.security) &&
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(security);
hash = hash * 31 + JodaBeanUtils.hashCode(longQuantity);
hash = hash * 31 + JodaBeanUtils.hashCode(shortQuantity);
return hash;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(256);
buf.append("EtdOptionPosition{");
buf.append("info").append('=').append(JodaBeanUtils.toString(info)).append(',').append(' ');
buf.append("security").append('=').append(JodaBeanUtils.toString(security)).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())).append(',').append(' ');
buf.append("currency").append('=').append(JodaBeanUtils.toString(getCurrency())).append(',').append(' ');
buf.append("securityId").append('=').append(JodaBeanUtils.toString(getSecurityId()));
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code EtdOptionPosition}.
*/
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", EtdOptionPosition.class, PositionInfo.class);
/**
* The meta-property for the {@code security} property.
*/
private final MetaProperty security = DirectMetaProperty.ofImmutable(
this, "security", EtdOptionPosition.class, EtdOptionSecurity.class);
/**
* The meta-property for the {@code longQuantity} property.
*/
private final MetaProperty longQuantity = DirectMetaProperty.ofImmutable(
this, "longQuantity", EtdOptionPosition.class, Double.TYPE);
/**
* The meta-property for the {@code shortQuantity} property.
*/
private final MetaProperty shortQuantity = DirectMetaProperty.ofImmutable(
this, "shortQuantity", EtdOptionPosition.class, Double.TYPE);
/**
* The meta-property for the {@code quantity} property.
*/
private final MetaProperty quantity = DirectMetaProperty.ofDerived(
this, "quantity", EtdOptionPosition.class, Double.TYPE);
/**
* The meta-property for the {@code currency} property.
*/
private final MetaProperty currency = DirectMetaProperty.ofDerived(
this, "currency", EtdOptionPosition.class, Currency.class);
/**
* The meta-property for the {@code securityId} property.
*/
private final MetaProperty securityId = DirectMetaProperty.ofDerived(
this, "securityId", EtdOptionPosition.class, SecurityId.class);
/**
* The meta-properties.
*/
private final Map> metaPropertyMap$ = new DirectMetaPropertyMap(
this, null,
"info",
"security",
"longQuantity",
"shortQuantity",
"quantity",
"currency",
"securityId");
/**
* Restricted constructor.
*/
private Meta() {
}
@Override
protected MetaProperty> metaPropertyGet(String propertyName) {
switch (propertyName.hashCode()) {
case 3237038: // info
return info;
case 949122880: // security
return security;
case 611668775: // longQuantity
return longQuantity;
case -2094395097: // shortQuantity
return shortQuantity;
case -1285004149: // quantity
return quantity;
case 575402001: // currency
return currency;
case 1574023291: // securityId
return securityId;
}
return super.metaPropertyGet(propertyName);
}
@Override
public EtdOptionPosition.Builder builder() {
return new EtdOptionPosition.Builder();
}
@Override
public Class extends EtdOptionPosition> beanType() {
return EtdOptionPosition.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 security} property.
* @return the meta-property, not null
*/
public MetaProperty security() {
return security;
}
/**
* 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;
}
/**
* The meta-property for the {@code currency} property.
* @return the meta-property, not null
*/
public MetaProperty currency() {
return currency;
}
/**
* The meta-property for the {@code securityId} property.
* @return the meta-property, not null
*/
public MetaProperty securityId() {
return securityId;
}
//-----------------------------------------------------------------------
@Override
protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
switch (propertyName.hashCode()) {
case 3237038: // info
return ((EtdOptionPosition) bean).getInfo();
case 949122880: // security
return ((EtdOptionPosition) bean).getSecurity();
case 611668775: // longQuantity
return ((EtdOptionPosition) bean).getLongQuantity();
case -2094395097: // shortQuantity
return ((EtdOptionPosition) bean).getShortQuantity();
case -1285004149: // quantity
return ((EtdOptionPosition) bean).getQuantity();
case 575402001: // currency
return ((EtdOptionPosition) bean).getCurrency();
case 1574023291: // securityId
return ((EtdOptionPosition) bean).getSecurityId();
}
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 EtdOptionPosition}.
*/
public static final class Builder extends DirectFieldsBeanBuilder {
private PositionInfo info;
private EtdOptionSecurity security;
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(EtdOptionPosition beanToCopy) {
this.info = beanToCopy.getInfo();
this.security = beanToCopy.getSecurity();
this.longQuantity = beanToCopy.getLongQuantity();
this.shortQuantity = beanToCopy.getShortQuantity();
}
//-----------------------------------------------------------------------
@Override
public Object get(String propertyName) {
switch (propertyName.hashCode()) {
case 3237038: // info
return info;
case 949122880: // security
return security;
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 949122880: // security
this.security = (EtdOptionSecurity) 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 EtdOptionPosition build() {
return new EtdOptionPosition(
info,
security,
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 underlying security.
* @param security the new value, not null
* @return this, for chaining, not null
*/
public Builder security(EtdOptionSecurity security) {
JodaBeanUtils.notNull(security, "security");
this.security = security;
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(256);
buf.append("EtdOptionPosition.Builder{");
buf.append("info").append('=').append(JodaBeanUtils.toString(info)).append(',').append(' ');
buf.append("security").append('=').append(JodaBeanUtils.toString(security)).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)).append(',').append(' ');
buf.append("currency").append('=').append(JodaBeanUtils.toString(null)).append(',').append(' ');
buf.append("securityId").append('=').append(JodaBeanUtils.toString(null));
buf.append('}');
return buf.toString();
}
}
//-------------------------- AUTOGENERATED END --------------------------
}