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

‘行末无续行符’语法错误解决方案及指标编写需求

Pine Script 语法错误修复与完整指标实现

一、语法错误修复

原代码报错原因有两点:一是未定义candleBody变量,二是缺少脚本版本声明导致语法兼容问题。修复后的基础条件代码如下:

//@version=5
indicator("Fix Syntax Error", overlay=true)

// 计算蜡烛实体长度
candleBody = math.abs(close - open)
// 3周期成交量均线
volSMA = ta.sma(volume, 3)

// 买入条件
buyCondition = candleBody >= 0.8 * (high - low) and
               volume > volSMA and
               close > open

// 卖出条件
sellCondition = candleBody >= 0.8 * (high - low) and
                volume > volSMA and
                close < open

二、完整指标实现(含规则逻辑)

以下代码完整实现了时间过滤、买卖信号、反转K线提示、止损规则及标签显示的全部需求:

//@version=5
indicator("Volume-Price Strategy with Stop Loss", overlay=true, shorttitle="Vol-Price Strat")

// --- 基础变量计算 ---
candleBody = math.abs(close - open)
volSMA = ta.sma(volume, 3)
// 10点后过滤(默认交易所时区,需UTC时区可改为 hour(time, "UTC") >=10)
isAfter10 = hour >= 10

// --- 买卖信号条件 ---
buyCond = isAfter10 and candleBody >= 0.8 * (high - low) and volume > volSMA and close > open
sellCond = isAfter10 and candleBody >= 0.8 * (high - low) and volume > volSMA and close < open

// 延迟信号到下一根K线开盘触发
buySignal = ta.barssince(buyCond) == 1
sellSignal = ta.barssince(sellCond) == 1

// --- 反转K线定义 ---
// 看跌反转:当前阴线且收盘价低于前一根开盘价
bearishReversal = close < open and close < open[1]
// 看涨反转:当前阳线且收盘价高于前一根开盘价
bullishReversal = close > open and close > open[1]

// --- 止损规则与持仓跟踪 ---
var float longEntry = na
var float longStop = na
var float shortEntry = na
var float shortStop = na

// 多头入场:止损设为信号K线的最低价
if (buySignal)
    longEntry := open
    longStop := low[1]
    shortEntry := na
    shortStop := na

// 空头入场:止损设为信号K线的最高价
if (sellSignal)
    shortEntry := open
    shortStop := high[1]
    longEntry := na
    longStop := na

// 止损离场
if (not na(longEntry) and low <= longStop)
    longEntry := na
    longStop := na
if (not na(shortEntry) and high >= shortStop)
    shortEntry := na
    shortStop := na

// --- 信号与标签绘制 ---
// 买入箭头(下一根K线开盘)
plotshape(buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="↑ BUY", textcolor=color.white, size=size.small)
// 卖出箭头(下一根K线开盘)
plotshape(sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="↓ SELL", textcolor=color.white, size=size.small)

// 持仓时的反转K线对勾提示
plotshape(bearishReversal and not na(longEntry), title="Bearish Reversal", location=location.abovebar, color=color.orange, style=shape.triangledown, text="✓", textcolor=color.white, size=size.small)
plotshape(bullishReversal and not na(shortEntry), title="Bullish Reversal", location=location.belowbar, color=color.orange, style=shape.triangleup, text="✓", textcolor=color.white, size=size.small)

// 入场与止损标签
var label longLabel = na
var label shortLabel = na

// 更新多头标签
if (buySignal)
    longLabel := label.new(bar_index, open, "Entry: " + str.tostring(open) + "\nStop: " + str.tostring(longStop), color=color.green, textcolor=color.white, style=label.style_label_down, size=size.small)
else if (not na(longEntry) and low <= longStop)
    label.delete(longLabel)

// 更新空头标签
if (sellSignal)
    shortLabel := label.new(bar_index, open, "Entry: " + str.tostring(open) + "\nStop: " + str.tostring(shortStop), color=color.red, textcolor=color.white, style=label.style_label_up, size=size.small)
else if (not na(shortEntry) and high >= shortStop)
    label.delete(shortLabel)

关键说明

  • 时间过滤:默认使用交易所时区,如需UTC时区可修改isAfter10的判断逻辑
  • 反转K线:当前采用收盘价突破前一根开盘价的逻辑,可根据需求替换为吞没线等其他反转形态
  • 止损设置:当前用信号K线的高低价作为止损,可自行调整为固定点数、ATR倍数等规则

内容的提问来源于stack exchange,提问作者BullDozz Money

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 09:10:00