> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Date conversion

> Guide to date and duration conversion in Atoti Market Risk, covering the maturity converter, tenor converter, day count conventions, and how to override the business day calendar factory that drives day-to-day shifts

Atoti Market Risk relies on the date conversion services from the Common Library to convert pillar values into durations. The business day calendar factory supplies the calendar that the `DAY`, `EOM`, `EOQ`, `EOY` and `WDA` members use to pick a date: the comparison date of day-to-day measures, or the market shift date of Taylor VaR.

## Maturity converter

The maturity converter service converts any pillar value into a duration, expressed in number of days, year fraction or a date relative to the current date. The interface of the bean is `IMaturityConverter`. It can be injected into any Atoti plugin that extends the `IMaturityConverterAware` interface.

The interface provides the following services:

| Method | Description |
| - | - |
| `toYearFraction` | Converts a pillar into a number of years. |
| `toFraction` | Converts a pillar into a number of days. |
| `toDate` | Converts a pillar into a future date relative to the as-of date. |

Here are the parameters needed to make a conversion:

| Parameter | Type | Example | Meaning |
| - | - | - | - |
| pivot | IActivePivot | The current cube | Used to pass the current cube to the service. |
| databaseVersion | IDatabaseVersion | The current database | Used to pass the current database to the service. |
| asOfDate | LocalDate | 2023-02-27 | The current date used to find the right duration. |
| bucketType | BucketType | TENOR\_INPUT | The kind of pillar, used to differentiate the tenor for the (underlying) maturity. |
| tenorSet | String | "Simple" | Used to select a specific set of tenors. |
| maturity | Object | 6M | The pillar to convert. |
| leafCoordinates | List\<Object> | \[] | Additional data to help the conversion. |

The default implementation provided is `SimpleMaturityConverter`.

## Tenor converter

The tenor converter transforms a pillar and an as-of date into the target date. The default implementation `SimpleTenorConverter` handles several pillar types:

| Type | Containing | Explanation | Examples |
| - | - | - | - |
| LocalDate | Date | The tenor is a fixed date. | 2035-03-12 |
| String | Overnight | One business day duration. | "ON", "O/N" |
| String | Two nights | Two business days duration. | "TN", "T/N" |
| Double | Years | A year fraction. | 1.5 |
| String | A duration | A duration. | "3D", "6M", "2W", "1Y6M" |
| String | IMM date | 3rd Wednesday of the month. | "MAR24", "SEP-2024" |
| String | ISO date | ISO 8601 date. | "2035-03-12" |

## Day count convention

The `IDayCountConvention` converts a pair of dates into a duration expressed in days or years. Several implementations are provided:

| Implementation | Rule | Description |
| - | - | - |
| `DayCount30on360ISDA` | A001 | All months have 30 days, year has 360 days. |
| `DayCountA360` | A004 | Exact days, year is 360 days. |
| `DayCountA365Fixed` | A005 | Exact days, year is 365 days. |
| `DayCountActualActualISDA` | A008 | Exact days and years (366 or 365 depending on current year). |
| `DayCountActualActual` | A009 | Exact days and years (366 or 365 depending on last year). |

## Business day calendar and convention

The `IBusinessDayCalendar` tells if a day is open or closed. The `IBusinessDayConvention` finds the matching business day from a given date.

The Common Library provides:

* `TargetBusinessDayConvention`: an `IBusinessDayCalendar`, despite its name, based on the [TARGET2 settlement system calendar](https://www.ecb.europa.eu/paym/target/target2/html/index.en.html). It treats weekends and TARGET2 holidays, including Good Friday and Easter Monday, as closed. Its nested `TargetBusinessDayConvention.Factory` implements `IBusinessDayCalendarFactory` and is a ready-made alternative to the default factory.
* `NextBusinessDayConvention`: returns the first open day on or after the date.

### What is the business day calendar factory?

The `IBusinessDayCalendarFactory` interface builds the `IBusinessDayCalendar` consumed by day-to-day shift calculations. `IBusinessDayCalendar` exposes a single method, `isBusinessDay(LocalDate date)`, which reports whether that date is open.

These calculations pick the comparison date for day-to-day measures in the VaR-ES, VaR-ES Summary and Sensitivity cubes. Examples include VaR DtD and Taylor VaR DtD. The shift moves along the [DayToDay](https://docs.activeviam.com/solutions/market-risk/6.0/cube/dimensions/dates.md) analysis hierarchy relative to `AsOfDate`.

A day-to-day measure subtracts the value at the shifted date from the value at `AsOfDate`. `UnfilteredDateShiftPostProcessor` resolves the shifted value with the DayToDay and date filters removed. A shift to a date with no loaded data therefore returns an empty result instead of an error.

`UnfilteredDateShiftPostProcessor` and the `IBusinessDayCalendar`/`IBusinessDayCalendarFactory` interfaces come from the Common Library. The default factory and its qualifier belong to Atoti Market Risk.

The factory bean is qualified with `SP_QUALIFIER__BUSINESS_DAY_CALENDAR_FACTORY` and defined in `MaturityConverterConfig`. This factory does not affect maturity or tenor conversion: the maturity converter is built without a calendar factory.

### Why does the default factory treat every day as a business day?

The default implementation, `PassThroughBusinessDayCalendarFactory`, categorizes every day as a business day.

Business day and holiday conventions differ by region and by client. Atoti Market Risk therefore ships no holiday calendar by default (see [How to override the business day calendar factory](#how-to-override-the-business-day-calendar-factory) for alternatives). To migrate from a version that used `TargetBusinessDayConvention.Factory` by default, see [Updated business calendar logic](https://docs.activeviam.com/solutions/market-risk/6.0/dev/dev-release/migrate.md#updated-business-calendar-logic).

### How do day-to-day measures find the previous date?

Day-to-day measures compare the selected `AsOfDate` with the date picked by the [DayToDay](https://docs.activeviam.com/solutions/market-risk/6.0/cube/dimensions/dates.md) member. Members are set in `mr.cubes.levels.day-to-day-members`, each in the format `Label=<KEY><offset>`. The offset is an integer, signed when non-zero, for example `Yesterday=DAY-1`.

`DAY` and the other calendar-based rules count business days through the business day calendar. `CUB` instead steps through the dates loaded in the cube and ignores the calendar.

| Rule | Moves to | Date source |
| - | - | - |
| `DAY-n` / `DAY+n` | n business days before / after (`DAY0`: the same date) | Business day calendar |
| `EOM-n`, `EOQ-n`, `EOY-n` | Last business day of the month / quarter / year, n periods back | Business day calendar |
| `WDA<d>` | Weekday `d` (1 for Monday through 7 for Sunday) on or before the date. If it is closed: the closest open day earlier in that week, otherwise the first open day after it, up to the date. If there is none, the rule starts again from the Sunday before the date. | Business day calendar |
| `CUB+n` / `CUB-n` | n dates earlier / later among the dates loaded in the cube | Loaded cube dates |

`DAY-1` and `CUB+1` both point to an earlier date. `CUB` moves through the `AsOfDate` members in hierarchy order, and `AsOfDate` is sorted newest first. `CUB+1` is therefore the previous loaded date, and `CUB-1` is the next one. If the selected `AsOfDate` is not loaded in the cube, the shift returns no result. `CUB+1` skips dates with no data anywhere in the cube. A partial load, such as one missing book, is not skipped.

With the default calendar every day is a business day. `Yesterday=DAY-1` on a Monday points to Sunday. `End Of Month=EOM-1` can equally point to a weekend day, for example 31 August 2025. If no data is loaded that day, the result is empty. The `Previous` member (`CUB+1`) fixes the Monday case but not the end-of-period members. Only a custom factory makes `EOM-1` skip weekends. Taylor VaR's default `YESTERDAY=DAY-1` has the same Monday issue.

To compare Monday with Friday, either:

* Select the `Previous` member (`CUB+1`) for that query.
* Make `Previous` the default by listing it first in `mr.cubes.levels.day-to-day-members`.
* Replace the business day calendar factory (see [How to override the business day calendar factory](#how-to-override-the-business-day-calendar-factory)).

> [!WARNING]
>
> Setting `mr.cubes.levels.day-to-day-members` replaces the whole default list. A configuration that lists only `Previous` loses `Yesterday`, `Day-2`, `Tomorrow`, and the end-of-period members.

The following configuration keeps every default member and makes `Previous` the default:

```yaml theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
mr:
  cubes:
    levels:
      day-to-day-members:
        - Previous=CUB+1
        - Yesterday=DAY-1
        - Day-2=DAY-2
        - Tomorrow=DAY+1
        - End Of Month=EOM-1
        - End Of Quarter=EOQ-1
        - End Of Year=EOY-1
```

### How to override the business day calendar factory

The `Previous` member (`CUB+1`) avoids most weekend and holiday gaps. End-of-period members, such as `EOM-1`, can still land on a day with no data. Replace the default factory when day-to-day shifts must follow a strict business day calendar. With a strict calendar, a business day with missing data shows an empty result instead of falling back to an earlier date.

> [!WARNING]
>
> The business day calendar factory also resolves the `MarketShiftDate` members used by Taylor VaR (`mr.taylor.market-shift-date-specific`). A custom factory changes the dates picked by both day-to-day measures and Taylor VaR.

Publish a `@Primary` bean under the `SP_QUALIFIER__BUSINESS_DAY_CALENDAR_FACTORY` qualifier, in a `@Configuration` class the application loads. Name the bean method differently from `businessDayCalendarFactory`, the default bean's name in `MaturityConverterConfig`, or startup fails with `BeanDefinitionOverrideException`. `CustomBusinessDayCalendarFactory` below is a placeholder name for the custom `IBusinessDayCalendarFactory` implementation.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
import static com.activeviam.mr.common.constants.SpringConstants.SP_QUALIFIER__BUSINESS_DAY_CALENDAR_FACTORY;

import com.activeviam.accelerator.common.dates.IBusinessDayCalendarFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class CustomBusinessDayCalendarFactoryConfig {

    @Bean
    @Primary
    @Qualifier(SP_QUALIFIER__BUSINESS_DAY_CALENDAR_FACTORY)
    public IBusinessDayCalendarFactory customBusinessDayCalendarFactory() {
        return new CustomBusinessDayCalendarFactory();
    }
}
```

Replace it with an implementation of `IBusinessDayCalendarFactory`. Its only abstract method is:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
IBusinessDayCalendar getBusinessDayCalendar(IActivePivot pivot, IDatabaseVersion databaseVersion, List<Object> leafCoordinates)
```

`databaseVersion` is `null` on a query cube, such as `MRCombinedCube` or the query node of a distributed deployment, so a custom factory that reads it must handle that case.

`leafCoordinates` can be empty. Taylor VaR always passes an empty list when it resolves `MarketShiftDate`, since it never calls the factory's leaf-coordinates hook. A factory that picks a calendar per book or location can fill `leafCoordinates` for day-to-day measures. It does so through `updateLeafCoordinatesFunction`, a default method of `ILeafCoordinatesService` that `IBusinessDayCalendarFactory` extends. `UnfilteredDateShiftPostProcessor` calls it once, when the post-processor initializes. For each location it expands, it applies the returned function and passes the result to `getBusinessDayCalendar` as `leafCoordinates`. A factory used for both must still return a sensible calendar when `leafCoordinates` is empty.

The following minimal factory treats weekends as closed and every other day as open. It follows the same shape as `PassThroughBusinessDayCalendarFactory`:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
import java.time.DayOfWeek;
import java.util.List;

import com.activeviam.accelerator.common.dates.IBusinessDayCalendar;
import com.activeviam.accelerator.common.dates.IBusinessDayCalendarFactory;
import com.activeviam.activepivot.core.intf.api.cube.IActivePivot;
import com.activeviam.database.api.IDatabaseVersion;

public class WeekendBusinessDayCalendarFactory implements IBusinessDayCalendarFactory {

    private final IBusinessDayCalendar businessDayCalendar =
            date -> date.getDayOfWeek() != DayOfWeek.SATURDAY && date.getDayOfWeek() != DayOfWeek.SUNDAY;

    @Override
    public IBusinessDayCalendar getBusinessDayCalendar(
            IActivePivot pivot, IDatabaseVersion databaseVersion, List<Object> leafCoordinates) {
        return businessDayCalendar;
    }
}
```

The Common Library's `TargetBusinessDayConvention.Factory` is a ready-made alternative that skips weekends and TARGET2 holidays. It replaces the bean method in `CustomBusinessDayCalendarFactoryConfig`:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
import com.activeviam.accelerator.common.dates.impl.TargetBusinessDayConvention;

@Bean
@Primary
@Qualifier(SP_QUALIFIER__BUSINESS_DAY_CALENDAR_FACTORY)
public IBusinessDayCalendarFactory targetBusinessDayCalendarFactory() {
    return new TargetBusinessDayConvention.Factory();
}
```
