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

TradingView类策略止盈准确率回测指标开发咨询

自定义交易策略开发参考需求

我正在寻找可供学习参考的交易指标,计划自主开发实现与下述示例功能相近的自定义交易策略:

示例截图

需实现的核心功能

  • 支持完整多空方向切换逻辑:可处理LONG转SHORT、平LONG后开SHORT再转回LONG等全场景多空循环
  • 逐档计算各止盈(take profits, TP)位的触发准确率:例如示例中6条绿色TP线的命中率,需在右侧信息面板展示对应统计值
  • 支持行情峰值点计算
  • 面板展示Net Profit(净利润)、Gross Profit(总盈利)、Gross Loss(总亏损)三类核心绩效指标

咨询问题

是否存在可实现此类策略“回测”功能的同类公开指标,可供参考学习?

现有基础实现代码

//@version=5
indicator("Strategy", overlay = true)

rsi = ta.rsi(close, 5)

// MACD
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

macd = request.security_lower_tf(syminfo.tickerid, "1", macdLine)
signal = request.security_lower_tf(syminfo.tickerid, "1", signalLine)
hist = request.security_lower_tf(syminfo.tickerid, "1", histLine)

// MA
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

offset = input.int(title = "Offset", defval = 0, minval = -500, maxval = 500, group = "MA")
typeMA = input.string(title = "Method", defval = "EMA", options = ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group = "MA")
length = input.int(title = "Length", defval = 200, minval = 1, maxval = 500, group = "MA")

ema = ma(close, length, typeMA)
plot(ema, title = "Line", color = #f37f20, offset = offset, display = display.all)

// Strategy
var string TD1 = "Both"
var string TD2 = "Longs Only"
var string TD3 = "Shorts Only"
string  i_tradeDirection = input.string(TD1, "Trade Direction", options = [TD1, TD2, TD3], group = "Strategy")
var bool doLongs  = (i_tradeDirection == TD2 or i_tradeDirection == TD1)
var bool doShorts = (i_tradeDirection == TD3 or i_tradeDirection == TD1)

// ————— States
var float entryPrice = na
var bool inLong = false
var bool inShort = false
bool inTrade = inLong or inShort

// ————— Conditions
buyCondition = close > ema 
enterLongCondition = not buyCondition[1] and buyCondition
enterShortCondition = buyCondition[1] and not buyCondition
exitLongCondition = enterShortCondition
exitShortCondition = enterLongCondition

// ————— Filter
filterLong = true
filterShort = true
// ————— Entries
enterLong  = doLongs and enterLongCondition
enterShort = doShorts and enterShortCondition

// ————— Stops
atr = ta.atr(14)
stopLong  = math.min(ta.lowest(5), math.min(close, open) - atr * 1.5)
stopShort = math.max(ta.highest(5), math.max(close, open) + atr * 1.5)

// ————— Exits
exitLong  = inLong  and (exitLongCondition  or close < stopLong[1])
exitShort = inShort and (exitShortCondition or close > stopShort[1])

// ————— States continuation
float i_multiplierTP1 = input.float(5, "Take Profit 1 RR", minval = 0.0, step = 0.1)
float i_multiplierTP2 = input.float(8, "Take Profit 2 RR", minval = 0.0, step = 0.1)
float i_multiplierTP3 = input.float(11, "Take Profit 3 RR", minval = 0.0, step = 0.1)
float i_atrMultiplier = input.float(4, "Trailing ATR Multiplier", minval = 0.0, step = 0.1)

var float tp1 = na
var float tp2 = na
var float tp3 = na
var float sl = na

if enterLong
    inLong := true
    entryPrice := close
    tp1 := entryPrice + atr * i_multiplierTP1
    tp2 := entryPrice + atr * i_multiplierTP2
    tp3 := entryPrice + atr * i_multiplierTP3
    sl := entryPrice - atr * i_atrMultiplier
else if enterShort
    inShort := true
    entryPrice := close
    tp1 := entryPrice - atr * i_multiplierTP1
    tp2 := entryPrice - atr * i_multiplierTP2
    tp3 := entryPrice - atr * i_multiplierTP3
    sl := entryPrice + atr * i_atrMultiplier
else if exitLong
    inLong := false
    entryPrice := na
    tp1 := na
    tp2 := na
    tp3 := na
    sl := na
else if exitShort
    inShort := false
    entryPrice := na
    tp1 := na
    tp2 := na
    tp3 := na
    sl := na

// Colors
var BARUP = color.white
var BARDOWN = color.rgb(15, 107, 255)

var BGLONG = color.rgb(13, 65, 225, 70)
var BGSHORT = color.rgb(10, 13, 51)

var ENTRY = color.rgb(0, 213, 237)
var TP = color.rgb(0, 230, 118)
var SL = color.rgb(228, 72, 25)

// Lines
plot(entryPrice, title = "Long entry", color = ENTRY, style = plot.style_linebr, linewidth = 2)
plot(entryPrice + atr * 1.5, title = "TP #1", color = TP, style = plot.style_linebr, linewidth = 2)

// Position background
filter = inLong ? BGLONG : inShort ? BGSHORT : na
bgcolor(filter, title = "Position background")

// Candle coloring
barUp = close > open
barDn = not barUp
barcolor(barUp ? BARUP : BARDOWN, title = "Candle coloring")

// Orders
plotshape(enterLong, text = "LONG", textcolor = color.white, title = "Long entry label", style = shape.labelup, location = location.belowbar, color = color.green, size = size.small)
plotshape(enterShort, text = "SHORT", textcolor = color.white, title = "Short entry label", style = shape.labeldown, location = location.abovebar, color = color.red, size = size.small)
// plotshape(exitLong, title = "Close BUY", style = shape.diamond, location = location.abovebar, color = color.green, size = size.normal)
// plotshape(exitShort, title = "Close SELL", style = shape.diamond, location = location.belowbar, color = color.red, size = size.normal)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 17:54:25