-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
278 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2023 Onur Cinar. All Rights Reserved. | ||
// The source code is provided under MIT License. | ||
// https://github.com/cinar/indicator | ||
|
||
package asset | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/cinar/indicator/helper" | ||
) | ||
|
||
// FileSystemRepository stores and retrieves asset snapshots using | ||
// the local file system. | ||
type FileSystemRepository struct { | ||
Repository | ||
|
||
// base is the root directory where asset snapshots are stored. | ||
base string | ||
} | ||
|
||
// NewFileSystemRepository initializes a file system repository with | ||
// the given base directory. | ||
func NewFileSystemRepository(base string) *FileSystemRepository { | ||
return &FileSystemRepository{ | ||
base: base, | ||
} | ||
} | ||
|
||
// Get attempts to return a channel of snapshots fo the asset with the given name. | ||
func (r *FileSystemRepository) Get(name string) (<-chan *Snapshot, error) { | ||
file := filepath.Join(r.base, fmt.Sprintf("%s.csv", name)) | ||
return helper.ReadFromCsvFile[Snapshot](file, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2023 Onur Cinar. All Rights Reserved. | ||
// The source code is provided under MIT License. | ||
// https://github.com/cinar/indicator | ||
|
||
package asset_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/indicator/asset" | ||
"github.com/cinar/indicator/helper" | ||
) | ||
|
||
func TestFileSystemRepositoryGet(t *testing.T) { | ||
repository := asset.NewFileSystemRepository("testdata") | ||
|
||
snapshots, err := repository.Get("brk-b") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
helper.Drain(snapshots) | ||
} | ||
|
||
func TestFileSystemRepositoryGetNonExisting(t *testing.T) { | ||
repository := asset.NewFileSystemRepository("testdata") | ||
|
||
_, err := repository.Get("brk") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) 2023 Onur Cinar. All Rights Reserved. | ||
// The source code is provided under MIT License. | ||
// https://github.com/cinar/indicator | ||
|
||
package asset | ||
|
||
import "time" | ||
|
||
// Repository serves as a centralized storage and retrieval | ||
// location for asset snapshots. | ||
type Repository interface { | ||
// Get attempts to return a channel of snapshots for | ||
// the asset with the given name. | ||
Get(name string) (<-chan *Snapshot, error) | ||
|
||
// LastDate returns the date of the last snapshot for | ||
// the asset with the given name, if any. Returns an | ||
// empty value if no snapshots exist. | ||
LastDate(name string) time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Date,Open,High,Low,Close,Adj Close,Volume | ||
2022-12-01,319,319.559998,313.299988,315.839996,315.839996,4351600 | ||
2022-12-02,313.48999,316.380005,312.75,316.149994,316.149994,3025700 | ||
2022-12-05,315.220001,315.660004,308.730011,310.570007,310.570007,3835800 | ||
2022-12-06,309.950012,310.290009,306.350006,307.779999,307.779999,3877400 | ||
2022-12-07,307.070007,309.380005,304.920013,305.820007,305.820007,4130800 | ||
2022-12-08,306,307.48999,305.089996,305.98999,305.98999,2351700 | ||
2022-12-09,305.320007,308.339996,304.709991,306.390015,306.390015,3326000 | ||
2022-12-12,307.549988,311.910004,305.459991,311.450012,311.450012,4366700 | ||
2022-12-13,318.399994,318.910004,310.820007,312.329987,312.329987,5042800 | ||
2022-12-14,312.73999,316.359985,308.399994,309.290009,309.290009,4056900 | ||
2022-12-15,306.429993,306.959991,299.450012,301.910004,301.910004,5103900 | ||
2022-12-16,299.049988,302.470001,297.76001,300,300,8305700 | ||
2022-12-19,300.51001,301.480011,297.149994,300.029999,300.029999,3842200 | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2023 Onur Cinar. All Rights Reserved. | ||
// The source code is provided under MIT License. | ||
// https://github.com/cinar/indicator | ||
|
||
package helper | ||
|
||
// Last takes a channel of values and returns a new channel containing the last N values. | ||
func Last[T any](c <-chan T, count int) <-chan T { | ||
result := make(chan T, cap(c)) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2023 Onur Cinar. All Rights Reserved. | ||
// The source code is provided under MIT License. | ||
// https://github.com/cinar/indicator | ||
|
||
package helper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/indicator/helper" | ||
) | ||
|
||
func TestLast(t *testing.T) { | ||
input := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
expected := helper.SliceToChan([]int{7, 8, 9, 10}) | ||
|
||
actual := helper.Last(input, 4) | ||
|
||
err := helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestLastLessValues(t *testing.T) { | ||
input := helper.SliceToChan([]int{1, 2}) | ||
expected := helper.SliceToChan([]int{1, 2}) | ||
|
||
actual := helper.Last(input, 4) | ||
|
||
err := helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters