Data API
The Data module provides comprehensive functionality for managing market data in Planar. It handles OHLCV (Open, High, Low, Close, Volume) data storage, retrieval, and manipulation using efficient storage formats like Zarr and LMDB.
Overview
The Data module is responsible for:
- OHLCV data storage and retrieval
- Data persistence using Zarr format for large datasets
- LMDB key-value storage for fast access
- Data validation and integrity checking
- Efficient data structures for time series analysis
Core Data Structures
Data Persistence
Zarr Storage
Planar uses Zarr format for efficient storage of large time series datasets.
LMDB Key-Value Storage
For fast metadata and configuration storage.
Complete API Reference
PlanarCore.Data.OHLCV_CHUNK_SIZE — Constant
Default ZArray chunk size.
PlanarCore.Data.OHLCV_COLUMNS — Constant
Columns for OHLCV data: timestamp, open, high, low, close, volume
PlanarCore.Data.OHLCV_COLUMNS_COUNT — Constant
Count of OHLCV_COLUMNS
PlanarCore.Data.OHLCV_COLUMNS_NOV — Constant
Only the OHLC columns of OHLCV_COLUMNS
PlanarCore.Data.OHLCV_COLUMNS_TS — Constant
The timestamp column of OHLCV_COLUMNS
PlanarCore.Data.compressor — Constant
Default zarr compressor used in the module (zstd, clevel=2).
PlanarCore.Data.Candle — Type
A struct representing a candlestick in financial trading.
timestampopenhighlowclosevolume
Candle{T} is a parametric struct that represents a candlestick with generic type T, which must be a subtype of AbstractFloat.
PlanarCore.Data.DictView6 — Type
A view into a dictionary (a subset of keys).
dkeys
PlanarCore.Data.EventTrace — Type
EventTrace structure for managing event data.
lock_buf_zi_arr_cachefreqlast_flush
Represents a collection of events with caching capabilities. It is designed to efficiently handle large datasets by caching event data in memory. The structure includes a ZarrInstance for data storage, a ZArray for data access, a cache for temporary storage, a frequency for event timing, and an index for the next event.
PlanarCore.Data.OHLCVTuple — Type
Similar to a StructArray (and should probably be replaced by it), used for fast conversion.
PlanarCore.Data.PairData — Type
PairData is a low level struct, to attach some metadata to a ZArray. (deprecated)"
nametfdataz
Instead of constructing a PairData, directly use the OHLCV DataFrame to hold the pair information and the ZArray itself.
PlanarCore.Data.TimeFrameError — Type
A custom exception representing a time frame error.
firstlasttd
PlanarCore.Data.ZarrInstance — Type
Candles data is stored with hierarchy PAIR -> [TIMEFRAMES...]. A pair is a ZGroup, a timeframe is a ZArray.
pathstoregroup
Base.delete! — Method
Delete an element from a DirectoryStore. Also removes the directory.
delete!(
store::Zarr.DirectoryStore,
paths::String...;
recursive
) -> Union{Nothing, Bool}
Base.delete! — Method
Delete the ZArray from the underlying storage.
delete!(z::Zarr.ZArray; ok) -> Union{Nothing, Bool}
Base.delete! — Method
Delete an element from a ZGroup. If the element is a group, it will be recursively deleted.
Base.empty! — Method
Resizes a ZArray to zero.
empty!(z::Zarr.ZArray) -> Zarr.ZArray
Base.empty! — Method
Removes all arrays and groups from a ZGroup.
empty!(g::Zarr.ZGroup)
Base.isempty — Method
A ZArray is empty if its size is 0.
isempty(z::Zarr.ZArray) -> Bool
Base.unique! — Method
Remove duplicate from a zarray.
In a 2d zarray where we want values where the second column is unique:
unique!(x->x[2], z)PlanarCore.Data._check_contiguity — Method
Check the contiguity of timestamps between data and saved data.
_check_contiguity(
data_first_ts::AbstractFloat,
data_last_ts::AbstractFloat,
saved_first_ts::AbstractFloat,
saved_last_ts::AbstractFloat,
td
) -> Bool
Used to check the contiguity of timestamps between the data and saved data. It takes in the first and last timestamps of the data (data_first_ts and data_last_ts) and the first and last timestamps of the saved data (saved_first_ts and saved_last_ts). Typically used as a helper function within the context of saving or loading OHLCV data to ensure the contiguity of timestamps.
PlanarCore.Data._get_zarray — Method
Get a ZArray object from a ZarrInstance.
_get_zarray(
zi::PlanarCore.Data.ZarrInstance,
key::AbstractString,
sz::Tuple;
type,
overwrite,
reset
)
This function is used to retrieve a ZArray object from a ZarrInstance. It takes in the ZarrInstance, key, size, and other optional parameters and returns the ZArray object.
PlanarCore.Data._load_ohlcv — Method
Load ohlcv pair data from zarr instance. za: The zarr array holding the data key: the name of the array to load from the zarr instance (in the format exchange/timeframe/pair) td: the timeframe (as integer in milliseconds) of the target ohlcv table to be loaded from, to: date range
PlanarCore.Data.candleat — Method
Get the candle at given date from a ohlcv dataframe as a Candle.
candleat(
df::DataFrames.AbstractDataFrame,
date::Dates.DateTime;
return_idx
) -> Union{PlanarCore.Data.Candle{Float64}, Tuple{PlanarCore.Data.Candle{Float64}, Any}}
PlanarCore.Data.candleavl — Method
Fetch the candle expected to be available at a specific date and time frame from an OHLCV DataFrame.
candleavl(
df::DataFrames.AbstractDataFrame,
tf::TimeFrames.TimeFrame,
date
) -> Union{PlanarCore.Data.Candle{Float64}, Tuple{PlanarCore.Data.Candle{Float64}, Any}}
The available candle is usually the candle that is date-wise left adjacent to the requested date.
PlanarCore.Data.candlelast — Method
Get the last candle from a ohlcv dataframe as a Candle.
candlelast(
df::DataFrames.AbstractDataFrame
) -> PlanarCore.Data.Candle{Float64}
PlanarCore.Data.candlepair — Method
Same as candleat but also fetches the previous candle, returning a Tuple{Candle, Candle}.
PlanarCore.Data.check_data — Method
Check the size of data against a ZArray.
check_data(data, arr::Zarr.ZArray)
Used to check the size of data against a ZArray arr. It takes in the data and the ZArray arr as input. Compares the size of the data with the size of the ZArray. If the sizes do not match, it raises a SizeMismatchError.
PlanarCore.Data.chunksize — Method
Choose chunk size depending on size of data with a predefined split (e.g. 1/100), padding to the nearest power of 2.
chunksize(data; parts, def) -> Tuple{Any, Vararg{Any}}
PlanarCore.Data.closeat — Method
See @candleat.
PlanarCore.Data.closeavl — Method
See @candleavl
PlanarCore.Data.closelast — Method
See @candlelast
PlanarCore.Data.contiguous_ts — Method
Check if a time series is contiguous based on a specified timeframe.
contiguous_ts(
series,
timeframe::AbstractString;
raise,
return_date
) -> Union{Bool, Tuple{Bool, Any, Any}}
This function is used to check if a time series is contiguous based on a specified timeframe. It takes in the series as the input time series and the timeframe as a string representing the timeframe (e.g., "1h", "1d"). Optional parameters raise and return_date can be specified to customize the behavior of the function.
raise: A flag indicating whether to raise aTimeFrameErrorif the time series is not contiguous. Default istrue.return_date: A flag indicating whether to return the first non-contiguous date found in the time series. Default isfalse.
PlanarCore.Data.default_value — Method
Get the default value of a given type t.
default_value(t::Type) -> PlanarCore.Data.Candle{Float64}
This function returns the default value of the specified type t.
PlanarCore.Data.df! — Method
Construct a DataFrame without copying.
PlanarCore.Data.empty_ohlcv — Method
An empty OHLCV dataframe.
PlanarCore.Data.highat — Method
See @candleat.
PlanarCore.Data.highavl — Method
See @candleavl
PlanarCore.Data.highlast — Method
See @candlelast
PlanarCore.Data.key_path — Method
The full key of the data stored for the (exchange, pair, timeframe) combination.
key_path(exc_name, pair, timeframe) -> String
PlanarCore.Data.load — Method
Load a pair ohlcv data from storage. as_z: returns the ZArray
PlanarCore.Data.load_data — Method
Load data from zarr instance.
load_data(
zi::PlanarCore.Data.ZarrInstance,
key;
serialized,
kwargs...
) -> Any
zi: The zarr instance to usekey: the name of the array to load from the zarr instance (full key path).type: Set to the type that zarr should use to store the data (only bits types). [Float64].sz: The chunks tuple which should match the shape of the already saved data.from: The starting index to load the data from. Default is an empty string, indicating no specific starting index.to: The ending index to load the data up to. Default is an empty string, indicating no specific ending index.z_col: The column in the Zarr array to load the data from. Default is1.type: The type of the data to be loaded. Default isFloat64.serialized: A flag indicating whether the data is serialized. Default isfalse. Iftrue,typeis ignored.as_z: A flag indicating whether to return the loaded data as a ZArray. Default isfalse.with_z: A flag indicating whether to return the loaded data along with the Zarr array (as tuple). Default isfalse.
PlanarCore.Data.load_ohlcv — Method
Load OHLCV data from a ZarrInstance.
load_ohlcv(
zi::PlanarCore.Data.ZarrInstance,
exc_name::AbstractString,
pairs,
timeframe;
raw,
kwargs...
) -> Union{Dict{String, Zarr.ZArray}, Dict{String, PlanarCore.Data.PairData}}
raw: A flag indicating whether to return the raw data or process it into an OHLCV format. Default isfalse.from: The starting timestamp (inclusive) for loading data. Default is an empty string, indicating loading from the beginning of the ZArray.to: The ending timestamp (exclusive) for loading data. Default is an empty string, indicating loading until the end of the ZArray.saved_col: The column index of the timestamp data in the ZArray. Default is 1.as_z: A flag indicating whether to return the loaded data as a ZArray. Default isfalse.with_z: A flag indicating whether to return the loaded data along with the ZArray object. Default isfalse.
This function is used to load OHLCV data from a ZarrInstance. It takes in the ZarrInstance zi, the exchange name exc_name, the currency pairs pairs, and the timeframe. Optional parameters raw and kwargs can be specified to customize the loading process.
PlanarCore.Data.lowat — Method
See @candleat.
PlanarCore.Data.lowavl — Method
See @candleavl
PlanarCore.Data.lowlast — Method
See @candlelast
PlanarCore.Data.ohlcvtuple — Method
Default OHLCVTuple value.
PlanarCore.Data.openat — Method
See @candleat.
PlanarCore.Data.openavl — Method
See @candleavl
PlanarCore.Data.openlast — Method
See @candlelast
PlanarCore.Data.save_data — Method
Save data to a ZarrInstance with additional options.
save_data(
zi::PlanarCore.Data.ZarrInstance,
key,
data;
serialize,
data_col,
kwargs...
) -> Zarr.ZArray
type: The type of the data to be saved. Default isFloat64.data_col: The column of the data to be saved. Default is1.z_col: The column in the Zarr array to save the data. Default is the same asdata_col.overwrite: A flag indicating whether to overwrite existing data at the specified key. Default istrue.reset: A flag indicating whether to reset the Zarr array before saving the data. Default isfalse.chunk_size: The size of the chunks to use when saving the data. Default isnothing, indicating auto-chunking.
Only dates seriality is ensured, not contiguity (unlike save_ohlcv) It creates a new array if needed, sets the chunk size if specified.
PlanarCore.Data.save_ohlcv — Method
Save OHLCV data to a ZArray.
save_ohlcv(
zi::PlanarCore.Data.ZarrInstance,
exc_name,
pair,
timeframe,
data;
kwargs...
) -> Union{Nothing, Zarr.ZArray}
data_col: The column index of the timestamp data in the inputdata. Default is 1.saved_col: The column index of the timestamp data in the existing data. Default is equal todata_col.type: The primitive type used for storing the data. Default isFloat64.existing: A flag indicating whether existing data should be considered during the save operation. Default istrue.overwrite: A flag indicating whether existing data should be overwritten during the save operation. Default istrue.reset: A flag indicating whether the ZArray should be reset before saving the data. Default isfalse.check::bounds(default) only checks that new data is adjacent to previous data.:allchecks full contiguity of previous and new data.:noneor anything else, no checks are done.
The saveohlcv function saves OHLCV data to a ZArray. It performs checks on the input data and existing data (if applicable) to ensure contiguity and validity. If the checks pass, it calculates the offset based on the time difference between the first timestamps of the new and existing data. Then, it updates the ZArray with the new data starting at the calculated offset. The function provides various optional parameters to customize the save operation, such as handling existing data, overwriting, resetting, and performing checks.
PlanarCore.Data.snakecased — Method
Normalizes or special characthers separators to _.
snakecased(pair::AbstractString) -> Any
PlanarCore.Data.stub! — Method
A stub! function usually fills a container with readily available data.
PlanarCore.Data.to_ohlcv — Method
Convert raw ccxt OHLCV data (matrix) to a dataframe.
PlanarCore.Data.to_ohlcv — Method
Construct an OHLCV dataframe backed by an OHLCVTuple.
PlanarCore.Data.to_ohlcv — Method
Convert data to OHLCV format.
to_ohlcv(
data::AbstractVector{PlanarCore.Data.Candle},
timeframe::TimeFrames.TimeFrame
) -> Any
This function converts the input data to the OHLCV (Open, High, Low, Close, Volume) format, using the specified timeframe. It returns the converted data as a DataFrame.
PlanarCore.Data.tobytes — Method
Convert a value data to its byte representation.
tobytes(data) -> Vector{UInt8}
This function converts the input value data to its byte representation.
PlanarCore.Data.todata — Method
Convert a byte array bytes to its original data representation.
todata(bytes) -> Any
This function converts the input byte array bytes back to its original data representation.
PlanarCore.Data.volumeat — Method
See @candleat.
PlanarCore.Data.volumeavl — Method
See @candleavl
PlanarCore.Data.volumelast — Method
See @candlelast
PlanarCore.Data.zdelete! — Method
Delete elements from a ZArray z within a specified date range.
zdelete!(
z::Zarr.ZArray,
from_dt::Union{Nothing, Dates.DateTime},
to_dt::Union{Nothing, Dates.DateTime};
by,
select,
serialized,
buffer
)
This function deletes elements from a ZArray z that fall within the specified date range. The range is defined by from_dt (inclusive) and to_dt (exclusive). The deletion is performed in place.
The by argument is optional and defaults to the identity function. It specifies the function used to extract the date value from each element of the ZArray. The select argument is optional and defaults to a function that selects the first column of each element in the ZArray. It specifies the function used to select the relevant portion of each element for deletion. The serialized argument is optional and defaults to false. If set to true, the ZArray is assumed to be serialized, and the deletion is performed on the serialized representation. The buffer argument is optional and can be used to provide an IOBuffer for intermediate storage during deletion.
PlanarCore.Data.@as_mat — Macro
Redefines given variable to a Matrix with type of the underlying container type.
PlanarCore.Data.@candleat — Macro
Get the candle value at a specific date from an OHLCV DataFrame.
This function returns the requested value at the specified date from the input OHLCV DataFrame. The optional parameter return_idx determines whether to also return the index of the opening price.
PlanarCore.Data.@candleavl — Macro
Fetch the candle value expected to be available at a specific date and time frame from an OHLCV DataFrame.
The available candle is usually the candle that is date-wise left adjacent to the requested date.
PlanarCore.Data.@candlelast — Macro
Get the last candle value from an OHLCV DataFrame (df).
PlanarCore.Data.@check_td — Macro
Check the time delta between two rows in a DataFrame.
This macro is used to check the time delta between two DataFrame to ensure they are of the same time delta. It throws a TimeFrameError if the time delta does not match the specified time delta value. If no args are provided, the macro uses the za value as the default data to check.
PlanarCore.Data.@checkkey — Macro
Macro for checking if a key exists in a DictView.
This macro checks if a given key is present in the keys field of the DictView (d).
PlanarCore.Data.@to_mat — Macro
Same as as_mat but returns the new matrix.
PlanarCore.Data.@zcreate — Macro
Create a ZArray using the zcreate macro.
This macro is used to create a ZArray object. It provides a convenient syntax for creating and initializing a ZArray with the specified elements. It's a dirty macro. Uses existing variables:
type: eltype of the array.key: path of the array.sz: size of the array.zi: ZarrInstance object.
See Also
- Data Management Guide - Complete guide to working with market data
- Processing API - Data processing and transformation functions
- DFUtils API - DataFrame manipulation utilities
- Engine API - Core execution engine functions
- Fetch API - Data fetching and retrieval utilities