声明:以下代码请在 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")
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
(close - m_min(low, {n})) / (m_max(high, {n}) - m_min(low, {n}) + 1e-12) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_imax(high, {n})/{n} AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_imin(low, {n})/{n} AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
(m_imax(high, {n})-m_imin(low, {n}))/{n} AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_corr(close, volume, {n}) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_corr(close/m_lag(close, 1), log(volume/m_lag(volume, 1)+1), {n}) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_avg(IF(close>m_lag(close, 1), 1, 0), {n}) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_avg(IF(close<m_lag(close, 1), 1, 0), {n}) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_avg(IF(close>m_lag(close, 1), 1, 0), {n}) - m_avg(IF(close<m_lag(close, 1), 1, 0), {n}) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_sum(IF(close-m_lag(close, 1)>0, close-m_lag(close, 1), 0), {n}) / (m_sum(abs(close-m_lag(close, 1)), {n}) + 1e-12) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_sum(IF(m_lag(close, 1)-close < 0, 0, m_lag(close, 1)-close), {n}) / (m_sum(abs(close-m_lag(close, 1)), {n}) + 1e-12) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
N:一般取值为5、20、60、120、250
n = 5
sql = f"""
SELECT
date,
instrument,
m_sum(IF(m_lag(close, 1)-close > 0, m_lag(close, 1)-close, 0), {n}) / (m_sum(abs(close-m_lag(close, 1)), {n})+1e-12) 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, 1) AND m_lag(close, 1) > m_lag(close, 2) AND m_lag(close, 2) > m_lag(close, 3), 1, 0 ) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
sql = f"""
SELECT
date,
instrument,
IF(price_limit_status = 3 AND m_lag(price_limit_status,1) = 3 AND m_lag(price_limit_status,2) = 3, 1, 0) AS factor
FROM cn_stock_status
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()
sql = f"""
SELECT
date,
instrument,
m_avg(close, 20) + 2 * m_stddev(close, 20) AS up_band,
IF(close > up_band, 1, 0) 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(close, 20) - 2 * m_stddev(close, 20) AS low_band,
IF(close < low_band, 1, 0) AS factor
FROM cn_stock_bar1d
ORDER BY date, instrument
"""
dai.query(sql, filters={'date':[sd, ed]}).df()