Skip to content

Commit

Permalink
Tiingo added.
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Dec 22, 2023
1 parent f99b4ec commit 2fc263e
Show file tree
Hide file tree
Showing 9 changed files with 603 additions and 10 deletions.
185 changes: 181 additions & 4 deletions asset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,20 @@ The information provided on this project is strictly for informational purposes
- [func \(r \*FileSystemRepository\) Append\(name string, snapshots \<\-chan \*Snapshot\) error](<#FileSystemRepository.Append>)
- [func \(r \*FileSystemRepository\) Assets\(\) \(\[\]string, error\)](<#FileSystemRepository.Assets>)
- [func \(r \*FileSystemRepository\) Get\(name string\) \(\<\-chan \*Snapshot, error\)](<#FileSystemRepository.Get>)
- [func \(r \*FileSystemRepository\) GetSince\(name string, date time.Time\) \(\<\-chan \*Snapshot, error\)](<#FileSystemRepository.GetSince>)
- [func \(r \*FileSystemRepository\) LastDate\(name string\) \(time.Time, error\)](<#FileSystemRepository.LastDate>)
- [type Repository](<#Repository>)
- [type Snapshot](<#Snapshot>)
- [type TiingoEndOfDay](<#TiingoEndOfDay>)
- [func \(e \*TiingoEndOfDay\) ToSnapshot\(\) \*Snapshot](<#TiingoEndOfDay.ToSnapshot>)
- [type TiingoMeta](<#TiingoMeta>)
- [type TiingoRepository](<#TiingoRepository>)
- [func NewTiingoRepository\(apiKey string\) \*TiingoRepository](<#NewTiingoRepository>)
- [func \(r \*TiingoRepository\) Append\(\_ string, \_ \<\-chan \*Snapshot\) error](<#TiingoRepository.Append>)
- [func \(r \*TiingoRepository\) Assets\(\) \(\[\]string, error\)](<#TiingoRepository.Assets>)
- [func \(r \*TiingoRepository\) Get\(name string\) \(\<\-chan \*Snapshot, error\)](<#TiingoRepository.Get>)
- [func \(r \*TiingoRepository\) GetSince\(name string, date time.Time\) \(\<\-chan \*Snapshot, error\)](<#TiingoRepository.GetSince>)
- [func \(r \*TiingoRepository\) LastDate\(name string\) \(time.Time, error\)](<#TiingoRepository.LastDate>)


<a name="FileSystemRepository"></a>
Expand All @@ -56,7 +67,7 @@ func NewFileSystemRepository(base string) *FileSystemRepository
NewFileSystemRepository initializes a file system repository with the given base directory.

<a name="FileSystemRepository.Append"></a>
### func \(\*FileSystemRepository\) [Append](<https://github.com/cinar/indicator/blob/v2/asset/file_system_repository.go#L79>)
### func \(\*FileSystemRepository\) [Append](<https://github.com/cinar/indicator/blob/v2/asset/file_system_repository.go#L93>)

```go
func (r *FileSystemRepository) Append(name string, snapshots <-chan *Snapshot) error
Expand All @@ -80,10 +91,19 @@ Assets returns the names of all assets in the repository.
func (r *FileSystemRepository) Get(name string) (<-chan *Snapshot, error)
```

Get attempts to return a channel of snapshots fo the asset with the given name.
Get attempts to return a channel of snapshots for the asset with the given name.

<a name="FileSystemRepository.GetSince"></a>
### func \(\*FileSystemRepository\) [GetSince](<https://github.com/cinar/indicator/blob/v2/asset/file_system_repository.go#L62>)

```go
func (r *FileSystemRepository) GetSince(name string, date time.Time) (<-chan *Snapshot, error)
```

GetSince attempts to return a channel of snapshots for the asset with the given name since the given date.

<a name="FileSystemRepository.LastDate"></a>
### func \(\*FileSystemRepository\) [LastDate](<https://github.com/cinar/indicator/blob/v2/asset/file_system_repository.go#L62>)
### func \(\*FileSystemRepository\) [LastDate](<https://github.com/cinar/indicator/blob/v2/asset/file_system_repository.go#L76>)

```go
func (r *FileSystemRepository) LastDate(name string) (time.Time, error)
Expand All @@ -92,7 +112,7 @@ func (r *FileSystemRepository) LastDate(name string) (time.Time, error)
LastDate returns the date of the last snapshot for the asset with the given name.

<a name="Repository"></a>
## type [Repository](<https://github.com/cinar/indicator/blob/v2/asset/repository.go#L11-L26>)
## type [Repository](<https://github.com/cinar/indicator/blob/v2/asset/repository.go#L11-L30>)

Repository serves as a centralized storage and retrieval location for asset snapshots.

Expand All @@ -105,6 +125,10 @@ type Repository interface {
// the asset with the given name.
Get(name string) (<-chan *Snapshot, error)

// GetSince attempts to return a channel of snapshots for
// the asset with the given name since the given date.
GetSince(name string, date time.Time) (<-chan *Snapshot, error)

// LastDate returns the date of the last snapshot for
// the asset with the given name.
LastDate(name string) (time.Time, error)
Expand Down Expand Up @@ -147,4 +171,157 @@ type Snapshot struct {
}
```

<a name="TiingoEndOfDay"></a>
## type [TiingoEndOfDay](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L41-L80>)

TiingoEndOfDay is the repose from the end\-of\-day endpoint. https://www.tiingo.com/documentation/end-of-day

```go
type TiingoEndOfDay struct {
// Date is the date this data pertains to.
Date time.Time `json:"date"`

// Open is the opening price.
Open float64 `json:"open"`

// High is the highest price.
High float64 `json:"high"`

// Low is the lowest price.
Low float64 `json:"low"`

// Close is the closing price.
Close float64 `json:"close"`

// Volume is the total volume.
Volume int64 `json:"volume"`

// AdjOpen is the adjusted opening price.
AdjOpen float64 `json:"adjOpen"`

// AdjHigh is the adjusted highest price.
AdjHigh float64 `json:"adjHigh"`

// AdjLow is the adjusted lowest price.
AdjLow float64 `json:"adjLow"`

// AdjClose is the adjusted closing price.
AdjClose float64 `json:"adjClose"`

// AdjVolume is the adjusted total volume.
AdjVolume int64 `json:"adjVolume"`

// Dividend is the dividend paid out.
Dividend float64 `json:"divCash"`

// Split to adjust values after a split.
Split float64 `json:"splitFactor"`
}
```

<a name="TiingoEndOfDay.ToSnapshot"></a>
### func \(\*TiingoEndOfDay\) [ToSnapshot](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L83>)

```go
func (e *TiingoEndOfDay) ToSnapshot() *Snapshot
```

ToSnapshot converts the Tiingo end\-of\-day to a snapshot.

<a name="TiingoMeta"></a>
## type [TiingoMeta](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L19-L37>)

TiingoMeta is the response from the meta endpoint. https://www.tiingo.com/documentation/end-of-day

```go
type TiingoMeta struct {
// Ticker related to the asset.
Ticker string `json:"ticker"`

// Name is the full name of the asset.
Name string `json:"name"`

// ExchangeCode is the exchange where the asset is listed on.
ExchangeCode string `json:"exchangeCode"`

// Description is the description of the asset.
Description string `json:"description"`

// StartDate is the earliest date for the asset data.
StartDate time.Time `json:"startDate"`

// EndDate is the latest date for the asset data.
EndDate time.Time `json:"endDate"`
}
```

<a name="TiingoRepository"></a>
## type [TiingoRepository](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L98-L109>)

TiingoRepository provides access to financial market data, retrieving asset snapshots, by interacting with the Tiingo Stock & Financial Markets API. To use this repository, you'll need a valid API key from https://www.tiingo.com.

```go
type TiingoRepository struct {
Repository

// BaseURL is the Tiingo API URL.
BaseURL string
// contains filtered or unexported fields
}
```

<a name="NewTiingoRepository"></a>
### func [NewTiingoRepository](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L113>)

```go
func NewTiingoRepository(apiKey string) *TiingoRepository
```

NewTiingoRepository initializes a file system repository with the given API key.

<a name="TiingoRepository.Append"></a>
### func \(\*TiingoRepository\) [Append](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L219>)

```go
func (r *TiingoRepository) Append(_ string, _ <-chan *Snapshot) error
```

Append adds the given snapshows to the asset with the given name.

<a name="TiingoRepository.Assets"></a>
### func \(\*TiingoRepository\) [Assets](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L122>)

```go
func (r *TiingoRepository) Assets() ([]string, error)
```

Assets returns the names of all assets in the repository.

<a name="TiingoRepository.Get"></a>
### func \(\*TiingoRepository\) [Get](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L127>)

```go
func (r *TiingoRepository) Get(name string) (<-chan *Snapshot, error)
```

Get attempts to return a channel of snapshots for the asset with the given name.

<a name="TiingoRepository.GetSince"></a>
### func \(\*TiingoRepository\) [GetSince](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L132>)

```go
func (r *TiingoRepository) GetSince(name string, date time.Time) (<-chan *Snapshot, error)
```

GetSince attempts to return a channel of snapshots for the asset with the given name since the given date.

<a name="TiingoRepository.LastDate"></a>
### func \(\*TiingoRepository\) [LastDate](<https://github.com/cinar/indicator/blob/v2/asset/tiingo_repository.go#L186>)

```go
func (r *TiingoRepository) LastDate(name string) (time.Time, error)
```

LastDate returns the date of the last snapshot for the asset with the given name.

Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
16 changes: 15 additions & 1 deletion asset/file_system_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,25 @@ func (r *FileSystemRepository) Assets() ([]string, error) {
return assets, nil
}

// Get attempts to return a channel of snapshots fo the asset with the given name.
// Get attempts to return a channel of snapshots for the asset with the given name.
func (r *FileSystemRepository) Get(name string) (<-chan *Snapshot, error) {
return helper.ReadFromCsvFile[Snapshot](r.getCsvFileName(name), true)
}

// GetSince attempts to return a channel of snapshots for the asset with the given name since the given date.
func (r *FileSystemRepository) GetSince(name string, date time.Time) (<-chan *Snapshot, error) {
snapshots, err := helper.ReadFromCsvFile[Snapshot](r.getCsvFileName(name), true)
if err != nil {
return nil, err
}

snapshots = helper.Filter(snapshots, func(s *Snapshot) bool {
return s.Date.Equal(date) || s.Date.After(date)
})

return snapshots, nil
}

// LastDate returns the date of the last snapshot for the asset with the given name.
func (r *FileSystemRepository) LastDate(name string) (time.Time, error) {
var last time.Time
Expand Down
34 changes: 32 additions & 2 deletions asset/file_system_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestFileSystemRepositoryAssets(t *testing.T) {
repository := asset.NewFileSystemRepository("testdata")
expected := []string{"brk-b", "empty"}
expected := []string{"brk-b", "empty", "since"}

actual, err := repository.Assets()
if err != nil {
Expand Down Expand Up @@ -50,9 +50,39 @@ func TestFileSystemRepositoryGet(t *testing.T) {
}

func TestFileSystemRepositoryGetNonExisting(t *testing.T) {
repository := asset.NewFileSystemRepository("testdata/non_existing")

_, err := repository.Get("brk-b")
if err == nil {
t.Fatal("expected error")
}
}

func TestFileSystemRepositoryGetSince(t *testing.T) {
repository := asset.NewFileSystemRepository("testdata")

date := time.Date(2022, 12, 20, 0, 0, 0, 0, time.UTC)
actual, err := repository.GetSince("brk-b", date)
if err != nil {
t.Fatal(err)
}

expected, err := helper.ReadFromCsvFile[asset.Snapshot]("testdata/since.csv", true)
if err != nil {
t.Fatal(err)
}

err = helper.CheckEquals(actual, expected)
if err != nil {
t.Fatal(err)
}
}

func TestFileSystemRepositoryGetSinceNonExisting(t *testing.T) {
repository := asset.NewFileSystemRepository("testdata")

_, err := repository.Get("brk")
date := time.Date(2022, 12, 01, 0, 0, 0, 0, time.UTC)
_, err := repository.GetSince("brk", date)
if err == nil {
t.Fatal("expected error")
}
Expand Down
4 changes: 4 additions & 0 deletions asset/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type Repository interface {
// the asset with the given name.
Get(name string) (<-chan *Snapshot, error)

// GetSince attempts to return a channel of snapshots for
// the asset with the given name since the given date.
GetSince(name string, date time.Time) (<-chan *Snapshot, error)

// LastDate returns the date of the last snapshot for
// the asset with the given name.
LastDate(name string) (time.Time, error)
Expand Down
9 changes: 9 additions & 0 deletions asset/testdata/since.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Date,Open,High,Low,Close,Adj Close,Volume
2022-12-20,300.089996,304.190002,297,302,302,3090700
2022-12-21,304.380005,308.540009,304.160004,307.820007,307.820007,3264600
2022-12-22,306.100006,306.5,297.640015,302.690002,302.690002,3560100
2022-12-23,302.880005,306.570007,300.929993,306.48999,306.48999,2460400
2022-12-27,306.450012,308.579987,304.649994,305.549988,305.549988,2730900
2022-12-28,304.769989,307.459991,303.26001,303.429993,303.429993,2628200
2022-12-29,305.940002,309.380005,305.23999,309.059998,309.059998,2846200
2022-12-30,306.950012,309.040009,305.619995,308.899994,308.899994,3298300
Loading

0 comments on commit 2fc263e

Please sign in to comment.