Skip to content

Commit 1bbf28b

Browse files
committed
simplify
1 parent bcc85a1 commit 1bbf28b

2 files changed

Lines changed: 8 additions & 121 deletions

File tree

example/README.md

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,16 @@
1-
# Example ZigXLL User Project
1+
# Example ZigXLL Project
22

3-
This is an example of how to use ZigXLL as a library.
3+
Example project showing how to use ZigXLL to create custom Excel functions.
44

5-
## Structure
6-
7-
```
8-
src/
9-
├── main.zig ← Entry point (re-exports zigxll framework)
10-
├── user_functions.zig ← Register your function modules here
11-
└── my_functions.zig ← Your custom functions
12-
```
13-
14-
## Building
5+
## Quick Start
156

167
```bash
178
zig build
189
```
1910

20-
Your XLL will be in `zig-out/lib/my_excel_functions.xll`
21-
22-
## Example Functions Included
11+
Output: `zig-out/lib/my_excel_functions.xll`
2312

24-
This example includes several custom Excel functions. **Note that these are for demonstration purposes.**
13+
## Example Functions
2514

26-
- **double(x)** - Doubles a number
27-
- **reverse(text)** - Reverses a string
28-
29-
- **MyFunctions.BSCall(S, K, T, r, sigma)** - Black-Scholes call option price
30-
- S: Current stock price
31-
- K: Strike price
32-
- T: Time to maturity (years)
33-
- r: Risk-free rate
34-
- sigma: Volatility
35-
36-
- **MyFunctions.BSPut(S, K, T, r, sigma)** - Black-Scholes put option price
37-
- S: Current stock price
38-
- K: Strike price
39-
- T: Time to maturity (years)
40-
- r: Risk-free rate
41-
- sigma: Volatility
42-
43-
Example usage in Excel:
44-
```
45-
=MyFunctions.BSCall(100, 105, 1, 0.05, 0.2)
46-
=MyFunctions.BSPut(100, 105, 1, 0.05, 0.2)
47-
```
15+
- `double(x)` - Doubles a number
16+
- `reverse(text)` - Reverses a string

example/src/my_functions.zig

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)