### Setup Plotting Environment Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/plotting.md Initializes the plotting environment and loads necessary packages. This is a prerequisite for all plotting examples. ```julia using Plots, MarketData, TimeSeries default(; show=false) # hide ENV["GKSwstype"] = "100" # hide gr() t = yahoo(:GOOG, YahooOpt(; period1=now() - Month(1))) ``` -------------------------------- ### Add MarketData Package Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/getting_started.md Install the MarketData package, which provides historical financial data. ```julia julia> Pkg.add("MarketData") ``` -------------------------------- ### Add TimeSeries Package Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/getting_started.md Install the TimeSeries package using the Julia package manager. ```julia julia> Pkg.add("TimeSeries") ``` -------------------------------- ### Create a TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Initializes a TimeArray with hourly timestamps and random data for demonstration purposes. This setup is required before using the `retime` function. ```julia using Plots, Dates, TimeSeries default(show = false) # hide ENV["GKSwstype"] = "100" # hide gr() timestamps = range(DateTime(2020, 1, 1), length = 7*24, step = Hour(1)) ta = TimeArray(timestamps, cumsum(randn(7*24)), [:a]) ``` -------------------------------- ### Install TimeSeries.jl Package Source: https://github.com/juliastats/timeseries.jl/blob/master/README.md Use this snippet to add the TimeSeries.jl package to your Julia environment. Ensure Julia and the Pkg manager are set up. ```julia using Pkg Pkg.add("TimeSeries") ``` -------------------------------- ### Create TimeArray with Date Timestamps Source: https://github.com/juliastats/timeseries.jl/blob/master/README.md Example demonstrating how to create a TimeArray using a range of Dates as timestamps and random data. Requires TimeSeries and Dates packages. ```julia using TimeSeries using Dates dates = Date(2018, 1, 1):Day(1):Date(2018, 12, 31) ta = TimeArray(dates, rand(length(dates))) ``` -------------------------------- ### Create TimeArray with DateTime Timestamps Source: https://github.com/juliastats/timeseries.jl/blob/master/README.md Example demonstrating how to create a TimeArray using a range of DateTimes as timestamps and random data. Requires TimeSeries and Dates packages. ```julia using TimeSeries using Dates timestamps = DateTime(2018, 1, 1):Hour(1):DateTime(2018, 12, 31) ta = TimeArray(timestamps, rand(length(timestamps))) ``` -------------------------------- ### Optimized Aggregation with Basecall Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Uses a base method for optimized performance when an aggregation function is available. This example uses `cumsum` for cumulative summation. ```julia using TimeSeries using MarketData basecall(cl, cumsum) ``` -------------------------------- ### Calculating Moving Average Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Performs a moving window calculation, such as a moving average. This example calculates a 10-period moving average using the `mean` function. ```julia using TimeSeries using MarketData using Statistics moving(mean, cl, 10) ``` -------------------------------- ### Aggregating Values Up To Present Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Aggregates values from the beginning of the time series up to the present timestamp using a specified function. This example calculates the cumulative sum. ```julia using TimeSeries using MarketData upto(sum, cl) ``` -------------------------------- ### Leading Time Series Data by an Arbitrary Distance Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Leads the time series data by a specified number of days. This example demonstrates leading by 499 days, placing the last observation into the first date's value slot. ```julia lead(cl, 499) ``` -------------------------------- ### TimeArray constructor with existing TimeArray in TimeSeries.jl Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Shows how to create a new `TimeArray` by copying an existing one, with options to override timestamp, values, column names, and metadata. The example demonstrates updating the metadata. ```julia TimeArray(ta::TimeArray; timestamp=..., values=..., colnames=..., meta=...) ``` ```julia julia> meta(cl) "AAPL" julia> cl′ = TimeArray(cl; meta=:AAPL); julia> meta(cl′) :AAPL ``` -------------------------------- ### Get First N Elements using `head` Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md The `head` method returns the first value in a TimeArray by default. The number of elements to return can be specified as the second argument. ```julia using TimeSeries using MarketData head(cl) ``` -------------------------------- ### moving Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Applies a function over a sliding window of a specified size. For example, a 10-period moving average. ```APIDOC ## moving ### Description Applies a function over a sliding window of a specified size. For example, a 10-period moving average. ### Method moving(func, time_array, window_size) ### Parameters - **func** (Function) - The function to apply to the window (e.g., `mean`, `sum`). - **time_array** (TimeArray) - The input time series. - **window_size** (Integer) - The size of the sliding window. ### Request Example ```julia moving(mean, cl, 10) ``` ``` -------------------------------- ### Truncate TimeArray from a Specific Date using `from` Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md The `from` method truncates a TimeArray starting from the specified date. ```julia using TimeSeries using MarketData from(cl, Date(2001, 12, 27)) ``` -------------------------------- ### from Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Truncates a TimeArray starting from the specified date. ```APIDOC ## `from` The `from` method truncates a `TimeArray` starting with the date passed to the method: ```@repl using TimeSeries using MarketData from(cl, Date(2001, 12, 27)) ``` ``` -------------------------------- ### 2D getindex for TimeArray in TimeSeries.jl Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Examples of using 2D indexing with `getindex` on a `TimeArray` to select specific rows and columns. Supports slicing by row range and specific column names. ```julia ohlc[1:42, [:High, :Low]] ``` ```julia ohlc[42:end, [:High, :Low]] ``` ```julia ohlc[:, [:High, :Low]] ``` ```julia ohlc[42, [:High, :Low]] ``` -------------------------------- ### Inner Join TimeArrays Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/combine.md Performs an inner join on two TimeArrays, keeping only timestamps common to both. This example merges AAPL and CAT stock data. ```julia using TimeSeries using MarketData AppleCat = merge(AAPL, CAT); length(AppleCat) ``` -------------------------------- ### Get Last N Elements using `tail` Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md The `tail` method returns the last value in a TimeArray by default. The number of elements to return can be specified as the second argument. ```julia using TimeSeries using MarketData tail(cl) ``` ```julia using TimeSeries using MarketData tail(cl, 3) ``` -------------------------------- ### Write TimeArray to CSV Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/readwrite.md Use `writetimearray` to save a TimeArray object to a CSV file. The example shows writing the first 5 elements of a TimeArray named 'cl'. ```julia writetimearray(cl[1:5], "close.csv") ``` -------------------------------- ### Compress Daily Data to Monthly with Last Value Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/combine.md Compresses daily closing prices to monthly, using the last known value for each month. Requires `TimeSeries` and `MarketData` packages. ```julia using TimeSeries using MarketData collapse(cl, month, last) ``` -------------------------------- ### Create Dummy TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/getting_started.md Generate a TimeArray with random data for a full year. This requires the Dates and TimeSeries packages. ```julia using Dates dates = Date(2018, 1, 1):Day(1):Date(2018, 12, 31) ta = TimeArray(dates, rand(length(dates))) ``` -------------------------------- ### Calculating Percent Change (Log Returns) Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Calculates the log returns of a time series. Log returns are useful for downstream calculations as they can be added instead of multiplied. ```julia percentchange(cl, :log) ``` -------------------------------- ### Calculating Percent Change (Simple Returns) Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Calculates the percent change between consecutive timestamps, also known as simple returns. This is the default behavior. ```julia using MarketData percentchange(cl) ``` -------------------------------- ### Apply Reduction Functions to TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Demonstrates applying standard reduction functions like `sum`, `mean`, `std`, and `var` to `TimeArray` objects. These can be applied to the entire array or specific dimensions. ```julia sum(ta) sum(ta, 2) ``` -------------------------------- ### Visualize Downsampling Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Visualizes the original TimeArray alongside the downsampled version, showing the effect of the mean aggregation over 6-hour intervals. ```julia plot(ta) plot!(ta_) savefig("retime-downsample.svg"); nothing # hide ``` -------------------------------- ### Date and DateTime Row Indexing for Time Series Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/indexing.md Index time series data using specific Date objects or ranges of Dates. Requires the Dates package. ```julia using MarketData using Dates ohlc[Date(2000, 1, 3)] ``` ```julia using MarketData using Dates ohlc[Date(2000, 1, 3):Day(1):Date(2000, 2, 4)] ``` -------------------------------- ### upto Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Applies an aggregation function cumulatively from the beginning of the time series to the present timestamp. ```APIDOC ## upto ### Description Applies an aggregation function cumulatively from the beginning of the time series to the present timestamp. ### Method upto(func, time_array) ### Parameters - **func** (Function) - The aggregation function to apply (e.g., `sum`, `prod`). - **time_array** (TimeArray) - The input time series. ### Request Example ```julia upto(sum, cl) ``` ``` -------------------------------- ### Displaying Time Series Data Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Shows the first three values of the cl time series. This is a common way to inspect time series data. ```julia using MarketData cl[1:3] ``` -------------------------------- ### Use TimeArray as Dictionary Key Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Demonstrates using a `TimeArray` as a key in a `Dict`. `isequal` and `hash` are now supported, allowing `TimeArray` objects to be correctly hashed and compared within dictionaries. ```julia d = Dict(cl => 42) d[cl] d[copy(cl)] ``` -------------------------------- ### Find Dates Meeting a Condition using `findwhen` Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Use `findwhen` to test a condition and return a vector of Dates or DateTimes where the condition is true. ```julia using TimeSeries using MarketData green = findwhen(ohlc[:Close] .> ohlc[:Open]); typel(green) ``` ```julia using TimeSeries using MarketData ohlc[green] ``` -------------------------------- ### Moving function with multi-column input in TimeSeries.jl Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Demonstrates using the `moving` function with multi-column input for user-defined functions. The input `ohlc` is a 500x4 `TimeArray`, and `A` will be a 10x4 `TimeArray` within the closure. ```julia moving(ohlc, 10; dims=2, colnames=[:A, ...]) do # given that `ohlc` is a 500x4 `TimeArray`, # size(A) is (10, 4) ... end ``` -------------------------------- ### Load TimeArray from CSV Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/tables.md Load a TimeArray from a CSV file using `CSV.File` and the TimeArray constructor. Specify the timestamp column using the `timestamp` keyword argument. ```julia using CSV TimeArray(CSV.File(filename); timestamp=:timestamp) ``` -------------------------------- ### Visualize Upsampling Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Plots the original TimeArray and the upsampled TimeArray to visually compare the results of linear interpolation. ```julia plot(ta) plot!(ta_) savefig("retime-upsampling.svg"); nothing # hide ``` -------------------------------- ### Aggregate by Time Period using `when` Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Use `when` to aggregate elements from a TimeArray into specific time periods like Mondays or October. The period argument must be a valid Date method. ```julia using TimeSeries using MarketData when(cl, dayofweek, 1) ``` ```julia using TimeSeries using MarketData when(cl, dayname, "Monday") ``` -------------------------------- ### Retime with Irregular Timestamps Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Demonstrates retime on a TimeArray with irregular timestamps, or using a vector of irregular timestamps. The function automatically handles upsampling or downsampling based on the input. ```julia new_timestamps = vcat( range(DateTime(2020, 1, 1), DateTime(2020, 1, 2)-Minute(15), step = Minute(15)), range(DateTime(2020, 1, 2), DateTime(2020, 1, 3), step = Hour(1)), ) retime(ta, new_timestamps) ``` -------------------------------- ### percentchange Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Calculates the percent change (simple or log returns) between consecutive points in a time series. Defaults to simple returns. ```APIDOC ## percentchange ### Description Calculates the percent change (simple or log returns) between consecutive points in a time series. Defaults to simple returns. ### Method percentchange(time_array, type=:simple) ### Parameters - **time_array** (TimeArray) - The input time series. - **type** (Symbol) - The type of return to calculate. Can be `:simple` or `:log`. Defaults to `:simple`. ### Request Example ```julia percentchange(cl) percentchange(cl, :log) ``` ``` -------------------------------- ### split Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Splits data by a given function, e.g. Dates.day, into periods. ```APIDOC ## Splitting by period Splitting data by a given function, e.g. `Dates.day` into periods. ```@repl using TimeSeries using MarketData split(cl, Dates.day) ``` ``` -------------------------------- ### Compress Data with Independent Timestamp and Value Functions Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/combine.md Compresses time series data by independently specifying the function to determine the timestamp and the function to determine the value. Requires the `Statistics` package. ```julia using Statistics collapse(cl, month, last, mean) ``` -------------------------------- ### basecall Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md A performance-optimized method for applying cumulative functions, often used as an alternative to `upto` for specific operations like summation. ```APIDOC ## basecall ### Description A performance-optimized method for applying cumulative functions, often used as an alternative to `upto` for specific operations like summation. ### Method basecall(time_array, func) ### Parameters - **time_array** (TimeArray) - The input time series. - **func** (Function) - The cumulative function to apply (e.g., `cumsum`). ### Request Example ```julia basecall(cl, cumsum) ``` ``` -------------------------------- ### Base Method Extensions for TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/api.md TimeSeries.jl extends several Base methods to provide enhanced functionality when working with TimeArray objects. These extensions allow for seamless integration with existing Julia workflows. ```APIDOC ## Base Method Extensions TimeSeries.jl extends several Base methods to work with `TimeArray` objects: - `Base.hcat(::TimeArray, ::TimeArray)` - `Base.hcat(::TimeArray, ::TimeArray, ::Vararg{TimeArray})` - `Base.vcat(::TimeArray...)` - `Base.map` - `Base.split(::TimeSeries.TimeArray, ::Function)` - `Base.:+` - `Base.:-` - `Base.:(==)` - `Base.eachrow` - `Base.eachcol` ``` -------------------------------- ### when Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Aggregates elements from a TimeArray into specific time periods based on a provided Date method and value. ```APIDOC ## `when` The `when` methods allows aggregating elements from a `TimeArray` into specific time periods, such as Mondays or the month of October: ```@repl using TimeSeries using MarketData when(cl, dayofweek, 1) when(cl, dayname, "Monday") ``` The period argument holds a valid `Date` method. Below are currently available alternatives. | Dates method | Example | |:------------------ |:------------------------ | | `day` | Jan 3, 2000 = 3 | | `dayname` | Jan 3, 2000 = "Monday" | | `week` | Jan 3, 2000 = 1 | | `month` | Jan 3, 2000 = 1 | | `monthname` | Jan 3, 2000 = "January" | | `year` | Jan 3, 2000 = 2000 | | `dayofweek` | Monday = 1 | | `dayofweekofmonth` | Fourth Monday in Jan = 4 | | `dayofyear` | Dec 31, 2000 = 366 | | `quarterofyear` | Dec 31, 2000 = 4 | | `dayofquarter` | Dec 31, 2000 = 93 | ``` -------------------------------- ### head Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Returns the first value(s) in a TimeArray. Defaults to the first value, but can be modified to return a specified number of elements from the top. ```APIDOC ## `head` The `head` method defaults to returning only the first value in a `TimeArray`. By selecting the second positional argument to a different value, the user can modify how many from the top are selected: ```@repl using TimeSeries using MarketData head(cl) ``` ``` -------------------------------- ### findall Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Tests a condition and returns a vector of Int representing the row in the array where the condition is true. ```APIDOC ## `findall` The `findall` method tests a condition and returns a vector of `Int` representing the row in the array where the condition is `true`: ```@repl using TimeSeries using MarketData red = findall(ohlc[:Close] .< ohlc[:Open]); typeof(red) ohlc[red] ``` The following example won't create a temporary `Bool` vector, and gains better performance. ```@setup findall using TimeSeries using MarketData ``` ```@repl findall findall(>(100), cl) ``` ``` -------------------------------- ### findwhen Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Tests a condition and returns a vector of Date or DateTime where the condition is true. ```APIDOC ## `findwhen` The `findwhen` method test a condition and returns a vector of `Date` or `DateTime` where the condition is `true`: ```@repl using TimeSeries using MarketData green = findwhen(ohlc[:Close] .> ohlc[:Open]); typeof(green) ohlc[green] ``` ``` -------------------------------- ### Find Row Indices Meeting a Condition using `findall` Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md The `findall` method tests a condition and returns a vector of Int representing the row indices where the condition is true. This method can be more performant as it avoids creating temporary boolean vectors. ```julia using TimeSeries using MarketData red = findall(ohlc[:Close] .< ohlc[:Open]); typel(red) ``` ```julia using TimeSeries using MarketData ohlc[red] ``` ```julia findall(>(100), cl) ``` -------------------------------- ### Leading Time Series Data Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Applies a lead of one time unit to the time series data. The last observation is omitted as there is no subsequent value to lead. ```julia using TimeSeries using MarketData lead(cl[1:3]) ``` -------------------------------- ### Left Join TimeArrays Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/combine.md Performs a left join on two TimeArrays, including all timestamps from the first TimeArray and matching timestamps from the second, introducing NaN for non-matches. ```julia merge(op[1:3], cl[2:4]; method=:left) ``` -------------------------------- ### Mixed Indexing for Time Series Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/indexing.md Combine row and column indexing for more specific data retrieval. Supports combinations like integer ranges with symbols, or symbols with Date objects. ```julia using MarketData using Dates ohlc[1:3, :Open] ``` ```julia using MarketData using Dates ohlc[:Open][Date(2000, 1, 3)] ``` -------------------------------- ### Define Custom Mapping Function for TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Illustrates how to use `map` with a callable object (struct with a call method) to transform `TimeArray` data. This allows for flexible, custom data manipulation based on timestamps and values. ```julia struct T end (::T)(timestamp, x) = (timestamp, x + 42) t = T() map(t, ta) ``` -------------------------------- ### Integer Row Indexing for Time Series Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/indexing.md Index time series data using single integers, ranges, ranges with steps, or combinations of ranges and integers. Supports indexing the last row with 'end'. ```julia using MarketData ohlc[1] ``` ```julia using MarketData ohlc[1:3] ``` ```julia using MarketData ohlc[1:2:10] ``` ```julia using MarketData ohlc[[1:3; 8]] ``` ```julia using MarketData ohlc[end] ``` -------------------------------- ### Symbol Column Indexing for Time Series Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/indexing.md Index time series data by column name using symbols. Supports single symbols, multiple symbols, or a collection of symbols. ```julia using MarketData using Dates ohlc[:Open] ``` ```julia using MarketData using Dates ohlc[:Open, :Close] ``` ```julia using MarketData using Dates cols = [:Open, :Close] ohlc[cols] ``` -------------------------------- ### Retime Using a New Timestamp Vector Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Applies retime using a custom vector of timestamps, allowing for irregular or specific timestamp alignments. Ensure the new timestamps cover the desired range. ```julia new_timestamps = range(DateTime(2020, 1, 1), DateTime(2020, 1, 2), step = Minute(15)) retime(ta, new_timestamps) ``` -------------------------------- ### Downsampling with Mean Aggregation Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Applies downsampling to the TimeArray, aggregating data within each interval using the mean. The `downsample` argument should be set to `Mean()` or `:mean`. ```julia ta_ = retime(ta, Hour(6), downsample=Mean()) ``` -------------------------------- ### Extrapolation with Missing Values Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Handles extrapolation at the beginning and end of the time series by filling with missing values. Use `MissingExtrapolate()` or `:missing` for the `extrapolate` argument. ```julia new_timestamps = range(DateTime(2019, 12, 31), DateTime(2020, 1, 2), step = Minute(15)) ta_ = retime(ta, new_timestamps, extrapolate=MissingExtrapolate()) ``` -------------------------------- ### Apply `all()` to TimeArray Columns Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Use `all()` with a specified dimension (e.g., `2` for columns) to check if all elements in a `TimeArray` satisfy a condition. This is useful for data validation and filtering. ```julia all(ta .> 3, 2) ``` -------------------------------- ### Lagging Time Series Data with Padding Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Applies a lag of one time unit and pads the result with NaN values for omitted observations. Use this when preserving the date index is important. ```julia lag(cl[1:3]; padding=true) ``` -------------------------------- ### Split TimeArray by a Function using `split` Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Splits data by a given function, such as `Dates.day`, into periods. ```julia using TimeSeries using MarketData split(cl, Dates.day) ``` -------------------------------- ### Symbol column indexing in TimeSeries.jl Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Demonstrates accessing columns of a `TimeArray` using symbols, which is the preferred method since version 0.14.0. String indexing is deprecated. ```julia using MarketData ohlc[:Close] # and cl["Close"] is deprecated ``` -------------------------------- ### uniformspaced and uniformspace Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Methods to check or enforce uniform spacing in time series data. ```APIDOC ## uniformspaced and uniformspace ### Description Methods to check or enforce uniform spacing in time series data. ### Method uniformspaced(time_array) uniformspace(time_array) ### Parameters - **time_array** (TimeArray) - The input time series. ### Request Example ```julia uniformspaced(cl) uniformspace(cl) ``` ``` -------------------------------- ### Merge function with variable length input in TimeSeries.jl Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Illustrates the `merge` function's support for variable length input, allowing multiple `TimeArray` objects to be merged. The `method` argument remains a keyword argument. ```julia merge(x, y, [zs...]; method=:outer) ``` -------------------------------- ### Upsampling with Linear Interpolation Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Performs upsampling of the TimeArray using linear interpolation when no exact data points align with the new timestamps. Requires `Linear()` or `:linear` as the `upsample` argument. ```julia ta_ = retime(ta, Minute(15), upsample=Linear()) ``` -------------------------------- ### Right Join TimeArrays Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/combine.md Performs a right join on two TimeArrays, including all timestamps from the second TimeArray and matching timestamps from the first, introducing NaN for non-matches. ```julia merge(op[1:3], cl[2:4]; method=:right) ``` -------------------------------- ### Merge TimeArrays with Custom Meta Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/combine.md Merges two TimeArrays and explicitly sets the meta field value for the resulting TimeArray. If not provided and meta fields are strings, they are concatenated. ```julia meta(AppleCat) ``` ```julia CatApple = merge(CAT, AAPL; meta=47); meta(CatApple) ``` ```julia meta(merge(AppleCat, CatApple)) ``` -------------------------------- ### Retime to a New Time Step Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/retime.md Resamples the TimeArray to a new, regular time step of 15 minutes. This is a common use case for aligning data to a different frequency. ```julia retime(ta, Minute(15)) ``` -------------------------------- ### diff Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Calculates the finite difference between consecutive points in a time series. The resulting series has fewer points. Use `padding=true` to fill initial missing values with NaN. ```APIDOC ## diff ### Description Calculates the finite difference between consecutive points in a time series. The resulting series has fewer points. Use `padding=true` to fill initial missing values with NaN. Higher order differences can be calculated using the `differences` parameter. ### Method diff(time_array; differences=1, padding=false) ### Parameters - **time_array** (TimeArray) - The input time series. - **differences** (Integer) - The order of the difference. Defaults to 1. - **padding** (Boolean) - If true, pads the beginning with NaN. Defaults to false. ### Request Example ```julia diff(cl) ``` ``` -------------------------------- ### Base.getproperty for TimeArray columns in TimeSeries.jl Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Illustrates accessing `TimeArray` columns using dot notation (e.g., `ta.column_name`), a feature supported since version 0.14.0. Accessing original fields like `values(ohlc)` requires using the function form. ```julia using MarketData ohlc.Open ``` ```julia ohlc.values # this is unavailable due to Base.getproperty support values(ohlc) # change to this ``` -------------------------------- ### Enforcing Uniform Spacing Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Enforces uniform spacing in a time series, potentially by resampling or interpolating. This is useful for preparing data for certain analyses. ```julia using TimeSeries uniformspace(cl) ``` -------------------------------- ### lead Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Shifts time series values forward by a specified number of periods. Similar to lag but in the opposite direction. Arbitrary time distances are supported. ```APIDOC ## lead ### Description Shifts time series values forward by a specified number of periods. Similar to lag but in the opposite direction. Arbitrary time distances are supported. ### Method lead(time_array, lead_distance=1) ### Parameters - **time_array** (TimeArray) - The input time series. - **lead_distance** (Integer) - The number of periods to lead. Defaults to 1. ### Request Example ```julia lead(cl[1:3]) lead(cl, 499) ``` ``` -------------------------------- ### Calculate Higher Order Differences with Lag Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md The `diff` function now supports calculating higher-order differences and specifying a lag in time steps. This is useful for analyzing trends and rates of change over extended periods. ```julia diff(cl, 5) ``` -------------------------------- ### Rename columns of a TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/modify.md Use `rename` to change column names. It accepts a single new name, an array of names, or a dictionary mapping old names to new names. Can also accept a function to transform existing names. ```julia first(rename(cl, :Close′)) ``` ```julia first(rename(cl, [:Close′])) ``` ```julia first(rename(ohlc, [:Open′, :High′, :Low′, :Close′])) ``` ```julia first(rename(ohlc, :Open => :Open′)) ``` ```julia first(rename(ohlc, :Open => :Open′, :Close => :Close′)) ``` ```julia first(rename(ohlc, Dict(:Open => :Open′, :Close => :Close′)...)) ``` ```julia first(rename(Symbol ∘ uppercase ∘ string, ohlc)) ``` ```julia first(rename(uppercase, ohlc, String)) ``` -------------------------------- ### Calculating Finite Differences Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Calculates the finite difference between consecutive points in a time series. The resulting series will have fewer points, with NaNs for padding if specified. ```julia using TimeSeries using MarketData diff(cl) ``` -------------------------------- ### Plot Candlestick Chart Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/plotting.md Generates a candlestick chart, which requires the TimeArray to have 'open', 'high', 'low', and 'close' columns. Ideal for financial data visualization. ```julia plot(ta; seriestype=:candlestick) savefig("cs.svg"); nothing; # hide ``` -------------------------------- ### to Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Truncates a TimeArray after the specified date. ```APIDOC ## `to` The `to` method truncates a `TimeArray` after the date passed to the method: ```@repl using TimeSeries using MarketData to(cl, Date(2000, 1, 5)) ``` ``` -------------------------------- ### Save TimeArray to CSV Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/tables.md Save a TimeArray object to a CSV file using the `CSV.write` function from the CSV.jl package. Ensure the CSV.jl package is imported. ```julia using CSV CSV.write(filename, ta) ``` -------------------------------- ### Checking for Uniform Spacing Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Checks if the time series has uniform spacing between timestamps. This is a utility function for data validation. ```julia using TimeSeries uniformspaced(cl) ``` -------------------------------- ### Merge function with keyword argument in TimeSeries.jl Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Shows the updated `merge` function signature where the `method` argument is now a keyword argument. This change was introduced in version 0.16.0. ```julia merge(x, y; method=:outer) ``` -------------------------------- ### Calculating Higher Order Differences Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Calculates higher order finite differences by specifying the 'differences' keyword argument. `differences=2` is equivalent to applying `diff` twice. ```julia diff(cl, differences=2) ``` -------------------------------- ### Outer Join TimeArrays Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/combine.md Performs an outer join on two TimeArrays, including all timestamps from both TimeArrays and introducing NaN for timestamps present in only one. ```julia merge(op[1:3], cl[2:4]; method=:outer) ``` -------------------------------- ### Compare TimeArrays for Equality Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Use `==` to compare two `TimeArray` objects for equality, checking all fields including dimensions. This is useful for verifying data integrity after operations. ```julia cl == copy(cl) ``` -------------------------------- ### BoundsError for TimeArray indexing in TimeSeries.jl Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Demonstrates the expected `BoundsError` when attempting to access an out-of-bounds index using `[]` on a `TimeArray`. This behavior was standardized in version 0.16.0. ```julia julia> cl[] ERROR: BoundsError: attempt to access TimeArray{Float64,1,Date,Array{Float64,1}} at index [] ``` -------------------------------- ### Convert TimeArray to DataFrame Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/tables.md Convert a TimeArray object to a DataFrame using the DataFrame constructor. This is useful for further analysis or manipulation with DataFrame-specific functions. ```julia using MarketData, DataFrames df = DataFrame(ohlc) ``` -------------------------------- ### TimeArray Struct Definition Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/timearray.md Defines the structure of the TimeArray, including its fields for timestamps, values, column names, and metadata. The inner constructor code is omitted for brevity. ```julia struct TimeArray{T,N,D<:TimeType,A<:AbstractArray{T,N}} timestamp::Vector{D} values::A # some kind of AbstractArray{T,N} colnames::Vector{Symbol} meta::Any # inner constructor code enforcing invariants end ``` -------------------------------- ### Truncate TimeArray to a Specific Date using `to` Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md The `to` method truncates a TimeArray after the specified date. ```julia using TimeSeries using MarketData to(cl, Date(2000, 1, 5)) ``` -------------------------------- ### Map TimeSeries Data Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/combine.md Use this to transform both timestamps and values in a TimeArray. The provided function receives the timestamp and values, returning new timestamp and values. ```julia using TimeSeries using Dates ta = TimeArray([Date(2015, 10, 01), Date(2015, 11, 01)], [15, 16]) map((timestamp, values) -> (timestamp + Year(1), values), ta) ``` -------------------------------- ### Iterate Rows of TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/tables.md Iterate over each row of a TimeArray using the `eachrow` function. Access columns by name, such as 'timestamp' and 'Close'. ```julia using MarketData for row in eachrow(ohlc) time = row.timestamp c = row.Close # ... end ``` -------------------------------- ### tail Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/split.md Returns the last value(s) in a TimeArray. Defaults to the last value, but can be modified to return a specified number of elements from the bottom. ```APIDOC ## `tail` The `tail` method defaults to returning only the last value in a `TimeArray`. By selecting the second positional argument to a different value, the user can modify how many from the bottom are selected: ```@repl using TimeSeries using MarketData tail(cl) tail(cl, 3) ``` ``` -------------------------------- ### Convert DataFrame to TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/tables.md Convert a DataFrame to a TimeArray, specifying the timestamp column using the `timestamp` keyword argument. The column name in the DataFrame will be used as the timestamp index. ```julia df′ = DataFrames.rename(df, :timestamp => :A) first(df′) TimeArray(df′; timestamp=:A) ``` -------------------------------- ### lag Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Shifts time series values by a specified number of periods. By default, it shifts by 1 period. Use `padding=true` to fill initial missing values with NaN. ```APIDOC ## lag ### Description Shifts time series values by a specified number of periods. By default, it shifts by 1 period. Use `padding=true` to fill initial missing values with NaN. ### Method lag(time_array, lag_distance=1; padding=false) ### Parameters - **time_array** (TimeArray) - The input time series. - **lag_distance** (Integer) - The number of periods to lag. Defaults to 1. - **padding** (Boolean) - If true, pads the beginning with NaN. Defaults to false. ### Request Example ```julia lag(cl[1:3]) lag(cl[1:3]; padding=true) ``` ``` -------------------------------- ### Customize Candlestick X-axis Ticks Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/plotting.md Controls the density of x-axis labels on a candlestick chart using the `xticks` attribute and rotates them with `xrotation` for better readability. ```julia plot(ta; seriestype=:candlestick, xticks=3, xrotation=60) savefig("xticks.svg"); nothing; # hide ``` -------------------------------- ### Lagging Time Series Data Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Applies a lag of one time unit to the time series data. The first observation is omitted as there is no prior value to lag. ```julia lag(cl[1:3]) ``` -------------------------------- ### Horizontally Concatenate TimeArrays Source: https://github.com/juliastats/timeseries.jl/blob/master/NEWS.md Use the `hcat` operator `[...]` for efficient horizontal concatenation of `TimeArray` objects that share the same timestamps. This is faster than `merge` for this specific case. ```julia [ta1 ta2] ``` -------------------------------- ### Plot Multiple Series Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/plotting.md Plots multiple variables from a TimeArray as individual lines on the same y-axis. Useful for comparing related time series data. ```julia plot(ta[:Open, :High, :Low, :Close]) savefig("multi-series.svg"); nothing; # hide ``` -------------------------------- ### Read CSV into TimeArray Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/readwrite.md Use `readtimearray` to parse a CSV file into a TimeArray object. Supports custom delimiters and date formats. ```julia readtimearray(fname; delim=',', meta=nothing, format="") ``` ```julia ta = readtimearray("close.csv"; format="dd/mm/yyyy HH:MM", delim=';') ``` -------------------------------- ### Customize Candlestick Bar Width Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/plotting.md Adjusts the width of the bars in a candlestick chart. The `bar_width` attribute accepts values between 0 and 1. ```julia plot(ta; seriestype=:candlestick, bar_width=0.7) savefig("bw.svg"); nothing; # hide ``` -------------------------------- ### dropnan Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Removes rows containing NaN (Not a Number) values from a TimeArray. ```APIDOC ## dropnan ### Description Removes rows containing NaN (Not a Number) values from a TimeArray. ### Method dropnan(time_array) ### Parameters - **time_array** (TimeArray) - The input time series. ### Request Example ```julia dropnan(cl) ``` ``` -------------------------------- ### Removing NaN Values Source: https://github.com/juliastats/timeseries.jl/blob/master/docs/src/apply.md Removes rows containing NaN values from a TimeArray. This is a common data cleaning step. ```julia using TimeSeries dropnan(cl) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.