Rolling window
TimeArrays.ta_rolling
— Functionta_rolling(f::Function, t_array::TimeArray, n::Int; kw...) -> TimeArray
Applies function f
in a sliding window with window size n
to the elements of t_array
. Function f
must accept a vector with elements of type V
as input and return a new value, which will be assigned to the corresponding timestamp of each window.
Keyword arguments
observations::Integer = n
: Number of observations in the rolling window to calculate the value (otherwiseNaN
).
Function signature:
f(x::AbstractVector{V})::Any = ...
Examples
julia> using Dates
julia> t_array = TimeArray([
TimeTick(DateTime("2024-01-01"), 1.0),
TimeTick(DateTime("2024-01-03"), 2.0),
TimeTick(DateTime("2024-01-04"), 3.0),
TimeTick(DateTime("2024-01-07"), 4.0),
TimeTick(DateTime("2024-01-09"), 5.0),
]);
julia> ta_rolling(sum, t_array, 3)
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-01T00:00:00, NaN)
TimeTick(2024-01-03T00:00:00, NaN)
TimeTick(2024-01-04T00:00:00, 6.0)
TimeTick(2024-01-07T00:00:00, 9.0)
TimeTick(2024-01-09T00:00:00, 12.0)
julia> ta_rolling(sum, t_array, 3; observations = 1)
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-01T00:00:00, 1.0)
TimeTick(2024-01-03T00:00:00, 3.0)
TimeTick(2024-01-04T00:00:00, 6.0)
TimeTick(2024-01-07T00:00:00, 9.0)
TimeTick(2024-01-09T00:00:00, 12.0)
ta_rolling(f::Function, t_array::TimeArray, p::Period; kw...) -> TimeArray
Applies function f
in a sliding window with window period p
to the elements of t_array
. Function f
must accept a vector with elements of type V
as input and return a new value, which will be assigned to the corresponding timestamp of each window.
Function signature:
f(x::AbstractVector{V})::Any = ...
Keyword arguments
observations::Integer = 1
: Number of observations in the rolling window to calculate the value (otherwiseNaN
).
The first elements of the t_array
during period p
will always be replaced by NaN
values (not enough elements in the window).
Examples
julia> using Dates
julia> t_array = TimeArray([
TimeTick(DateTime("2024-01-01"), 1.0),
TimeTick(DateTime("2024-01-03"), 2.0),
TimeTick(DateTime("2024-01-04"), 3.0),
TimeTick(DateTime("2024-01-07"), 4.0),
TimeTick(DateTime("2024-01-09"), 5.0),
]);
julia> ta_rolling(sum, t_array, Day(3))
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-01T00:00:00, NaN)
TimeTick(2024-01-03T00:00:00, 3.0)
TimeTick(2024-01-04T00:00:00, 5.0)
TimeTick(2024-01-07T00:00:00, 4.0)
TimeTick(2024-01-09T00:00:00, 9.0)
julia> ta_rolling(sum, t_array, Day(3); observations = 2)
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-01T00:00:00, NaN)
TimeTick(2024-01-03T00:00:00, 3.0)
TimeTick(2024-01-04T00:00:00, 5.0)
TimeTick(2024-01-07T00:00:00, NaN)
TimeTick(2024-01-09T00:00:00, 9.0)
TimeArrays.ta_sma
— Functionta_sma(t_array::TimeArray, n::Integer; kw...) -> TimeArray
ta_sma(t_array::TimeArray, p::Period; kw...) -> TimeArray
Applies Simple Moving Average algorithm with window size n
or period p
to the elements of t_array
.
Keyword arguments
- See
ta_rolling
for more information.
Examples
julia> using Dates
julia> t_array = TimeArray([
TimeTick(DateTime("2024-01-02"), 1.0),
TimeTick(DateTime("2024-01-03"), 2.0),
TimeTick(DateTime("2024-01-05"), 3.0),
TimeTick(DateTime("2024-01-06"), 4.0),
TimeTick(DateTime("2024-01-09"), 5.0),
]);
julia> ta_sma(t_array, 2)
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-02T00:00:00, NaN)
TimeTick(2024-01-03T00:00:00, 1.5)
TimeTick(2024-01-05T00:00:00, 2.5)
TimeTick(2024-01-06T00:00:00, 3.5)
TimeTick(2024-01-09T00:00:00, 4.5)
julia> t_array = TimeArray([
TimeTick(DateTime("2024-01-02"), 1.0),
TimeTick(DateTime("2024-01-03"), 2.0),
TimeTick(DateTime("2024-01-05"), 3.0),
TimeTick(DateTime("2024-01-06"), 4.0),
TimeTick(DateTime("2024-01-09"), 5.0),
]);
julia> ta_sma(t_array, Day(3))
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-02T00:00:00, NaN)
TimeTick(2024-01-03T00:00:00, 1.5)
TimeTick(2024-01-05T00:00:00, 2.5)
TimeTick(2024-01-06T00:00:00, 3.5)
TimeTick(2024-01-09T00:00:00, 5.0)
TimeArrays.ta_wma
— Functionta_wma(t_array::TimeArray, n::Integer; kw...) -> TimeArray
ta_wma(t_array::TimeArray, p::Period; kw...) -> TimeArray
Applies Weighted Moving Average algorithm with window size n
or period p
to the elements of t_array
.
Keyword arguments
- See
ta_rolling
for more information.
Examples
julia> using Dates
julia> t_array = TimeArray([
TimeTick(DateTime("2024-01-02"), 1.0),
TimeTick(DateTime("2024-01-03"), 2.0),
TimeTick(DateTime("2024-01-05"), 3.0),
TimeTick(DateTime("2024-01-06"), 4.0),
TimeTick(DateTime("2024-01-09"), 5.0),
]);
julia> ta_wma(t_array, 3)
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-02T00:00:00, NaN)
TimeTick(2024-01-03T00:00:00, NaN)
TimeTick(2024-01-05T00:00:00, 2.333333333333333)
TimeTick(2024-01-06T00:00:00, 3.333333333333333)
TimeTick(2024-01-09T00:00:00, 4.333333333333333)
julia> ta_wma(t_array, Day(3); observations = 2)
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-02T00:00:00, NaN)
TimeTick(2024-01-03T00:00:00, 1.6666666666666665)
TimeTick(2024-01-05T00:00:00, 2.6666666666666665)
TimeTick(2024-01-06T00:00:00, 3.6666666666666665)
TimeTick(2024-01-09T00:00:00, NaN)
TimeArrays.ta_ema
— Functionta_ema(t_array::TimeArray, window::Integer) -> TimeArray
Applies Exponential Moving Average algorithm with window size n
to the elements of t_array
.
Examples
julia> using Dates
julia> t_array = TimeArray([
TimeTick(DateTime("2024-01-02"), 1.0),
TimeTick(DateTime("2024-01-03"), 2.0),
TimeTick(DateTime("2024-01-05"), 3.0),
TimeTick(DateTime("2024-01-06"), 4.0),
TimeTick(DateTime("2024-01-09"), 5.0),
]);
julia> ta_ema(t_array, 3)
5-element TimeArray{DateTime, Float64}:
TimeTick(2024-01-02T00:00:00, 1.0)
TimeTick(2024-01-03T00:00:00, 1.5)
TimeTick(2024-01-05T00:00:00, 2.25)
TimeTick(2024-01-06T00:00:00, 3.125)
TimeTick(2024-01-09T00:00:00, 4.0625)
TimeArrays.ta_lag
— Functionta_lag(t_array::TimeArray, n::Integer) -> TimeArray
Shifts the values of t_array
elements by n
positions forward. Displaced elements that remain without a value are set to NaN
.
Examples
julia> using Dates
julia> t_array = TimeArray([
TimeTick(DateTime("2024-03-01"), 1.0),
TimeTick(DateTime("2024-03-02"), 2.0),
TimeTick(DateTime("2024-03-03"), 3.0),
TimeTick(DateTime("2024-03-04"), 4.0),
]);
julia> ta_lag(t_array, 2)
4-element TimeArray{DateTime, Float64}:
TimeTick(2024-03-01T00:00:00, NaN)
TimeTick(2024-03-02T00:00:00, NaN)
TimeTick(2024-03-03T00:00:00, 1.0)
TimeTick(2024-03-04T00:00:00, 2.0)
TimeArrays.ta_lead
— Functionta_lead(t_array::TimeArray{T,V}, n::Integer) -> TimeArray
Shifts the values of t_array
elements by n
positions backward. Displaced elements that remain without a value are set to NaN
.
Examples
julia> using Dates
julia> t_array = TimeArray([
TimeTick(DateTime("2024-03-01"), 1.0),
TimeTick(DateTime("2024-03-02"), 2.0),
TimeTick(DateTime("2024-03-03"), 3.0),
TimeTick(DateTime("2024-03-04"), 4.0),
]);
julia> ta_lead(t_array, 2)
4-element TimeArray{DateTime, Float64}:
TimeTick(2024-03-01T00:00:00, 3.0)
TimeTick(2024-03-02T00:00:00, 4.0)
TimeTick(2024-03-03T00:00:00, NaN)
TimeTick(2024-03-04T00:00:00, NaN)