首页 > 解决方案 > 在时间范围内绘制框/矩形

问题描述

我试图在一段时间内的价格范围内画一个框。我成功地绘制了盒子,但是很难跟踪价格。

关键事项:

这是我的代码

//@version=4
study("Box over range", overlay=true)

showRange = input(title="Show Range", type=input.bool, defval=true)
rangeTime = input(title="Session Time", type=input.session, defval="1800-0259")
extendUntil = input(title="Session Time", type=input.session, defval="0300-1700")

resolutionInMinutes() => 
    resInMinutes = timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)

f_timeFrom(_from, length, _units) =>
    int _timeFrom = na
    _unit = str.replace_all(_units, "s", "")
    _timeFrom := int(time + (resolutionInMinutes() * 60 * 1000 * length))

linelength = 9 //(temporary length)

inSession = not na(time(timeframe.period, rangeTime))

startTime = inSession and not inSession[1] ? time : na
endTime = f_timeFrom("bar", linelength, "chart")

line lowHLine = na
line topHLine = na
line leftVLine = na
line rightHLine = na

// Build the Box
if not na(startTime)
    highestPoint = high
    lowestPoint = low

    //x1, y1, x2, y2
    lowHLine = line.new(startTime, lowestPoint, endTime, lowestPoint, xloc=xloc.bar_time, color=color.blue, style=line.style_solid, width = 1)
    topHLine = line.new(startTime, highestPoint, endTime, highestPoint, xloc=xloc.bar_time, color=color.blue, style=line.style_solid, width = 1)
    leftVLine = line.new(startTime, highestPoint, startTime, lowestPoint, xloc=xloc.bar_time, color=color.blue, style=line.style_solid, width = 1)
    rightHLine = line.new(endTime, highestPoint, endTime, lowestPoint, xloc=xloc.bar_time, color=color.blue, style=line.style_solid, width = 1)

    line.delete(lowHLine[1])
    line.delete(topHLine[1])
    line.delete(leftVLine[1])
    line.delete(rightHLine[1])
    
//Track price
if inSession
    lineHigh = line.get_y2(topHLine)

    if high > lineHigh
        line.set_y1(topHLine, high)
        line.set_y2(topHLine, high)

轨道价格只是一个粗略的代码,显然我明白为什么它不起作用,但我找不到一个优雅的解决方案。谢谢

标签: pine-script

解决方案


推荐阅读