Class CommodityCurve
- Namespace
- NanoQL.Experimental
- Assembly
- NanoQL.dll
A commodity forward curve: prices at dates, forward-flat between them and flat before the first and after the last, optionally on a basis curve whose prices it adds.
[Experimental("NQL0001")]
public sealed class CommodityCurve : TermStructure
- Inheritance
-
CommodityCurve
- Inherited Members
- Extension Methods
Remarks
Immutable: QuantLib's setPrices and setBasisOfCurve change the curve, and here return a copy. Its
constructor from dates has a fixed reference date, the first date; its constructor without dates follows the
evaluation date (TermStructure(0, …)) and is the factory Create(string, CommodityType, Currency, UnitOfMeasure, Calendar, EvaluationContext, DayCounter?), whose curve gets its prices from
SetPrices(IReadOnlyDictionary<Date, double>).
The node times are measured from the reference date. QuantLib's setPrices measures them from the first price's
date, so on the moving-date curve with the first price ten days after today every price in the ten days before
each node is the next node's (Divergences/CommodityCurveTests.cs); on the curve from dates the two are one.
A basis curve is read at the date, on its own reference date and day counter, and its price converted by the factor from this curve's unit to its unit, a price's conversion. QuantLib reads it at this curve's time, on this curve's clock, and converts by the factor from the basis curve's unit to this one's, which inverts it; where the two curves share their reference date, day counter and unit both give QuantLib's prices.
QuantLib's operator== compares the curves' names; compare Name, as for an index
(FinancialIndex).
Constructors
CommodityCurve(string, CommodityType, Currency, UnitOfMeasure, Calendar, IReadOnlyList<Date>, IReadOnlyList<double>, DayCounter?)
Initializes a new instance of the CommodityCurve class, its reference date its first date.
public CommodityCurve(string name, CommodityType commodityType, Currency currency, UnitOfMeasure unitOfMeasure, Calendar calendar, IReadOnlyList<Date> dates, IReadOnlyList<double> prices, DayCounter? dayCounter = null)
Parameters
namestringThe curve's name.
commodityTypeCommodityTypeThe commodity type.
currencyCurrencyThe currency its prices are in.
unitOfMeasureUnitOfMeasureThe unit its prices are per.
calendarCalendarThe calendar.
datesIReadOnlyList<Date>The price dates, increasing; at least two.
pricesIReadOnlyList<double>The prices, one per date.
dayCounterDayCounterThe day counter; QuantLib's default, Actual/365 (Fixed), when null.
Exceptions
- ArgumentNullException
A string, the type, the currency, the unit, the calendar or a list is null.
- NanoQLException
Fewer than two dates ("too few dates"), the counts differ, or a date isn't after the one before.
Properties
BasisOfCurve
Gets the basis curve whose prices this curve adds; null for none.
public CommodityCurve? BasisOfCurve { get; }
Property Value
CommodityType
Gets the commodity type.
public CommodityType CommodityType { get; }
Property Value
Currency
Gets the currency its prices are in.
public Currency Currency { get; }
Property Value
Dates
Gets the price dates.
public ImmutableArray<Date> Dates { get; }
Property Value
Empty
Gets a value indicating whether the curve has no prices.
public bool Empty { get; }
Property Value
MaxDate
Gets the latest date for which the curve can return values.
public override Date MaxDate { get; }
Property Value
Exceptions
- NanoQLException
The curve has no prices.
Name
Gets the curve's name.
public string Name { get; }
Property Value
Nodes
Gets the dates with their prices.
public ImmutableArray<(Date Date, double Price)> Nodes { get; }
Property Value
Prices
Gets the prices.
public ImmutableArray<double> Prices { get; }
Property Value
Times
Gets the node times, from the reference date.
public ImmutableArray<double> Times { get; }
Property Value
UnitOfMeasure
Gets the unit its prices are per.
public UnitOfMeasure UnitOfMeasure { get; }
Property Value
Methods
BasisOfPrice(Date)
The basis curves' price at a date, converted into this curve's unit; 0 without a basis curve.
public double BasisOfPrice(Date d)
Parameters
dDateThe date.
Returns
- double
The basis price.
Exceptions
- ArgumentException
disdefault(Date).- NanoQLException
A basis curve has no prices.
Create(string, CommodityType, Currency, UnitOfMeasure, Calendar, EvaluationContext, DayCounter?)
A curve with no prices whose reference date follows the evaluation date: QuantLib's constructor without dates, its reference date the evaluation date adjusted on the calendar. SetPrices(IReadOnlyDictionary<Date, double>) gives it its prices.
public static CommodityCurve Create(string name, CommodityType commodityType, Currency currency, UnitOfMeasure unitOfMeasure, Calendar calendar, EvaluationContext context, DayCounter? dayCounter = null)
Parameters
namestringThe curve's name.
commodityTypeCommodityTypeThe commodity type.
currencyCurrencyThe currency its prices are in.
unitOfMeasureUnitOfMeasureThe unit its prices are per.
calendarCalendarThe calendar.
contextEvaluationContextThe evaluation context, whose evaluation date is read.
dayCounterDayCounterThe day counter; Actual/365 (Fixed) when null.
Returns
- CommodityCurve
The curve, with a fixed reference date and no prices.
Exceptions
- ArgumentNullException
An argument but the day counter is null.
Price(Date, ImmutableSortedDictionary<Date, ExchangeContract>?, int)
The price at a date, with every basis curve's: at the nearby contract's underlying start date when the offset is positive.
public double Price(Date d, ImmutableSortedDictionary<Date, ExchangeContract>? exchangeContracts, int nearbyOffset)
Parameters
dDateThe date.
exchangeContractsImmutableSortedDictionary<Date, ExchangeContract>The exchange contracts by date; read only when the offset is positive.
nearbyOffsetintThe nearby contract, from 1; 0 or less for the date's own price.
Returns
- double
The price.
Exceptions
- ArgumentException
disdefault(Date).- NanoQLException
The curve has no prices ("error retrieving price for curve …"), or not enough nearby contracts follow the date.
SetBasisOfCurve(CommodityCurve, EvaluationContext)
The curve on a basis curve: QuantLib's setBasisOfCurve.
public CommodityCurve SetBasisOfCurve(CommodityCurve basisOfCurve, EvaluationContext context)
Parameters
basisOfCurveCommodityCurveThe basis curve.
contextEvaluationContextThe evaluation context, whose unit conversions give the basis price's factor.
Returns
- CommodityCurve
A copy adding the basis curve's prices; this curve is unchanged.
Exceptions
- ArgumentNullException
An argument is null.
- NanoQLException
No conversion between the curves' units is held.
SetPrices(IReadOnlyDictionary<Date, double>)
The curve with new prices: QuantLib's setPrices, the dates in order, timed from the reference
date.
public CommodityCurve SetPrices(IReadOnlyDictionary<Date, double> prices)
Parameters
pricesIReadOnlyDictionary<Date, double>The prices by date; at least two.
Returns
- CommodityCurve
A copy with the prices, keeping the basis curve; this curve is unchanged.
Exceptions
- ArgumentNullException
pricesis null.- NanoQLException
Fewer than two prices ("too few prices").
ToString()
The curve as QuantLib's operator<< writes it: "[name] (USD/BBL)", and "; basis to (" with the basis
curve's text and ")" where there is one.
public override string ToString()
Returns
- string
The text.
UnderlyingPriceDate(Date, ImmutableSortedDictionary<Date, ExchangeContract>?, int)
The date a nearby curve prices a date at: the underlying start date of the contract the offset counts to among those on or after the date; the date itself when none is on or after it.
public Date UnderlyingPriceDate(Date date, ImmutableSortedDictionary<Date, ExchangeContract>? exchangeContracts, int nearbyOffset)
Parameters
dateDateThe date.
exchangeContractsImmutableSortedDictionary<Date, ExchangeContract>The exchange contracts by date.
nearbyOffsetintThe nearby contract, from 1.
Returns
- Date
The date to price at.
Exceptions
- ArgumentNullException
exchangeContractsis null.- NanoQLException
The offset isn't positive, or not enough contracts follow the date.