【问题标题】:Pine Script not drawing boxesPine Script 不画框
【发布时间】:2021-12-15 08:00:45
【问题描述】:

我正在制作一个免费的 VPVR,据说比目前免费提供的更好。

我注意到,如果我增加直方图的数量,则图表不会显示其中一些直方图。

第一张图片有 27 个直方图。

第二张图片包含 34 个直方图。

我确定值在那里,因为我可以看到条被偏移(看第二张图片):

  1. 为什么不画那些条?
  2. 我该如何解决这个问题?

我已经尝试过使用线条,它也是如此。 好像有一个限制,如果我删除两个 box.new 调用之一,我会将这个限制达到 > 54 个直方图。

遵循我目前正在编写的脚本:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Fr3d0C0rl30n3

//@version=4
study("Fr3d0's Volume Profile Visible Range", "VPVR", overlay=true)

DEFAULT_COLOR = color.new(color.gray, 0)
BUY_COLOR = color.new(color.green, 0)
SELL_COLOR = color.new(color.red, 0)
SPECIAL_COLOR = color.new(color.yellow, 0)
TIME_UNIT = time - time[1]

numOfBars = input(100, 'Number of bars', minval=20, maxval=500)
rangeHigh = highest(high, numOfBars)
rangeLow = lowest(low, numOfBars)
rangeHeight = rangeHigh - rangeLow

numOfHistograms = input(50, 'Number of histograms', minval=20, maxval=50)
widestHistogramWidth = input(50, 'Width of the PoC', minval=20, maxval=100)
histogramHeight = rangeHeight / numOfHistograms

histogramLowList = array.new_float(numOfHistograms, na)
histogramHighList = array.new_float(numOfHistograms, na)
histogramPriceList = array.new_float(numOfHistograms, 0.0)

histogramBuyVolumeList = array.new_float(numOfHistograms, 0.0)
histogramSellVolumeList = array.new_float(numOfHistograms, 0.0)
histogramVolumePercentageList = array.new_float(numOfHistograms, 0.0)

histogramIsPOCList = array.new_bool(numOfHistograms, false)

if barstate.islast

    // Define lows and highs of the histograms
    for i = 0 to numOfHistograms - 1
        histogramLow = rangeLow + histogramHeight * i
        histogramHigh = rangeLow + histogramHeight * (i + 1)
        array.set(histogramLowList, i, histogramLow)
        array.set(histogramHighList, i, histogramHigh)
        array.set(histogramPriceList, i, (histogramLow + histogramHigh) / 2)
        
    // Assign bar's volumes to histograms
    for i = 0 to numOfBars - 1
        currentBarHeight = high[i] - low[i]
        currentBuyVolume = iff((high[i] == low[i]), 0, volume[i] * (close[i] - low[i]) / currentBarHeight)
        currentSellVolume = iff((high[i] == low[i]), 0, volume[i] * (high[i] - close[i]) / currentBarHeight)
        
        // Define the percentages of the current volume to give to histograms
        for j = 0 to numOfHistograms - 1
            histogramLow = array.get(histogramLowList, j)
            histogramHigh = array.get(histogramHighList, j)
            target = max(histogramHigh, high[i]) - min(histogramLow, low[i])
                   - (max(histogramHigh, high[i]) - min(histogramHigh, high[i]))
                   - (max(histogramLow, low[i]) - min(histogramLow, low[i]))
            histogramVolumePercentage = target / currentBarHeight
            
            histogramBuyVolume = array.get(histogramBuyVolumeList, j)
            histogramSellVolume = array.get(histogramSellVolumeList, j)
            
            // If there is at least one histogram affected
            // then divide the current volume by the number of histograms affected
            if histogramVolumePercentage > 0
                array.set(histogramBuyVolumeList, j, histogramBuyVolume + currentBuyVolume * histogramVolumePercentage)
                array.set(histogramSellVolumeList, j, histogramSellVolume + currentSellVolume * histogramVolumePercentage)
    
    // Find the histogram with the highest volume
    highestHistogramVolume = 0.0
    for i = 0 to numOfHistograms - 1
        histogramBuyVolume = array.get(histogramBuyVolumeList, i)
        histogramSellVolume = array.get(histogramSellVolumeList, i)
        histogramVolume = histogramBuyVolume + histogramSellVolume
        highestHistogramVolume := max(highestHistogramVolume, histogramVolume)
    
    // Mark the Point of Control
    for i = 0 to numOfHistograms - 1
        histogramVolume = array.get(histogramBuyVolumeList, i) + array.get(histogramSellVolumeList, i)
        if histogramVolume == highestHistogramVolume
            array.set(histogramIsPOCList, i, true)
    
    // Draw top and bottom of the range considered
    line.new(time[numOfBars], rangeHigh, time_close, rangeHigh, xloc=xloc.bar_time, color=DEFAULT_COLOR, width = 2)
    line.new(time[numOfBars], rangeLow, time_close, rangeLow, xloc=xloc.bar_time, color=DEFAULT_COLOR, width = 2)
    
    // Draw histograms and highlight the Point of Control
    for i = 0 to numOfHistograms - 1
        histogramLow = array.get(histogramLowList, i)
        histogramHigh = array.get(histogramHighList, i)
        histogramBuyVolume = array.get(histogramBuyVolumeList, i)
        histogramSellVolume = array.get(histogramSellVolumeList, i)
        histogramVolume = histogramBuyVolume + histogramSellVolume
        histogramWidth = widestHistogramWidth * histogramVolume / highestHistogramVolume
        histogramBuyWidth = floor(histogramWidth * histogramBuyVolume / histogramVolume)
        histogramSellWidth = floor(histogramWidth * histogramSellVolume / histogramVolume)
        isPOC = array.get(histogramIsPOCList, i)
        
        // Draw buy and send histograms
        box.new(left=bar_index + 1, top=histogramHigh, right=bar_index + 1 + histogramBuyWidth, bottom=histogramLow, bgcolor=BUY_COLOR, border_color=isPOC ? SPECIAL_COLOR : color.new(color.black, 80))
        box.new(left=bar_index + 1 + histogramBuyWidth, top=histogramHigh, right=bar_index + 1 + histogramBuyWidth + histogramSellWidth, bottom=histogramLow, bgcolor=SELL_COLOR, border_color=isPOC ? SPECIAL_COLOR : color.new(color.black, 80))

谢谢, 弗雷多

【问题讨论】:

    标签: algorithm pine-script algorithmic-trading trading


    【解决方案1】:

    对于线条、标签和框,如果您需要超过可用的默认限制 50,则需要为它们设置 study/strategy() 参数,例如

    study("My study", max_lines_count = 500, max_boxes_count = 500, max_labels_count = 500)
    

    最大限制为 500。

    https://www.tradingview.com/pine-script-reference/v4/#fun_study

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2019-04-16
      相关资源
      最近更新 更多