Skip to content

Commit

Permalink
SLog logger is added. (#225)
Browse files Browse the repository at this point in the history
# Describe Request

SLog logger is added.

Fixed #224

# Change Type

New feature.
  • Loading branch information
cinar authored Sep 18, 2024
1 parent bff1856 commit 74dbcbd
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 74 deletions.
26 changes: 16 additions & 10 deletions asset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ type Snapshot struct {
```

<a name="Sync"></a>
## type [Sync](<https://github.com/cinar/indicator/blob/master/asset/sync.go#L25-L35>)
## type [Sync](<https://github.com/cinar/indicator/blob/master/asset/sync.go#L25-L38>)

Sync represents the configuration parameters for synchronizing assets between repositories.

Expand All @@ -396,11 +396,14 @@ type Sync struct {
// Assets is the name of the assets to be synced. If it is empty, all assets in the target repository
// will be synced instead.
Assets []string

// Logger is the slog logger instance.
Logger *slog.Logger
}
```

<a name="NewSync"></a>
### func [NewSync](<https://github.com/cinar/indicator/blob/master/asset/sync.go#L38>)
### func [NewSync](<https://github.com/cinar/indicator/blob/master/asset/sync.go#L41>)

```go
func NewSync() *Sync
Expand All @@ -409,7 +412,7 @@ func NewSync() *Sync
NewSync function initializes a new sync instance with the default parameters.

<a name="Sync.Run"></a>
### func \(\*Sync\) [Run](<https://github.com/cinar/indicator/blob/master/asset/sync.go#L47>)
### func \(\*Sync\) [Run](<https://github.com/cinar/indicator/blob/master/asset/sync.go#L51>)

```go
func (s *Sync) Run(source, target Repository, defaultStartDate time.Time) error
Expand Down Expand Up @@ -502,7 +505,7 @@ type TiingoMeta struct {
```

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

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.

Expand All @@ -512,12 +515,15 @@ type TiingoRepository struct {

// BaseURL is the Tiingo API URL.
BaseURL string

// Logger is the slog logger instance.
Logger *slog.Logger
// contains filtered or unexported fields
}
```

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

```go
func NewTiingoRepository(apiKey string) *TiingoRepository
Expand All @@ -526,7 +532,7 @@ 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/master/asset/tiingo_repository.go#L234>)
### func \(\*TiingoRepository\) [Append](<https://github.com/cinar/indicator/blob/master/asset/tiingo_repository.go#L238>)

```go
func (*TiingoRepository) Append(_ string, _ <-chan *Snapshot) error
Expand All @@ -535,7 +541,7 @@ func (*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/master/asset/tiingo_repository.go#L122>)
### func \(\*TiingoRepository\) [Assets](<https://github.com/cinar/indicator/blob/master/asset/tiingo_repository.go#L126>)

```go
func (*TiingoRepository) Assets() ([]string, error)
Expand All @@ -544,7 +550,7 @@ func (*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/master/asset/tiingo_repository.go#L127>)
### func \(\*TiingoRepository\) [Get](<https://github.com/cinar/indicator/blob/master/asset/tiingo_repository.go#L131>)

```go
func (r *TiingoRepository) Get(name string) (<-chan *Snapshot, error)
Expand All @@ -553,7 +559,7 @@ 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/master/asset/tiingo_repository.go#L132>)
### func \(\*TiingoRepository\) [GetSince](<https://github.com/cinar/indicator/blob/master/asset/tiingo_repository.go#L136>)

```go
func (r *TiingoRepository) GetSince(name string, date time.Time) (<-chan *Snapshot, error)
Expand All @@ -562,7 +568,7 @@ func (r *TiingoRepository) GetSince(name string, date time.Time) (<-chan *Snapsh
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/master/asset/tiingo_repository.go#L194>)
### func \(\*TiingoRepository\) [LastDate](<https://github.com/cinar/indicator/blob/master/asset/tiingo_repository.go#L198>)

```go
func (r *TiingoRepository) LastDate(name string) (time.Time, error)
Expand Down
16 changes: 10 additions & 6 deletions asset/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package asset

import (
"errors"
"log"
"log/slog"
"sync"
"time"

Expand All @@ -32,6 +32,9 @@ type Sync struct {
// Assets is the name of the assets to be synced. If it is empty, all assets in the target repository
// will be synced instead.
Assets []string

// Logger is the slog logger instance.
Logger *slog.Logger
}

// NewSync function initializes a new sync instance with the default parameters.
Expand All @@ -40,13 +43,14 @@ func NewSync() *Sync {
Workers: DefaultSyncWorkers,
Delay: DefaultSyncDelay,
Assets: []string{},
Logger: slog.Default(),
}
}

// Run synchronizes assets between the source and target repositories using multi-worker concurrency.
func (s *Sync) Run(source, target Repository, defaultStartDate time.Time) error {
if len(s.Assets) == 0 {
log.Print("No asset names provided. Syncing in all assets in the target repository.")
s.Logger.Warn("No asset names provided. Syncing in all assets in the target repository.")

assets, err := target.Assets()
if err != nil {
Expand All @@ -56,7 +60,7 @@ func (s *Sync) Run(source, target Repository, defaultStartDate time.Time) error
s.Assets = assets
}

log.Printf("Will sync %d assets.", len(s.Assets))
s.Logger.Info("Start syncing.", "assets", len(s.Assets))
jobs := helper.SliceToChan(s.Assets)

hasErrors := false
Expand All @@ -76,18 +80,18 @@ func (s *Sync) Run(source, target Repository, defaultStartDate time.Time) error
lastDate = defaultStartDate
}

log.Printf("Syncing %s starting %s", name, lastDate.Format("2006-01-02"))
s.Logger.Info("Syncing asset.", "asset", name, "start", lastDate.Format("2006-01-02"))

snapshots, err := source.GetSince(name, lastDate)
if err != nil {
log.Printf("GetSince failed for %s with %v", name, err)
s.Logger.Error("GetSince failed.", "asset", name, "error", err)
hasErrors = true
continue
}

err = target.Append(name, snapshots)
if err != nil {
log.Printf("Append failed for %s with %v", name, err)
s.Logger.Error("Append failed.", "asset", name, "error", err)
hasErrors = true
continue
}
Expand Down
14 changes: 9 additions & 5 deletions asset/tiingo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"errors"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"time"
)
Expand Down Expand Up @@ -106,6 +106,9 @@ type TiingoRepository struct {

// BaseURL is the Tiingo API URL.
BaseURL string

// Logger is the slog logger instance.
Logger *slog.Logger
}

// NewTiingoRepository initializes a file system repository with
Expand All @@ -115,6 +118,7 @@ func NewTiingoRepository(apiKey string) *TiingoRepository {
apiKey: apiKey,
client: &http.Client{},
BaseURL: "https://api.tiingo.com",
Logger: slog.Default(),
}
}

Expand Down Expand Up @@ -159,7 +163,7 @@ func (r *TiingoRepository) GetSince(name string, date time.Time) (<-chan *Snapsh

_, err = decoder.Token()
if err != nil {
log.Print(err)
r.Logger.Error("Unable to read token.", "error", err)
return
}

Expand All @@ -168,7 +172,7 @@ func (r *TiingoRepository) GetSince(name string, date time.Time) (<-chan *Snapsh

err = decoder.Decode(&data)
if err != nil {
log.Print(err)
r.Logger.Error("Unable to decode data.", "error", err)
break
}

Expand All @@ -177,13 +181,13 @@ func (r *TiingoRepository) GetSince(name string, date time.Time) (<-chan *Snapsh

_, err = decoder.Token()
if err != nil {
log.Printf("GetSince failed with %v", err)
r.Logger.Error("GetSince failed.", "error", err)
return
}

err = res.Body.Close()
if err != nil {
log.Print(err)
r.Logger.Error("Unable to close respose.", "error", err)
}
}()

Expand Down
26 changes: 16 additions & 10 deletions backtest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func RegisterReportBuilder(name string, builder ReportBuilderFunc)
RegisterReportBuilder registers the given builder.

<a name="Backtest"></a>
## type [Backtest](<https://github.com/cinar/indicator/blob/master/backtest/backtest.go#L43-L61>)
## type [Backtest](<https://github.com/cinar/indicator/blob/master/backtest/backtest.go#L43-L64>)

Backtest function rigorously evaluates the potential performance of the specified strategies applied to a defined set of assets. It generates comprehensive visual representations for each strategy\-asset pairing.

Expand All @@ -109,12 +109,15 @@ type Backtest struct {

// LastDays is the number of days backtest should go back.
LastDays int

// Logger is the slog logger instance.
Logger *slog.Logger
// contains filtered or unexported fields
}
```

<a name="NewBacktest"></a>
### func [NewBacktest](<https://github.com/cinar/indicator/blob/master/backtest/backtest.go#L64>)
### func [NewBacktest](<https://github.com/cinar/indicator/blob/master/backtest/backtest.go#L67>)

```go
func NewBacktest(repository asset.Repository, report Report) *Backtest
Expand All @@ -123,7 +126,7 @@ func NewBacktest(repository asset.Repository, report Report) *Backtest
NewBacktest function initializes a new backtest instance.

<a name="Backtest.Run"></a>
### func \(\*Backtest\) [Run](<https://github.com/cinar/indicator/blob/master/backtest/backtest.go#L79>)
### func \(\*Backtest\) [Run](<https://github.com/cinar/indicator/blob/master/backtest/backtest.go#L83>)

```go
func (b *Backtest) Run() error
Expand Down Expand Up @@ -222,7 +225,7 @@ type DataStrategyResult struct {
```

<a name="HTMLReport"></a>
## type [HTMLReport](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L36-L53>)
## type [HTMLReport](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L36-L56>)

HTMLReport is the backtest HTML report.

Expand All @@ -235,12 +238,15 @@ type HTMLReport struct {

// DateFormat is the date format that is used in the reports.
DateFormat string

// Logger is the slog logger instance.
Logger *slog.Logger
// contains filtered or unexported fields
}
```

<a name="NewHTMLReport"></a>
### func [NewHTMLReport](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L77>)
### func [NewHTMLReport](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L80>)

```go
func NewHTMLReport(outputDir string) *HTMLReport
Expand All @@ -249,7 +255,7 @@ func NewHTMLReport(outputDir string) *HTMLReport
NewHTMLReport initializes a new HTML report instance.

<a name="HTMLReport.AssetBegin"></a>
### func \(\*HTMLReport\) [AssetBegin](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L100>)
### func \(\*HTMLReport\) [AssetBegin](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L104>)

```go
func (h *HTMLReport) AssetBegin(name string, strategies []strategy.Strategy) error
Expand All @@ -258,7 +264,7 @@ func (h *HTMLReport) AssetBegin(name string, strategies []strategy.Strategy) err
AssetBegin is called when backtesting for the given asset begins.

<a name="HTMLReport.AssetEnd"></a>
### func \(\*HTMLReport\) [AssetEnd](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L155>)
### func \(\*HTMLReport\) [AssetEnd](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L159>)

```go
func (h *HTMLReport) AssetEnd(name string) error
Expand All @@ -267,7 +273,7 @@ func (h *HTMLReport) AssetEnd(name string) error
AssetEnd is called when backtesting for the given asset ends.

<a name="HTMLReport.Begin"></a>
### func \(\*HTMLReport\) [Begin](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L87>)
### func \(\*HTMLReport\) [Begin](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L91>)

```go
func (h *HTMLReport) Begin(assetNames []string, _ []strategy.Strategy) error
Expand All @@ -276,7 +282,7 @@ func (h *HTMLReport) Begin(assetNames []string, _ []strategy.Strategy) error
Begin is called when the backtest starts.

<a name="HTMLReport.End"></a>
### func \(\*HTMLReport\) [End](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L184>)
### func \(\*HTMLReport\) [End](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L188>)

```go
func (h *HTMLReport) End() error
Expand All @@ -285,7 +291,7 @@ func (h *HTMLReport) End() error
End is called when the backtest ends.

<a name="HTMLReport.Write"></a>
### func \(\*HTMLReport\) [Write](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L112>)
### func \(\*HTMLReport\) [Write](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L116>)

```go
func (h *HTMLReport) Write(assetName string, currentStrategy strategy.Strategy, snapshots <-chan *asset.Snapshot, actions <-chan strategy.Action, outcomes <-chan float64) error
Expand Down
Loading

0 comments on commit 74dbcbd

Please sign in to comment.