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

如何在TradingView上回测Pine Script编写的交易策略?

在TradingView上回测你的SuperTrend策略指南

你的现有Pine Script代码已经实现了SuperTrend的信号逻辑,但缺少触发交易的下单指令,这是无法完成回测的核心原因。以下是具体解决步骤:

一、添加交易执行逻辑

在代码中找到Buy和Sell信号的定义后,补充策略下单函数,让信号实际触发交易:

  • 当Buy信号触发时,平空开多
  • 当Sell信号触发时,平多开空

可以直接用strategy.entry的反转模式简化代码,也可以结合通道线设置动态止损:

// 执行交易:信号触发时开仓,自动反向平仓
if (Buy)
    strategy.entry("Long", strategy.long)
if (Sell)
    strategy.entry("Short", strategy.short)

// 动态止损:基于通道线设置止损位
if (strategy.position_size > 0)
    strategy.exit("Exit Long", "Long", stop=up)
if (strategy.position_size < 0)
    strategy.exit("Exit Short", "Short", stop=dn)

二、配置回测基础参数

在开头的strategy函数中,补充回测相关参数,让回测更贴近真实交易场景:

strategy('Buy or Sell Signal', overlay=true, 
         initial_capital=10000, // 初始资金
         commission_type=strategy.commission.percent, commission_value=0.1, // 佣金比例0.1%
         slippage=0.1, // 滑点0.1个点
         default_qty_type=strategy.cash, default_qty_value=1000, // 每次用1000元开仓
         margin_long=100, margin_short=100) // 保证金比例(100%即非杠杆)

三、修改后的完整代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at 
https://mozilla.org/MPL/2.0/
//@version=5
strategy('Buy or Sell Signal', overlay=true, 
         initial_capital=10000,
         commission_type=strategy.commission.percent, commission_value=0.1,
         slippage=0.1,
         default_qty_type=strategy.cash, default_qty_value=1000,
         margin_long=100, margin_short=100)

//inputs
Source = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3.0)

//Compute ATR Levels
atr = ta.atr(input(title='ATR Period', defval=10))

//Creating Upper Channel
up = Source - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up

//Creating Down Channel
dn = Source + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn

//Compute the Trend Stream +1/-1
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

//Create Stoploss for Longs
upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))

//Buy Signal
Buy = trend == 1 and trend[1] == -1

plotshape(Buy ? up : na, title='Go Long', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(Buy ? up : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))

dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0))

//Sell Signal
Sell = trend == -1 and trend[1] == 1

plotshape(Sell ? dn : na, title='Go Short', location=location.absolute, style=shape.circle, size=size.tiny, color=color.rgb(255, 82, 82, 10))
plotshape(Sell ? dn : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.rgb(255, 82, 82, 6), textcolor=color.new(color.white, 0))

iPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0)

longFillColor = trend == 1 ? color.rgb(76, 175, 79, 90) : color.rgb(255, 255, 255, 11)
shortFillColor = trend == -1 ? color.rgb(255, 82, 82, 90) : color.rgb(255, 255, 255, 11)

fill(iPlot, upPlot, title='UpTrend Highligter', color=longFillColor)
fill(iPlot, dnPlot, title='DownTrend Highligter', color=shortFillColor)

//Alerts
alertcondition(Buy, title='SuperTrend Buy', message='SuperTrend Buy!')
alertcondition(Sell, title='SuperTrend Sell', message='SuperTrend Sell!')

buycontiue = ta.barssince(Sell) > ta.barssince(Buy)
sellcontinue = ta.barssince(Buy) > ta.barssince(Sell)

color = buycontiue[1] ? color.green : sellcontinue ? color.red : na
barcolor(color)

// 新增:交易执行逻辑
if (Buy)
    strategy.entry("Long", strategy.long)
if (Sell)
    strategy.entry("Short", strategy.short)

// 新增:动态止损(基于通道线)
if (strategy.position_size > 0)
    strategy.exit("Exit Long", "Long", stop=up)
if (strategy.position_size < 0)
    strategy.exit("Exit Short", "Short", stop=dn)

四、运行回测并查看结果

  1. 将修改后的代码粘贴到TradingView的Pine编辑器中,点击「Add to Chart」
  2. 点击图表下方的「Strategy Tester」标签(回测面板)
  3. 在面板中选择回测的时间范围、品种,点击「Run Strategy」
  4. 查看回测报告,包括胜率、盈亏比、最大回撤等关键指标

内容的提问来源于stack exchange,提问作者Bjarni Leifsson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 09:05:39