情绪周期中涨跌停数、最高板数等代码编写
由deledeleboy创建,最终由deledeleboy 被浏览 22 用户
情绪周期中涨跌停数、最高板数等代码编写
1、涨跌停数
-- 涨停为1,其他为0
if (price_limit_status==3,1,0) as upper_limit,
-- 跌停为1,其他为0
if (price_limit_status==1,1,0) as lower_limit,
-- 截面求和
sum(upper_limit) over (partition by date) as upper_limit_count,
sum(lower_limit) over (partition by date) as lower_limit_count,
2、最高板数
-- 连续涨停板数量
abs(upper_limit-m_lag(upper_limit,1)) as _upper_diff,
sum(_upper_diff) over (partition by instrument order by date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as _diff_upper_sum,
sum(upper_limit) over (partition by instrument,_diff_upper_sum order by date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as continous_upper_limit_times,
-- 连续跌停板数量
abs(lower_limit-m_lag(lower_limit,1)) as _lower_diff,
sum(_lower_diff) over (partition by instrument order by date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as _diff_lower_sum,
sum(lower_limit) over (partition by instrument,_diff_lower_sum order by date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as continous_lower_limit_times,
3、代码
https://bigquant.com/codesharev2/82e1c2f7-16b8-42f7-a290-f32f0409ab37
\