问答交流

【新版】如何在新版3.0中如何写下面的因子

由bqynh4yx创建,最终由small_q 被浏览 85 用户

如何在AIStudio3.0环境中写出以下因子

-- 统计30天内主力流入占比大于12%的天数

-- 总资产报酬率roa要大于5

-- 5天的收益率/20天的收益率

-- 最近5日的成交额排名

-- 平均10天的换手率

-- 统计30天内主力流入占比大于12%的天数

-- 现金流量

-- 当日收盘价破 56天最高价(创新高)

-- 10天的sma线/30天的sma线

-- SAR抛物线指标

-- 10天的波动率/60天的波动率

-- CCI14天的指标

-- 3天收益率的 排名

-- 判断 当日的资金流入净额>昨日资金流入净额

-- 当天的超大单流入净量>平均30天内的超大单流入净量(30天超大单MA线)

-- 当天涨幅>5%

-- 30天内的涨幅大于125%

-- 5天内的涨幅>116%

-- 布林带28天均线

-- 统计80天内 涨停板的次数大于5

\

使用新版实现-表达式

我们可以把以上因子分为两类:表达式特征与表达式过滤条件,我们需要将“输入特征(DAI SQL)”模块的模式调整为“表达式

  1. 表达式特征:需要在“输入特征(DAI SQL)”模块中的“表达式特征”一栏,输入以下表达式:
-- 统计30天内主力流入占比大于12%的天数
m_sum(IF(cn_stock_money_flow.main_rate > 0.12, 1, 0), 30) 

-- 5天的收益率/20天的收益率
(cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 5) - 1) / (cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 20) - 1) 

-- 最近5日的成交额排名
m_rank(cn_stock_prefactors.amount, 5) 

-- 平均10天的换手率
m_avg(cn_stock_prefactors.turn, 10) 

-- 现金流量
-- 使用每股现金流向乘以总股本,可以求出总现金流量
cn_stock_factors_financial_indicators.cce_ps_lf * cn_stock_factors_base.total_shares 

-- 10天的sma线/30天的sma线
m_ta_sma(cn_stock_prefactors.close, 10) / m_ta_sma(cn_stock_prefactors.close, 30) 

-- SAR抛物线指标
-- m_ta_sar函数的功能是求SAR指标
m_ta_sar(cn_stock_prefactors.high, cn_stock_prefactors.low) 

-- 10天的波动率/60天的波动率
m_stddev(cn_stock_prefactors.close, 10) / m_stddev(cn_stock_prefactors.close, 60) 

-- CCI14天的指标
-- cn_stock_factors_ta表中预计算好了CCI14天的指标
cn_stock_factors_ta.cci_14 

-- 3天收益率的 排名
c_rank(cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 3) - 1) 

-- 布林带28天均线
-- m_ta_bbands_m函数的功能是求布林带中轨
-- timeperiod参数表示窗口大小;nbdevup参数表示向上取几倍标准差(决定上轨);nbdevdn参数表示向下取几倍标准差(决定下轨)
m_ta_bbands_m(cn_stock_prefactors.close, timeperiod:=28, nbdevup:=1.5, nbdevdn:=1.5, matype:=0) 
  1. 表达式过滤条件:需要在“输入特征(DAI SQL)”模块中的“表达式过滤条件”一栏,输入以下表达式:

注意以下表达式只是表达式编写的演示,因为筛选条件太严格,所以可能提取不出数据

-- 总资产报酬率roa要大于5
cn_stock_factors_financial_indicators.roa2_avg_lf > 0.05

-- 当日收盘价破56天最高价(创新高)
cn_stock_prefactors.close > m_lag(m_max(cn_stock_prefactors.high, 56), 1)

-- 判断当日的资金流入净额>昨日资金流入净额
cn_stock_money_flow.money_netflow > m_lag(cn_stock_money_flow.money_netflow, 1)

-- 当天的超大单流入净量>平均30天内的超大单流入净量(30天超大单MA线)
cn_stock_money_flow.netflow_xl > m_avg(cn_stock_money_flow.netflow_xl, 30)

-- 当天涨幅>5%
cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 1) - 1 > 0.05

-- 30天内的涨幅大于125%
cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 30) - 1 > 1.25

-- 5天内的涨幅>116%
cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 5) - 1 > 1.16

-- 统计80天内 涨停板的次数大于5
m_sum(IF(cn_stock_prefactors.price_limit_status = 3, 1, 0), 80) > 5

\

使用新版实现-SQL

使用SQL实现这些因子和过滤条件,只需将它们编写为以下SQL即可,这时我们需要将“输入特征(DAI SQL)”模块的模式调整为“SQL

注意以下SQL只做代码实现的演示,运行这个SQL可能提不出数据,因为筛选条件太严格

SELECT
    date,
    instrument,
    -- 统计30天内主力流入占比大于12%的天数
    m_sum(IF(cn_stock_money_flow.main_rate > 0.12, 1, 0), 30),
    -- 5天的收益率/20天的收益率
    (cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 5) - 1) / (cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 20) - 1),
    -- 最近5日的成交额排名
    m_rank(cn_stock_prefactors.amount, 5),
    -- 平均10天的换手率
    m_avg(cn_stock_prefactors.turn, 10),
    -- 现金流量
    -- 使用每股现金流向乘以总股本,可以求出总现金流量
    cn_stock_factors_financial_indicators.cce_ps_lf * cn_stock_factors_base.total_shares,
    -- 10天的sma线/30天的sma线
    m_ta_sma(cn_stock_prefactors.close, 10) / m_ta_sma(cn_stock_prefactors.close, 30),
    -- SAR抛物线指标
    -- m_ta_sar函数的功能是求SAR指标
    m_ta_sar(cn_stock_prefactors.high, cn_stock_prefactors.low),
    -- 10天的波动率/60天的波动率
    m_stddev(cn_stock_prefactors.close, 10) / m_stddev(cn_stock_prefactors.close, 60),
    -- CCI14天的指标
    -- cn_stock_factors_ta表中预计算好了CCI14天的指标
    cn_stock_factors_ta.cci_14,
    -- 3天收益率的 排名
    c_rank(cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 3) - 1),
    -- 布林带28天均线
    -- m_ta_bbands_m函数的功能是求布林带中轨
    -- timeperiod参数表示窗口大小;nbdevup参数表示向上取几倍标准差(决定上轨);nbdevdn参数表示向下取几倍标准差(决定下轨)
    m_ta_bbands_m(cn_stock_prefactors.close, timeperiod:=28, nbdevup:=1.5, nbdevdn:=1.5, matype:=0),

FROM cn_stock_prefactors
JOIN cn_stock_money_flow USING (date, instrument)
JOIN cn_stock_factors_ta USING (date, instrument)
JOIN cn_stock_factors_financial_indicators USING (date, instrument)
JOIN cn_stock_factors_base USING (date, instrument)

QUALIFY
    -- 总资产报酬率roa要大于5
    cn_stock_factors_financial_indicators.roa2_avg_lf > 0.05
    -- 当日收盘价破56天最高价(创新高)
    AND cn_stock_prefactors.close > m_lag(m_max(cn_stock_prefactors.high, 56), 1)
    -- 判断当日的资金流入净额>昨日资金流入净额
    AND cn_stock_money_flow.money_netflow > m_lag(cn_stock_money_flow.money_netflow, 1)
    -- 当天的超大单流入净量>平均30天内的超大单流入净量(30天超大单MA线)
    AND cn_stock_money_flow.netflow_xl > m_avg(cn_stock_money_flow.netflow_xl, 30)
    -- 当天涨幅>5%
    AND cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 1) - 1 > 0.05
    -- 30天内的涨幅大于125%
    AND cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 30) - 1 > 1.25
    -- 5天内的涨幅>116%
    AND cn_stock_prefactors.close / m_lag(cn_stock_prefactors.close, 5) - 1 > 1.16
    -- 统计80天内 涨停板的次数大于5
    AND m_sum(IF(cn_stock_prefactors.price_limit_status = 3, 1, 0), 80) > 5

ORDER BY date, instrument

使用这些因子构建一个AI策略

我们可以使用这些因子构建一个AI策略,当然我们在表达式过滤条件这里只使用一部分过滤条件,避免条件太严格选不出数据

https://bigquant.com/codesharev2/6f9bd6b5-e942-480b-9d7d-5e00b52d3e8b

\

标签

股票因子量化交易交易策略数据分析
评论
  • 好的,已经加入到
{link}