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")
|
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")
|
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
|
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]
|
<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>
[<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)
<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>
module YahooFinance
--------------------
type YahooFinance = static member PriceHistory: symbol: string * ?startDate: DateTime * ?endDate: DateTime * ?interval: Interval -> PriceObs list + 1 overload
static member YahooFinance.PriceHistory: symbol: string * ?startDate: DateTime * ?endDate: DateTime * ?interval: Interval -> PriceObs list