【问题标题】:How to draw a fan in Pine v5? Lines connecting open/close in a range?如何在 Pine v5 中画扇子?连接范围内打开/关闭的线?
【发布时间】:2022-01-09 16:33:48
【问题描述】:

您好,我是 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")

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

我想画什么

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

【问题讨论】:

    标签: for-loop conditional-statements pine-script trading pinescript-v5


    【解决方案1】:

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

    //@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)    
    

    【讨论】:

    • 非常感谢这篇文章让我对松树有了更多的了解,然后花了几个小时阅读手册!现在我有另一个问题:有没有办法在查看其他时间范围时保持我的线路固定?想象一下,我想放大那些烛芯,看看所有这些拒绝是如何在较短的时间范围内发生的,我想在图表上“绘制”这些线,就像我通过手动绘制它们一样。我希望我的问题足够清楚,再次感谢。
    • 我已经更新了您后续问题的答案
    • 再次感谢您提供另一个明确的答案,我很高兴我正在考虑类似的事情,但您的代码是正确的!我会尽快研究它:)
    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2014-03-29
    相关资源
    最近更新 更多