-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStockTools.fs
More file actions
104 lines (88 loc) · 4.01 KB
/
StockTools.fs
File metadata and controls
104 lines (88 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module StockAdvisorFS.StockTools
open System.Threading.Tasks
// ============================================================================
// ASYNC F# FUNCTIONS - Simulating real API calls with Task<string>
// XML doc comments become the AI's tool descriptions automatically!
// ============================================================================
/// <summary>Gets current stock information including price, change, and basic metrics</summary>
/// <param name="symbol">The stock ticker symbol (e.g., AAPL, MSFT, GOOGL)</param>
let getStockInfo (symbol: string) : Task<string> = task {
// Simulate API latency
do! Task.Delay(50)
let stocks =
dict [
"AAPL", (178.50m, 2.35m, 28.5m, 2.8m)
"MSFT", (378.25m, -1.20m, 35.2m, 2.9m)
"GOOGL", (141.80m, 0.95m, 24.8m, 1.8m)
"AMZN", (178.90m, 3.10m, 62.5m, 1.9m)
"TSLA", (248.50m, -5.20m, 72.3m, 0.8m)
]
match stocks.TryGetValue(symbol.ToUpper()) with
| true, (price, change, pe, marketCap) ->
return $"""Stock: {symbol.ToUpper()}
Price: ${price}
Change: {if change >= 0m then "+" else ""}{change} ({change / price * 100m:F2}%%)
P/E Ratio: {pe}
Market Cap: ${marketCap}T"""
| false, _ ->
return $"Stock symbol '{symbol}' not found."
}
/// <summary>Gets historical price data for a stock</summary>
/// <param name="symbol">The stock ticker symbol</param>
/// <param name="days">Number of days of history to retrieve (max 30)</param>
let getHistoricalPrices (symbol: string) (days: int) : Task<string> = task {
do! Task.Delay(75) // Simulate API latency
let days = min days 30
let basePrice =
match symbol.ToUpper() with
| "AAPL" -> 175.0m
| "MSFT" -> 375.0m
| "GOOGL" -> 140.0m
| _ -> 100.0m
let random = System.Random(symbol.GetHashCode())
let prices =
[ for i in days .. -1 .. 0 do
let date = System.DateTime.Today.AddDays(float -i)
let variance = decimal (random.NextDouble() * 10.0 - 5.0)
let dateStr = date.ToString("yyyy-MM-dd")
let priceStr = (basePrice + variance).ToString("F2")
yield $" {dateStr}: ${priceStr}" ]
let pricesStr = String.concat "\n" prices
return $"Historical prices for {symbol.ToUpper()} (last {days} days):\n{pricesStr}"
}
/// <summary>Calculates volatility metrics for a stock</summary>
/// <param name="symbol">The stock ticker symbol</param>
let calculateVolatility (symbol: string) : Task<string> = task {
do! Task.Delay(50) // Simulate API latency
let volatilities =
dict [
"AAPL", (1.2m, 22.5m, "Moderate")
"MSFT", (1.0m, 19.8m, "Low-Moderate")
"GOOGL", (1.4m, 26.3m, "Moderate")
"AMZN", (1.6m, 30.1m, "Moderate-High")
"TSLA", (3.2m, 60.5m, "High")
]
match volatilities.TryGetValue(symbol.ToUpper()) with
| true, (daily, annual, rating) ->
return $"""Volatility Analysis for {symbol.ToUpper()}:
Daily Volatility: {daily}%%
Annualized Volatility: {annual}%%
Risk Rating: {rating}"""
| false, _ ->
return $"Volatility data not available for '{symbol}'."
}
/// <summary>Compares fundamental metrics between two stocks</summary>
/// <param name="symbol1">The first stock ticker symbol</param>
/// <param name="symbol2">The second stock ticker symbol</param>
let compareStocks (symbol1: string) (symbol2: string) : Task<string> = task {
do! Task.Delay(100) // Simulate API latency
return $"""Comparison: {symbol1.ToUpper()} vs {symbol2.ToUpper()}
{symbol1.ToUpper()}:
- P/E Ratio: {if symbol1.ToUpper() = "AAPL" then "28.5" else "35.2"}
- Revenue Growth: {if symbol1.ToUpper() = "AAPL" then "8.2%" else "12.5%"}
- Profit Margin: {if symbol1.ToUpper() = "AAPL" then "25.3%" else "36.7%"}
{symbol2.ToUpper()}:
- P/E Ratio: {if symbol2.ToUpper() = "AAPL" then "28.5" else "35.2"}
- Revenue Growth: {if symbol2.ToUpper() = "AAPL" then "8.2%" else "12.5%"}
- Profit Margin: {if symbol2.ToUpper() = "AAPL" then "25.3%" else "36.7%"}"""
}