簡體   English   中英

Pine Script:使用可變回溯期計數(barssince)

[英]Pine Script: Counting with variable lookback period (barssince)

這應該是一件容易的事,但不知何故我無法弄清楚。 我已經嘗試了 sum-function、for 循環和我在互聯網上找到的其他提示,但沒有任何效果。

我想做以下事情:當 RSI 超買時,我想計算 50 SMA 上升了多少根柱線。 因此,回溯期是可變的,從 RSI 從下方穿過 80 時開始。

我可以用 barsince 得到這個周期,但看起來我不能將此值用作 sum 函數 math.sum(condition, barssince(...)) 中的周期或作為“for 循環”中的限制(對於 i = 1到 barsince(...))。

我還使用 int(max(1, nz(barssince(...)) + 1)) 來避免將 na 或零作為句點,並且我將 max_bars_back 定義為指標 () 中的參數。 但這沒有幫助。

可變期間不適用於 sum 函數。 例如,我可以使它與其他函數一起使用,例如最低()。 所以這是 sum 函數的問題。

我需要一個解決方法。 有任何想法嗎? 請問有人可以幫助我嗎?

//@version=5
indicator("Counter")

rsi = ta.rsi(close, 14)
sma = ta.sma(close, 50)

cross = ta.crossover(rsi, 80)

lookback = int(math.max(1, nz(ta.barssince(cross)) + 1))

// First idea with sum function

cond = rsi > 80 and sma > sma[1] ? 1 : 0

count = math.sum(cond, lookback)



// Second idea with for loop

count = 0

if barstate.islast
    for i = 1 to lookback
        if rsi > 80 and sma50 > sma50[1]
            count += 1       

你在某種程度上很接近。 您可以組合多個條件並同時計數。

當 RSI 高於 50 且 SMA 上升時,下面的代碼將開始計數。 如果 RSI 低於 50 或 SMA 停止上升,它會重置計數器。

注意:我將 RSI 限制降低到 50,因此您可以在下面的屏幕截圖中看到更多操作。

//@version=5
indicator("Counter")

rsi = ta.rsi(close, 14)
sma = ta.sma(close, 50)

var sma_up_count = 0
sma_up_count := rsi > 50 ? sma > sma[1] ? sma_up_count + 1 : 0 : 0

plot(sma_up_count)

在此處輸入圖像描述

正如你的描述,我想你可能會遇到和我一樣的問題。

可變期間不適用於 sum 函數。

你可以試試這個:

sum = 0
for i = 0 to s
    sum := sum + k[i]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM