Skip to content

Commit

Permalink
enable query
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbean committed Jun 30, 2024
1 parent 4a1272e commit 31ac79d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
6 changes: 4 additions & 2 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Changelog for Hastructure

## 0.28.11
## 0.28.13
### 2024-06-30
* NEW: new assumption `issue bond` which allow funding by issuing new bonds during cashflow projection.
* FIX: `formula` will return `inf` if a `divide` with zero instead of just throw exception
* NEW: new asset class `projectScheduleFlow` which can be divided projected cashflow with fix portion and float portions. The interest from the float portion will be affected by interest rate assumption.
* ENHANCE: enable formula `bondRate`/`bondWaRate` on `bondGroup`
* FIX: `formula` will return `inf` if a `divide` with zero instead of just throw exception
* FIX: `financial reports` was failing because it can't access to `interest due` on bond group.
* FIX: enable formula query on `bond groups`


## 0.28.8
### 2024-06
* FIX: `limit` on `payFee` was not working with `duePct`
Expand Down
4 changes: 3 additions & 1 deletion src/Deal/DealAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ calcDueInt t calc_date mBal mRate b@(L.Bond bn bt bo bi _ bond_bal bond_rate _ i


calcDuePrin :: Ast.Asset a => TestDeal a -> T.Day -> L.Bond -> L.Bond
calcDuePrin t calc_date b@(L.BondGroup bMap) = L.BondGroup $ Map.map (calcDuePrin t calc_date) bMap
calcDuePrin t calc_date b@(L.Bond _ L.Sequential _ _ _ bondBal _ _ _ _ _ _ _ _)
= b {L.bndDuePrin = bondBal }

Expand Down Expand Up @@ -393,6 +394,7 @@ priceAssetUnion (ACM.LO m) d pm aps = Ast.priceAsset m d pm aps
priceAssetUnion (ACM.IL m) d pm aps = Ast.priceAsset m d pm aps
priceAssetUnion (ACM.LS m) d pm aps = Ast.priceAsset m d pm aps
priceAssetUnion (ACM.RE m) d pm aps = Ast.priceAsset m d pm aps
priceAssetUnion (ACM.PF m) d pm aps = Ast.priceAsset m d pm aps

priceAssetUnionList :: [ACM.AssetUnion] -> Date -> PricingMethod -> AP.ApplyAssumptionType -> Maybe [RateAssumption] -> [PriceResult]
priceAssetUnionList assetList d pm (AP.PoolLevel assetPerf) mRates
Expand Down Expand Up @@ -784,7 +786,7 @@ performAction d t@TestDeal{bonds=bndMap, accounts=accMap, liqProvider=liqMap} (W
Just (DueCapAmt amt) -> min amt (accBal + supportAvail)
_ -> error $ "Not support for limit when pay int by seq" ++ show mLimit
bndsList = (Map.!) bndMap <$> bnds
dueAmts = L.bndDueIntOverInt<$> bndsList
dueAmts = L.bndDueIntOverInt <$> bndsList
actualPaids = paySeqLiabilitiesAmt amtAvailable dueAmts
-- update bond paid
bondsPaid = uncurry (L.payInt d) <$> zip actualPaids bndsList
Expand Down
20 changes: 17 additions & 3 deletions src/Deal/DealQuery.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ patchDateToStats d t
_ -> t -- `debug` ("Failed to patch date to stats"++show t)



queryDealRate :: P.Asset a => TestDeal a -> DealStats -> Micro
queryDealRate t s =
fromRational $
Expand Down Expand Up @@ -125,14 +126,27 @@ queryDealRate t s =
cumuPoolDefBal / originPoolBal -- `debug` (show idx ++" cumulative p def rate"++show cumuPoolDefBal++">>"++show originPoolBal)


BondRate bn -> toRational $ L.bndRate $ bonds t Map.! bn
BondRate bn -> case Map.lookup bn (bonds t) of
Just b@(L.Bond {}) -> toRational $ L.bndRate b
Just b@(L.BondGroup bSubMap) ->
let
bnds = Map.elems bSubMap
rates = toRational <$> L.bndRate <$> bnds
bals = L.getCurBalance <$> bnds
in
weightedBy bals rates
Nothing ->
case viewDealBondsByNames t [bn] of
[b] -> toRational $ L.bndRate b
_ -> error ("Failed to find bond by name"++bn)

BondWaRate bns ->
let
rs = toRational <$> (\bn -> queryDealRate t (BondRate bn)) <$> bns
ws = toRational <$> (\bn -> queryDeal t (CurrentBondBalanceOf [bn])) <$> bns
ws = (\bn -> queryDeal t (CurrentBondBalanceOf [bn])) <$> bns
in
toRational $ sum (zipWith (+) ws rs) / sum ws
-- toRational $ safeDivide $ sum (zipWith (*) ws rs) $ sum ws
weightedBy ws rs

PoolWaRate mPns ->
let
Expand Down
3 changes: 2 additions & 1 deletion src/Liability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module Liability
,priceBond,PriceResult(..),pv,InterestInfo(..),RateReset(..)
,weightAverageBalance,calcZspread,payYield,scaleBond,totalDueInt
,buildRateResetDates,isAdjustble,StepUp(..),isStepUp,getDayCountFromInfo
,calcWalBond,patchBondFactor,fundWith,writeOff,InterestOverInterestType(..))
,calcWalBond,patchBondFactor,fundWith,writeOff,InterestOverInterestType(..)
,getCurBalance)
where

import Language.Haskell.TH
Expand Down
5 changes: 5 additions & 0 deletions src/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Util
,maximum',minimum',roundingBy,roundingByM
,floorWith,slice,toPeriodRateByInterval, dropLastN
,lastOf,findBox
,safeDivide
-- for debug
,zyj
)
Expand All @@ -27,6 +28,7 @@ import Lib
import Types
import DateUtil

import Numeric.Limits (infinity)
import Text.Printf
import Control.Exception

Expand Down Expand Up @@ -305,6 +307,9 @@ findBox (Exc,Exc) x ((l,h):xs)
| otherwise = findBox (Exc,Exc) x xs


safeDivide :: RealFloat a => a -> a -> a
safeDivide _ 0 = infinity
safeDivide x y = x / y


----- DEBUG/PRINT
Expand Down
2 changes: 1 addition & 1 deletion swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -17098,7 +17098,7 @@
"name": "BSD 3"
},
"title": "Hastructure API",
"version": "0.28.11"
"version": "0.28.12"
},
"openapi": "3.0.0",
"paths": {
Expand Down

0 comments on commit 31ac79d

Please sign in to comment.