Learn Fsharp-finance - 10 Code Examples & CST Typing Practice Test
F# is a functional-first programming language on the .NET platform, widely used in finance for quantitative modeling, risk analysis, and algorithmic trading due to its strong type system, immutability, and functional programming paradigms.
View all 10 Fsharp-finance code examples →
Learn FSHARP-FINANCE with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
Calculate Compound Interest
let compoundInterest principal rate periods =
principal * ((1.0 + rate) ** periods)
let amount = compoundInterest 1000.0 0.05 10.0
printfn "Compound Interest Amount: %f" amount
Compute the compound interest for a given principal, rate, and number of periods.
Black-Scholes Option Pricing
open System
open MathNet.Numerics.Distributions
let blackScholesCall S K r sigma T =
let d1 = (log(S/K) + (r + 0.5 * sigma*sigma) * T) / (sigma * sqrt T)
let d2 = d1 - sigma * sqrt T
S * Normal.CDF(0.0,1.0,d1) - K * exp(-r*T) * Normal.CDF(0.0,1.0,d2)
let callPrice = blackScholesCall 100.0 100.0 0.05 0.2 1.0
printfn "Call Option Price: %f" callPrice
Calculate the price of a European call option using the Black-Scholes formula in F#.
Simulate Stock Price Path
open System
open MathNet.Numerics.Distributions
let simulateStockPath S0 mu sigma T steps =
let dt = T / float steps
let rnd = Normal(0.0,1.0)
let rec loop S n acc =
if n > steps then List.rev acc
else let Snext = S * exp((mu - 0.5 * sigma*sigma)*dt + sigma*sqrt(dt)*rnd.Sample())
loop Snext (n+1) (Snext::acc)
loop S0 1 [S0]
let path = simulateStockPath 100.0 0.05 0.2 1.0 10
printfn "%A" path
Simulate a simple geometric Brownian motion for stock price evolution.
Calculate Portfolio Return
let portfolioReturn weights returns =
List.map2 (*) weights returns |> List.sum
let weights = [0.6; 0.4]
let returns = [0.02; 0.03]
let pReturn = portfolioReturn weights returns
printfn "Portfolio Return: %f" pReturn
Compute the weighted return of a portfolio from individual asset returns.
Compute Portfolio Variance
let covMatrix = [[0.0004;0.0002];[0.0002;0.0003]]
let weights = [0.6;0.4]
let portfolioVariance =
List.mapi (fun i row -> List.mapi (fun j x -> x * weights.[i] * weights.[j]) row |> List.sum) covMatrix |> List.sum
printfn "Portfolio Variance: %f" portfolioVariance
Calculate portfolio variance given asset covariance matrix and weights.
Compute Portfolio Standard Deviation
let portfolioStdDev variance = sqrt variance
let stdDev = portfolioStdDev 0.00028
printfn "Portfolio Std Dev: %f" stdDev
Compute the standard deviation of a portfolio from variance.
Calculate Sharpe Ratio
let sharpeRatio expectedReturn riskFreeRate stdDev = (expectedReturn - riskFreeRate)/stdDev
let sr = sharpeRatio 0.025 0.01 0.0167
printfn "Sharpe Ratio: %f" sr
Compute the Sharpe ratio of a portfolio.
Simulate Multiple Stock Paths
let simulateMultiplePaths S0 mu sigma T steps nPaths =
[1..nPaths] |> List.map (fun _ -> simulateStockPath S0 mu sigma T steps)
let paths = simulateMultiplePaths 100.0 0.05 0.2 1.0 10 5
printfn "%A" paths
Simulate multiple correlated stock price paths using geometric Brownian motion.
Calculate Forward Price
let forwardPrice S r T = S * exp(r*T)
let F = forwardPrice 100.0 0.05 1.0
printfn "Forward Price: %f" F
Compute the forward price of an asset given spot price, risk-free rate, and time to maturity.
Discount Cash Flows
let discountCashFlows cashFlows r =
List.mapi (fun i cf -> cf / ((1.0 + r) ** float (i+1))) cashFlows |> List.sum
let pv = discountCashFlows [100.0;100.0;100.0] 0.05
printfn "Present Value: %f" pv
Compute present value of future cash flows.
Frequently Asked Questions about Fsharp-finance
What is Fsharp-finance?
F# is a functional-first programming language on the .NET platform, widely used in finance for quantitative modeling, risk analysis, and algorithmic trading due to its strong type system, immutability, and functional programming paradigms.
What are the primary use cases for Fsharp-finance?
Algorithmic trading and backtesting. Derivatives pricing and financial modeling. Portfolio optimization and risk analysis. Time series analysis for financial data. Integrating functional code with enterprise .NET systems
What are the strengths of Fsharp-finance?
Reduces bugs in complex calculations with strong typing. Encourages concise, composable code. Ideal for high-performance financial computations. Simplifies data access with type providers. Functional style suits mathematical and statistical modeling
What are the limitations of Fsharp-finance?
Smaller ecosystem compared to Python in finance. Steep learning curve for developers new to functional programming. Limited interactive visualization libraries. Less community support for niche finance libraries. Verbose interop may be required when integrating with legacy C# systems
How can I practice Fsharp-finance typing speed?
CodeSpeedTest offers 10+ real Fsharp-finance code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.