成交量加权平均价格
由bq6ety4j创建,最终由bq6ety4j 被浏览 2 用户
成交量加权平均价格(VWAP)通常用于衡量市场的平均价格,考虑了成交量的影响。
-
公式:
VWAP 用于计算某个时间段内的加权平均价格,权重是每分钟的成交量。这个因子在股票市场中非常常见,用于判断价格的整体趋势和市场的流动性。
\
- 代码:
def main(datasource, start_date, end_date):
"""
factor function
Args:
datasource (str): Datasource table name
start_date (str): Start date in 'YYYY-MM-DD' format
end_date (str): End date in 'YYYY-MM-DD' format
Returns:
pd.DataFrame: Factor data with columns ['date', 'instrument', 'factor']
"""
import pandas as pd
import dai
# This function demonstrates how to:
# 1. Query market from the specified datasource
# 2. Calculate a simple factor using SQL aggregation
# 3. Return properly formatted factor data for analysis
# 因子公式 https://bigquant.com/wiki/doc/Rceb2JQBdS
# This represents the volume-weighted average price (VWAP): sum(amount) / sum(volume)
sql = f"""
SELECT
date::DATE::DATETIME AS date,
instrument,
SUM(close * volume) / SUM(volume) AS factor
FROM {datasource}
GROUP BY date::DATE, instrument
"""
# Data is queried with a 7-day lookback buffer for rolling calculations
lookback_days = 7
query_start_date = pd.to_datetime(start_date) - pd.Timedelta(days=lookback_days)
df = dai.query(sql, filters={'date': [query_start_date, end_date]}).df()
# Final output is filtered to the exact date range requested
df = df[df['date'].between(start_date, end_date)]
return df
if __name__ == '__main__':
"""
FOR Development in __main__
This section demonstrates how to:
1. Calculate factors using the main() function
2. Visualize and analyze results using the factorlens module
Tips:
- Adjust the date range to test different time periods
- Modify the SQL query in main() to create your own factors
- Use factorlens to evaluate factor performance metrics
"""
from bigmodule import M
import structlog
logger = structlog.get_logger()
datasource = 'cpt_jyc_2025_stock_csi1000_bar1m'
start_date = '2023-01-01'
end_date = '2024-12-31'
logger.info(f"Calculating factor for period: {start_date} to {end_date}")
data = main(datasource, start_date, end_date)
logger.info(f"Factor data shape: {data.shape}")
logger.info(f"\nSample data:\n{data.head()}")
m = M.factorlens._latest(
data=data,
m_cached=False,
)
\