models

package
v0.0.0-...-bbd37fe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PositionTypeLong  = "long"
	PositionTypeShort = "short"
)
View Source
const AttrCluster = "cluster"
View Source
const (
	Resolution1Min = "1m"
)
View Source
const SecurityKeyName = "symbol"

Variables

This section is empty.

Functions

func CompareBarWithTimestamp

func CompareBarWithTimestamp(bar *Bar, t time.Time) int

func CompareBarsByTimestamp

func CompareBarsByTimestamp(a, b *Bar) int

func QuantityMeanStddev

func QuantityMeanStddev(q *Quantity) (cluster.Point, error)

Types

type Bar

type Bar struct {
	Timestamp  time.Time `json:"t"`
	Volume     uint64    `json:"v"`
	TradeCount uint64    `json:"n"`
	VWAP       float64   `json:"vw"`

	*OHLC
}

func NewBarFromAlpaca

func NewBarFromAlpaca(alpacaBar marketdata.Bar) *Bar

func (*Bar) Date

func (b *Bar) Date() date.Date

func (*Bar) HeikinAshi

func (b *Bar) HeikinAshi(prev *OHLC) *OHLC

func (*Bar) Localize

func (b *Bar) Localize()

type BarSet

type BarSet struct {
	Date string `json:"date" bson:"_id"`
	Bars Bars   `json:"bars"`
}

func (*BarSet) GetKey

func (db *BarSet) GetKey() string

func (*BarSet) Validate

func (db *BarSet) Validate() []error

type Bars

type Bars []*Bar

func LoadBars

func LoadBars(r io.Reader) (Bars, error)

func LoadBarsFromFile

func LoadBarsFromFile(fpath string) (Bars, error)

func LoadRunBars

func LoadRunBars(
	ctx context.Context,
	symbol string,
	ts timespan.TimeSpan,
	loc *time.Location,
	load LoadBarsFunc,
	warmupPeriod int,
) (Bars, error)

func (Bars) BinarySearch

func (bars Bars) BinarySearch(t time.Time) (int, bool)

func (Bars) ClosePrices

func (bars Bars) ClosePrices() []float64

func (Bars) IndexForward

func (bars Bars) IndexForward(t time.Time) (int, bool)

func (Bars) IndexReverse

func (bars Bars) IndexReverse(t time.Time) (int, bool)

func (Bars) Last

func (bars Bars) Last() *Bar

func (Bars) LastN

func (bars Bars) LastN(n int) Bars

func (Bars) Localize

func (bars Bars) Localize()

func (Bars) NextN

func (bars Bars) NextN(index, n int) Bars

func (Bars) Store

func (bars Bars) Store(w io.Writer) error

func (Bars) StoreToFile

func (bars Bars) StoreToFile(fpath string) error

func (Bars) TimeSpan

func (bars Bars) TimeSpan() timespan.TimeSpan

func (Bars) Timestamps

func (bars Bars) Timestamps() []time.Time

type Collection

type Collection interface {
	GetInfo() *CollectionInfo
	GetLocation() *time.Location
	GetFirstDate() date.Date
	GetLastDate() date.Date

	IsEmpty() bool

	LoadBars(start, endIncl date.Date) (Bars, error)
	StoreBars(Bars) error
}

type CollectionInfo

type CollectionInfo struct {
	Symbol     string    `json:"symbol"`
	Resolution string    `json:"resolution"`
	TimeZone   string    `json:"timeZone"`
	StartDate  date.Date `json:"startDate"`
	CoreHours  *Hours    `json:"coreHours"`
}

type Direction

type Direction int
const (
	DirNone Direction = iota
	DirDown
	DirUp
)

func (Direction) String

func (d Direction) String() string

type Hours

type Hours struct {
	Open  *TimeOfDay `json:"open"`
	Close *TimeOfDay `json:"close"`
}

type LoadBarsFunc

type LoadBarsFunc func(ctx context.Context, d date.Date) (Bars, error)

type MakePointFunc

type MakePointFunc func(q *Quantity) (cluster.Point, error)

type OHLC

type OHLC struct {
	Open  float64 `json:"o"`
	High  float64 `json:"h"`
	Low   float64 `json:"l"`
	Close float64 `json:"c"`
}

func (*OHLC) Float64s

func (ohlc *OHLC) Float64s() []float64

func (*OHLC) HL2

func (ohlc *OHLC) HL2() float64

func (*OHLC) HLC3

func (ohlc *OHLC) HLC3() float64

func (*OHLC) HLCC4

func (ohlc *OHLC) HLCC4() float64

func (*OHLC) OCC3

func (ohlc *OHLC) OCC3() float64

func (*OHLC) OHLC4

func (ohlc *OHLC) OHLC4() float64

type Position

type Position struct {
	Type       string     `json:"type"`
	Entry      *TimePrice `json:"entry"`
	Exit       *TimePrice `json:"exit"`
	ExitReason string     `json:"exitReason"`
	ClosedPL   float64    `json:"closedPL"`
}

func NewLongPosition

func NewLongPosition(t time.Time, price float64) *Position

func NewShortPosition

func NewShortPosition(t time.Time, price float64) *Position

func (*Position) Close

func (p *Position) Close(t time.Time, price float64, reason string)

func (*Position) IsOpen

func (p *Position) IsOpen() bool

func (*Position) Localize

func (p *Position) Localize()

func (*Position) OpenProfitLoss

func (p *Position) OpenProfitLoss(currentPrice float64) (float64, bool)

type PositionType

type PositionType int

type Positions

type Positions []*Position

func (Positions) Analyze

func (ps Positions) Analyze() *PositionsAnalysis

func (Positions) Localize

func (ps Positions) Localize()

func (Positions) Store

func (ps Positions) Store(w io.Writer) error

func (Positions) StoreToFile

func (ps Positions) StoreToFile(fpath string) error

type PositionsAnalysis

type PositionsAnalysis struct {
	Winning, TotalPL float64
}

type Quantity

type Quantity struct {
	Name       string           `json:"name"`
	Records    []QuantityRecord `json:"records"`
	Attributes map[string]any   `json:"attributes"`
}

func NewQuantity

func NewQuantity(name string, records ...QuantityRecord) *Quantity

func (*Quantity) AddRecord

func (q *Quantity) AddRecord(r QuantityRecord)

func (*Quantity) DropRecordsBefore

func (q *Quantity) DropRecordsBefore(t time.Time)

func (*Quantity) FindRecord

func (q *Quantity) FindRecord(t time.Time) (QuantityRecord, bool)

func (*Quantity) FindRecordsAfter

func (q *Quantity) FindRecordsAfter(t time.Time) []QuantityRecord

func (*Quantity) IsEmpty

func (q *Quantity) IsEmpty() bool

func (*Quantity) RecordValues

func (q *Quantity) RecordValues() []float64

func (*Quantity) SortByTime

func (q *Quantity) SortByTime()

type QuantityRecord

type QuantityRecord = TimeValue[float64]

type Security

type Security struct {
	Symbol   string `json:"symbol" bson:"_id"`
	Days     int    `json:"days"`
	TimeZone string `json:"timeZone"`
}

func (*Security) GetKey

func (s *Security) GetKey() string

func (*Security) Validate

func (s *Security) Validate() []error

type TimeOfDay

type TimeOfDay struct {
	Hour   int
	Minute int
}

func (*TimeOfDay) MarshalJSON

func (tod *TimeOfDay) MarshalJSON() ([]byte, error)

func (*TimeOfDay) MinuteOfDay

func (tod *TimeOfDay) MinuteOfDay() int

func (*TimeOfDay) On

func (tod *TimeOfDay) On(d date.Date, loc *time.Location) time.Time

func (*TimeOfDay) Parse

func (tod *TimeOfDay) Parse(str string) error

func (*TimeOfDay) String

func (tod *TimeOfDay) String() string

func (*TimeOfDay) UnmarshalJSON

func (tod *TimeOfDay) UnmarshalJSON(d []byte) error

type TimePrice

type TimePrice struct {
	Time  time.Time
	Price float64
}

type TimeSeries

type TimeSeries struct {
	Quantities []*Quantity `json:"quantities"`
}

func NewTimeSeries

func NewTimeSeries() *TimeSeries

func (*TimeSeries) AddQuantity

func (ts *TimeSeries) AddQuantity(q *Quantity)

func (*TimeSeries) Cluster

func (ts *TimeSeries) Cluster(k int, makePoint MakePointFunc) error

func (*TimeSeries) DropRecordsBefore

func (ts *TimeSeries) DropRecordsBefore(t time.Time)

func (*TimeSeries) FindQuantity

func (ts *TimeSeries) FindQuantity(name string) (*Quantity, bool)

func (*TimeSeries) IsEmpty

func (ts *TimeSeries) IsEmpty() bool

func (*TimeSeries) SortByTime

func (ts *TimeSeries) SortByTime() bool

type TimeValue

type TimeValue[TValue any] struct {
	Time  time.Time `json:"t"`
	Value TValue    `json:"v"`
}

func NewTimeValue

func NewTimeValue[TValue any](t time.Time, v TValue) TimeValue[TValue]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL