变量没有成为一系列值。为什么?

发布于 2025-01-28 11:31:44 字数 1847 浏览 2 评论 0原文

我创建了此脚本来检测秋千高数和低点,然后确定这些摇摆的高值和低值。该代码适用于最后一个栏,但我希望这些值是我可以参考的系列,但事实并非如此。它只是保留最后值。不知道为什么。我包含了指示器的图片,其中包括最后5个摇摆高值的角落中的一个copeand桌子。...您可以看到,桌子上有不同的秋千高点,但表格中的值相同。这是代码:

//Swing Highs and Lows
swing_detection(index)=>
    swing_high = false
    swing_low = false
    start = (index*2) - 1 // -1 so we have an even number of
    swing_point_high = high[index]
    swing_point_low = low[index]
    
    //Swing Highs
    for i = 0 to start
        swing_high := true
        if i < index 
            if high[i] > swing_point_high 
                swing_high := false
                break
        // Have to do checks before pivot and after seperately because we can get
        // two highs of the same value in a row. Notice the > and >= difference
        if i > index
            if high[i] >= swing_point_high 
                swing_high := false
                break
        
    //Swing lows
    for i = 0 to start
        swing_low := true
        if i < index
            if low[i] < swing_point_low 
                swing_low := false
                break  
        // Have to do checks before pivot and after seperately because we can get
        // two lows of the same value in a row. Notice the > and >= difference
        if i > index
            if low[i] <= swing_point_low 
                swing_low := false
                break 
        
    [swing_high, swing_low]

// Check for a swing
[swing_high, swing_low] = swing_detection(barsback)

//Determine Swing High and Low Values

var float swing_high_price = na
var float swing_low_price = na
 
if swing_high
    swing_high_price := high[barsback] 
if swing_low
    swing_low_price := low[barsback] 

用此代码中的指示器的示例

I created this script to detect Swing Highs and Lows and then to determine the High and Low values of these Swings. The code works for the last bar, but I would like these values to be a series I can refer back to, but it's not. It's just retaining the last value. Not sure why. I included a picture of the indicator that includes this codeand a table in the corner with the last 5 swing high values....as you can see there are different swing highs but the same value in the table. Here is the code:

//Swing Highs and Lows
swing_detection(index)=>
    swing_high = false
    swing_low = false
    start = (index*2) - 1 // -1 so we have an even number of
    swing_point_high = high[index]
    swing_point_low = low[index]
    
    //Swing Highs
    for i = 0 to start
        swing_high := true
        if i < index 
            if high[i] > swing_point_high 
                swing_high := false
                break
        // Have to do checks before pivot and after seperately because we can get
        // two highs of the same value in a row. Notice the > and >= difference
        if i > index
            if high[i] >= swing_point_high 
                swing_high := false
                break
        
    //Swing lows
    for i = 0 to start
        swing_low := true
        if i < index
            if low[i] < swing_point_low 
                swing_low := false
                break  
        // Have to do checks before pivot and after seperately because we can get
        // two lows of the same value in a row. Notice the > and >= difference
        if i > index
            if low[i] <= swing_point_low 
                swing_low := false
                break 
        
    [swing_high, swing_low]

// Check for a swing
[swing_high, swing_low] = swing_detection(barsback)

//Determine Swing High and Low Values

var float swing_high_price = na
var float swing_low_price = na
 
if swing_high
    swing_high_price := high[barsback] 
if swing_low
    swing_low_price := low[barsback] 

Example of indicator with this code in it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮云壑 2025-02-04 11:31:44

我看不到任何明确构建系列/数组的代码,所以我想您期望swing_high_price是逐键添加值bar bar bar(由Pine Engine构建)时。

但是,使用var定义的变量需要谨慎处理,否则您将在其系列中添加空白,而Pine通常会在其中barfs。

而不是最后的IF结构,请尝试以下操作:

swing_high_price := swing_high ? high[barsback] : swing_high_price

swing_high_price将始终在每个栏上设置。这将导致连续的系列,这使Pine感到高兴。

I don't see any code that explicitly builds a series/array so I suppose you expect swing_high_price to be the series as you add value bar-by-bar (built by the Pine engine automatically).

However Variables defined with var needs to be handled with care or you will put gaps into their series, on which Pine usually barfs.

Instead of the IF structure at the end, try this:

swing_high_price := swing_high ? high[barsback] : swing_high_price

That is, swing_high_price will be always set on every bar. This will result in a continuous series, which keeps Pine happy.

傲世九天 2025-02-04 11:31:44

谢谢。我让它可以使用valuewhen()创建函数。

//Swing Highs and Lows
swing_detection(index)=>
    swing_high = false
    swing_low = false
    start = (index*2) - 1 // -1 so we have an even number of
    swing_point_high = high[index]
    swing_point_low = low[index]
    
    //Swing Highs
    for i = 0 to start
        swing_high := true
        if i < index 
            if high[i] > swing_point_high 
                swing_high := false
                break
        // Have to do checks before pivot and after seperately because we can get
        // two highs of the same value in a row. Notice the > and >= difference
        if i > index
            if high[i] >= swing_point_high 
                swing_high := false
                break
        
    //Swing lows
    for i = 0 to start
        swing_low := true
        if i < index
            if low[i] < swing_point_low 
                swing_low := false
                break  
        // Have to do checks before pivot and after seperately because we can get
        // two lows of the same value in a row. Notice the > and >= difference
        if i > index
            if low[i] <= swing_point_low 
                swing_low := false
                break 
        
    [swing_high, swing_low]

// Check for a swing
[swing_high, swing_low] = swing_detection(barsback)
////////////////////////////////////////////////////////////////////////////////

swing_high_price(_vw) =>
    _return=valuewhen(conf_swing_high, high[barsback],_vw)
swing_low_price(_vw) =>
    _return=valuewhen(conf_swing_low, low[barsback],_vw)

Thank you. I got it working creating a function using valuewhen().

//Swing Highs and Lows
swing_detection(index)=>
    swing_high = false
    swing_low = false
    start = (index*2) - 1 // -1 so we have an even number of
    swing_point_high = high[index]
    swing_point_low = low[index]
    
    //Swing Highs
    for i = 0 to start
        swing_high := true
        if i < index 
            if high[i] > swing_point_high 
                swing_high := false
                break
        // Have to do checks before pivot and after seperately because we can get
        // two highs of the same value in a row. Notice the > and >= difference
        if i > index
            if high[i] >= swing_point_high 
                swing_high := false
                break
        
    //Swing lows
    for i = 0 to start
        swing_low := true
        if i < index
            if low[i] < swing_point_low 
                swing_low := false
                break  
        // Have to do checks before pivot and after seperately because we can get
        // two lows of the same value in a row. Notice the > and >= difference
        if i > index
            if low[i] <= swing_point_low 
                swing_low := false
                break 
        
    [swing_high, swing_low]

// Check for a swing
[swing_high, swing_low] = swing_detection(barsback)
////////////////////////////////////////////////////////////////////////////////

swing_high_price(_vw) =>
    _return=valuewhen(conf_swing_high, high[barsback],_vw)
swing_low_price(_vw) =>
    _return=valuewhen(conf_swing_low, low[barsback],_vw)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文