为什么我的追踪止损在 Pine Script v5 中绘制在当前/入场价格上方而不是下方?

问题描述 投票:0回答:1

我在指标中实现追踪止损功能时,在 Pine Script v5 中遇到了问题。追踪止损绘制在当前价格和入场价格上方,而不是下方,它应该落后于当前价格和入场价格。止损最初应设置在入场价格下方,然后始终按照用户在追踪止损美元输入中指定的距离跟随当前价格。随着价格上涨,止损和预计追踪盈亏应相应向上调整,但如果价格下跌,追踪止损和预计追踪盈亏应保持固定在之前的高点。

我不确定问题是出在我的计算逻辑上还是图表上的绘制方式上。对此问题的任何指导或解决方案将不胜感激。谢谢!

截图: (带有粗红线的“TS”出现在当前价格上方而不是下方)

Pinescript 代码:

//@version=5
indicator("P/L Tracker", overlay=true)

// User Inputs
var float entry_price = input.float(0.0, title="Entry Price", step=0.01)   // Option fill price input
contracts = input.int(1, title="Number of Contracts", minval=1)  // Number of contracts input
use_trailing_stop = input.bool(true, title="Use Trailing Stop?") 
trailing_stop_dollars = input.float(0.75, title="🔴 Trailing Stop ($)", step=0.01)

// Get the current price within TradingView chart
current_price = close

// Trailing stop logic
var float trailing_stop = na
if use_trailing_stop
    if na(trailing_stop)
        // Initialize the trailing stop based on entry price
        trailing_stop := entry_price - trailing_stop_dollars // Set initial value below entry price
    else
        // Update the trailing stop only if the current price minus dollars is higher than the previous stop
        new_trailing_stop = current_price - trailing_stop_dollars
        if new_trailing_stop > trailing_stop  // Only move up
            trailing_stop := new_trailing_stop

// Function to format number with commas
formatNumber(number) =>
    str.format("{0,number,#,###.##}", number)

// Label for current trailing stop value
if barstate.islast and use_trailing_stop and not na(trailing_stop)
    projected_trailing_pl = (current_price - trailing_stop) * 100 * contracts  // Calculate projected trailing stop P/L
    ts_label := label.new(bar_index + 15, trailing_stop, "TS: $" + str.tostring(trailing_stop, "#.##") + " | $" + formatNumber(projected_trailing_pl) + " USD", style=label.style_label_left, color=#000000, textcolor=color.red)

if use_trailing_stop and current_price <= trailing_stop
    alert("TRAILING STOP HIT!", alert.freq_once_per_bar)

// Create a entry price label on the right side of the line chart
price_label := label.new(bar_index + 15, entry_price, "Entry Price: $" + formatNumber(entry_price), style=label.style_label_left, color=color.gray, textcolor=color.white)

// Draw Trailing Stop line if enabled
var line trailing_stop_line = na
if use_trailing_stop and not na(trailing_stop)
    if na(trailing_stop_line)
        trailing_stop_line := line.new(bar_index[1], trailing_stop, bar_index, trailing_stop, width=2, color=color.red, extend=extend.both)
    else
        line.set_y1(trailing_stop_line, trailing_stop)
        line.set_y2(trailing_stop_line, trailing_stop)
        line.set_x2(trailing_stop_line, bar_index)
pine-script pine-script-v5 tradingview-api trading technical-indicator
1个回答
0
投票

您的脚本记录了前一个柱的收盘价,该收盘价高于当前开盘/收盘价,然后将该值用于后续柱。

对 Trailing_stop 变量使用 var 使其在整个时间轴上保持不变,这样,如果价格大幅下跌,您的尾随线将在当前柱上方停留一段时间,这似乎是合乎逻辑的。

© www.soinside.com 2019 - 2024. All rights reserved.