量价技术因子构建(5)

声明:以下代码请在 AIStudio 3.0.0 环境下运行

In [1]:
import dai
import pandas as pd
from datetime import datetime, timedelta 

sd = '2021-01-01'
ed = datetime.now().date().strftime("%Y-%m-%d")

MFI资金流向指标

计算方式:

  • 典型价格(TP)=当日最高价、最低价与收盘价的算术平均值;货币流量(MF)=典型价格(TP)*当日成交量;
  • 如果当日MF>昨日MF,则将当日的MF值视为正货币流量(PMF),将N日内的正货币流量加总代入公式5;
  • 如果当日MF<昨日MF,则将当日的MF值视为负货币流量(NMF),将N日内的负货币流量加总代入公式5;MFI = 100 -[100/(1+PMF/NMF)]
In [2]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_ta_mfi(high,low,close,volume,14) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[2]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 40.223366
3859032 2024-04-15 873706.BJ 56.195538
3859033 2024-04-15 873726.BJ 36.866006
3859034 2024-04-15 873806.BJ 41.624951
3859035 2024-04-15 873833.BJ 35.603691

3859036 rows × 3 columns

OBV能量潮

计算方式:

  • 从上市第一天起,逐日累计每日上市股票总成交量若当日收盘价高于前一日收盘价,则当日OBV为前OBV加上本日成交量;
  • 若当日收盘价低于前一日收盘价,则当日OBV为前OBV减去本日成交量为了提高OBV的准确性,采取多空比率净额法对其进行修正返回结果OBV/100"
In [3]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_ta_obv(close,volume) AS _obv,
    if(close>pre_close,((close-low)-(high-close))/(high-low)*(_obv+volume),((close-low)-(high-low))/(high-low)*(_obv-volume)) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[3]:
date instrument factor
0 2021-01-04 000001.SZ -0.000000e+00
1 2021-01-04 000002.SZ -0.000000e+00
2 2021-01-04 000004.SZ 2.698908e+06
3 2021-01-04 000005.SZ -0.000000e+00
4 2021-01-04 000006.SZ -0.000000e+00
... ... ... ...
3859031 2024-04-15 873703.BJ -7.114116e+05
3859032 2024-04-15 873706.BJ 1.243548e+06
3859033 2024-04-15 873726.BJ -1.640185e+07
3859034 2024-04-15 873806.BJ -5.663584e+07
3859035 2024-04-15 873833.BJ -2.324831e+07

3859036 rows × 3 columns

PVT量价趋势指标

计算方式:设x=(今日收盘价—昨日收盘价)/昨日收盘价×当日成交量,那么当日PVT指标值则为从第一个交易日起每日X值的累加

In [4]:
sql = f"""
SELECT
    date, 
    instrument, 
    sum((close-pre_close)/pre_close*volume) OVER(PARTITION BY instrument ORDER BY date ROWS UNBOUNDED PRECEDING) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[4]:
date instrument factor
0 2021-01-04 000001.SZ -5.946847e+06
1 2021-01-04 000002.SZ -4.707199e+06
2 2021-01-04 000004.SZ 4.235441e+04
3 2021-01-04 000005.SZ -1.935312e+04
4 2021-01-04 000006.SZ -1.304706e+05
... ... ... ...
3859031 2024-04-15 873703.BJ -6.643223e+05
3859032 2024-04-15 873706.BJ -2.065066e+06
3859033 2024-04-15 873726.BJ 2.216776e+06
3859034 2024-04-15 873806.BJ -2.634666e+06
3859035 2024-04-15 873833.BJ -1.503991e+05

3859036 rows × 3 columns

SOBV能量潮

计算方式:

  • 从上市第一天起,逐日累计股票总成交量若当日收盘价高于开盘价,则当日SOBV为前SOBV加上当日成交量;
  • 若当日收盘价低于开盘价,则当日SOBV为前SOBV减去当日成交量
In [5]:
sql = f"""
SELECT
    date, 
    instrument, 
    sum(if(close>open,volume,-1*volume)) OVER(PARTITION BY instrument ORDER BY date ROWS UNBOUNDED PRECEDING) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[5]:
date instrument factor
0 2021-01-04 000001.SZ -155421643
1 2021-01-04 000002.SZ -146844133
2 2021-01-04 000004.SZ 2739801
3 2021-01-04 000005.SZ -4896339
4 2021-01-04 000006.SZ -8132668
... ... ... ...
3859031 2024-04-15 873703.BJ 18508495
3859032 2024-04-15 873706.BJ -28560686
3859033 2024-04-15 873726.BJ 23944261
3859034 2024-04-15 873806.BJ 140861197
3859035 2024-04-15 873833.BJ 44713171

3859036 rows × 3 columns

WVAD威廉变异离散量

计算方式: N日Σ((收盘价-开盘价)/(最高价-最低价)*成交量)

In [6]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_sum((close - open)/(high - low)*volume,6) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument 
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[6]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -1.495275e+05
3859032 2024-04-15 873706.BJ 3.679563e+06
3859033 2024-04-15 873726.BJ 6.725787e+05
3859034 2024-04-15 873806.BJ -6.765240e+06
3859035 2024-04-15 873833.BJ -4.626548e+05

3859036 rows × 3 columns

BBIBOLL多空布林线中线

计算方式: (3日简单移动平均价+6日简单移动平均价+12日简单移动平均价+24日简单移动平均价)/4

In [7]:
sql = f"""
SELECT
    date, 
    instrument, 
    (m_avg(close,3)+m_avg(close,6)+m_avg(close,12)+m_avg(close,24))/4 AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[7]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 25.320729
3859032 2024-04-15 873706.BJ 13.184519
3859033 2024-04-15 873726.BJ 24.414687
3859034 2024-04-15 873806.BJ 9.006667
3859035 2024-04-15 873833.BJ 10.438267

3859036 rows × 3 columns

BBIBOLL多空布林线上轨

计算方式: BBIBOLL多空布林线中线加上3倍BBIBOLL的10日估算标准差

In [8]:
sql = f"""
SELECT
    date, 
    instrument, 
    (m_avg(close,3)+m_avg(close,6)+m_avg(close,12)+m_avg(close,24))/4 AS _BIBIBOLL,
    _BIBIBOLL + 3*m_stddev(_BIBIBOLL,10) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[8]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 26.254776
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 26.206018
3859034 2024-04-15 873806.BJ 9.955562
3859035 2024-04-15 873833.BJ 11.104326

3859036 rows × 3 columns

BBIBOLL多空布林线下轨

计算方式: BBIBOLL多空布林线中线减去3倍BBIBOLL的10日估算标准差

In [9]:
sql = f"""
SELECT
    date, 
    instrument, 
    (m_avg(close,3)+m_avg(close,6)+m_avg(close,12)+m_avg(close,24))/4 AS _BIBIBOLL,
    _BIBIBOLL - 3*m_stddev(_BIBIBOLL,10) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[9]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 24.386682
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 22.623357
3859034 2024-04-15 873806.BJ 8.057772
3859035 2024-04-15 873833.BJ 9.772209

3859036 rows × 3 columns

BOLL布林带中线

计算方式: N日的移动平均线

In [10]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_ta_bbands_m(close,timeperiod:=26) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[10]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 26.277308
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 25.530385
3859034 2024-04-15 873806.BJ 9.615769
3859035 2024-04-15 873833.BJ 10.978935

3859036 rows × 3 columns

BOLL布林带上轨

计算方式: 中轨线+两倍的标准差

In [11]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_ta_bbands_u(close,timeperiod:=26) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[11]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 28.831686
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 28.816816
3859034 2024-04-15 873806.BJ 10.776607
3859035 2024-04-15 873833.BJ 12.117475

3859036 rows × 3 columns

BOLL布林带下轨

计算方式: 中轨线-两倍的标准差

In [12]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_ta_bbands_l(close,timeperiod:=26) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[12]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 23.722930
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 22.243953
3859034 2024-04-15 873806.BJ 8.454932
3859035 2024-04-15 873833.BJ 9.840396

3859036 rows × 3 columns

BBIBOLL多空布林线中线CDP逆势操作CDP

计算方式: (前日最高价+前日最低价+前日收盘价*2)/4

In [13]:
sql = f"""
SELECT
    date, 
    instrument, 
    (m_lag(high,1)+m_lag(low,1)+2*m_lag(close,1))/4 AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[13]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 25.050000
3859032 2024-04-15 873706.BJ 13.367532
3859033 2024-04-15 873726.BJ 24.880000
3859034 2024-04-15 873806.BJ 8.652500
3859035 2024-04-15 873833.BJ 10.242923

3859036 rows × 3 columns

CDP逆势操作AH

计算方式: CDP+(前日最高价-前日最低价)

In [14]:
sql = f"""
SELECT
    date, 
    instrument, 
    (m_lag(high,1)+m_lag(low,1)+2*m_lag(close,1))/4 AS _CDP,
    _CDP + (m_lag(high,1)-m_lag(low,1)) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument 
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[14]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 25.510000
3859032 2024-04-15 873706.BJ 15.036056
3859033 2024-04-15 873726.BJ 26.480000
3859034 2024-04-15 873806.BJ 8.922500
3859035 2024-04-15 873833.BJ 10.426459

3859036 rows × 3 columns

CDP逆势操作AL

计算方式: CDP-(前日最高价-前日最低价)

In [15]:
sql = f"""
SELECT
    date, 
    instrument, 
    (m_lag(high,1)+m_lag(low,1)+2*m_lag(close,1))/4 AS _CDP,
    _CDP - (m_lag(high,1)-m_lag(low,1)) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument 
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[15]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 24.590000
3859032 2024-04-15 873706.BJ 11.699007
3859033 2024-04-15 873726.BJ 23.280000
3859034 2024-04-15 873806.BJ 8.382500
3859035 2024-04-15 873833.BJ 10.059387

3859036 rows × 3 columns

CDP逆势操作NH

计算方式:

  • CDP*2-前日最低价
In [16]:
sql = f"""
SELECT
    date, 
    instrument, 
    (m_lag(high,1)+m_lag(low,1)+2*m_lag(close,1))/4 AS _CDP,
    2*_CDP - m_lag(low,1) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument 
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[16]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 25.330000
3859032 2024-04-15 873706.BJ 13.895161
3859033 2024-04-15 873726.BJ 25.490000
3859034 2024-04-15 873806.BJ 8.725000
3859035 2024-04-15 873833.BJ 10.337390

3859036 rows × 3 columns

CDP逆势操作NL

计算方式: CDP*2-前日最高价

In [17]:
sql = f"""
SELECT
    date, 
    instrument, 
    (m_lag(high,1)+m_lag(low,1)+2*m_lag(close,1))/4 AS _CDP,
    2*_CDP - m_lag(high,1) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument 
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[17]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 24.870000
3859032 2024-04-15 873706.BJ 12.226637
3859033 2024-04-15 873726.BJ 23.890000
3859034 2024-04-15 873806.BJ 8.455000
3859035 2024-04-15 873833.BJ 10.153854

3859036 rows × 3 columns

ENV指标upper

计算方式: 简单移动平均(收盘价,N)*1.06

In [18]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_avg(close,14)*1.06 AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[18]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 26.705943
3859032 2024-04-15 873706.BJ 13.680573
3859033 2024-04-15 873726.BJ 25.614143
3859034 2024-04-15 873806.BJ 9.758814
3859035 2024-04-15 873833.BJ 11.135833

3859036 rows × 3 columns

ENV指标lower

计算方式: 简单移动平均(收盘价,N)*0.94

In [19]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_avg(close,14)*0.94 AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[19]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 23.682629
3859032 2024-04-15 873706.BJ 12.131829
3859033 2024-04-15 873726.BJ 22.714429
3859034 2024-04-15 873806.BJ 8.654043
3859035 2024-04-15 873833.BJ 9.875173

3859036 rows × 3 columns

MIKE麦克指标WR

计算方式:

  • TYP+TYP-N天最低价
  • 其中TYP=(最高价+最低价+收盘价)/3
In [20]:
sql = f"""
SELECT
    date, 
    instrument,
    (high+low+close)/3 AS _TYP,
    _TYP + _TYP - m_min(low,12) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[20]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 26.070000
3859032 2024-04-15 873706.BJ 14.932003
3859033 2024-04-15 873726.BJ 24.376667
3859034 2024-04-15 873806.BJ 8.630000
3859035 2024-04-15 873833.BJ 10.238425

3859036 rows × 3 columns

MIKE麦克指标MR

计算方式: TYP+N天最高价-N天最低价,其中TYP=(最高价+最低价+收盘价)/3

In [21]:
sql = f"""
SELECT
    date, 
    instrument,
    (high+low+close)/3 AS _TYP,
    _TYP + m_max(high,12) - m_min(low,12) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument

"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[21]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 27.190000
3859032 2024-04-15 873706.BJ 16.062769
3859033 2024-04-15 873726.BJ 27.153333
3859034 2024-04-15 873806.BJ 10.340000
3859035 2024-04-15 873833.BJ 11.332443

3859036 rows × 3 columns

MIKE麦克指标SR

计算方式: 2×N天最高价-N天最低价

In [22]:
sql = f"""
SELECT
    date, 
    instrument,
    2*m_max(high,12) - m_min(low,12) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[22]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 28.310000
3859032 2024-04-15 873706.BJ 17.193535
3859033 2024-04-15 873726.BJ 29.930000
3859034 2024-04-15 873806.BJ 12.050000
3859035 2024-04-15 873833.BJ 12.426461

3859036 rows × 3 columns

量比指标

计算方式: 当天的成交量除以过去N日的平均成交量

In [23]:
sql = f"""
SELECT
    date, 
    instrument,
    volume/m_avg(volume,12) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[23]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 0.840912
3859032 2024-04-15 873706.BJ 1.640511
3859033 2024-04-15 873726.BJ 1.189139
3859034 2024-04-15 873806.BJ 0.807442
3859035 2024-04-15 873833.BJ 1.043410

3859036 rows × 3 columns

VMA量简单移动平均

计算方式: 成交量的简单算术平均值

In [24]:
sql = f"""
SELECT
    date, 
    instrument,
    m_avg(volume,5) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[24]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 400111.2
3859032 2024-04-15 873706.BJ 4735905.0
3859033 2024-04-15 873726.BJ 960786.0
3859034 2024-04-15 873806.BJ 3889851.2
3859035 2024-04-15 873833.BJ 434282.8

3859036 rows × 3 columns

VMACD量指数平滑异同平均DIFF

计算方式: 指数平滑移动平均(成交量,S) - 指数平滑移动平均(成交量,LONG)

In [25]:
sql = f"""
SELECT
    date, 
    instrument,
    m_ta_macd_dif(volume) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[25]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -2.784494e+05
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ -1.110097e+05
3859034 2024-04-15 873806.BJ -2.402600e+06
3859035 2024-04-15 873833.BJ -1.688084e+05

3859036 rows × 3 columns

VMACD量指数平滑异同平均DEA

计算方式: 指数平滑移动平均(VMACD_DIFF,M)

In [26]:
sql = f"""
SELECT
    date, 
    instrument,
    m_ta_macd_dea(volume) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[26]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -2.995367e+05
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ -1.999552e+05
3859034 2024-04-15 873806.BJ -2.737370e+06
3859035 2024-04-15 873833.BJ -1.990238e+05

3859036 rows × 3 columns

VMACD量指数平滑异同平均MACD

计算方式: VMACD_DIFF-VMACD_DEA,用成交量计算

In [27]:
sql = f"""
SELECT
    date, 
    instrument,
    m_ta_macd_hist(volume) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[27]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 42174.530763
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 177890.856689
3859034 2024-04-15 873806.BJ 669540.148926
3859035 2024-04-15 873833.BJ 60430.860163

3859036 rows × 3 columns

VOSC成交量震荡

计算方式:

  • 先分别计算短期移动平均线(SHORT)和长期移动平均线(LONG),
  • 然后算两者的差值,再求差值与短期移动平均线(SHORT)的比,
  • 最后将比值放大100倍
In [28]:
sql = f"""
SELECT
    date, 
    instrument,
    100 * ((m_avg(volume,12) - m_avg(volume,26)) / m_avg(volume,12)) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[28]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -104.743574
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ -15.370604
3859034 2024-04-15 873806.BJ -10.457437
3859035 2024-04-15 873833.BJ -34.450521

3859036 rows × 3 columns

TAPI加权指数成交值

计算方式: 加权指数成交值TAPI=当日成交总值/当日股票加权指数后再进行移动平均

In [29]:
sql = f"""
SELECT
    date, 
    instrument,
    close * total_market_cap / c_sum(total_market_cap) AS _index,
    m_sum(amount / _index, 6) AS factor
FROM cn_stock_bar1d 
LEFT JOIN cn_stock_valuation USING (date, instrument)
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[29]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 1.153979e+11
3859032 2024-04-15 873706.BJ 1.899726e+12
3859033 2024-04-15 873726.BJ 2.306867e+11
3859034 2024-04-15 873806.BJ 8.503070e+11
3859035 2024-04-15 873833.BJ 2.823047e+11

3859036 rows × 3 columns

VSTD成交量标准差

计算方式: 求N周期的成交量的估算标准差,VSTD = STD(VOL, N)

In [30]:
sql = f"""
SELECT
    date, 
    instrument,
    m_stddev(volume, 10) AS factor
FROM cn_stock_bar1d 
LEFT JOIN cn_stock_valuation USING (date, instrument)
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[30]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 1.771918e+05
3859032 2024-04-15 873706.BJ 2.134834e+06
3859033 2024-04-15 873726.BJ 4.546031e+05
3859034 2024-04-15 873806.BJ 1.156472e+06
3859035 2024-04-15 873833.BJ 1.638082e+05

3859036 rows × 3 columns

ADTM动态买卖气指标

计算方式:

  • 如果开盘价≤昨日开盘价,DTM=0如果开盘价>昨日开盘价,DTM=(最高价-开盘价)和(开盘价-昨日开盘价)的较大值
  • 如果开盘价≥昨日开盘价,DBM=0如果开盘价<昨日开盘价,DBM=(开盘价-最低价)和(开盘价-昨日开盘价)的较大值
  • STM=DTM在N日内的和
  • SBM=DBM在N日内的和
  • 如果STM>SBM,ADTM=(STM-SBM)/STM如果STM<SBM,ADTM=(STM-SBM)/SBM,如果STM=SBM,ADTM=0
In [31]:
sql = f"""
SELECT
    date, 
    instrument, 
    IF(open <= m_lag(open, 1), 0, GREATEST((high - open), (open - m_lag(open, 1)))) AS _DTM,
    IF(open >= m_lag(open, 1), 0, GREATEST((open - low), (open - m_lag(open, 1)))) AS _DBM,
    m_sum(_DTM, 23) AS _STM,
    m_sum(_DBM, 23) AS _SBM,
    IF(_STM > _SBM, (_STM - _SBM) / _STM, (_STM - _SBM) / _SBM) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[31]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -0.296954
3859032 2024-04-15 873706.BJ -0.242086
3859033 2024-04-15 873726.BJ 0.003337
3859034 2024-04-15 873806.BJ -0.251634
3859035 2024-04-15 873833.BJ -0.430070

3859036 rows × 3 columns

MI动量指标

计算方式:计算收盘价与N天前的收盘价之差,然后对该差值进行N日移动平均

In [32]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_avg(close - m_lag(close, 12), 12) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[32]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -1.882500
3859032 2024-04-15 873706.BJ -1.810330
3859033 2024-04-15 873726.BJ -2.710833
3859034 2024-04-15 873806.BJ -0.786667
3859035 2024-04-15 873833.BJ -0.936573

3859036 rows × 3 columns

RC变化率指数

计算方式:计算收盘价与N天前的收盘价之比,然后对该比值进行N日移动平均

In [33]:
sql = f"""
SELECT
    date, 
    instrument, 
    m_avg(close / m_lag(close, 50), 50) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[33]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ NaN
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 0.881913
3859034 2024-04-15 873806.BJ NaN
3859035 2024-04-15 873833.BJ 0.749365

3859036 rows × 3 columns

SRMI-MI修正指标

计算方式:

  • 如果收盘价小于N天前的收盘价,则取(收盘价-向前引用(收盘价,N))/向前引用(收盘价,N)
  • 如果收盘价等于N天前的收盘价,则取0
  • 否则取(收盘价-向前引用(收盘价,N))/收盘价
In [34]:
sql = f"""
SELECT
    date, 
    instrument, 
    IF(
        close < m_lag(close, 9),
        (close - m_lag(close, 9)) / m_lag(close, 9),
        IF(
            close = m_lag(close, 9),
            0,
            (close - m_lag(close, 9)) / close
        )
    ) AS factor
FROM cn_stock_bar1d 
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[34]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -0.016843
3859032 2024-04-15 873706.BJ 0.041667
3859033 2024-04-15 873726.BJ -0.055601
3859034 2024-04-15 873806.BJ -0.145361
3859035 2024-04-15 873833.BJ -0.083083

3859036 rows × 3 columns

MSI大盘同步指标

计算方式: 统计总数((收盘价>开盘价 AND 对应大盘收盘价>对应大盘开盘价) OR (收盘价<开盘价 AND 对应大盘收盘价<对应大盘开盘价),N)除以N

In [35]:
sql = f"""
SELECT
    date, 
    T1.instrument,
    AVG(
        IF(
            (T1.close >= T1.open AND T2.close >= T2.open) OR (T1.close < T1.open AND T2.close < T2.open),
            1,
            0
        )
    ) OVER (PARTITION BY T1.instrument ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS factor
FROM cn_stock_bar1d AS T1 
LEFT JOIN cn_stock_index_bar1d AS T2 USING (date)
WHERE T2.instrument = '000001.SH'
ORDER BY date, T1.instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[35]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 0.428571
3859032 2024-04-15 873706.BJ 0.857143
3859033 2024-04-15 873726.BJ 0.714286
3859034 2024-04-15 873806.BJ 0.571429
3859035 2024-04-15 873833.BJ 0.285714

3859036 rows × 3 columns

PSI阶段强势指标

计算方式: A/B,其中A是(收盘价 > 开盘价且对应大盘收盘价 < 对应大盘开盘价,N)的统计总数,B是(对应大盘收盘价 < 对应大盘开盘价,N)的统计总数

In [36]:
sql = f"""
SELECT
    date, 
    T1.instrument,
    SUM(IF(T1.close >= T1.open AND T2.close < T2.open, 1, 0)) OVER (PARTITION BY T1.instrument ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS _A,
    SUM(IF(T2.close < T2.open, 1, 0)) OVER (PARTITION BY T1.instrument ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS _B,
    _A / _B AS factor
FROM cn_stock_bar1d AS T1 
LEFT JOIN cn_stock_index_bar1d AS T2 USING (date)
WHERE T2.instrument = '000001.SH'
ORDER BY date, T1.instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[36]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 0.50
3859032 2024-04-15 873706.BJ 0.25
3859033 2024-04-15 873726.BJ 0.25
3859034 2024-04-15 873806.BJ 0.25
3859035 2024-04-15 873833.BJ 0.50

3859036 rows × 3 columns

PWI阶段弱势指标

计算方式: A/B,其中A是(收盘价 < 开盘价且对应大盘收盘价 > 对应大盘开盘价,N)的统计总数,B是(对应大盘收盘价 > 对应大盘开盘价,N)的统计总数

In [37]:
sql = f"""
SELECT
    date, 
    T1.instrument,
    SUM(IF(T1.close < T1.open AND T2.close > T2.open, 1, 0)) OVER (PARTITION BY T1.instrument ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS _A,
    SUM(IF(T2.close > T2.open, 1, 0)) OVER (PARTITION BY T1.instrument ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS _B,
    _A / _B AS factor
FROM cn_stock_bar1d AS T1 
LEFT JOIN cn_stock_index_bar1d AS T2 USING (date)
WHERE T2.instrument = '000001.SH'
ORDER BY date, T1.instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[37]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 0.666667
3859032 2024-04-15 873706.BJ 0.000000
3859033 2024-04-15 873726.BJ 0.333333
3859034 2024-04-15 873806.BJ 0.666667
3859035 2024-04-15 873833.BJ 1.000000

3859036 rows × 3 columns

BBI筑底指标short

计算方式:b为收盘价大于等于前一天收盘价的统计总数除以收盘价小于前一天收盘价的统计总数,取125日移动平均

In [38]:
sql = f"""
SELECT
    date, 
    instrument,
    m_sum(IF(close >= m_lag(close, 1), 1, 0), 125) / m_sum(IF(close < m_lag(close, 1), 1, 0), 125) AS _A,
    m_avg(_A, 5) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[38]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ NaN
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ NaN
3859034 2024-04-15 873806.BJ NaN
3859035 2024-04-15 873833.BJ NaN

3859036 rows × 3 columns

BBI筑底指标long

计算方式:长期用125日移动平均,计算收盘价大于等于前一天收盘价的统计总数除以收盘价小于前一天收盘价的统计总数

In [39]:
sql = f"""
SELECT
    date, 
    instrument,
    m_sum(IF(close >= m_lag(close, 1), 1, 0), 125) / m_sum(IF(close < m_lag(close, 1), 1, 0), 125) AS _A,
    m_avg(_A, 20) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[39]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ NaN
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ NaN
3859034 2024-04-15 873806.BJ NaN
3859035 2024-04-15 873833.BJ NaN

3859036 rows × 3 columns

ATR真实波幅

计算方式:计算每日的真实范围(TR),然后对TR进行N日简单移动平均

In [40]:
sql = f"""
SELECT
    date, 
    instrument,
    m_avg(greatest(abs(high - low), abs(pre_close - high), abs(pre_close - low)), 14) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[40]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 0.800714
3859032 2024-04-15 873706.BJ 0.780592
3859033 2024-04-15 873726.BJ 1.185000
3859034 2024-04-15 873806.BJ 0.467857
3859035 2024-04-15 873833.BJ 0.328514

3859036 rows × 3 columns

MASS梅丝指标

计算方式:计算最高价与最低价的差值,并对其进行指数平均,然后再对指数平均进行指数平均,最后对 AHL/BHL 求 N2 日的指数平均

In [41]:
sql = f"""
SELECT
    date, 
    instrument,
    high - low AS _DIF,
    m_ta_ema(_DIF, 9) AS _AHL,
    m_ta_ema(m_ta_ema(_DIF, 9), 9) AS _BHL,
    m_ta_sum(m_ta_ema(_DIF, 9) / m_ta_ema(m_ta_ema(_DIF, 9), 9), 25) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[41]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 21.792683
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 21.947070
3859034 2024-04-15 873806.BJ 22.944530
3859035 2024-04-15 873833.BJ 21.422030

3859036 rows × 3 columns

STD标准差

计算方式:对过去26天的平均涨跌幅序列进行标准差计算

In [42]:
sql = f"""
SELECT
    date, 
    instrument,
    m_stddev(m_avg(close/m_lag(close,1)-1, 26), 26) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[42]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ 0.004431
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ 0.003951
3859034 2024-04-15 873806.BJ 0.003249
3859035 2024-04-15 873833.BJ 0.004198

3859036 rows × 3 columns

VHF纵横指标

计算方式:过去28天内收盘价的最高值与最低值的差值,除以过去28天内收盘价变化的总和

In [43]:
sql = f"""
SELECT
    date, 
    instrument,
    (m_max(close, 28) - m_min(close, 28)) / (m_sum(abs(close - m_lag(close, 1)), 28)) AS _factor_temp,
    -1 * _factor_temp AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[43]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -0.270754
3859032 2024-04-15 873706.BJ NaN
3859033 2024-04-15 873726.BJ -0.317690
3859034 2024-04-15 873806.BJ -0.321484
3859035 2024-04-15 873833.BJ -0.368635

3859036 rows × 3 columns

CVLT佳庆离散指标

计算方式:指数平滑移动平均(最高价-最低价,N)的变化率

In [44]:
sql = f"""
SELECT
    date, 
    instrument,
    m_ta_ema(high - low, 10) AS _EC,
    (_EC - m_lag(_EC, 10)) / m_lag(_EC, 10) * 100 AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""

dai.query(sql, filters={'date':[sd, ed]}).df()
Out[44]:
date instrument factor
0 2021-01-04 000001.SZ NaN
1 2021-01-04 000002.SZ NaN
2 2021-01-04 000004.SZ NaN
3 2021-01-04 000005.SZ NaN
4 2021-01-04 000006.SZ NaN
... ... ... ...
3859031 2024-04-15 873703.BJ -25.517136
3859032 2024-04-15 873706.BJ 24.398246
3859033 2024-04-15 873726.BJ 14.701564
3859034 2024-04-15 873806.BJ -17.009079
3859035 2024-04-15 873833.BJ -10.917692

3859036 rows × 3 columns