Skip to content

Commit

Permalink
expose formula in rateSwap
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbean committed Jan 2, 2025
1 parent c10a457 commit b4e6188
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 15 deletions.
18 changes: 14 additions & 4 deletions src/Deal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ applyFloatRate2 (IR.Floater _ idx spd _r _ mFloor mCap mRounding) d ras
rateAtDate <- AP.lookupRate0 ras idx d
return $ flooring mFloor $ capping mCap $ rateAtDate + spd

updateRateSwapRate :: Maybe [RateAssumption] -> Date -> HE.RateSwap -> Either String HE.RateSwap
updateRateSwapRate Nothing _ _ = Left "Failed to update rate swap: No rate input assumption"
updateRateSwapRate (Just rAssumps) d rs@HE.RateSwap{ HE.rsType = rt }
updateRateSwapRate :: Ast.Asset a => TestDeal a -> Maybe [RateAssumption] -> Date -> HE.RateSwap -> Either String HE.RateSwap
updateRateSwapRate t Nothing _ _ = Left "Failed to update rate swap: No rate input assumption"
updateRateSwapRate t (Just rAssumps) d rs@HE.RateSwap{ HE.rsType = rt }
= let
getRate x = AP.lookupRate rAssumps x d
in
Expand All @@ -268,6 +268,16 @@ updateRateSwapRate (Just rAssumps) d rs@HE.RateSwap{ HE.rsType = rt }
do
_r <- getRate flter
return (r, _r)
HE.FormulaToFloating ds flter ->
do
_r <- queryCompound t d (patchDateToStats d ds)
r <- getRate flter
return (fromRational _r, r)
HE.FloatingToFormula flter ds ->
do
r <- getRate flter
_r <- queryCompound t d (patchDateToStats d ds)
return (r, fromRational _r)
return rs {HE.rsPayingRate = pRate, HE.rsReceivingRate = rRate }

updateRateSwapBal :: Ast.Asset a => TestDeal a -> Date -> HE.RateSwap -> Either String HE.RateSwap
Expand Down Expand Up @@ -560,7 +570,7 @@ run t@TestDeal{accounts=accMap,fees=feeMap,triggers=mTrgMap,bonds=bndMap,status=
Nothing -> Left $ " No rate swaps modeled when looking for "++ sn
Just rSwap ->
do
newRateSwap_rate <- adjustM (updateRateSwapRate rates d) sn rSwap
newRateSwap_rate <- adjustM (updateRateSwapRate t rates d) sn rSwap
newRateSwap_bal <- adjustM (updateRateSwapBal t d) sn newRateSwap_rate
let newRateSwap_acc = Map.adjust (HE.accrueIRS d) sn $ newRateSwap_bal
run (t{rateSwap = Just newRateSwap_acc}) poolFlowMap (Just ads) rates calls rAssump log
Expand Down
5 changes: 3 additions & 2 deletions src/Deal/DealAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,14 @@ calcDueInt t d mBal mRate b@(L.Bond bn bt bo (L.WithIoI intInfo ioiIntInfo) _ bo
newBondWithIntInfo <- calcDueInt t d mBal mRate newBond
return newBondWithIntInfo { L.bndInterestInfo = L.WithIoI intInfo ioiIntInfo}



-- TODO: to enable override rate & balance
-- accure interest by rate
calcDueInt t d mBal mRate b@(L.MultiIntBond {})
= Right $ L.accrueInt d b

calcDueInt t d _ _ b@(L.Bond {})
= Right $ L.accrueInt d b

calcDueInt t d mBal mRate b = error $ "Not implemented for calcDueInt for bond type" ++ show b


Expand Down
15 changes: 12 additions & 3 deletions src/Deal/DealQuery.hs
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,23 @@ queryCompound t@TestDeal{accounts=accMap, bonds=bndMap, ledgers=ledgersM, fees=f
rs <- sequenceA $ (\bn -> queryCompound t d (BondRate bn)) <$> bns
ws <- sequenceA $ (\bn -> queryCompound t d (CurrentBondBalanceOf [bn])) <$> bns
return $ weightedBy (fromRational <$> ws) rs
PoolWaRate mPns ->

PoolWaRate Nothing ->
let
latestCfs = filter isJust $ Map.elems $ getLatestCollectFrame t mPns
rates = toRational . maybe 0.0 CF.mflowRate <$> latestCfs
latestCfs = filter isJust $ Map.elems $ getLatestCollectFrame t Nothing
rates = toRational . maybe 0.0 CF.mflowRate <$> latestCfs
bals = maybe 0.0 CF.mflowBalance <$> latestCfs
in
Right $ weightedBy (toRational <$> bals) rates

PoolWaRate (Just pName) ->
let
latestCfs = filter isJust $ Map.elems $ getLatestCollectFrame t (Just [pName])
rates = toRational . maybe 0.0 CF.mflowRate <$> latestCfs
in
Right $ sum rates


-- int query
FutureCurrentPoolBorrowerNum _d mPns ->
let
Expand Down
2 changes: 2 additions & 0 deletions src/Hedge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ data RateSwapBase = Fixed Balance -- ^ a fixed balance as notional base
data RateSwapType = FloatingToFloating Floater Floater -- ^ Paying Floating rate and receiving Floating Rate
| FloatingToFixed Floater IRate -- ^ Paying Floating Rate and receiving Fixed Rate
| FixedToFloating IRate Floater -- ^ Paying Fixed Rate and receiving Floating rate
| FormulaToFloating DealStats Floater -- ^ Paying Formula Rate and receiving Floating rate
| FloatingToFormula Floater DealStats -- ^ Paying Floating Rate and receiving Formula rate
deriving(Show,Generic,Eq,Ord)

data RateSwap = RateSwap {rsType :: RateSwapType -- ^ swap type
Expand Down
2 changes: 1 addition & 1 deletion src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ data DealStats = CurrentBondBalance
| HasPassedMaturity [BondName]
| TriggersStatus DealCycle String
-- rate type
| PoolWaRate (Maybe [PoolId])
| PoolWaRate (Maybe PoolId)
| BondRate BondName
| CumulativeNetLossRatio (Maybe [PoolId])
| FutureCurrentBondFactor Date
Expand Down
87 changes: 82 additions & 5 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -8674,10 +8674,7 @@
{
"properties": {
"contents": {
"items": {
"$ref": "#/components/schemas/PoolId"
},
"type": "array"
"$ref": "#/components/schemas/PoolId"
},
"tag": {
"enum": [
Expand Down Expand Up @@ -14780,6 +14777,86 @@
],
"title": "FixedToFloating",
"type": "object"
},
{
"properties": {
"contents": {
"items": [
{
"$ref": "#/components/schemas/DealStats"
},
{
"items": [
{
"$ref": "#/components/schemas/Index"
},
{
"multipleOf": 1.0e-6,
"type": "number"
}
],
"maxItems": 2,
"minItems": 2,
"type": "array"
}
],
"maxItems": 2,
"minItems": 2,
"type": "array"
},
"tag": {
"enum": [
"FormulaToFloating"
],
"type": "string"
}
},
"required": [
"tag",
"contents"
],
"title": "FormulaToFloating",
"type": "object"
},
{
"properties": {
"contents": {
"items": [
{
"items": [
{
"$ref": "#/components/schemas/Index"
},
{
"multipleOf": 1.0e-6,
"type": "number"
}
],
"maxItems": 2,
"minItems": 2,
"type": "array"
},
{
"$ref": "#/components/schemas/DealStats"
}
],
"maxItems": 2,
"minItems": 2,
"type": "array"
},
"tag": {
"enum": [
"FloatingToFormula"
],
"type": "string"
}
},
"required": [
"tag",
"contents"
],
"title": "FloatingToFormula",
"type": "object"
}
]
},
Expand Down Expand Up @@ -19882,7 +19959,7 @@
"name": "BSD 3"
},
"title": "Hastructure API",
"version": "0.40.13"
"version": "0.40.14"
},
"openapi": "3.0.0",
"paths": {
Expand Down

0 comments on commit b4e6188

Please sign in to comment.