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

PineScript报错:无法解析周期'15',求SuperTrend策略解决方案

问题分析与解决方案

错误原因

报错"Cannot parse resolution '15'"是因为**time()函数使用错误**:time()的第一个参数需要传入时间周期(如"D"代表日线、"H1"代表1小时线),而非直接传入时间字符串"15:00:00"。

修正后的完整代码

//@version=4
strategy("Supertrend 策略", overlay=true)

// 输入参数
stopLossPct = input(5, title="止损百分比")
takeProfitPct = input(10, title="止盈百分比")
squareOffHour = input(15, title="平仓小时")
squareOffMinute = input(0, title="平仓分钟")

// SuperTrend 指标计算
Periods = input(title="ATR周期", type=input.integer, defval=10)
src = input(hl2, title="数据源")
Multiplier = input(title="ATR乘数", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="切换ATR计算方式?", type=input.bool, defval=true)

atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2

up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up

dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn

trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

buySignal = trend == 1 and trend[1] == -1
sellSignal = trend == -1 and trend[1] == 1

// 策略逻辑
// 开多单
strategy.entry("多单", strategy.long, when=buySignal)

// 合并止损、止盈与平仓条件
var float entryPrice = na
if (strategy.position_size > 0 and na(entryPrice))
    entryPrice := strategy.position_avg_price

stopLevel = entryPrice * (1 - stopLossPct/100)
profitLevel = entryPrice * (1 + takeProfitPct/100)
squareOffCondition = hour == squareOffHour and minute == squareOffMinute

// 平仓条件:止损/止盈触发,或到指定时间平仓
strategy.exit("平仓", "多单", stop=stopLevel, limit=profitLevel, when=squareOffCondition or strategy.position_size > 0)

// 重置入场价格
if (strategy.position_size == 0)
    entryPrice := na

// 绘制趋势信号
plot(trend, title="Supertrend", style=plot.style_circles)

关键修正点

  • 替换时间判断逻辑:将直接传入时间字符串改为通过hour()和minute()函数判断是否到达指定平仓时间
  • 合并止损止盈:用单个strategy.exit同时设置止损(stop)和止盈(limit),避免多个exit指令冲突
  • 新增入场价格变量:确保止损止盈价格基于开仓时的均价计算,不会随价格波动变化

内容的提问来源于stack exchange,提问作者Sushant Pawar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 05:15:21