0

您好,我是 pine 新手,我有一个问题。我想编写一个脚本,帮助我绘制连接选定范围内第一根蜡烛的收盘价和该范围内所有其他蜡烛的收盘价的线。

我认为我在理解 pine 运行时遇到了一些问题,因为使用 for 循环或条件结构似乎很糟糕,但我找不到解决方案。我试过if但没有成功,想法是在我选择开始/结束点后,代码应该是这样的:

if bar_index > bar_index[barStart] and bar_index < bar_index[barEnd]
     line.new(bar_index[barStart], close[barStart], bar_index, close)
   else na

在此之后我尝试了一个for循环,再次没有成功:

for i = bar_index[barStart]+1 to bar_index[barEnd]
line.new(bar_index[barStart], close[barStart], bar_index[i], close[i])

我用来选择范围并计算其中的蜡烛的代码是这样的:

//@version=5
indicator("Close", overlay=true)
//      Range Start
t0          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
p0          = input.price(defval = 0,                               confirm = true)
//      Range End
t1          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
p1          = input.price(defval = 0,                               confirm = true)
///////////////////////////////////////////////////////////////////////////////////

// Bar counting 
t_bar(_t) =>
    var int _bar = na
    if time_close[1] <= _t and time >= _t
        _bar := bar_index
    _bar
    
start       =   int(t_bar(t0))
end         =   int(t_bar(t1))
//Counting bars in the selected range
barStart    =   bar_index - start
barEnd      =   bar_index - end
barDelta    =   end - start
//Print results
plot(barStart,  "Range start")
plot(barEnd,    "Range end")
plot(barDelta,  "Candles in range")

但从这里开始,我不知道如何进行。这应该很容易,但我被卡住了。

我想画什么 我想画什么

感谢任何愿意提供帮助的人!!

4

1 回答 1

0

您不需要循环或输入价格变量。当脚本执行进入您的时间范围时,可以逐条绘制线条,同时也可以获得价格变量。

//@version=5
indicator("Close", overlay=true)
//      Range Start
t0          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
//      Range End
t1          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
///////////////////////////////////////////////////////////////////////////////////

first_bar = time >= t0 and time[1] < t0
in_range = time > t0 and time <= t1
post_bar = time > t1 and time[1] <= t1

var float start_close = na
var int start_index = na

if first_bar
    start_close := close
    start_index := bar_index

if in_range and not first_bar
    line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)
    
if post_bar
    num_bars = bar_index[1] - start_index
    delta = close[1] - start_close
    info_text = "Start Bar : " + str.tostring(start_index) + "\nEnd Bar : " + str.tostring(bar_index[1]) + "\nNumber of bars : " + str.tostring(num_bars) + "\nPrice delta : " + str.tostring(delta)
    label.new(x = bar_index[1], y = high[1], style = label.style_label_lower_left, size = size.small, text = info_text)

跟进问题:

在较高的时间范围内绘制线条并在您移至较低的时间范围后让它们“持续存在”有点棘手。您将不得不使用输入手动设置更高的时间范围,因为脚本无法确定它应用到的上一个时间范围。

当您在较高的时间范围内设置时t0t1时间戳值将对应于那些较高时间范围柱的开盘时间。这并不理想,因为同时开始的较低时间框架蜡烛不是我们所追求的收盘价。

通过使用request.security(),我们可以获得具有我们想要的收盘值的较高时间框架柱的实际收盘时间。

因此,我们可以使用time来确定我们何时开始正确的较高时间框架柱,然后用于time_close确定我们何时处于与较高时间框架收盘重合的较低时间框架柱上。

//@version=5
indicator("MTF Close", overlay=true)
//      Range Start
t0          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
//      Range End
t1          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
///////////////////////////////////////////////////////////////////////////////////

tf = input.timeframe("240", title = "higher timeframe")

htf_close = request.security(syminfo.tickerid, tf, time_close)

is_htf_closing_bar = time_close == htf_close

new_htf = ta.change(time(tf)) != 0

var bool started_first_htf_bar = false
var float start_close = na
var int start_index = na

var bool started_last_htf_bar = false

if time >= t0 and time[1] < t0 and new_htf
    started_first_htf_bar := true
else if new_htf
    started_first_htf_bar := false

if started_first_htf_bar and is_htf_closing_bar and na(start_close)
    start_close := close
    start_index := bar_index
else if not started_first_htf_bar and is_htf_closing_bar and time > t0 and time < t1
    line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)

if time >= t1 and time[1] < t1 and new_htf
    started_last_htf_bar := true
else if new_htf
    started_last_htf_bar := false

if started_last_htf_bar and is_htf_closing_bar
    line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)

post_bar = new_htf and started_last_htf_bar[1]

if post_bar
    num_bars = bar_index[1] - start_index
    delta = close[1] - start_close
    info_text = "Start Bar : " + str.tostring(start_index) + "\nEnd Bar : " + str.tostring(bar_index[1]) + "\nNumber of bars : " + str.tostring(num_bars) + "\nPrice delta : " + str.tostring(delta)
    label.new(x = bar_index[1], y = high[1], style = label.style_label_lower_left, size = size.small, text = info_text)    
于 2022-01-09T23:37:54.433 回答