Skip to content

Latest commit

 

History

History
579 lines (409 loc) · 21 KB

README.md

File metadata and controls

579 lines (409 loc) · 21 KB

asset

import "github.com/cinar/indicator/v2/asset"

Package asset contains the asset related functions.

This package belongs to the Indicator project. Indicator is a Golang module that supplies a variety of technical indicators, strategies, and a backtesting framework for analysis.

License

Copyright (c) 2021-2024 Onur Cinar.
The source code is provided under GNU AGPLv3 License.
https://github.com/cinar/indicator

Disclaimer

The information provided on this project is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.

Index

Constants

const (
    // InMemoryRepositoryBuilderName is the name for the in memory repository builder.
    InMemoryRepositoryBuilderName = "memory"

    // FileSystemRepositoryBuilderName is the name for the file system repository builder.
    FileSystemRepositoryBuilderName = "filesystem"

    // TiingoRepositoryBuilderName is the name of the Tiingo repository builder.
    TiingoRepositoryBuilderName = "tiingo"
)

const (
    // DefaultSyncWorkers is the default number of workers to use to synchronize.
    DefaultSyncWorkers = 1

    // DefaultSyncDelay is the default delay in seconds between each get request.
    DefaultSyncDelay = 5
)

Variables

ErrRepositoryAssetEmpty indicates that the given asset has no snapshots.

var ErrRepositoryAssetEmpty = errors.New("asset empty")

ErrRepositoryAssetNotFound indicates that the given asset name is not found in the repository.

var ErrRepositoryAssetNotFound = errors.New("asset is not found")

func RegisterRepositoryBuilder(name string, builder RepositoryBuilderFunc)

RegisterRepositoryBuilder registers the given builder.

func SnapshotsAsClosings(snapshots <-chan *Snapshot) <-chan float64

SnapshotsAsClosings extracts the close field from each snapshot in the provided channel and returns a new channel containing only those close values.The original snapshots channel can no longer be directly used afterwards.

func SnapshotsAsDates(snapshots <-chan *Snapshot) <-chan time.Time

SnapshotsAsDates extracts the date field from each snapshot in the provided channel and returns a new channel containing only those date values.The original snapshots channel can no longer be directly used afterwards.

func SnapshotsAsHighs(snapshots <-chan *Snapshot) <-chan float64

SnapshotsAsHighs extracts the high field from each snapshot in the provided channel and returns a new channel containing only those high values.The original snapshots channel can no longer be directly used afterwards.

func SnapshotsAsLows(snapshots <-chan *Snapshot) <-chan float64

SnapshotsAsLows extracts the low field from each snapshot in the provided channel and returns a new channel containing only those low values.The original snapshots channel can no longer be directly used afterwards.

func SnapshotsAsOpenings(snapshots <-chan *Snapshot) <-chan float64

SnapshotsAsOpenings extracts the open field from each snapshot in the provided channel and returns a new channel containing only those open values.The original snapshots channel can no longer be directly used afterwards.

func SnapshotsAsVolumes(snapshots <-chan *Snapshot) <-chan float64

SnapshotsAsVolumes extracts the volume field from each snapshot in the provided channel and returns a new channel containing only those volume values.The original snapshots channel can no longer be directly used afterwards.

FileSystemRepository stores and retrieves asset snapshots using the local file system.

type FileSystemRepository struct {
    // contains filtered or unexported fields
}

func NewFileSystemRepository(base string) *FileSystemRepository

NewFileSystemRepository initializes a file system repository with the given base directory.

func (*FileSystemRepository) Append

func (r *FileSystemRepository) Append(name string, snapshots <-chan *Snapshot) error

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

func (*FileSystemRepository) Assets

func (r *FileSystemRepository) Assets() ([]string, error)

Assets returns the names of all assets in the repository.

func (*FileSystemRepository) Get

func (r *FileSystemRepository) Get(name string) (<-chan *Snapshot, error)

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

func (*FileSystemRepository) GetSince

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.

func (*FileSystemRepository) LastDate

func (r *FileSystemRepository) LastDate(name string) (time.Time, error)

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

InMemoryRepository stores and retrieves asset snapshots using an in memory storage.

type InMemoryRepository struct {
    // contains filtered or unexported fields
}

func NewInMemoryRepository() *InMemoryRepository

NewInMemoryRepository initializes an in memory repository.

func (*InMemoryRepository) Append

func (r *InMemoryRepository) Append(name string, snapshots <-chan *Snapshot) error

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

func (*InMemoryRepository) Assets

func (r *InMemoryRepository) Assets() ([]string, error)

Assets returns the names of all assets in the repository.

func (*InMemoryRepository) Get

func (r *InMemoryRepository) Get(name string) (<-chan *Snapshot, error)

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

func (*InMemoryRepository) GetSince

func (r *InMemoryRepository) 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.

func (*InMemoryRepository) LastDate

func (r *InMemoryRepository) LastDate(name string) (time.Time, error)

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

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

type Repository interface {
    // Assets returns the names of all assets in the repository.
    Assets() ([]string, error)

    // Get attempts to return a channel of snapshots for
    // 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)

    // Append adds the given snapshows to the asset with the
    // given name.
    Append(name string, snapshots <-chan *Snapshot) error
}

func NewRepository(name, config string) (Repository, error)

NewRepository builds a new repository by the given name type and the configuration.

RepositoryBuilderFunc defines a function to build a new repository using the given configuration parameter.

type RepositoryBuilderFunc func(config string) (Repository, error)

Snapshot captures a single observation of an asset's price at a specific moment.

type Snapshot struct {
    // Date represents the specific timestamp.
    Date time.Time `format:"2006-01-02"`

    // Open represents the opening price for the
    // snapshot period.
    Open float64

    // High represents the highest price reached
    // during the snapshot period.
    High float64

    // Low represents the lowest price reached
    // during the snapshot period.
    Low float64

    // Close represents the closing price for the
    // snapshot period.
    Close float64

    // Volume represents the total trading activity for
    // the asset during the snapshot period.
    Volume float64
}

type Sync

Sync represents the configuration parameters for synchronizing assets between repositories.

type Sync struct {
    // Number of workers to use.
    Workers int

    // Delay between repository get requests to minimize the load to the remote server.
    Delay int

    // 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
}

func NewSync

func NewSync() *Sync

NewSync function initializes a new sync instance with the default parameters.

func (*Sync) Run

func (s *Sync) Run(source, target Repository, defaultStartDate time.Time) error

Run synchronizes assets between the source and target repositories using multi-worker concurrency.

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

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"`
}

func (*TiingoEndOfDay) ToSnapshot

func (e *TiingoEndOfDay) ToSnapshot() *Snapshot

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

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

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"`
}

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.

type TiingoRepository struct {
    Repository

    // BaseURL is the Tiingo API URL.
    BaseURL string

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

func NewTiingoRepository(apiKey string) *TiingoRepository

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

func (*TiingoRepository) Append

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

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

func (*TiingoRepository) Assets

func (*TiingoRepository) Assets() ([]string, error)

Assets returns the names of all assets in the repository.

func (*TiingoRepository) Get

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

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

func (*TiingoRepository) GetSince

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.

func (*TiingoRepository) LastDate

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