line.new 绘制 2 条线而不是 1 条线 [英] line.new draws 2 lines instead of 1

查看:82
本文介绍了line.new 绘制 2 条线而不是 1 条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个简单的 Pine 脚本

Consider this simple Pine script

//@version=4
level1 = 3050
study("FutureLine", overlay=true)
line.new(timestamp(year,month,dayofmonth,08,30), level1, timestamp(year,month,dayofmonth,13,30), level1, xloc=xloc.bar_time)

应该在最后一根柱线当天的 08:30 到 13:30 之间画一条线.
但是,它会在最后一根柱线的当天和前一天绘制该线.
知道这是为什么吗?

It's supposed to draw a line from 08:30 to 13:30 at the day of the last bar.
However, it draws that line on the day of the last bar AND on the day before that.
Any idea why that is?

示例是在 SPX 的 15 分钟柱上

Example is on 15min bars of SPX

推荐答案

year 是运行脚本的栏的年份.year(timenow) 是当前年份.发生的事情是您使用该条的年、月和日在每个条上画一条线,而垃圾收集仅保留最后一条.只有 2 个显示,但还有更多,叠加.

year is the year of the bar the script is running on. year(timenow) is the current year. What was happening is that you were drawing a line on each bar using that bar's year, month and day, and garbage collection was only keeping the last ones. Only 2 showed but there were many more, superimposed.

此代码仅在数据集的第一个条形上创建一行,然后仅在脚本到达最后一个条形时对其进行修改,因此它使用该条形的日期:

This code only creates a line on the dataset's first bar and then only modifies it when the script reaches the last bar, so it uses that bar's date:

//@version=4
study("FutureLine", overlay=true)
level1 = 3050
start = timestamp(year,month,dayofmonth,08,30)
stop  = timestamp(year,month,dayofmonth,13,30)
var line ln = line.new(start, level1, stop, level1, xloc=xloc.bar_time)
if barstate.islast
    line.set_x1(ln, start)
    line.set_x2(ln, stop)

修改现有行比删除并创建新行更有效,后者只需要保留最后一行.

It's more efficient to modify an existing line than to delete and create a new one, which is what would be required to keep only the last one.

这篇关于line.new 绘制 2 条线而不是 1 条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆