@@ -36,88 +36,6 @@ fn reverseFunc(text: []const u8) ![]const u8 {
3636 for (text , 0.. ) | c , i | {
3737 result [text .len - 1 - i ] = c ;
3838 }
39- return result ;
40- }
41-
42- // Black-Scholes Call Option Price
43- pub const bs_call = ExcelFunction (.{
44- .name = "MyFunctions.BSCall" ,
45- .description = "Black-Scholes European Call Option Price" ,
46- .category = "Finance" ,
47- .params = &[_ ]ParamMeta {
48- .{ .name = "S" , .description = "Current stock price" },
49- .{ .name = "K" , .description = "Strike price" },
50- .{ .name = "T" , .description = "Time to maturity (years)" },
51- .{ .name = "r" , .description = "Risk-free rate" },
52- .{ .name = "sigma" , .description = "Volatility" },
53- },
54- .func = blackScholesCall ,
55- });
56-
57- fn blackScholesCall (S : f64 , K : f64 , T : f64 , r : f64 , sigma : f64 ) ! f64 {
58- if (T <= 0 ) return error .InvalidMaturity ;
59- if (sigma <= 0 ) return error .InvalidVolatility ;
60- if (S <= 0 ) return error .InvalidStockPrice ;
61- if (K <= 0 ) return error .InvalidStrikePrice ;
62-
63- const d1 = (std .math .log (f64 , std .math .e , S / K ) + (r + sigma * sigma / 2.0 ) * T ) / (sigma * std .math .sqrt (T ));
64- const d2 = d1 - sigma * std .math .sqrt (T );
65-
66- const call_price = S * cumulativeNormal (d1 ) - K * @exp (- r * T ) * cumulativeNormal (d2 );
67- return call_price ;
68- }
69-
70- // Black-Scholes Put Option Price
71- pub const bs_put = ExcelFunction (.{
72- .name = "MyFunctions.BSPut" ,
73- .description = "Black-Scholes European Put Option Price" ,
74- .category = "Finance" ,
75- .params = &[_ ]ParamMeta {
76- .{ .name = "S" , .description = "Current stock price" },
77- .{ .name = "K" , .description = "Strike price" },
78- .{ .name = "T" , .description = "Time to maturity (years)" },
79- .{ .name = "r" , .description = "Risk-free rate" },
80- .{ .name = "sigma" , .description = "Volatility" },
81- },
82- .func = blackScholesPut ,
83- });
84-
85- fn blackScholesPut (S : f64 , K : f64 , T : f64 , r : f64 , sigma : f64 ) ! f64 {
86- if (T <= 0 ) return error .InvalidMaturity ;
87- if (sigma <= 0 ) return error .InvalidVolatility ;
88- if (S <= 0 ) return error .InvalidStockPrice ;
89- if (K <= 0 ) return error .InvalidStrikePrice ;
90-
91- const d1 = (std .math .log (f64 , std .math .e , S / K ) + (r + sigma * sigma / 2.0 ) * T ) / (sigma * std .math .sqrt (T ));
92- const d2 = d1 - sigma * std .math .sqrt (T );
93-
94- const put_price = K * @exp (- r * T ) * cumulativeNormal (- d2 ) - S * cumulativeNormal (- d1 );
95- return put_price ;
96- }
9739
98- // Cumulative Normal Distribution (approximation)
99- fn cumulativeNormal (x : f64 ) f64 {
100- const a1 : f64 = 0.319381530 ;
101- const a2 : f64 = -0.356563782 ;
102- const a3 : f64 = 1.781477937 ;
103- const a4 : f64 = -1.821255978 ;
104- const a5 : f64 = 1.330274429 ;
105- const gamma : f64 = 0.2316419 ;
106-
107- const k = 1.0 / (1.0 + gamma * @abs (x ));
108- const k2 = k * k ;
109- const k3 = k2 * k ;
110- const k4 = k3 * k ;
111- const k5 = k4 * k ;
112-
113- const sqrt_2pi = std .math .sqrt (2.0 * std .math .pi );
114- const pdf = @exp (-0.5 * x * x ) / sqrt_2pi ;
115-
116- const cdf_approx = 1.0 - pdf * (a1 * k + a2 * k2 + a3 * k3 + a4 * k4 + a5 * k5 );
117-
118- if (x >= 0.0 ) {
119- return cdf_approx ;
120- } else {
121- return 1.0 - cdf_approx ;
122- }
40+ return result ;
12341}
0 commit comments