You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何运行含筛选逻辑的Pine Script代码以获取股票列表?

问题描述

我编写的Pine Script代码无错误且已成功保存,但无法运行以获取筛选后的股票列表。该代码用于扫描44MA和200MA均严格上涨的股票,代码内容如下:

//@version=5
//@author=YourName

// This script scans for stocks whose 44MA and 200MA are both strictly increasing.

indicator("MA Cross", overlay=true)

// Calculate the moving averages
ma44 = ta.sma(close, 44)
ma200 = ta.sma(close, 200)

// Check if both moving averages are strictly increasing
ma44_increasing = ta.change(ma44) > 0
ma200_increasing = ta.change(ma200) > 0

// Condition to find stocks with strictly increasing moving averages
condition = ma44_increasing and ma200_increasing

// Plotting the condition on the chart
plotshape(condition, title="Condition Met", location=location.belowbar, color=color.green, style=shape.labelup, text="MA Increasing")

// Printing the list of stocks that meet the condition
var stocks = ""
if condition
    stocks := syminfo.ticker + ", " + stocks

// Displaying the list of stocks
label.new(bar_index, high, text="Stocks: " + stocks, color=color.white, style=label.style_label_left)

代码界面显示脚本已正常保存,请问如何操作才能获取符合条件的股票列表?

解决方案

核心问题

你的代码逻辑和使用场景错误:指标脚本只能在单只股票图表上运行,无法跨品种收集股票列表,必须用TradingView的**扫描器(Scanner)**功能实现批量筛选。

步骤1:修改代码为扫描器专用脚本

把原代码改成输出筛选信号的格式,去掉无效的列表拼接逻辑:

//@version=5
indicator("44&200MA 严格上涨筛选", overlay=true)

// 计算均线
ma44 = ta.sma(close, 44)
ma200 = ta.sma(close, 200)

// 严格上涨判断:连续2根K线均线上涨(可根据需求调整数字)
// 原代码的ta.change仅判断当前与前一根的变化,ta.rising能识别连续上涨趋势
ma44_increasing = ta.rising(ma44, 2)
ma200_increasing = ta.rising(ma200, 2)

// 合并筛选条件
condition = ma44_increasing and ma200_increasing

// 图表上标记符合条件的K线
plotshape(condition, title="符合条件", location=location.belowbar, color=color.green, style=shape.labelup, text="MA上涨")

// 给扫描器输出信号(关键)
plot(condition ? 1 : 0, display=display.data_window)

步骤2:用TradingView扫描器批量筛选

  1. 打开TradingView,点击顶部导航栏的扫描器(Scanner)
  2. 在扫描器设置中:
    • 选择目标市场/板块(比如美股、沪深A股等)
    • 在「条件(Condition)」下拉菜单中,选择你刚保存的筛选脚本
    • 设置时间周期(推荐日线,适配200MA的趋势判断)
  3. 点击「运行扫描」,即可得到所有符合条件的股票列表

原代码的错误点

  • 试图在单品种图表上拼接跨品种股票列表:指标脚本只能访问当前股票的数据,无法获取其他品种信息,这个逻辑从一开始就不成立
  • 严格上涨的判断不准确:ta.change(ma44) > 0仅判断当前K线均线比前一根高,不是真正的上涨趋势,ta.rising(ma44, n)能判断连续n根K线均线上涨,更符合“严格上涨”的需求

内容的提问来源于stack exchange,提问作者Pros Cons

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 15:15:31