Skip to content

Commit cb1e5d1

Browse files
committed
Add first draft of UI code for TARF calculator
1 parent 9c700a7 commit cb1e5d1

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

derivatives/target_redemption_forward.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ inline double findZeroNPVStrike(double notional,
8080
double spot,
8181
double sigma,
8282
const RatesCurve& foreign_rates,
83-
const RatesCurve& domestic_rates) {
83+
const RatesCurve& domestic_rates,
84+
size_t num_paths = 2000) {
8485
// TODO: ALSO then verify that the value of one is positive and the other is
8586
// negative.
8687
double atm_fwd = weightedAvgForward(spot,
@@ -108,7 +109,7 @@ inline double findZeroNPVStrike(double notional,
108109
direction);
109110

110111
double npv_mid =
111-
tarf_mid.price(spot, sigma, dt, 5000, foreign_rates, domestic_rates);
112+
tarf_mid.price(spot, sigma, dt, num_paths, foreign_rates, domestic_rates);
112113

113114
if (npv_mid > 0) {
114115
k_low = k_mid;

explorer/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cc_binary(
3030
":asset_visualiser",
3131
":global_rates",
3232
":rate_curve_visualiser",
33+
":tarf_visualiser",
3334
":vol_surface_factories",
3435
"//trees:propagators",
3536
"//volatility",
@@ -97,6 +98,17 @@ cc_library(
9798
],
9899
)
99100

101+
cc_library(
102+
name = "tarf_visualiser",
103+
hdrs = ["tarf_visualiser.h"],
104+
deps = [
105+
":explorer_params",
106+
":gui_widgets",
107+
"//derivatives:target_redemption_forward",
108+
"@imgui",
109+
],
110+
)
111+
100112
cc_library(
101113
name = "tree_render",
102114
hdrs = ["tree_render.h"],

explorer/explorer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "implot.h"
1010
#include "implot3d.h"
1111
#include "rate_curve_visualiser.h"
12+
#include "tarf_visualiser.h"
1213
#include "time/time_enums.h"
1314
#include "trees/propagators.h"
1415
#include "volatility/volatility.h"
@@ -103,6 +104,7 @@ int main(int, char**) {
103104
&global_currencies);
104105
smileexplorer::ExplorerParams localvol_prop_params(&global_rates,
105106
&global_currencies);
107+
smileexplorer::ExplorerParams tarf_params(&global_rates, &global_currencies);
106108

107109
while (!glfwWindowShouldClose(window)) {
108110
glfwPollEvents();
@@ -120,6 +122,8 @@ int main(int, char**) {
120122
ImVec2(display_size.x * 0.45, display_size.y * 0.95));
121123
smileexplorer::plotForwardRateCurves(crr_prop_params);
122124

125+
smileexplorer::plotTarfVisualiser(tarf_params);
126+
123127
ImGui::SetNextWindowPos(ImVec2(10, window_spacing));
124128
smileexplorer::displayPairedAssetDerivativePanel<
125129
smileexplorer::CRRPropagator,

explorer/tarf_visualiser.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifndef SMILEEXPLORER_EXPLORER_TARF_VISUALISER_H_
2+
#define SMILEEXPLORER_EXPLORER_TARF_VISUALISER_H_
3+
4+
#include <cmath>
5+
#include "derivatives/target_redemption_forward.h"
6+
#include "explorer/explorer_params.h"
7+
#include "explorer/gui_widgets.h"
8+
#include "imgui.h"
9+
10+
namespace smileexplorer {
11+
12+
inline void plotTarfVisualiser(ExplorerParams& params) {
13+
ImGui::Begin("TARF Calculator");
14+
15+
static float notional = 1.0;
16+
static float target = 6.0;
17+
static float volatility = 0.10;
18+
static int num_paths = 5000;
19+
static float end_date_years = 4.0;
20+
static float settlement_frequency = 0.25;
21+
22+
ImGui::SliderFloat("Notional (FOR)", &notional, 0.0f, 1e7f, "%.2f", ImGuiSliderFlags_Logarithmic);
23+
ImGui::SliderFloat("Target (DOM)", &target, 0.0f, 1e7f, "%.2f", ImGuiSliderFlags_Logarithmic);
24+
ImGui::SliderFloat("Volatility", &volatility, 0.0f, 1.0f, "%.2f");
25+
ImGui::SliderInt("MC Paths", &num_paths, 100, 50000);
26+
ImGui::SliderFloat("Maturity (years)", &end_date_years, 0.25f, 10.0f, "%.2f");
27+
end_date_years = std::max(0.25f, roundf(end_date_years / 0.25f) * 0.25f);
28+
ImGui::SliderFloat("Settlement Freq (years)", &settlement_frequency, 0.25f, 1.0f, "%.2f");
29+
settlement_frequency = std::max(0.25f, roundf(settlement_frequency / 0.25f) * 0.25f);
30+
31+
static size_t foreign_idx = 0;
32+
static size_t domestic_idx = 1;
33+
static double avg_fwd = 0.0;
34+
static double price = 0.0;
35+
static double zero_npv_strike = 0.0;
36+
static double strike = 0.0;
37+
static bool initialized = false;
38+
39+
displayCurrencyCombo(
40+
"Foreign Currency", foreign_idx, params,
41+
[&](Currency currency) { params.foreign_currency = currency; });
42+
displayCurrencyCombo(
43+
"Domestic Currency", domestic_idx, params,
44+
[&](Currency currency) { params.currency = currency; });
45+
46+
if (ImGui::Button("Recalculate") || !initialized) {
47+
params.spot_price = (*params.global_currencies)(params.foreign_currency, params.currency).value_or(1.0);
48+
const auto& foreign_curve = *params.global_rates->curves[params.foreign_currency];
49+
const auto& domestic_curve = *params.global_rates->curves[params.currency];
50+
51+
avg_fwd = weightedAvgForward(
52+
params.spot_price, end_date_years, settlement_frequency, foreign_curve, domestic_curve);
53+
54+
zero_npv_strike = findZeroNPVStrike(notional, target, end_date_years, settlement_frequency, FxTradeDirection::kLong, params.spot_price, volatility, foreign_curve, domestic_curve, num_paths);
55+
56+
if (!initialized) {
57+
strike = zero_npv_strike;
58+
}
59+
60+
TargetRedemptionForward tarf(notional, target, strike, end_date_years,
61+
settlement_frequency,
62+
FxTradeDirection::kLong);
63+
64+
price = tarf.price(params.spot_price, volatility,
65+
settlement_frequency / 4, num_paths,
66+
foreign_curve, domestic_curve);
67+
initialized = true;
68+
}
69+
70+
displayValueAsReadOnlyText("Avg Fwd", avg_fwd);
71+
displayValueAsReadOnlyText("Zero NPV Strike", zero_npv_strike);
72+
ImGui::InputDouble("Strike", &strike);
73+
displayValueAsReadOnlyText("NPV (DOM)", price);
74+
75+
ImGui::End();
76+
}
77+
78+
} // namespace smileexplorer
79+
80+
#endif // SMILEEXPLORER_EXPLORER_TARF_VISUALISER_H_

0 commit comments

Comments
 (0)