Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

pine script - Input Variables not accessible inside function

Here are my inputs:

BandBars = input(14, "Count of Bars for Band")
Deviation = input(1.5, "Deviation")

Here is the function

Trend_Risk(close_tf, low_tf, high_tf) =>
    float SmoothPrice = na
    SmoothPrice := na(SmoothPrice[1]) ? close_tf : (SmoothPrice[1] * (BandBars - 1) + close_tf) / BandBars
    float SmoothRange = na
    SmoothRange := na(SmoothRange[1]) ? high_tf - low_tf : (SmoothRange[1] * (BandBars - 1) + high_tf - low_tf) / BandBars
    Top = SmoothPrice + SmoothRange * Deviation
    Bottom = SmoothPrice - SmoothRange * Deviation 
    [Top, Bottom]

// Indicator calculations for trade signal
Ema_5m = security(syminfo.tickerid, tf2, ema(close, len_ema))
f() => [close, low, high]
[close_5m, low_5m, high_5m] = security(syminfo.tickerid, tf2, f())
[Top_5m, Bottom_5m] = Trend_Risk(close_tf = close_5m, low_tf = low_5m, high_tf = high_5m)

//. 1 H time frame for confirmation of trend

Ema_1h = security(syminfo.tickerid, tf1, ema(close, len_ema))
[close_1h, low_1h, high_1h] = security(syminfo.tickerid, tf1, f())
[Top_1h, Bottom_1h] = Trend_Risk(close_tf = close_1h, low_tf = low_1h, high_tf = high_1h)
// below this are entry exit rules which uses top and bottom and price comparison and plots. 
plot(Top_chart, "Top", #EF5350)
plot(Bottom_chart, "Bottom", #26A69A)
plot(Ema_chart, "EMA", color.blue)

Here is the Error:

./TVScriptAnalyzer.g: node from line undefined:-1 Mismatched input 'BandBars' expecting <UP>.

Any help would be much appreciated as if you have encountered this error before, what would be the cases when this error shows up or if I am doing something that is out of the scope of pine script, please ask questions for clarification if required.

question from:https://stackoverflow.com/questions/66064241/input-variables-not-accessible-inside-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Variables BandBars and Deviation are invisible in the local scope of the function Trend_Risk. Declare the variables as global

var BandBars = input(14, "Count of Bars for Band")
var Deviation = input(1.5, "Deviation")

There is no way to check it because the incomplete script is not compiled.

line 20: Undeclared identifier 'tf2';

line 20: Undeclared identifier 'len_ema';

line 22: Undeclared identifier 'tf2';

line 22: variableType.itemType is not a function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...