Learn JULIA-FINANCE-PACKAGES with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Black-Scholes Option Pricing in Julia
using QuantLib
today = Date(2025,9,24)
Settings.instance().evaluationDate = today
S = 100.0; K = 100.0; r = 0.05; sigma = 0.2; T = 1.0
option_type = :Call
payoff = PlainVanillaPayoff(option_type, K)
exercise = EuropeanExercise(today + Year(1))
option = VanillaOption(payoff, exercise)
spot = SimpleQuote(S)
term_structure = FlatForward(today, r, Actual365Fixed())
vol_ts = BlackConstantVol(today, TARGET(), sigma, Actual365Fixed())
process = BlackScholesMertonProcess(QuoteHandle(spot), YieldTermStructureHandle(), YieldTermStructureHandle(term_structure), BlackVolTermStructureHandle(vol_ts))
option.setPricingEngine(AnalyticEuropeanEngine(process))
println("Call Option NPV: ", option.NPV())
Compute the price of a European call option using QuantLib.jl in Julia.
2
Portfolio Returns with MarketData.jl
using MarketData, Plots
prices = get(MarketData.SP500)
returns = diff(log.(prices), dims=1)
cum_returns = cumsum(returns, dims=1)
plot(cum_returns, title="Cumulative Returns", legend=:topright)
Compute and plot cumulative returns for multiple assets using MarketData.jl.
3
Technical Indicators with FinancialToolbox.jl
using FinancialToolbox
prices = [100,102,101,105,107]
sma = sma(prices, 3)
rsi_values = rsi(prices, 14)
println("SMA: ", sma)
println("RSI: ", rsi_values)
Calculate a simple moving average and RSI for a stock series using FinancialToolbox.jl.
4
Monte Carlo Simulation for Option Pricing
using Random
S0 = 100.0; K = 100.0; r = 0.05; sigma = 0.2; T = 1.0; N = 100000
z = randn(N)
ST = S0 .* exp.((r - 0.5*sigma^2)*T .+ sigma*sqrt(T).*z)
payoff = max.(ST .- K, 0.0)
optionPrice = exp(-r*T) * mean(payoff)
println("Monte Carlo Option Price: ", optionPrice)
Estimate European option price using Monte Carlo simulation.
5
Calculate Portfolio Variance
weights = [0.6,0.4]
cov_matrix = [0.0004 0.0002; 0.0002 0.0003]
portfolio_variance = weights' * cov_matrix * weights
println("Portfolio Variance: ", portfolio_variance)
Compute variance of a portfolio given weights and covariance matrix.
6
Compute Sharpe Ratio
returns = [0.02,0.03,0.015,0.01]
risk_free = 0.01
sharpe_ratio = (mean(returns) - risk_free)/std(returns)
println("Sharpe Ratio: ", sharpe_ratio)
Calculate the Sharpe ratio for a portfolio with given returns and risk-free rate.
7
Calculate Forward Price
S = 100.0; r = 0.05; T = 1.0
forward_price = S * exp(r*T)
println("Forward Price: ", forward_price)
Compute the forward price of an asset given spot price, rate, and time.
8
Discounted Cash Flow Valuation
cashflows = [100.0, 100.0, 100.0]; r = 0.05
pv = sum(cashflows ./ (1 .+ r).^(1:length(cashflows)))
println("Present Value: ", pv)
Compute present value of future cash flows.
9
Correlation Between Assets
returns = [0.02 0.01 0.03; 0.01 0.015 0.02]
cor_matrix = cor(returns)
println("Correlation Matrix:\n", cor_matrix)
Compute correlation matrix for multiple asset returns.
10
Yield Curve Construction
maturities = [1,2,3,4,5]
prices = [0.99,0.975,0.96,0.945,0.93]
zero_rates = -log.(prices) ./ maturities
using Plots
plot(maturities, zero_rates, marker=:o, xlabel="Years", ylabel="Zero Rate", title="Zero-Coupon Yield Curve")
Construct a zero-coupon yield curve from bond prices.