Header menu logo Teaching

BinderScriptNotebook

This will introduce using #load to load scripts with external code.

When you type #load "Script.fsx" in the REPL, F# interactive compiles the code in Script.fsx and puts it into a code module with the same name as the script.

We are going to use a helper script called YahooFinance.fsx that includes code for requesting price histories from yahoo. To download it, go to the YahooFinance page and click the "download script" button at the top. Make sure that you have saved it in the same directory as this file.

If you have downloaded it correctly then the following code will evaluate to true.

System.IO.File.Exists("YahooFinance.fsx")
true

Assuming that the above code evaluated to true we can now load it into our session.

#load "YahooFinance.fsx"

Namespaces are a hierarchical way of organizing code. If we open a namespace, then we have access to the code inside the namespace directly.

It is common to open the System namespace.

open System

Now we can leave System off when accessing code in the System namespace, such as System.IO.File.Exists that we used above.

IO.File.Exists("YahooFinance.fsx")
true

We also want to open the YahooFinance module from YahooFinance.fsx, which is similar to a namespace.

open YahooFinance

We are ready to request some data. Let's define our start and end dates. DateTime is a type in the System namespace. We have opened that namespace so we can access the type directly.

let myStart = DateTime(2010,1,1)
let myEnd = DateTime.UtcNow
myEnd
2/20/2024 12:49:13 PM

Our YahooFinance module has code for requesting price histories of stocks.

let bnd = YahooFinance.PriceHistory("BND",startDate=myStart,endDate=myEnd,interval = Interval.Daily)
let vti = YahooFinance.PriceHistory("VTI",startDate=myStart,endDate=myEnd,interval = Interval.Daily)

This returns several data items for each point in time.

vti[0..3]
[{ Symbol = "VTI"
   Date = 1/4/2010 12:00:00 AM
   Open = 56.860001
   High = 57.380001
   Low = 56.84
   Close = 57.310001
   AdjustedClose = 44.335243
   Volume = 2251500.0 }; { Symbol = "VTI"
                           Date = 1/5/2010 12:00:00 AM
                           Open = 57.34
                           High = 57.540001
                           Low = 57.110001
                           Close = 57.529999
                           AdjustedClose = 44.505432
                           Volume = 1597700.0 }; { Symbol = "VTI"
                                                   Date = 1/6/2010 12:00:00 AM
                                                   Open = 57.5
                                                   High = 57.720001
                                                   Low = 57.41
                                                   Close = 57.610001
                                                   AdjustedClose = 44.567322
                                                   Volume = 2120300.0 };
 { Symbol = "VTI"
   Date = 1/7/2010 12:00:00 AM
   Open = 57.549999
   High = 57.889999
   Low = 57.290001
   Close = 57.849998
   AdjustedClose = 44.752975
   Volume = 1656700.0 }]
namespace System
namespace System.IO
type File = static member AppendAllLines: path: string * contents: IEnumerable<string> -> unit + 1 overload static member AppendAllLinesAsync: path: string * contents: IEnumerable<string> * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload static member AppendAllText: path: string * contents: string -> unit + 1 overload static member AppendAllTextAsync: path: string * contents: string * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload static member AppendText: path: string -> StreamWriter static member Copy: sourceFileName: string * destFileName: string -> unit + 1 overload static member Create: path: string -> FileStream + 2 overloads static member CreateSymbolicLink: path: string * pathToTarget: string -> FileSystemInfo static member CreateText: path: string -> StreamWriter static member Decrypt: path: string -> unit ...
<summary>Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of <see cref="T:System.IO.FileStream" /> objects.</summary>
IO.File.Exists(path: string) : bool
module YahooFinance
val myStart: DateTime
Multiple items
[<Struct>] type DateTime = new: year: int * month: int * day: int -> unit + 16 overloads member Add: value: TimeSpan -> DateTime member AddDays: value: float -> DateTime member AddHours: value: float -> DateTime member AddMicroseconds: value: float -> DateTime member AddMilliseconds: value: float -> DateTime member AddMinutes: value: float -> DateTime member AddMonths: months: int -> DateTime member AddSeconds: value: float -> DateTime member AddTicks: value: int64 -> DateTime ...
<summary>Represents an instant in time, typically expressed as a date and time of day.</summary>

--------------------
DateTime ()
   (+0 other overloads)
DateTime(ticks: int64) : DateTime
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : DateTime
   (+0 other overloads)
DateTime(date: DateOnly, time: TimeOnly) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : DateTime
   (+0 other overloads)
DateTime(date: DateOnly, time: TimeOnly, kind: DateTimeKind) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : DateTime
   (+0 other overloads)
val myEnd: DateTime
property DateTime.UtcNow: DateTime with get
<summary>Gets a <see cref="T:System.DateTime" /> object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).</summary>
<returns>An object whose value is the current UTC date and time.</returns>
val bnd: PriceObs list
Multiple items
module YahooFinance

--------------------
type YahooFinance = static member PriceHistory: symbol: string * ?startDate: DateTime * ?endDate: DateTime * ?interval: Interval -> PriceObs list + 1 overload
static member YahooFinance.PriceHistory: symbols: string seq * ?startDate: DateTime * ?endDate: DateTime * ?interval: Interval -> PriceObs list
static member YahooFinance.PriceHistory: symbol: string * ?startDate: DateTime * ?endDate: DateTime * ?interval: Interval -> PriceObs list
type Interval = | Daily | Weekly | Monthly override ToString: unit -> string
union case Interval.Daily: Interval
val vti: PriceObs list

Type something to start searching.