UNPKG

@kaishen/quantlib

Version:
399 lines (358 loc) 10.7 kB
const {loadBinding} = require('@node-rs/helper'); /** * __dirname means load native addon from current dir * 'quantlib' is the name of native addon * the second arguments was decided by `napi.name` field in `package.json` * the third arguments was decided by `name` field in `package.json` * `loadBinding` helper will load `quantlib.[PLATFORM].node` from `__dirname` first * If failed to load addon, it will fallback to load from `@kaishen/quantlib-[PLATFORM]` */ const quantlib = loadBinding(__dirname, 'quantlib', '@kaishen/quantlib'); let DayCountDes; (function (DayCountDes) { DayCountDes[DayCountDes["ActualActual"] = 0] = "ActualActual"; DayCountDes[DayCountDes["Actual360"] = 1] = "Actual360"; DayCountDes[DayCountDes["Actual365"] = 2] = "Actual365"; DayCountDes[DayCountDes["Thirty360"] = 3] = "Thirty360"; DayCountDes[DayCountDes["ISMAThirty360"] = 4] = "ISMAThirty360"; DayCountDes[DayCountDes["ISMAThirty360NONEOM"] = 5] = "ISMAThirty360NONEOM"; })(DayCountDes || (DayCountDes = {})); let AssetType; (function (AssetType) { AssetType[AssetType["Bond"] = 0] = "Bond"; AssetType[AssetType["Future"] = 1] = "Future"; AssetType[AssetType["Deposit"] = 2] = "Deposit"; AssetType[AssetType["Fx"] = 3] = "Fx"; AssetType[AssetType["Repo"] = 4] = "Repo"; AssetType[AssetType["CDS"] = 5] = "CDS"; AssetType[AssetType["FUND"] = 6] = "FUND"; })(AssetType || (AssetType = {})); let ActionType; (function (ActionType) { ActionType[ActionType["Buy"] = 0] = "Buy"; ActionType[ActionType["Sell"] = 1] = "Sell"; ActionType[ActionType["ModifyRate"] = 2] = "ModifyRate"; ActionType[ActionType["ChangePrincipal"] = 3] = "ChangePrincipal"; ActionType[ActionType["RepoReSize"] = 4] = "RepoReSize"; ActionType[ActionType["RepoReRate"] = 5] = "RepoReRate"; })(ActionType || (ActionType = {})); let AssetActionType; (function (AssetActionType) { AssetActionType[AssetActionType["Called"] = 0] = "Called"; AssetActionType[AssetActionType["Sinking"] = 1] = "Sinking"; AssetActionType[AssetActionType["Pay"] = 2] = "Pay"; AssetActionType[AssetActionType["ChangeCPN"] = 3] = "ChangeCPN"; AssetActionType[AssetActionType["Defaulted"] = 4] = "Defaulted"; AssetActionType[AssetActionType["CancelDefaulted"] = 5] = "CancelDefaulted"; AssetActionType[AssetActionType["FlatTRADING"] = 6] = "FlatTRADING"; AssetActionType[AssetActionType["CancelFlatTRADING"] = 7] = "CancelFlatTRADING"; })(AssetActionType || (AssetActionType = {})); let CashType; (function (CashType) { CashType[CashType["Trade"] = 0] = "Trade"; CashType[CashType["Interest"] = 1] = "Interest"; CashType[CashType["Called"] = 2] = "Called"; CashType[CashType["Sinkable"] = 3] = "Sinkable"; CashType[CashType["Commission"] = 4] = "Commission"; })(CashType || (CashType = {})); let CalcType; (function (CalcType) { CalcType[CalcType["Normal"] = 0] = "Normal"; CalcType[CalcType["AustraliaExDiv"] = 23] = "AustraliaExDiv"; })(CalcType || (CalcType = {})); let FxType; (function (FxType) { FxType[FxType["Spot"] = 0] = "Spot"; FxType[FxType["Forward"] = 1] = "Forward"; })(FxType || (FxType = {})); let Currency; (function (Currency) { Currency[Currency["Default"] = 99] = "Default"; Currency[Currency["HKD"] = 0] = "HKD"; Currency[Currency["USD"] = 1] = "USD"; Currency[Currency["AUD"] = 2] = "AUD"; Currency[Currency["EUR"] = 3] = "EUR"; Currency[Currency["GBP"] = 4] = "GBP"; Currency[Currency["NZD"] = 5] = "NZD"; Currency[Currency["CHF"] = 6] = "CHF"; Currency[Currency["CNH"] = 7] = "CNH"; Currency[Currency["CNY"] = 8] = "CNY"; Currency[Currency["JPY"] = 9] = "JPY"; Currency[Currency["SGD"] = 10] = "SGD"; })(Currency || (Currency = {})); class Investments { } class Fx extends Investments { constructor( fx_type, trade_currency, target_currency, base_currency, trade_quantity, target_quantity, mid_vd, ) { super(); this._self_ = { helper: quantlib.fx_helper, valuation: quantlib.fx_valuation, cashflow: quantlib.fx_cashflow, }; this._self_.helper( fx_type, trade_currency, target_currency, base_currency, trade_quantity, target_quantity, mid_vd, ); } valuation() { return this._self_.valuation(); } cashflow(date, trades) { return this._self_.cashflow(date, trades._self_); } } class Fund extends Investments { constructor() { super(); this._self_ = { helper: quantlib.fund_helper, valuation: quantlib.fund_valuation, cashflow: quantlib.fund_cashflow, }; this._self_.helper() } valuation(date, price, trades) { return this._self_.valuation(date, price, trades._self_); } cashflow(date, trades) { return this._self_.cashflow(date, trades._self_); } } class Repo extends Investments { constructor(basis, quantity) { super(); this._self_ = { helper: quantlib.repo_helper, valuation: quantlib.repo_valuation, cashflow: quantlib.repo_cashflow, }; this._self_.helper( basis, quantity, ) } valuation(date, trades) { return this._self_.valuation(date, trades._self_); } cashflow(date, trades) { return this._self_.cashflow(date, trades._self_); } } class Deposit extends Investments { constructor(basis) { super(); this._self_ = { helper: quantlib.deposit_helper, valuation: quantlib.deposit_valuation, cashflow: quantlib.deposit_cashflow, }; this._self_.helper( basis, ) } valuation(date, trades) { return this._self_.valuation(date, trades._self_); } cashflow(date, trades) { return this._self_.cashflow(date, trades._self_); } } class Future extends Investments { constructor(commission) { super(); this._self_ = { helper: quantlib.future_helper, valuation: quantlib.future_valuation, cashflow: quantlib.future_cashflow, }; if (!commission) { commission = 3.5; } this._self_.helper( commission, ) } valuation(price, trades) { return this._self_.valuation(price, trades._self_); } cashflow(trades) { return this._self_.cashflow(trades._self_); } } class CDS extends Investments { constructor( start_date, frequency, basis, ) { super(); this._self_ = { helper: quantlib.cds_helper, valuation: quantlib.cds_valuation, cashflow: quantlib.cds_cashflow, }; this._self_.helper( start_date, frequency, basis, ) } valuation(date, price, trades) { return this._self_.valuation(date, price, trades._self_); } cashflow(date, trades) { return this._self_.cashflow(date, trades._self_); } } class Bond extends Investments { constructor( issue_date, interest_accrual_date, first_cpn_date, maturity, frequency, basis, cpn_rate, calc_type, ) { super(); this._self_ = { helper: quantlib.bond_helper, calendars: quantlib.bond_calendars, accrued: quantlib.bond_accrued, valuation: quantlib.bond_valuation, cashflow: quantlib.bond_cashflow, discount_rate_to_price: quantlib.bond_discount_rate_to_price, }; let self_calc_type = 0; if (calc_type) { self_calc_type = calc_type } this._self_.helper( issue_date, interest_accrual_date, first_cpn_date, maturity, frequency, basis, cpn_rate, self_calc_type, ) } calendars() { return this._self_.calendars(); } accrued(date, quantity) { return this._self_.accrued(date, quantity); } valuation(date, price, trades, actions) { let actions_obj = new Actions('temp', AssetType.Bond); if (actions) { actions_obj = actions; } return this._self_.valuation(date, price, trades._self_, actions_obj._self_); } cashflow(date, trades, actions, ex_dvd_days) { let actions_obj = new Actions('temp', AssetType.Bond); if (actions) { actions_obj = actions; } if (!ex_dvd_days) { ex_dvd_days = 0; } return this._self_.cashflow(date, trades._self_, actions_obj._self_, ex_dvd_days); } discount_rate_to_price(date, rate) { return this._self_.discount_rate_to_price(date, rate); } } class Actions { constructor( name, asset_type, ) { this._self_ = { helper: quantlib.actions_helper, add: quantlib.actions_add, }; this._self_.helper( name, asset_type, ) } add(action) { return this._self_.add(action); } } class Trades { constructor( name, asset_type, ) { this._self_ = { helper: quantlib.trades_helper, add: quantlib.trades_add, }; this._self_.helper( name, asset_type, ) } add(trade) { if (!trade.contract_size && trade.contract_size !== 0) { trade.contract_size = 1; } if (!trade.rate) { trade.rate = 1; } if (!trade.price) { trade.price = 100; } if (!trade.quantity) { trade.quantity = 0; } if (!trade.currency && trade.currency !== 0) { trade.currency = Currency.Default; } return this._self_.add(trade); } } const day_counter = (begin_date, target_date, basis) => { return quantlib.day_counter(begin_date, target_date, basis); } module.exports = { Utils: { day_counter, }, Bond, Future, Fx, Deposit, Repo, Trades, DayCountDes, AssetType, ActionType, Actions, AssetActionType, CashType, CalcType, FxType, Currency, CDS, Fund, }