pinescript - 当价格越过一条线时立即绘制一条水平线(不是在收盘时)

pinescript - draw a horizontal line immediately (not at closing) when the price cross over a line

当价格超过 10 均线时,多头开仓,当价格低于 10 时,空头。多头目标为l线,空头目标为s线。

例如,在做多时,我想在价格越过 l 线时立即(不是在收盘时)画一条水平线。同样,我想在空头价格下穿 s 线时画一条水平线。我无法画一条线,因为 l 和 s 不是常数。我想计算上下交叉的价格

Here is an example of a picture

//@version=4
study(title="ema buy sell", overlay=true)
ema1 = ema(close, 10)
l = ema1 * 1.02
s = ema1 * 0.98


plot(ema1, title="Ema 10", color=color.blue, linewidth=1, transp=0)
plot(l, title="Take Long TP", color=color.red, linewidth=2, transp=0)
plot(s, title="Take Short TP", color=color.green, linewidth=1, transp=0)


longCond = crossover(high, ema1)
shortCond = crossunder(low, ema1)

plotshape(series=longCond, title="Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="LONG", size=size.small)
plotshape(series=shortCond, title="Short", style=shape.triangledown, location=location.abovebar, color=color.red, text="SHORT", size=size.small)

此脚本将按照您的描述打印 high/upperband 交叉点上的线。

//@version=4
study(title="ema buy sell", overlay=true)
ema1 = ema(close, 10)
l = ema1 * 1.02
s = ema1 * 0.98

plot(ema1, title="Ema 10", color=color.blue, linewidth=1, transp=0)
plot(l, title="Take Long TP", color=color.red, linewidth=2, transp=0)
plot(s, title="Take Short TP", color=color.green, linewidth=1, transp=0)

longCond = crossover(high, ema1)
shortCond = crossunder(low, ema1)

plotshape(series=longCond, title="Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="LONG", size=size.small)
plotshape(series=shortCond, title="Short", style=shape.triangledown, location=location.abovebar, color=color.red, text="SHORT", size=size.small)

var float lineOnCrossOver = na
if crossover(high, l)
    lineOnCrossOver := l
plot(lineOnCrossOver, color = change(lineOnCrossOver)? na : color.green)