问答交流

【代码报错】Parser Error: syntax error at or near "ORDER"

由bqy056es创建,最终由small_q 被浏览 10 用户

ORDER BY  报错 帮我看下哪里有问题

import dai


import pandas as pd

# 提取股票数据
stock_sql = """
WITH 
zuori1 AS (
    SELECT 
        cn_stock_bar1d.date, 
        cn_stock_bar1d.instrument,
        close,
        volume,
        volume AS volume_1,
        close AS close_1,
        pe_ttm,
    FROM cn_stock_bar1d JOIN cn_stock_valuation USING (date, instrument)
    WHERE 
        cn_stock_bar1d.date = CURRENT_DATE - INTERVAL '1 DAY'
    AND pe_ttm>0    
)

SELECT 
    date,
    instrument,
    close,
    close_1
    volume,
    volume/volume_1 AS ce,

FROM 
    zuori1

WHERE 
    zuori1.date BETWEEN '2024-01-01' AND '2024-10-31'


QUALIFY ce >1.2;
ORDER BY ce DESC
LIMIT 3;
    
"""


stock_data = dai.query(stock_sql, filters={"date": ["2019-12-01", "2024-11-11"]}).df()
stock_data_cleaned = stock_data.dropna()

# 仓位分配
#设定持仓股票数量
count_num = 3
# 假设 stock_data_cleaned 是包含股票数据的 DataFrame,并且已经计算出了 'ce' 列
filtered_df = stock_data_cleaned.sort_values(by=['date', 'ce'], ascending=[True, False]).groupby('date').head(count_num)

# 设置仓位
filtered_df['position'] = 1 / count_num

# 计算 'ce' 的排名
filtered_df['score_rank'] = filtered_df.groupby('date')['ce'].rank(ascending=False).astype(int)

# 重命名 'ce' 为 'score'
filtered_df = filtered_df.rename(columns={'ce': 'score'})
# 设置回测起始时间和终止时间
start_date = '2020-01-01'
end_date = '2024-08-14'

这个报错提示 Parser Error: syntax error at or near "ORDER"\n

这里的ORDER BY 应该怎么写39-41这几行

我下面的照抄的模板里面的购买股票仓位设置 这里也帮我看下写法有没有问题



\

标签

Python数据处理
评论
  • 非Bug问题请复制保存,提问QuantChat。或前往宽客学院,有免费的sql教程和付费的进阶课程。
{link}