簡體   English   中英

不能使用可變變量作為安全函數的參數嗎?

[英]Can not use a mutable variable as an argument of the security function?

我將如何編碼以解決錯誤“無法使用可變變量作為安全函數的參數”。 任何幫助都會很棒。 提前致謝

res4 = input(title="time frame", type=input.resolution, defval="240")

len = input(14)
th = input(20)

TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0



SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange


SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus



SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, len)


//security function
DIPlus4H = security("USDCAD", res4, DIPlus)
DIMinus4H = security("USDCAD", res4, DIMinus)

因此,錯誤消息很清楚。 您不能將可變對象與security功能一起使用。

解決方法是,將與要在security()調用中使用的特定變量相關的所有計算移動到一個函數中,然后在security()調用該函數。

//@version=4
study("My Script")
res4 = input(title="time frame", type=input.resolution, defval="240")

len = input(14)
th = input(20)

get_DIPlus() =>
    TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
    DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
    DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0
    SmoothedTrueRange = 0.0
    SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
    SmoothedDirectionalMovementPlus = 0.0
    SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
    DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100

//security function
DIPlus4H = security("USDCAD", res4, get_DIPlus())
plot(DIPlus4H)

暫無
暫無

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

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