Volume Bars Sampling
Data Preparation & Feature Engineering DS practice problem on Onlearn.
Difficulty: medium.
Topics: Volume Bars Sampling, Cumulative Volume Thresholding, Tick Data Aggregation, Sampling Frequency Invariance, Information Decay Rate, Heteroskedasticity Mitigation, Financial Econometrics, Time Series Analysis, Data Preprocessing, Statistical Signal Processing, Algorithmic Trading Systems, Information-Driven Sampling, Market Microstructure Modeling, Feature Transformation Techniques, Non-Parametric Data Resampling, Volatility Estimation Methods.
Implement volume bars sampling, an alternative bar sampling method used in financial machine learning. Unlike traditional time based bars (e.g., daily or hourly candles), volume bars form a new bar whenever a predefined cumulative volume threshold is reached. Given an array of trade prices, an array of corresponding trade volumes, and a volume threshold, generate volume bars. Each bar should contain: Open: The first price in the bar High: The maximum price in the bar Low: The minimum price in the bar Close: The last price in the bar Volume: The total volume accumulated in the bar When the cumulative volume reaches or exceeds the threshold, close the current bar and start a new one. If there is remaining data that does not reach the threshold, include it as an incomplete bar at the end. Return a list of bars, where each bar is represented as [open, high, low, close, volume] with values rounded to 4 decimal places.