Dolphindb реализация (ссылка) < /p>
Код: Выделить всё
// 1. Calculate minute-level VWAP
priceMatrix = exec wavg(Trade_Price, Trade_Volume)
from trades
where Time between 09:30:00.000000000 : 16:00:00.000000000
pivot by minute(Time) as minute, Symbol
// 2. Forward fill missing values
priceMatrix.ffill!()
// 3. Compute minute-level returns
retMatrix = each(def(x): ratios(x)-1, priceMatrix)
// 4. Calculate pairwise correlations
corrMatrix = cross(corr, retMatrix)
import pandas as pd
import numpy as np
from datetime import datetime, time
import time as tm
# 1. Generate sample trade data (replace with your actual data)
num_stocks = 3000
symbols = [f'S_{i}' for i in range(num_stocks)]
times = pd.date_range("2023-01-01 09:30:00", "2023-01-01 16:00:00", freq="s")
np.random.seed(42)
trades = pd.DataFrame({
'Time': np.random.choice(times, size=10_000_000),
'Symbol': np.random.choice(symbols, size=10_000_000),
'Trade_Price': np.random.uniform(10, 500, size=10_000_000),
'Trade_Volume': np.random.randint(1, 10000, size=10_000_000)
})
# Filter market hours
trades = trades[trades['Time'].dt.time.between(time(9,30), time(16,0))]
start = tm.time()
# 2. Calculate minute-level VWAP
vwap = (trades.groupby([pd.Grouper(key='Time', freq='1min'), 'Symbol'])
.apply(lambda x: np.average(x['Trade_Price'], weights=x['Trade_Volume']))
.unstack()
.ffill())
print(f"VWAP calculation completed in {tm.time() - start:.2f} seconds") # 158.85s
start = tm.time()
# 3. Compute returns
returns = vwap.pct_change()
print(f"Returns calculation completed in {tm.time() - start:.2f} seconds") # 0.25s
start = tm.time()
# 4. Calculate correlation matrix (optimized)
corr_matrix = returns.corr()
print(f"Correlation matrix calculation completed in {tm.time() - start:.2f} seconds") #11.54s
< /code>
Приведенный выше код Python выполняется очень медленно при расчете парных корреляций в течение 3000 акций. Существуют ли более эффективные подходы к крупномасштабным корреляционным вычислениям в Python?
Подробнее здесь: https://stackoverflow.com/questions/796 ... lphindb-vs