![](https://crypto4nerd.com/wp-content/uploads/2023/02/0FT21ZGNlrsiKHUYF-1024x683.jpeg)
In this article we will discuss a C script that I created and aggregates single binance trades (raw tick data) into OHLC candles with buy and sell volume. The script can then be used to calculate cumulative volume delta (CVD) and volume delta, which can help traders make better decisions.
- download historical raw tick data either for spot or futures from: https://data.binance.vision/?prefix=data/spot/daily/trades/.
- Git clone my repository: https://github.com/DGabri/tick_data_aggregator.git
- Start the script and enjoy OHLCV from tick data!
The script starts by reading line by line all the historical trades, then it checks if the current timestamp belongs to the current time bucket. If it does, the script updates the candle with the new trade data; if not, the script inserts the current candle into the output CSV and creates a new candle for the new time interval.
The OHLC data for each candle is calculated as follows:
- Open: The first traded price in the time bucket
- High: The highest traded price in the time bucket
- Low: The lowest price in the time bucket
- Close: The last traded price in the time bucket
The buy and sell volume for each candle is also calculated. The buy volume is the sum of the quantity for all the executed buy trades in the time interval; sell volume is the same but for sell trades.
When a new candle is closed the script saves it to file and reiterates the process for the remaining dataset. The output file can be used by traders/quants to calculate CVD and volume delta.
CVD (cumulative volume delta) is calculated by subtracting the sell volume from the buy volume and cumulating the values over time, usually a period of 60 is applied. A positive CVD indicates buying pressure, while a negative CVD indicates selling pressure.
Volume delta is a measure of the difference between the buy and sell volume in each candle. It can be calculated by subtracting the sell volume from the buy volume for each candle.
/* aggregation data struct */
typedef struct kline {
unsigned long open_ts;
unsigned long close_ts;
float open;
float high;
float low;
float close;
float buy_vol_usdt;
float sell_vol_usdt;
int buy_trades;
int sell_trades;
} Kline;
/* AGGREGATION FUNCTION */
// first data point in the dataset
if (row == 2) {
candle -> close_ts = next_timestamp(ts, resample_frequency);
candle -> open_ts = (candle->close_ts - (resample_frequency * 1000));
candle -> open = price;
}if (ts > (candle -> close_ts)) {
candle -> close = price;
// write to file
if (write_kline(candle, output_file) < 0) {
printf("Error writing new line, exiting...");
return (-1);
}
// reset candle struct
candle -> open_ts = candle->close_ts;
candle -> close_ts = next_timestamp(ts, resample_frequency);
candle -> open = price;
candle -> high = 0;
candle -> low = 100000000000;
candle -> close = 0;
candle -> buy_vol_usdt = 0;
candle -> sell_vol_usdt = 0;
candle -> buy_trades = 0;
candle -> sell_trades = 0;
}
// update price values
if (price > (candle -> high)) {
candle -> high = price;
}
if (price < (candle -> low)) {
candle -> low = price;
}
// market sell
if (side == -1) {
candle -> sell_vol_usdt += quote_qty;
candle -> sell_trades += 1;
} else {
// market buy
candle -> buy_vol_usdt += quote_qty;
candle -> buy_trades += 1;
}
Concluding, the script that I created is useful if you want to aggregate single trade into OHLCV. This is still a work in progress so future updates are probable, if you find some bugs or want to improve the script feel fre to contact me or PR on GitHub! Thanks for reading, feel free to check my other articles if you want to learn more about algorithmic trading infrastructure.