kline

package
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FifteenSecond = Interval(15 * time.Second)
	OneMin        = Interval(time.Minute)
	ThreeMin      = 3 * OneMin
	FiveMin       = 5 * OneMin
	TenMin        = 10 * OneMin
	FifteenMin    = 15 * OneMin
	ThirtyMin     = 30 * OneMin
	OneHour       = Interval(time.Hour)
	TwoHour       = 2 * OneHour
	FourHour      = 4 * OneHour
	SixHour       = 6 * OneHour
	EightHour     = 8 * OneHour
	TwelveHour    = 12 * OneHour
	OneDay        = 24 * OneHour
	ThreeDay      = 3 * OneDay
	SevenDay      = 7 * OneDay
	FifteenDay    = 15 * OneDay
	OneWeek       = 7 * OneDay
	TwoWeek       = 2 * OneWeek
	OneMonth      = 31 * OneDay
	OneYear       = 365 * OneDay
)

Consts here define basic time intervals

View Source
const (
	// ErrRequestExceedsExchangeLimits locale for exceeding rate limits message
	ErrRequestExceedsExchangeLimits = "requested data would exceed exchange limits please lower range or use GetHistoricCandlesEx"
)

Variables

View Source
var (
	// ErrUnsetInterval is an error for date range calculation
	ErrUnsetInterval = errors.New("cannot calculate range, interval unset")
	// ErrUnsupportedInterval returns when the provided interval is not supported by an exchange
	ErrUnsupportedInterval = errors.New("interval unsupported by exchange")
	// ErrCanOnlyDownscaleCandles returns when attempting to upscale candles
	ErrCanOnlyDownscaleCandles = errors.New("interval must be a longer duration to scale")
	// ErrWholeNumberScaling returns when old interval data cannot neatly fit into new interval size
	ErrWholeNumberScaling = errors.New("new interval must scale properly into new candle")

	// ErrNotFoundAtTime returned when looking up a candle at a specific time
	ErrNotFoundAtTime = errors.New("candle not found at time")

	// SupportedIntervals is a list of all supported intervals
	SupportedIntervals = []Interval{
		FifteenSecond,
		OneMin,
		ThreeMin,
		FiveMin,
		TenMin,
		FifteenMin,
		ThirtyMin,
		OneHour,
		TwoHour,
		FourHour,
		SixHour,
		EightHour,
		TwelveHour,
		OneDay,
		ThreeDay,
		SevenDay,
		FifteenDay,
		OneWeek,
		TwoWeek,
		OneMonth,
		OneYear,
	}
)

Functions

func StoreInDatabase

func StoreInDatabase(in *Item, force bool) (uint64, error)

StoreInDatabase returns Item from database seeded data

func TotalCandlesPerInterval

func TotalCandlesPerInterval(start, end time.Time, interval Interval) (out float64)

TotalCandlesPerInterval turns total candles per period for interval

Types

type ByDate

type ByDate []Candle

ByDate allows for sorting candle entries by date

func (ByDate) Len

func (b ByDate) Len() int

func (ByDate) Less

func (b ByDate) Less(i, j int) bool

func (ByDate) Swap

func (b ByDate) Swap(i, j int)

type Candle

type Candle struct {
	Time             time.Time
	Open             float64
	High             float64
	Low              float64
	Close            float64
	Volume           float64
	ValidationIssues string
}

Candle holds historic rate information.

func LoadFromGCTScriptCSV

func LoadFromGCTScriptCSV(file string) (out []Candle, errRet error)

LoadFromGCTScriptCSV loads kline data from a CSV file

type Error

type Error struct {
	Asset    asset.Item
	Pair     currency.Pair
	Interval Interval
	Err      error
}

Error struct to hold kline interval errors

func (*Error) Error

func (e *Error) Error() string

Error returns short interval unsupported message

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns interval unsupported message

type ExchangeCapabilitiesEnabled

type ExchangeCapabilitiesEnabled struct {
	Intervals   map[string]bool `json:"intervals,omitempty"`
	ResultLimit uint32
}

ExchangeCapabilitiesEnabled all kline related exchange enabled options

type ExchangeCapabilitiesSupported

type ExchangeCapabilitiesSupported struct {
	Intervals  bool
	DateRanges bool
}

ExchangeCapabilitiesSupported all kline related exchange supported options

type Interval

type Interval time.Duration

Interval type for kline Interval usage

func (Interval) Duration

func (i Interval) Duration() time.Duration

Duration returns interval casted as time.Duration for compatibility

func (*Interval) IntervalsPerYear

func (i *Interval) IntervalsPerYear() float64

IntervalsPerYear helps determine the number of intervals in a year used in CAGR calculation to know the amount of time of an interval in a year

func (Interval) Short

func (i Interval) Short() string

Short returns short string version of interval

func (Interval) String

func (i Interval) String() string

String returns numeric string

func (Interval) Word

func (i Interval) Word() string

Word returns text version of Interval

type IntervalData

type IntervalData struct {
	Start   IntervalTime
	End     IntervalTime
	HasData bool
}

IntervalData is used to monitor which candles contain data to determine if any data is missing

type IntervalRange

type IntervalRange struct {
	Start     IntervalTime
	End       IntervalTime
	Intervals []IntervalData
}

IntervalRange is a subset of candles based on exchange API request limits

type IntervalRangeHolder

type IntervalRangeHolder struct {
	Start  IntervalTime
	End    IntervalTime
	Ranges []IntervalRange
}

IntervalRangeHolder holds the entire range of intervals and the start end dates of everything

func CalculateCandleDateRanges

func CalculateCandleDateRanges(start, end time.Time, interval Interval, limit uint32) (*IntervalRangeHolder, error)

CalculateCandleDateRanges will calculate the expected candle data in intervals in a date range If an API is limited in the amount of candles it can make in a request, it will automatically separate ranges into the limit

func (*IntervalRangeHolder) DataSummary

func (h *IntervalRangeHolder) DataSummary(includeHasData bool) []string

DataSummary returns a summary of a data range to highlight where data is missing

func (*IntervalRangeHolder) HasDataAtDate

func (h *IntervalRangeHolder) HasDataAtDate(t time.Time) bool

HasDataAtDate determines whether a there is any data at a set date inside the existing limits

func (*IntervalRangeHolder) SetHasDataFromCandles

func (h *IntervalRangeHolder) SetHasDataFromCandles(c []Candle)

SetHasDataFromCandles will calculate whether there is data in each candle allowing any missing data from an API request to be highlighted

type IntervalTime

type IntervalTime struct {
	Time  time.Time
	Ticks int64
}

IntervalTime benchmarks demonstrate, see BenchmarkJustifyIntervalTimeStoringUnixValues1 && BenchmarkJustifyIntervalTimeStoringUnixValues2

func CreateIntervalTime

func CreateIntervalTime(tt time.Time) IntervalTime

CreateIntervalTime is a simple helper function to set the time twice

func (*IntervalTime) Equal

func (i *IntervalTime) Equal(tt time.Time) bool

Equal allows for easier unix comparison

type Item

type Item struct {
	Exchange        string
	Pair            currency.Pair
	Asset           asset.Item
	Interval        Interval
	Candles         []Candle
	SourceJobID     uuid.UUID
	ValidationJobID uuid.UUID
}

Item holds all the relevant information for internal kline elements

func ConvertToNewInterval

func ConvertToNewInterval(item *Item, newInterval Interval) (*Item, error)

ConvertToNewInterval allows the scaling of candles to larger candles eg convert OneDay candles to ThreeDay candles, if there are adequate candles incomplete candles are NOT converted eg an 4 OneDay candles will convert to one ThreeDay candle, skipping the fourth

func CreateKline

func CreateKline(trades []order.TradeHistory, interval Interval, p currency.Pair, a asset.Item, exchange string) (Item, error)

CreateKline creates candles out of trade history data for a set time interval

func LoadFromDatabase

func LoadFromDatabase(exchange string, pair currency.Pair, a asset.Item, interval Interval, start, end time.Time) (Item, error)

LoadFromDatabase returns Item from database seeded data

func (*Item) FillMissingDataWithEmptyEntries

func (k *Item) FillMissingDataWithEmptyEntries(i *IntervalRangeHolder)

FillMissingDataWithEmptyEntries amends a kline item to have candle entries for every interval between its start and end dates derived from ranges

func (*Item) FormatDates

func (k *Item) FormatDates()

FormatDates converts all date to UTC time

func (*Item) GetClosePriceAtTime

func (k *Item) GetClosePriceAtTime(t time.Time) (float64, error)

GetClosePriceAtTime returns the close price of a candle at a given time

func (*Item) RemoveDuplicates

func (k *Item) RemoveDuplicates()

RemoveDuplicates removes any duplicate candles

func (*Item) RemoveOutsideRange

func (k *Item) RemoveOutsideRange(start, end time.Time)

RemoveOutsideRange removes any candles outside the start and end date

func (*Item) SortCandlesByTimestamp

func (k *Item) SortCandlesByTimestamp(desc bool)

SortCandlesByTimestamp sorts candles by timestamp

type Kline

type Kline struct {
	Amount    float64   `json:"amount" description:"成交量"`
	Count     int       `json:"count" description:"成交笔数"`
	Open      float64   `json:"open" description:"开盘价"`
	Close     float64   `json:"close" description:"收盘价"`
	Low       float64   `json:"low" description:"最低价"`
	High      float64   `json:"high" description:"最高价"`
	Vol       float64   `json:"vol" description:"成交额,即SUM(每一笔成交价 * 该笔的成交数量)"`
	OpenTime  time.Time `json:"opentime" description:"开盘时间"`
	CloseTime time.Time `json:"closetime" description:"收盘时间"`
}

Kline K线的映射

Jump to

Keyboard shortcuts

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