买一、卖一的高开低收系列因子
由bq7zuymm创建,最终由bq7zuymm 被浏览 28 用户
这一章我们来加工针对买一价、卖一价的高开低收的分钟因子。
数据定义
我们都知道真正的高开低收是如何加工出来的:
- 开盘价: 每分钟的第一个成交价格;
- 最高价: 每分钟内最高成交价格;
- 最低价: 每分钟内最低成交价格;
- 收盘价: 每分钟的最后一个成交价格.
那么只需要将成交价格替换成我们的买一、卖一价即可得到买一、卖一的高开低收。
因子加工代码
import dai
import time
import numpy as np
instruments = "('000002.SZ')"
dai.pull_data_to_table(datasource='cn_stock_level2_snapshot', table_name='stock_table', overwrite=True, lookback_time=72*60*60)
sql = f"""
SELECT date_trunc('minute', to_timestamp(datetime * 1.0 / 1000 + 8 * 60 * 60)) as date, instrument, datetime,
ask_price1, bid_price1
FROM stock_table
WHERE instrument in {instruments}
"""
engine = dai.stream_factor(sql, 'test', True, 'datetime ASC')
# 这是用来聚合的函数
def OLCH(df):
df = df.sort_values('datetime')
df['open_best_bid'] = df['bid_price1'].iloc[0]
df['close_best_bid'] = df['bid_price1'].iloc[-1]
df['high_best_bid'] = df['bid_price1'].max()
df['low_best_bid'] = df['bid_price1'].min()
df['open_best_ask'] = df['ask_price1'].iloc[0]
df['close_best_ask'] = df['ask_price1'].iloc[-1]
df['high_best_ask'] = df['ask_price1'].max()
df['low_best_ask'] = df['ask_price1'].min()
result = pd.DataFrame(
{
"date": [df['date'].iloc[-1]],
"instrument": [df['instrument'].iloc[-1]],
"open_best_bid": [df['open_best_bid'].iloc[-1]],
"close_best_bid": [df['close_best_bid'].iloc[-1]],
"high_best_bid": [df['high_best_bid'].iloc[-1]],
"low_best_bid": [df['low_best_bid'].iloc[-1]],
"open_best_ask": [df['open_best_ask'].iloc[-1]],
"close_best_ask": [df['close_best_ask'].iloc[-1]],
"high_best_ask": [df['high_best_ask'].iloc[-1]],
"low_best_ask": [df['high_best_ask'].iloc[-1]]
}
)
return result
# 分钟因子加工
while True:
time.sleep(60)
data = engine.df()
if len(data)==0:
continue
stream_data = data.groupby(['date', 'instrument']).apply(OLCH).reset_index(drop=True)