Documentation ¶
Overview ¶
Package helper contains the helper 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/jonpastore/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
- func Abs[T Number](c <-chan T) <-chan T
- func Add[T Number](ac, bc <-chan T) <-chan T
- func AppendOrWriteToCsvFile[T any](fileName string, hasHeader bool, rows <-chan *T) error
- func Apply[T Number](c <-chan T, f func(T) T) <-chan T
- func Buffered[T any](c <-chan T, size int) <-chan T
- func ChanToJSON[T any](c <-chan T, w io.Writer) error
- func ChanToSlice[T any](c <-chan T) []T
- func Change[T Number](c <-chan T, before int) <-chan T
- func ChangePercent[T Number](c <-chan T, before int) <-chan T
- func ChangeRatio[T Number](c <-chan T, before int) <-chan T
- func CheckEquals[T comparable](inputs ...<-chan T) error
- func CloseAndLogError(closer io.Closer, message string)
- func CommonPeriod(periods ...int) int
- func Count[T Number, O any](from T, other <-chan O) <-chan T
- func DaysBetween(from, to time.Time) int
- func DecrementBy[T Number](c <-chan T, d T) <-chan T
- func Divide[T Number](ac, bc <-chan T) <-chan T
- func DivideBy[T Number](c <-chan T, d T) <-chan T
- func Drain[T any](c <-chan T)
- func Duplicate[T any](input <-chan T, count int) []<-chan T
- func Echo[T any](input <-chan T, last, count int) <-chan T
- func Field[T, S any](c <-chan *S, name string) (<-chan T, error)
- func Filter[T any](c <-chan T, p func(T) bool) <-chan T
- func First[T any](c <-chan T, count int) <-chan T
- func Gcd(values ...int) int
- func Head[T Number](c <-chan T, count int) <-chan T
- func IncrementBy[T Number](c <-chan T, i T) <-chan T
- func JSONToChan[T any](r io.Reader) <-chan T
- func KeepNegatives[T Number](c <-chan T) <-chan T
- func KeepPositives[T Number](c <-chan T) <-chan T
- func Last[T any](c <-chan T, count int) <-chan T
- func Lcm(values ...int) int
- func Map[F, T any](c <-chan F, f func(F) T) <-chan T
- func MapWithPrevious[F, T any](c <-chan F, f func(T, F) T, previous T) <-chan T
- func Multiply[T Number](ac, bc <-chan T) <-chan T
- func MultiplyBy[T Number](c <-chan T, m T) <-chan T
- func Operate[A any, B any, R any](ac <-chan A, bc <-chan B, o func(A, B) R) <-chan R
- func Operate3[A any, B any, C any, R any](ac <-chan A, bc <-chan B, cc <-chan C, o func(A, B, C) R) <-chan R
- func Pipe[T any](f <-chan T, t chan<- T)
- func Pow[T Number](c <-chan T, y T) <-chan T
- func ReadFromCsvFile[T any](fileName string, hasHeader bool) (<-chan *T, error)
- func RoundDigit[T Number](n T, d int) T
- func RoundDigits[T Number](c <-chan T, d int) <-chan T
- func Seq[T Number](from, to, increment T) <-chan T
- func Shift[T any](c <-chan T, count int, fill T) <-chan T
- func Sign[T Number](c <-chan T) <-chan T
- func Since[T comparable, R Number](c <-chan T) <-chan R
- func Skip[T any](c <-chan T, count int) <-chan T
- func SliceToChan[T any](slice []T) <-chan T
- func Sqrt[T Number](c <-chan T) <-chan T
- func Subtract[T Number](ac, bc <-chan T) <-chan T
- func SyncPeriod[T any](commonPeriod, period int, c <-chan T) <-chan T
- func Waitable[T any](wg *sync.WaitGroup, c <-chan T) <-chan T
- type Bst
- type BstNode
- type Csv
- type Float
- type Integer
- type Number
- type Report
- type ReportColumn
- type Ring
Constants ¶
const ( // CsvHeaderTag represents the parameter name for the column header. CsvHeaderTag = "header" // CsvFormatTag represents the parameter name for the column format. CsvFormatTag = "format" // DefaultDateTimeFormat denotes the default format of a date and time column. DefaultDateTimeFormat = "2006-01-02 15:04:05" )
const (
// DefaultReportDateFormat is the default date format used in the report.
DefaultReportDateFormat = "2006-01-02"
)
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
func Abs[T Number](c <-chan T) <-chan T
Abs calculates the absolute value of each value in a channel of float64.
Example:
abs := helper.Abs(helper.SliceToChan([]int{-10, 20, -4, -5})) fmt.Println(helper.ChanToSlice(abs)) // [10, 20, 4, 5]
func Add ¶
func Add[T Number](ac, bc <-chan T) <-chan T
Add adds each pair of values from the two input channels of float64 and returns a new channel containing the sums.
Example:
ac := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) bc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) actual := helper.ChanToSlice(helper.Add(ac, bc)) fmt.Println(actual) // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
func AppendOrWriteToCsvFile ¶
AppendOrWriteToCsvFile writes the provided rows of data to the specified file, appending to the existing file if it exists or creating a new one if it doesn't. In append mode, the function assumes that the existing file's column order matches the field order of the given row struct to ensure consistent data structure.
func Apply ¶
func Apply[T Number](c <-chan T, f func(T) T) <-chan T
Apply applies the given transformation function to each element in the input channel and returns a new channel containing the transformed values. The transformation function takes a float64 value as input and returns a float64 value as output.
Example:
timesTwo := helper.Apply(c, func(n int) int { return n * 2 })
func Buffered ¶
Buffered takes a channel of any type and returns a new channel of the same type with a buffer of the specified size. This allows the original channel to continue sending data even if the receiving end is temporarily unavailable.
Example:
func ChanToJSON ¶
ChanToJSON converts a channel of values into JSON format and writes it to the specified writer.
Example:
input := helper.SliceToChan([]int{2, 4, 6, 8}) var buffer bytes.Buffer err := helper.ChanToJSON(input, &buffer) fmt.Println(buffer.String()) // Output: [2,4,6,8,9]
func ChanToSlice ¶
func ChanToSlice[T any](c <-chan T) []T
ChanToSlice converts a channel of float64 to a slice of float64.
Example:
c := make(chan int, 4) c <- 1 c <- 2 c < -3 c <- 4 close(c) fmt.Println(helper.ChanToSlice(c)) // [1, 2, 3, 4]
func Change ¶
Change calculates the difference between the current value and the value N before.
Example:
input := []int{1, 2, 5, 5, 8, 2, 1, 1, 3, 4} output := helper.Change(helper.SliceToChan(input), 2) fmt.Println(helper.ChanToSlice(output)) // [4, 3, 3, -3, -7, -1, 2, 3]
func ChangePercent ¶
ChangePercent calculates the percentage change between the current value and the value N positions before.
Example:
c := helper.ChanToSlice([]float64{1, 2, 5, 5, 8, 2, 1, 1, 3, 4}) actual := helper.ChangePercent(c, 2)) fmt.Println(helper.ChanToSlice(actual)) // [400, 150, 60, -60, -87.5, -50, 200, 300]
func ChangeRatio ¶
ChangeRatio calculates the ratio change between the current value and the value N positions before.
Example:
c := helper.ChanToSlice([]float64{1, 2, 5, 5, 8, 2, 1, 1, 3, 4}) actual := helper.ChangeRatio(c, 2)) fmt.Println(helper.ChanToSlice(actual)) // [400, 150, 60, -60, -87.5, -50, 200, 300]
func CheckEquals ¶
func CheckEquals[T comparable](inputs ...<-chan T) error
CheckEquals determines whether the two channels are equal.
func CloseAndLogError ¶
CloseAndLogError attempts to close the closer and logs any error.
func CommonPeriod ¶
CommonPeriod calculates the smallest period at which all data channels can be synchronized
Example:
// Synchronize channels with periods 4, 2, and 3. commonPeriod := helper.CommonPeriod(4, 2, 3) // commonPeriod = 4 // Synchronize the first channel c1 := helper.Sync(commonPeriod, 4, c1) // Synchronize the second channel c2 := helper.Sync(commonPeriod, 2, c2) // Synchronize the third channel c3 := helper.Sync(commonPeriod, 3, c3)
func Count ¶
Count generates a sequence of numbers starting with a specified value, from, and incrementing by one until the given other channel continues to produce values.
Example:
other := make(chan int, 4) other <- 1 other <- 1 other <- 1 other <- 1 close(other) c := Count(0, other) fmt.Println(<- s) // 1 fmt.Println(<- s) // 2 fmt.Println(<- s) // 3 fmt.Println(<- s) // 4
func DaysBetween ¶
DaysBetween calculates the days between the given two times.
func DecrementBy ¶
func DecrementBy[T Number](c <-chan T, d T) <-chan T
DecrementBy decrements each element in the input channel by the specified decrement value and returns a new channel containing the decremented values.
Example:
input := helper.SliceToChan([]int{1, 2, 3, 4}) substractOne := helper.DecrementBy(input, 1) fmt.Println(helper.ChanToSlice(substractOne)) // [0, 1, 2, 3]
func Divide ¶
func Divide[T Number](ac, bc <-chan T) <-chan T
Divide takes two channels of float64 and divides the values from the first channel with the values from the second one. It returns a new channel containing the results of the division.
Example:
ac := helper.SliceToChan([]int{2, 4, 6, 8, 10}) bc := helper.SliceToChan([]int{2, 1, 3, 2, 5}) division := helper.Divide(ac, bc) fmt.Println(helper.ChanToSlice(division)) // [1, 4, 2, 4, 2]
func DivideBy ¶
func DivideBy[T Number](c <-chan T, d T) <-chan T
DivideBy divides each element in the input channel of float64 values by the given divider and returns a new channel containing the divided values.
Example:
half := helper.DivideBy(helper.SliceToChan([]int{2, 4, 6, 8}), 2) fmt.Println(helper.ChanToSlice(half)) // [1, 2, 3, 4]
func Duplicate ¶
Duplicate duplicates a given receive-only channel by reading each value coming out of that channel and sending them on requested number of new output channels.
Example:
expected := helper.SliceToChan([]float64{-10, 20, -4, -5}) outputs := helper.Duplicates[float64](helper.SliceToChan(expected), 2) fmt.Println(helper.ChanToSlice(outputs[0])) // [-10, 20, -4, -5] fmt.Println(helper.ChanToSlice(outputs[1])) // [-10, 20, -4, -5]
func Echo ¶
Echo takes a channel of numbers, repeats the specified count of numbers at the end by the specified count.
Example:
input := helper.SliceToChan([]int{2, 4, 6, 8}) output := helper.Echo(input, 2, 4)) fmt.Println(helper.ChanToSlice(output)) // [2, 4, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8]
func Field ¶
Field extracts a specific field from a channel of struct pointers and delivers it through a new channel.
func Filter ¶
Filter filters the items from the input channel based on the provided predicate function. The predicate function takes a float64 value as input and returns a boolean value indicating whether the value should be included in the output channel.
Example:
even := helper.Filter(c, func(n int) bool { return n%2 == 0 })
func First ¶
First takes a channel of values and returns a new channel containing the first N values.
func Head ¶
Head retrieves the specified number of elements from the given channel of float64 values and delivers them through a new channel.
Example:
c := helper.SliceToChan([]int{2, 4, 6, 8}) actual := helper.Head(c, 2) fmt.Println(helper.ChanToSlice(actual)) // [2, 4]
func IncrementBy ¶
func IncrementBy[T Number](c <-chan T, i T) <-chan T
IncrementBy increments each element in the input channel by the specified increment value and returns a new channel containing the incremented values.
Example:
input := []int{1, 2, 3, 4} actual := helper.IncrementBy(helper.SliceToChan(input), 1) fmt.Println(helper.ChanToSlice(actual)) // [2, 3, 4, 5]
func JSONToChan ¶
JSONToChan reads values from the specified reader in JSON format into a channel of values.
Example:
func KeepNegatives ¶
func KeepNegatives[T Number](c <-chan T) <-chan T
KeepNegatives processes a stream of float64 values, retaining negative values unchanged and replacing positive values with zero.
Example:
c := helper.SliceToChan([]int{-10, 20, 4, -5}) negatives := helper.KeepPositives(c) fmt.Println(helper.ChanToSlice(negatives)) // [-10, 0, 0, -5]
func KeepPositives ¶
func KeepPositives[T Number](c <-chan T) <-chan T
KeepPositives processes a stream of float64 values, retaining positive values unchanged and replacing negative values with zero.
Example:
c := helper.SliceToChan([]int{-10, 20, 4, -5}) positives := helper.KeepPositives(c) fmt.Println(helper.ChanToSlice(positives)) // [0, 20, 4, 0]
func Map ¶
func Map[F, T any](c <-chan F, f func(F) T) <-chan T
Map applies the given transformation function to each element in the input channel and returns a new channel containing the transformed values. The transformation function takes a float64 value as input and returns a float64 value as output.
Example:
timesTwo := helper.Map(c, func(n int) int { return n * 2 })
func MapWithPrevious ¶
func MapWithPrevious[F, T any](c <-chan F, f func(T, F) T, previous T) <-chan T
MapWithPrevious applies a transformation function to each element in an input channel, creating a new channel with the transformed values. It maintains a "memory" of the previous result, allowing the transformation function to consider both the current element and the outcome of the previous transformation. This enables functions that rely on accumulated state or sequential dependencies between elements.
Example:
sum := helper.MapWithPrevious(c, func(p, c int) int { return p + c }, 0)
func Multiply ¶
func Multiply[T Number](ac, bc <-chan T) <-chan T
Multiply takes two channels of float64 and multiples the values from the first channel with the values from the second channel. It returns a new channel containing the results of the multiplication.
Example:
ac := helper.SliceToChan([]int{1, 4, 2, 4, 2}) bc := helper.SliceToChan([]int{2, 1, 3, 2, 5}) multiplication := helper.Multiply(ac, bc) fmt.Println(helper.ChanToSlice(multiplication)) // [2, 4, 6, 8, 10]
func MultiplyBy ¶
func MultiplyBy[T Number](c <-chan T, m T) <-chan T
MultiplyBy multiplies each element in the input channel of float64 values by the given multiplier and returns a new channel containing the multiplied values.
Example:
c := helper.SliceToChan([]int{1, 2, 3, 4}) twoTimes := helper.MultiplyBy(c, 2) fmt.Println(helper.ChanToSlice(twoTimes)) // [2, 4, 6, 8]
func Operate ¶
Operate applies the provided operate function to corresponding values from two numeric input channels and sends the resulting values to an output channel.
Example:
add := helper.Operate(ac, bc, func(a, b int) int { return a + b })
func Operate3 ¶
func Operate3[A any, B any, C any, R any](ac <-chan A, bc <-chan B, cc <-chan C, o func(A, B, C) R) <-chan R
Operate3 applies the provided operate function to corresponding values from three numeric input channels and sends the resulting values to an output channel.
Example:
add := helper.Operate3(ac, bc, cc, func(a, b, c int) int { return a + b + c })
func Pipe ¶
func Pipe[T any](f <-chan T, t chan<- T)
Pipe function takes an input channel and an output channel and copies all elements from the input channel into the output channel.
Example:
input := helper.SliceToChan([]int{2, 4, 6, 8}) output := make(chan int) helper.Pipe(input, output) fmt.println(helper.ChanToSlice(output)) // [2, 4, 6, 8]
func Pow ¶
func Pow[T Number](c <-chan T, y T) <-chan T
Pow takes a channel of float64 values and returns the element-wise base-value exponential of y.
Example:
c := helper.SliceToChan([]int{2, 3, 5, 10}) squared := helper.Pow(c, 2) fmt.Println(helper.ChanToSlice(squared)) // [4, 9, 25, 100]
func ReadFromCsvFile ¶
ReadFromCsvFile creates a CSV instance, parses CSV data from the provided filename, maps the data to corresponding struct fields, and delivers it through the channel.
func RoundDigit ¶
RoundDigit rounds the given float64 number to d decimal places.
Example:
n := helper.RoundDigit(10.1234, 2) fmt.Println(n) // 10.12
func RoundDigits ¶
RoundDigits takes a channel of float64 numbers and rounds them to d decimal places.
Example:
c := helper.SliceToChan([]float64{10.1234, 5.678, 6.78, 8.91011}) rounded := helper.RoundDigits(c, 2) fmt.Println(helper.ChanToSlice(rounded)) // [10.12, 5.68, 6.78, 8.91]
func Seq ¶
func Seq[T Number](from, to, increment T) <-chan T
Seq generates a sequence of numbers starting with a specified value, from, and incrementing by a specified amount, increment, until a specified value, to, is reached or exceeded. The sequence includes both from and to.
Example:
s := Seq(1, 5, 1) defer close(s) fmt.Println(<- s) // 1 fmt.Println(<- s) // 2 fmt.Println(<- s) // 3 fmt.Println(<- s) // 4
func Shift ¶
Shift takes a channel of numbers, shifts them to the right by the specified count, and fills in any missing values with the provided fill value.
Example:
input := helper.SliceToChan([]int{2, 4, 6, 8}) output := helper.Shift(input, 4, 0) fmt.Println(helper.ChanToSlice(output)) // [0, 0, 0, 0, 2, 4, 6, 8]
func Sign ¶
func Sign[T Number](c <-chan T) <-chan T
Sign takes a channel of float64 values and returns their signs as -1 for negative, 0 for zero, and 1 for positive.
Example:
c := helper.SliceToChan([]int{-10, 20, -4, 0}) sign := helper.Sign(c) fmt.Println(helper.ChanToSlice(sign)) // [-1, 1, -1, 0]
func Since ¶
func Since[T comparable, R Number](c <-chan T) <-chan R
Since counts the number of periods since the last change of value in a channel of numbers.
func Skip ¶
Skip skips the specified number of elements from the given channel of float64.
Example:
c := helper.SliceToChan([]int{2, 4, 6, 8}) actual := helper.Skip(c, 2) fmt.Println(helper.ChanToSlice(actual)) // [6, 8]
func SliceToChan ¶
func SliceToChan[T any](slice []T) <-chan T
SliceToChan converts a slice of float64 to a channel of float64.
Example:
slice := []float64{2, 4, 6, 8} c := helper.SliceToChan(slice) fmt.Println(<- c) // 2 fmt.Println(<- c) // 4 fmt.Println(<- c) // 6 fmt.Println(<- c) // 8
func Sqrt ¶
func Sqrt[T Number](c <-chan T) <-chan T
Sqrt calculates the square root of each value in a channel of float64.
Example:
c := helper.SliceToChan([]int{9, 81, 16, 100}) sqrt := helper.Sqrt(c) fmt.Println(helper.ChanToSlice(sqrt)) // [3, 9, 4, 10]
func Subtract ¶
func Subtract[T Number](ac, bc <-chan T) <-chan T
Subtract takes two channels of float64 and subtracts the values from the second channel from the first one. It returns a new channel containing the results of the subtractions.
Example:
ac := helper.SliceToChan([]int{2, 4, 6, 8, 10}) bc := helper.SliceToChan([]int{1, 2, 3, 4, 5}) actual := helper.Subtract(ac, bc) fmt.Println(helper.ChanToSlice(actual)) // [1, 2, 3, 4, 5]
func SyncPeriod ¶
SyncPeriod adjusts the given channel to match the given common period.
Types ¶
type Bst ¶
type Bst[T Number] struct { // contains filtered or unexported fields }
Bst represents the binary search tree.
func (*Bst[T]) Insert ¶
func (b *Bst[T]) Insert(value T)
Insert adds a new value to the binary search tree.
func (*Bst[T]) Max ¶
func (b *Bst[T]) Max() T
Max function returns the maximum value in the binary search tree.
type BstNode ¶
type BstNode[T Number] struct { // contains filtered or unexported fields }
BstNode represents the binary search tree node.
type Csv ¶
type Csv[T any] struct { // contains filtered or unexported fields }
Csv represents the configuration for CSV reader and writer.
func NewCsv ¶
NewCsv function initializes a new CSV instance. The parameter hasHeader indicates whether the CSV contains a header row.
func (*Csv[T]) AppendToFile ¶
AppendToFile appends the provided rows of data to the end of the specified file, creating the file if it doesn't exist. In append mode, the function assumes that the existing file's column order matches the field order of the given row struct to ensure consistent data structure.
func (*Csv[T]) ReadFromFile ¶
ReadFromFile parses the CSV data from the provided file name, maps the data to corresponding struct fields, and delivers the resulting rows through the channel.
func (*Csv[T]) ReadFromReader ¶
ReadFromReader parses the CSV data from the provided reader, maps the data to corresponding struct fields, and delivers the resulting it through the channel.
func (*Csv[T]) WriteToFile ¶
WriteToFile creates a new file with the given name and writes the provided rows of data to it, overwriting any existing content.
type Report ¶
type Report struct { Title string Date <-chan time.Time Columns []ReportColumn Views [][]int DateFormat string GeneratedOn string }
Report generates an HTML file containing an interactive chart that visually represents the provided data and annotations.
The generated HTML file can be opened in a web browser to explore the data visually, interact with the chart elements, and view the associated annotations.
func NewReport ¶
NewReport takes a channel of time as the time axis and returns a new instance of the Report struct. This instance can later be used to add data and annotations and subsequently generate a report.
func (*Report) AddChart ¶
AddChart adds a new chart to the report and returns its unique identifier. This identifier can be used later to refer to the chart and add columns to it.
func (*Report) AddColumn ¶
func (r *Report) AddColumn(column ReportColumn, charts ...int)
AddColumn adds a new data column to the specified charts. If no chart is specified, it will be added to the main chart.
func (*Report) WriteToFile ¶
WriteToFile writes the generated report content to a file with the specified name. This allows users to conveniently save the report for later viewing or analysis.
type ReportColumn ¶
type ReportColumn interface { // Name returns the name of the report column. Name() string // Type returns the data type of the report column. Type() string // Role returns the role of the report column. Role() string // Value returns the next data value for the report column. Value() string }
ReportColumn defines the interface that all report data columns must implement. This interface ensures that different types of data columns can be used consistently within the report generation process.
func NewAnnotationReportColumn ¶
func NewAnnotationReportColumn(values <-chan string) ReportColumn
NewAnnotationReportColumn returns a new instance of a annotation column for a report.
func NewNumericReportColumn ¶
func NewNumericReportColumn[T Number](name string, values <-chan T) ReportColumn
NewNumericReportColumn returns a new instance of a numeric data column for a report.
type Ring ¶
type Ring[T any] struct { // contains filtered or unexported fields }
Ring represents a ring structure that can be instantiated using the NewRing function.
Example:
ring := helper.NewRing[int](2) fmt.Println(ring.Insert(1)) // 0 fmt.Println(ring.Insert(2)) // 0 fmt.Println(ring.Insert(3)) // 1 fmt.Println(ring.Insert(4)) // 2
func (*Ring[T]) Get ¶
Get retrieves the available value from the ring buffer. If empty, it returns the default value (T) and false.
Source Files ¶
- abs.go
- add.go
- annotation_report_column.go
- apply.go
- bst.go
- buffered.go
- chan_to_json.go
- chan_to_slice.go
- change.go
- change_percent.go
- change_ratio.go
- check.go
- closer.go
- count.go
- csv.go
- days_between.go
- decrement_by.go
- divide.go
- divide_by.go
- drain.go
- duplicate.go
- echo.go
- field.go
- filter.go
- first.go
- gcd.go
- head.go
- helper.go
- increment_by.go
- json_to_chan.go
- keep_negatives.go
- keep_positives.go
- last.go
- lcm.go
- map.go
- map_with_previous.go
- multiply.go
- multiply_by.go
- numeric_report_column.go
- operate.go
- operate3.go
- pipe.go
- pow.go
- reflect.go
- report.go
- ring.go
- round_digit.go
- round_digits.go
- seq.go
- shift.go
- sign.go
- since.go
- skip.go
- slice_to_chan.go
- sqrt.go
- subtract.go
- sync.go
- waitable.go