声明:以下代码请在 AIStudio 3.0.0 环境下运行
import dai
import pandas as pd
from datetime import datetime, timedelta
sd = '2021-01-01'
ed = datetime.now().date().strftime("%Y-%m-%d")
计算方式:
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()
计算方式:
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()
计算方式:设x=(今日收盘价—昨日收盘价)/昨日收盘价×当日成交量,那么当日PVT指标值则为从第一个交易日起每日X值的累加
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()
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()
计算方式: N日Σ((收盘价-开盘价)/(最高价-最低价)*成交量)
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()
计算方式: (3日简单移动平均价+6日简单移动平均价+12日简单移动平均价+24日简单移动平均价)/4
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()
计算方式: BBIBOLL多空布林线中线加上3倍BBIBOLL的10日估算标准差
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()
计算方式: BBIBOLL多空布林线中线减去3倍BBIBOLL的10日估算标准差
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()
计算方式: N日的移动平均线
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()
计算方式: 中轨线+两倍的标准差
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()
计算方式: 中轨线-两倍的标准差
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()
计算方式: (前日最高价+前日最低价+前日收盘价*2)/4
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()
计算方式: CDP+(前日最高价-前日最低价)
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()
计算方式: CDP-(前日最高价-前日最低价)
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()
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()
计算方式: CDP*2-前日最高价
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()
计算方式: 简单移动平均(收盘价,N)*1.06
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()
计算方式: 简单移动平均(收盘价,N)*0.94
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()
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()
计算方式: TYP+N天最高价-N天最低价,其中TYP=(最高价+最低价+收盘价)/3
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()
计算方式: 2×N天最高价-N天最低价
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()
计算方式: 当天的成交量除以过去N日的平均成交量
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()
计算方式: 成交量的简单算术平均值
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()
计算方式: 指数平滑移动平均(成交量,S) - 指数平滑移动平均(成交量,LONG)
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()
计算方式: 指数平滑移动平均(VMACD_DIFF,M)
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()
计算方式: VMACD_DIFF-VMACD_DEA,用成交量计算
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()
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()
计算方式: 加权指数成交值TAPI=当日成交总值/当日股票加权指数后再进行移动平均
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()
计算方式: 求N周期的成交量的估算标准差,VSTD = STD(VOL, N)
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()
计算方式:
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()
计算方式:计算收盘价与N天前的收盘价之差,然后对该差值进行N日移动平均
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()
计算方式:计算收盘价与N天前的收盘价之比,然后对该比值进行N日移动平均
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()
计算方式:
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()
计算方式: 统计总数((收盘价>开盘价 AND 对应大盘收盘价>对应大盘开盘价) OR (收盘价<开盘价 AND 对应大盘收盘价<对应大盘开盘价),N)除以N
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()
计算方式: A/B,其中A是(收盘价 > 开盘价且对应大盘收盘价 < 对应大盘开盘价,N)的统计总数,B是(对应大盘收盘价 < 对应大盘开盘价,N)的统计总数
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()
计算方式: A/B,其中A是(收盘价 < 开盘价且对应大盘收盘价 > 对应大盘开盘价,N)的统计总数,B是(对应大盘收盘价 > 对应大盘开盘价,N)的统计总数
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()
计算方式:b为收盘价大于等于前一天收盘价的统计总数除以收盘价小于前一天收盘价的统计总数,取125日移动平均
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()
计算方式:长期用125日移动平均,计算收盘价大于等于前一天收盘价的统计总数除以收盘价小于前一天收盘价的统计总数
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()
计算方式:计算每日的真实范围(TR),然后对TR进行N日简单移动平均
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()
计算方式:计算最高价与最低价的差值,并对其进行指数平均,然后再对指数平均进行指数平均,最后对 AHL/BHL 求 N2 日的指数平均
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()
计算方式:对过去26天的平均涨跌幅序列进行标准差计算
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()
计算方式:过去28天内收盘价的最高值与最低值的差值,除以过去28天内收盘价变化的总和
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()
计算方式:指数平滑移动平均(最高价-最低价,N)的变化率
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()