Skip to content

Commit

Permalink
add delta discounting
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdomrom committed May 29, 2024
1 parent d43ec45 commit 759e962
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/math/Black76.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ library Black76 {
{
unchecked {
uint tAnnualised = annualise(b76Input.timeToExpirySec);
// products of <128 bit numbers, cannot overflow here when caseted to 256
// products of <128 bit numbers, cannot overflow here when cast to 256
uint totalVol = uint(b76Input.volatility) * uint(FixedPointMathLib.sqrt(tAnnualised)) / 1e18;
uint fwd = uint(b76Input.fwdPrice);
uint fwdDiscounted = fwd * uint(b76Input.discount) / 1e18;
if (b76Input.strikePrice == 0) {
return (fwdDiscounted, uint(0), 1e18);
return (fwdDiscounted, uint(0), uint(b76Input.discount));
}

uint strikeDiscounted = uint(b76Input.strikePrice) * uint(b76Input.discount) / 1e18;
Expand All @@ -92,6 +92,7 @@ library Black76 {
// putPrice * fwdDiscounted takes up at most (128 + 59 + 64 - 59) < 256 bits
callPrice = callPrice * fwdDiscounted / 1e18;
putPrice = putPrice * fwdDiscounted / 1e18;
callDelta = callDelta * uint(b76Input.discount) / 1e18;

// cap the theo prices to resolve any potential rounding errors with super small/big spots/strikes
callPrice = callPrice > fwdDiscounted ? fwdDiscounted : callPrice;
Expand All @@ -103,7 +104,7 @@ library Black76 {
(callPrice, putPrice,) = pricesAndDelta(b76Input);
}

function callDelta(Black76Inputs memory b76Input) public pure returns (uint callDelta) {
function getCallDelta(Black76Inputs memory b76Input) public pure returns (uint callDelta) {
(,, callDelta) = pricesAndDelta(b76Input);
}

Expand Down Expand Up @@ -164,10 +165,10 @@ library Black76 {
function _standardPrices(uint moneyness, uint totalVol)
internal
pure
returns (uint stdCallPrice, uint stdPutPrice, uint callDelta)
returns (uint stdCallPrice, uint stdPutPrice, uint stdCallDelta)
{
unchecked {
(stdCallPrice, callDelta) = _standardCall(moneyness, totalVol);
(stdCallPrice, stdCallDelta) = _standardCall(moneyness, totalVol);
stdPutPrice = _standardPutFromCall(moneyness, stdCallPrice);
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/math/Black76.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract Black76Tester {
}

function callDelta(Black76.Black76Inputs memory b76Input) external pure returns (uint) {
return b76Input.callDelta();
return b76Input.getCallDelta();
}
}

Expand Down Expand Up @@ -140,16 +140,16 @@ contract Black76Test is Test {
assert(b76TestInputs.length == benchmarkResults.length);

for (uint i = 0; i < b76TestInputs.length; i++) {
uint delta = b76TestInputs[i].callDelta();
uint delta = b76TestInputs[i].getCallDelta();
(uint call, uint put) = b76TestInputs[i].prices();

assertApproxEqAbs(delta, deltaBenchmarkResults[i], accuracy);
assertApproxEqAbs(delta, deltaBenchmarkResults[i] * uint(b76TestInputs[i].discount) / 1e18, accuracy);
assertApproxEqAbs(int(call), benchmarkResults[i][0], accuracy);
assertApproxEqAbs(int(put), benchmarkResults[i][1], accuracy);

(call, put, delta) = b76TestInputs[i].pricesAndDelta();

assertApproxEqAbs(delta, deltaBenchmarkResults[i], accuracy);
assertApproxEqAbs(delta, deltaBenchmarkResults[i] * uint(b76TestInputs[i].discount) / 1e18, accuracy);
assertApproxEqAbs(int(call), benchmarkResults[i][0], accuracy);
assertApproxEqAbs(int(put), benchmarkResults[i][1], accuracy);
}
Expand Down

0 comments on commit 759e962

Please sign in to comment.