Added vwat info to the features.db.

This commit is contained in:
Kalzu Rekku
2026-07-14 21:54:55 +03:00
parent b754e40abf
commit e22dc05352
4 changed files with 17 additions and 6 deletions
+2 -1
View File
@@ -131,7 +131,8 @@ CREATE TABLE five_second_features (
realized_vol REAL NOT NULL, -- Std dev of tick-to-tick log returns realized_vol REAL NOT NULL, -- Std dev of tick-to-tick log returns
ofi REAL NOT NULL, -- Buy volume Sell volume ofi REAL NOT NULL, -- Buy volume Sell volume
volume_sum REAL NOT NULL, -- Total volume in bucket volume_sum REAL NOT NULL, -- Total volume in bucket
close_price REAL NOT NULL -- Last trade price in bucket close_price REAL NOT NULL, -- Last trade price in bucket
vwap REAL NOT NULL -- Volume-Weighted Average Price in bucket
); );
``` ```
+12 -4
View File
@@ -92,10 +92,10 @@ func (a *Aggregator) flushBucket() {
_, err := a.featDB.Exec(` _, err := a.featDB.Exec(`
INSERT OR IGNORE INTO five_second_features INSERT OR IGNORE INTO five_second_features
(timestamp, log_return, realized_vol, ofi, volume_sum, close_price) (timestamp, log_return, realized_vol, ofi, volume_sum, close_price, vwap)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?)
`, bucket.Timestamp, bucket.LogReturn, bucket.RealizedVol, `, bucket.Timestamp, bucket.LogReturn, bucket.RealizedVol,
bucket.OFI, bucket.VolumeSum, bucket.ClosePrice) bucket.OFI, bucket.VolumeSum, bucket.ClosePrice, bucket.VWAP)
if err != nil { if err != nil {
log.Printf("[aggregator] Failed to write feature bucket: %v", err) log.Printf("[aggregator] Failed to write feature bucket: %v", err)
@@ -118,10 +118,12 @@ func (a *Aggregator) computeFeatures(ticks []Tick) FeatureBucket {
} }
logReturn := math.Log(closePrice / refPrice) logReturn := math.Log(closePrice / refPrice)
// Order Flow Imbalance (OFI) and total volume // Order Flow Imbalance (OFI), total volume, and sum of price * volume
var buyVol, sellVol, volumeSum float64 var buyVol, sellVol, volumeSum float64
var priceVolumeSum float64
for _, t := range ticks { for _, t := range ticks {
volumeSum += t.Volume volumeSum += t.Volume
priceVolumeSum += t.Price * t.Volume
if t.Side == "Buy" { if t.Side == "Buy" {
buyVol += t.Volume buyVol += t.Volume
} else { } else {
@@ -130,6 +132,11 @@ func (a *Aggregator) computeFeatures(ticks []Tick) FeatureBucket {
} }
ofi := buyVol - sellVol ofi := buyVol - sellVol
vwap := closePrice
if volumeSum > 0 {
vwap = priceVolumeSum / volumeSum
}
// Realized volatility: standard deviation of tick-to-tick log returns // Realized volatility: standard deviation of tick-to-tick log returns
realizedVol := 0.0 realizedVol := 0.0
if n > 1 { if n > 1 {
@@ -164,5 +171,6 @@ func (a *Aggregator) computeFeatures(ticks []Tick) FeatureBucket {
OFI: ofi, OFI: ofi,
VolumeSum: volumeSum, VolumeSum: volumeSum,
ClosePrice: closePrice, ClosePrice: closePrice,
VWAP: vwap,
} }
} }
+2 -1
View File
@@ -116,7 +116,8 @@ func (sm *StorageManager) initFeaturesDB() error {
realized_vol REAL NOT NULL, realized_vol REAL NOT NULL,
ofi REAL NOT NULL, ofi REAL NOT NULL,
volume_sum REAL NOT NULL, volume_sum REAL NOT NULL,
close_price REAL NOT NULL close_price REAL NOT NULL,
vwap REAL NOT NULL
); );
`) `)
return err return err
+1
View File
@@ -16,6 +16,7 @@ type FeatureBucket struct {
OFI float64 // Net volume (Buy volume - Sell volume) OFI float64 // Net volume (Buy volume - Sell volume)
VolumeSum float64 // Total volume exchanged VolumeSum float64 // Total volume exchanged
ClosePrice float64 // Final transaction price in the bucket ClosePrice float64 // Final transaction price in the bucket
VWAP float64 // Volume-Weighted Average Price in the bucket
} }
// BybitWSMessage represents the top-level WebSocket message from Bybit V5 publicTrade. // BybitWSMessage represents the top-level WebSocket message from Bybit V5 publicTrade.