Skip to content

Commit

Permalink
Add Tracy profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
gituliar committed Feb 23, 2024
1 parent b8a572b commit b503f28
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
55 changes: 35 additions & 20 deletions src/Analytics/Model_BlackScholes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/target.hpp>

#include "tracy/Tracy.hpp"

namespace ql = QuantLib;

using namespace tasty;
Expand Down Expand Up @@ -49,6 +51,8 @@ Model_BlackScholes::calibrate(const Quote& quote, const RateCurve* rateCurve)
Error
Model_BlackScholes::calibrateAmerican(f64 v_, f64 s, f64 k, f64 dte, f64 r, f64 q, Parity w, f64& z)
{
ZoneScoped;

Error err;

f64 v = v_;
Expand All @@ -59,13 +63,13 @@ Model_BlackScholes::calibrateAmerican(f64 v_, f64 s, f64 k, f64 dte, f64 r, f64
/// Initial guess
///
if (err = calibrateEuropean(v_, s, k, dte, r, q, w, z); !err.empty())
goto error;
return "Model_BlackScholes::calibrateAmerican : " + err;

/// Newton-Raphson solver
///
while (n-- > 0 && !std::isnan(z)) {
if (err = priceAmerican(s, k, dte, z, r, q, w, v); !err.empty())
goto error;
return "Model_BlackScholes::calibrateAmerican : " + err;
if (std::isnan(v))
break;

Expand All @@ -85,15 +89,14 @@ Model_BlackScholes::calibrateAmerican(f64 v_, f64 s, f64 k, f64 dte, f64 r, f64
///
z = NaN;
return "";

error:
return "Model_BlackScholes::calibrateAmerican : " + err;
}


Error
Model_BlackScholes::calibrateEuropean(f64 v, f64 s, f64 k, f64 dte, f64 r, f64 q, Parity w, f64& z)
{
ZoneScoped;

f64 t = f64(dte) / kDaysInYear;
f64 k_ = k * exp(-t * r);

Expand All @@ -116,36 +119,48 @@ Model_BlackScholes::calibrateEuropean(f64 v, f64 s, f64 k, f64 dte, f64 r, f64 q
Error
Model_BlackScholes::priceAmerican(f64 s, f64 k, f64 dte, f64 z, f64 r, f64 q, Parity w, f64& v) const
{
auto w_ = (w == Parity_Call) ? ql::Option::Call : ql::Option::Put;
auto payoff = ql::ext::make_shared<ql::PlainVanillaPayoff>(w_, k);

// set up dates
/// Anchor + Maturity
///
auto anchor = ql::Date(31, ql::Jul, 1944);
auto dayCounter = ql::Actual365Fixed();
auto act365 = ql::Actual365Fixed();
auto maturity = anchor + std::ceil(dte);

ql::Settings::instance().evaluationDate() = anchor;

auto americanExercise = ql::ext::make_shared<ql::AmericanExercise>(anchor, maturity);


ql::Handle<ql::YieldTermStructure> flatTermStructure(ql::ext::make_shared<ql::FlatForward>(anchor, r, dayCounter));
/// Option Data
///
ql::Option::Type
w_ = (w == Parity_Call) ? ql::Option::Call : ql::Option::Put;

ql::Handle<ql::YieldTermStructure> flatDividendTS(ql::ext::make_shared<ql::FlatForward>(anchor, q, dayCounter));
ql::Handle<ql::YieldTermStructure>
r_(ql::ext::make_shared<ql::FlatForward>(anchor, r, act365));

ql::Handle<ql::Quote> underlyingH(ql::ext::make_shared<ql::SimpleQuote>(s));
ql::Handle<ql::YieldTermStructure>
q_(ql::ext::make_shared<ql::FlatForward>(anchor, q, act365));

ql::Handle<ql::BlackVolTermStructure> flatVolTS(
ql::ext::make_shared<ql::BlackConstantVol>(anchor, ql::TARGET(), std::abs(z), dayCounter));
ql::Handle<ql::Quote>
s_(ql::ext::make_shared<ql::SimpleQuote>(s));

auto bsmProcess = ql::ext::make_shared<ql::BlackScholesMertonProcess>(underlyingH, flatDividendTS, flatTermStructure, flatVolTS);
ql::Handle<ql::BlackVolTermStructure>
z_(ql::ext::make_shared<ql::BlackConstantVol>(anchor, ql::TARGET(), abs(z), act365));

auto engine = ql::ext::make_shared<ql::QdFpAmericanEngine>(bsmProcess, ql::QdFpAmericanEngine::fastScheme());
/// Black-Scholes Model
///
auto bsm = ql::ext::make_shared<ql::BlackScholesMertonProcess>(s_, q_, r_, z_);
auto engine = ql::ext::make_shared<ql::QdFpAmericanEngine>(
bsm, ql::QdFpAmericanEngine::fastScheme());

auto payoff = ql::ext::make_shared<ql::PlainVanillaPayoff>(w_, k);
auto americanExercise = ql::ext::make_shared<ql::AmericanExercise>(anchor, maturity);
ql::VanillaOption americanOption(payoff, americanExercise);

americanOption.setPricingEngine(engine);

/// Boundary-Interpolation Pricer
///
try {
ZoneScoped;

v = americanOption.NPV();
if (z < 0)
{
Expand Down
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ find_package(
find_package(
QuantLib CONFIG REQUIRED)

find_package(
Tracy CONFIG REQUIRED)
find_package(
Threads REQUIRED)



target_include_directories(
tastyhedge_lib
Expand All @@ -38,6 +44,7 @@ target_link_libraries(
tastyhedge_lib
PRIVATE
QuantLib::QuantLib
Tracy::TracyClient
)


Expand Down
6 changes: 6 additions & 0 deletions src/Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ using namespace std::literals;

namespace fs = std::filesystem;

/// ---- Tracy Profiler --------------------------------------------------------
///
/// Comment this line to ENABLE profiling
///
#undef TRACY_ENABLE


/// Common Types
/// ------------
Expand Down
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"dependencies": [
"docopt",
"gtest",
"tracy",
"quantlib"
],
"builtin-baseline": "716c3524a54f1f50a25d16a4cdd360f5a7fcc150"
Expand Down

0 comments on commit b503f28

Please sign in to comment.