这是我第一次写指标,我想让它像止损一样,这样当价格上涨时,指标(下面的线)随着价格上涨,当价格下跌时,指标(下面的线)保持不变,预计价格可能会触及该线。
但问题是:我用于存储启动时价格的变量与实时价格完全一致,并且不断更新。这样的东西还能吃吗?
如果你点亮它,我会非常高兴,如何正确地做到这一点:)
currentPrice = close
var float lowerLevel = na
if na(lowerLevel)
lowerLevel := currentPrice * 0.97
if currentPrice > lowerLevel * 1.03
lowerLevel := currentPrice * 0.97
lineLength = 300
var line lowerLine = line.new(na, na, na, na, color=color.green, width=1)
line.set_xy1(lowerLine, bar_index - lineLength, lowerLevel)
line.set_xy2(lowerLine, bar_index + lineLength, lowerLevel)
label.new(bar_index, currentPrice, "Current Price: " + str.tostring(currentPrice), style=label.style_label_up, color=color.white, textcolor=color.blue)
label.new(bar_index, lowerLevel, "Lower Level: " + str.tostring(lowerLevel), style=label.style_label_up, color=color.white, textcolor=color.green)
看来我找到了问题的答案,至少它坏了,但它有效,我在 1 分钟的时间范围内检查了它。
// Current price calculation
currentPrice = close
// Variable to store levels
var float lowerLevel = na // Initially uninitialized
// Flag to control updates
var bool isUpdating = true
// Initialize lowerLevel only on the first run
if na(lowerLevel)
lowerLevel := currentPrice * 0.97 // Set the level to 3% below the current price on the first bar
// Check to update lowerLevel
if currentPrice > lowerLevel * 1.03
lowerLevel := currentPrice * 0.97 // Update lowerLevel to 3% below the current price only if price has increased more than 3%
isUpdating := true // Allow further updates
// Stop updating if the price drops
if currentPrice < lowerLevel
isUpdating := false // Block updates
// Check for price touching the lowerLevel
if low <= lowerLevel and high >= lowerLevel
label.new(bar_index, lowerLevel, "Success! Price touched the green line.", style=label.style_label_up, color=color.white, textcolor=color.green)
// Set lines
lineLength = 300 // line length
var line lowerLine = line.new(na, na, na, na, color=color.green, width=1)
// Update the line only when the price rises and updates are allowed
if isUpdating
line.set_xy1(lowerLine, bar_index - lineLength, lowerLevel)
line.set_xy2(lowerLine, bar_index + lineLength, lowerLevel)
// Debug logs
label.new(bar_index, currentPrice, "Current Price: " + str.tostring(currentPrice), style=label.style_label_up, color=color.white, textcolor=color.blue)
label.new(bar_index, lowerLevel, "Lower Level: " + str.tostring(lowerLevel), style=label.style_label_up, color=color.white, textcolor=color.green)