簡體   English   中英

PineScript:將“for loop”索引傳遞給 function 不起作用

[英]PineScript: passing 'for loop" index to a function does not work

為了檢查 security() 功能,我編寫了非常簡單的代碼:

get_val( _idx ) => 
    security( syminfo.tickerid, "30", low[ _idx ] ) 

if barstate.islast
    for i = 1 to 10 
        curr_val = get_val( i ) 
        // curr_val = low[i] 
        label2 = label.new( bar_index-i, low[i]-300, text = tostring( curr_val, "0" ), 
             style = label.style_label_center, color = color.green, size = size.small)

我希望它會在每根蠟燭上正確打印“低”值,但由於未知原因,它沒有正確地將“i”的值傳遞給 get_val() function。

Q1。 當它是'for循環'索引時,'i'有什么問題

Q2。 我的目標是當我到達圖表中的最后一根蠟燭時,在最后 9 個 30 分鍾期間獲得特定股票代碼的高/低/ohlc4 值,並基於此計算這 9 個期間的平均價格差異——通過 security() 執行此操作的正確/有效/有效方法是什么,因為我希望在一個圖表中為幾個不同的代碼計算它。

我閱讀了https://www.tradingview.com/script/90mqACUV-MTF-Selection-Framework-PineCoders-FAQ/https://www.tradingview.com/pine-script-docs/en/v4/中的信息essential/Context_switching_the_security_function.htmlhttps://www.tradingview.com/script/00jFIl5w-security-revisited-PineCoders/ ,但我仍然找不到有效的解決方案

先感謝您!

您不能在循環內部使用這樣的security() 無論您需要在security()的 HTF 上下文中完成什么,都必須在發送給它的 function 中完成。 這顯示了幾個例子。

不確定f_htf5()是否符合您的想法,但它會給您一個想法。 當發送到security()的 function 返回一個元組時,處理重繪/不重繪會更加棘手:

//@version=4
study("", "", true)
var string ON  = "On"
var string OFF = "Off"
bool i_repaint = input(OFF, "Repaint", options = [ON, OFF]) == ON

f_htf1(_src, _p) =>
    // #1 Avg of last `_p` values of `_src`
    float _return = sma(_src, _p)
    
f_htf2(_src, _p) =>
    // #2 Sum of last `_p` values of `_src`
    float _return = sum(_src, _p)
    
f_htf3(_src, _p) =>
    // #3 Last value is lower than all `_p` past values
    int _return = _src < lowest(_src, _p)[1] ? 1 : 0
    
f_htf4(_src, _p) =>
    // #4 Find the closest value to `_src` in `_p` past values
    float _delta = 10e15
    float _closestVal = na
    for _i = 1 to _p
        float _diff = abs(_src - _src[_i])
        if _diff < _delta
            _delta := _diff
            _closestVal := _src[_i]
    float _return = _closestVal

f_htf5(_p, _offset) =>
    // #4 Return the avg of past `_p` `high`, `low`, `ohlc4` values
    float _hi = sma(high, _p)
    float _lo = sma(low, _p)
    float _ohlc4 = sma(ohlc4, _p)
    [_hi[_offset], _lo[_offset], _ohlc4[_offset]]

// Function from: https://www.tradingview.com/script/00jFIl5w-security-revisited-PineCoders/
f_security(_sym, _res, _src, _rep) => security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]

// For functions returning one value (f_htf1 - f_htf4), execute the function in `security()`'s HTF context.
htfValue = f_security(syminfo.tickerid, "30", f_htf3(low, 10), i_repaint)

// For functions returning a tuple.
int ofst1 = not i_repaint and barstate.isrealtime ? 1 : 0
int ofst2 = i_repaint or barstate.isrealtime ? 0 : 1
[hi01_, lo01_, ohlc01_] = security(syminfo.tickerid, "30", f_htf5(9, ofst1))
float hi01   = hi01_[ofst2]
float lo01   = lo01_[ofst2]
float ohlc01 = ohlc01_[ofst2]

plot(hi01, "hi01", color.fuchsia)
plot(lo01, "lo01", color.aqua)
plot(ohlc01, "ohlc01", color.green)

在此處輸入圖像描述

暫無
暫無

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

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