Исправленный код
//@version=5
indicator("Long Candle with TP", overlay=true)
// === SETTINGS ===
// Timeframe filter: skip 1-minute charts
isOneMinute = timeframe.period == "1"
showSignal = not isOneMinute
// === CANDLE METRICS ===
body = math.abs(close - open)
prevRange = high[1] - low[1]
isBull = close > open
isBear = open > close
// Upper and lower shadows
upperShadow = high - math.max(close, open)
lowerShadow = math.min(close, open) - low
// Shadow condition: no shadow or very small (≤ 2% of body)
maxShadow = body * 0.02
noOrTinyUpper = upperShadow <= maxShadow
noOrTinyLower = lowerShadow <= maxShadow
// Condition 1: body > full range of previous candle
bodyGreaterThanPrevRange = body > prevRange
// Final condition (signal)
validShadow = (isBull and noOrTinyUpper) or (isBear and noOrTinyLower)
longCandle = bodyGreaterThanPrevRange and validShadow and showSignal
// === TP STATE (последний сигнал) ===
var line tpLine = na
var float tpPrice = na
var bool lastBullSignal = false // направление последнего сигнала
var float sigHigh = na // high сигнальной свечи
var float sigLow = na // low сигнальной свечи
// TP уровни по телу свечи от цены закрытия
bullTpPrice = close + body
bearTpPrice = close - body
// === НОВЫЙ СИГНАЛ: СОЗДАЁМ ЛИНИЮ TP ===
if longCandle
// удаляем старую линию, если была
if not na(tpLine)
line.delete(tpLine)
// запоминаем направление и экстремумы сигнальной свечи
lastBullSignal := isBull
sigHigh := high
sigLow := low
// фиксируем TP-цену для этого сигнала
tpPrice := isBull ? bullTpPrice : bearTpPrice
// создаём линию от бара сигнала вправо
tpLine := line.new(bar_index, tpPrice, bar_index + 1, tpPrice, extend=extend.right)
line.set_color(tpLine, isBull ? color.new(color.green, 0) : color.new(color.red, 0))
line.set_style(tpLine, line.style_dashed)
line.set_width(tpLine, 2)
// === УДАЛЯЕМ ЛИНИЮ, КОГДА TP ДОСТИГНУТ ИЛИ СИГНАЛ ОТМЕНЁН ===
if not na(tpLine) and not na(tpPrice)
// TP-достижение
reachedBull = lastBullSignal and high >= tpPrice
reachedBear = not lastBullSignal and low <= tpPrice
// ОТМЕНА СИГНАЛА:
// для бычьего — цена ушла ниже минимума сигнальной свечи
cancelBull = lastBullSignal and low < sigLow
// для медвежьего — цена ушла выше максимума сигнальной свечи
cancelBear = not lastBullSignal and high > sigHigh
if reachedBull or reachedBear or cancelBull or cancelBear
line.delete(tpLine)
tpLine := na
tpPrice := na
sigHigh := na
sigLow := na
// === ВИЗУАЛ СИГНАЛОВ ===
plotshape(longCandle and isBull,
title = "Green Triangle",
style = shape.triangleup,
location = location.belowbar,
color = color.green,
size = size.small)
plotshape(longCandle and isBear,
title = "Red Triangle",
style = shape.triangledown,
location = location.abovebar,
color = color.red,
size = size.small)
Начать дискуссию