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.
Examples
Example 1:
Input:
prices = [100.0, 101.0, 99.0, 102.0, 98.0, 100.0], volumes = [50.0, 30.0, 40.0, 60.0, 20.0, 50.0], volume_threshold = 100.0Output:
[[100.0, 101.0, 99.0, 99.0, 120.0], [102.0, 102.0, 98.0, 100.0, 130.0]]Explanation: Bar 1: Starts at price 100.0 with volume 50. Adding prices 101.0 (vol=30) and 99.0 (vol=40) brings cumulative volume to 120, which exceeds threshold 100. Bar closes with O=100, H=101, L=99, C=99, V=120. Bar 2: Starts fresh at price 102.0 with volume 60. Adding prices 98.0 (vol=20) and 100.0 (vol=50) brings cumulative volume to 130. Bar closes with O=102, H=102, L=98, C=100, V=130.
Starter Code
import numpy as np
def volume_bars_sampling(prices: np.ndarray, volumes: np.ndarray, volume_threshold: float) -> list:
"""
Generate volume bars from tick/trade data.
Args:
prices: Array of trade prices for each trade/tick
volumes: Array of trade volumes corresponding to each price
volume_threshold: Volume threshold that triggers formation of a new bar
Returns:
List of bars, where each bar is [open, high, low, close, total_volume]
All values rounded to 4 decimal places
"""
passPython3
ReadyLines: 1Characters: 0
Ready