# `@kaishen/quantlib`

![actions](https://github.com/cherishs001/quantlib/workflows/CI/badge.svg)

## Install this package

```
npm install @kaishen/quantlib --save
```

## Support matrix

|                 | node10 | node12 | node14 | node15 |
| --------------- | ------ | ------ | ------ | ------ |
| Windows x64     | ✓      | ✓      | ✓      | ✓      |
| Windows x32     | ✓      | ✓      | ✓      | ✓      |
| macOS x64       | ✓      | ✓      | ✓      | ✓      |
| macOS arm64     | ✓      | ✓      | ✓      | ✓      |
| Linux x64 gnu   | ✓      | ✓      | ✓      | ✓      |
| Linux x64 musl  | ✓      | ✓      | ✓      | ✓      |
| Android arm64   | ✓      | ✓      | ✓      | ✓      |

## Performance

```bash
派息日历 @ 债券 x 174,647 ops/sec ±5.55% (92 runs sampled)
应计利息 @ 债券 x 1,854,755 ops/sec ±0.93% (95 runs sampled)
Fastest is 应计利息 @ 债券
```

## Develop requirements

- Install latest `Rust`
- Install `NodeJS@10+` which supports `N-API`

## Test in local

- `npm install`
- `npm run build`
- `npm run test`

And you will see:

```bash
$ ava --tap | tap-nyan

 138 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__,------,
 0   -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__|  /\_/\
 0   -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_~|_( ^ .^)
     -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ ""  ""
  Pass!
```

## Usage

### Bond Calendars
```typescript
import {Bond, DayCountDes} from '@kaishen/quantlib';

const bond = new Bond(
    '2013-05-09',
    '2013-05-09',
    '2013-11-09',
    '2043-05-09',
    2,
    DayCountDes.ISMAThirty360,
    4.25,
);

const calendars = bond.calendars();
console.log(`calendars: ${calendars}`);
```

```bash
$ console
calendars: ["2013-05-09","2013-11-09","2014-05-09","2014-11-09","2015-05-09","2015-11-09","2016-05-09","2016-11-09","2017-05-09","2017-11-09","2018-05-09","2018-11-09","2019-05-09","2019-11-09","2020-05-09","2020-11-09","2021-05-09","2021-11-09","2022-05-09","2022-11-09","2023-05-09","2023-11-09","2024-05-09","2024-11-09","2025-05-09","2025-11-09","2026-05-09","2026-11-09","2027-05-09","2027-11-09","2028-05-09","2028-11-09","2029-05-09","2029-11-09","2030-05-09","2030-11-09","2031-05-09","2031-11-09","2032-05-09","2032-11-09","2033-05-09","2033-11-09","2034-05-09","2034-11-09","2035-05-09","2035-11-09","2036-05-09","2036-11-09","2037-05-09","2037-11-09","2038-05-09","2038-11-09","2039-05-09","2039-11-09","2040-05-09","2040-11-09","2041-05-09","2041-11-09","2042-05-09","2042-11-09","2043-05-09"]
```

### Bond Accrued
```typescript
import {Bond, DayCountDes} from '@kaishen/quantlib';

const bond = new Bond(
    '2013-05-09',
    '2013-05-09',
    '2013-11-09',
    '2043-05-09',
    2,
    DayCountDes.ISMAThirty360,
    4.25,
);

const accrued = bond.accrued('2021-05-07', 1000000);
console.log(`accrued: ${accrued}`);
```

```bash
$ console
accrued: 21013.888888888887
```

### Bond Gav
```typescript
import {Bond, Trade, AssetType, DayCountDes, ActionType} from '@kaishen/quantlib';

const trades = new Trades('Bond', AssetType.Bond);
trades.add({
    action: ActionType.Buy,
    quantity: 1000000,
    price: 101,
    trade_date: '2020-01-02',
    settle_date: '2020-01-03',
});

const bond = new Bond(
    '2013-05-09',
    '2013-05-09',
    '2013-11-09',
    '2043-05-09',
    2,
    DayCountDes.ISMAThirty360,
    4.25,
);

const gav = bond.gav('2021-05-07', 110, trades);
console.log(`gav: ${gav}`);
```

```bash
$ console
gav: 1121131.9444444445
```
