简体   繁体   English

Pinescript - 带有输入变量的 for 循环

[英]Pinescript - for loop with input variable

i use tradingview with pinescript 4. I want to insert information on tickers i use (time, buy, sold).我将 tradingview 与 pinescript 4 一起使用。我想插入有关我使用的代码的信息(时间、买入、卖出)。 When i select a ticker, the script must make a loop to verify if there is variable for this ticker.当我 select 一个股票代码时,脚本必须循环来验证这个股票代码是否有变量。 If the the test is ok, i want to use this variable (buy1 or buy2, sold1 or sold2) on my script by replacing "BuyAlertLevel" and "SellAlertLevel" to draw line.如果测试没问题,我想通过替换“BuyAlertLevel”和“SellAlertLevel”来在我的脚本上使用这个变量(buy1 或 buy2,sold1 或 sold2)来画线。

The problem is "line 41: Undeclared identifier 'buy'; line 42: Undeclared identifier 'sold'"问题是“第 41 行:未声明的标识符 'buy';第 42 行:未声明的标识符 'sold'”

RSIPeriod = input(41, minval=1)
BuyAlertLevel = input(5, step=1, title="Buy Alert")
SellAlertLevel = input(15, step=1, title="Sell Alert")
RSIHistoModify = input(1.5, step=0.1, title="RSI Modify")

tickers1 = input(title="S", type=input.string, defval="PUST", group = "Suivi des Valeurs", inline = "1")
time1 =input(title="T", type=input.integer, defval=120, group = "Suivi des Valeurs", inline = "1")
buy1 =input(defval=0, title="B", type=input.integer,  group = "Suivi des Valeurs", inline = "1")
sold1 =input(title="S", type=input.integer, defval=15, group = "Suivi des Valeurs", inline = "1")

tickers2 = input(title="S", type=input.string, defval="PANX", group = "Suivi des Valeurs", inline = "2")
time2 =input(title="T", type=input.integer, defval=120, group = "Suivi des Valeurs", inline = "2")
buy2 =input(title="B", type=input.integer, defval=0,  group = "Suivi des Valeurs", inline = "2")
sold2 =input(title="S", type=input.integer, defval=15, group = "Suivi des Valeurs", inline = "2")

var tickers = (syminfo.ticker)

for int i = 1 to 2
    if (tickers == tickers[i] and timeframe.multiplier == time[i])
        BuyAlertLevel := buy[i]
        SellAlertLevel := sold[i]

hline(0, "hline" ,color=color.purple, linestyle=hline.style_solid)
plot(BuyAlertLevel, color=color.green, style=plot.style_line)
plot(SellAlertLevel, color=color.red, style=plot.style_line)

thanks谢谢

This uses the levels from one of the two symbols when the chart's symbol matches it, otherwise it uses the input values for the levels:当图表的符号与其匹配时,这将使用两个符号之一的级别,否则它使用级别的输入值:

//@version=4
study("")
RSIPeriod = input(41, minval=1)
i_BuyAlertLevel = input(5, step=1, title="Buy Alert")
i_SellAlertLevel = input(15, step=1, title="Sell Alert")
RSIHistoModify = input(1.5, step=0.1, title="RSI Modify")

tickers1 = input(title="S", type=input.string, defval="PUST", group = "Suivi des Valeurs", inline = "1")
time1 =input(title="T", type=input.integer, defval=120, group = "Suivi des Valeurs", inline = "1")
buy1 =input(defval=0, title="B", type=input.integer,  group = "Suivi des Valeurs", inline = "1")
sold1 =input(title="S", type=input.integer, defval=15, group = "Suivi des Valeurs", inline = "1")

tickers2 = input(title="S", type=input.string, defval="PANX", group = "Suivi des Valeurs", inline = "2")
time2 =input(title="T", type=input.integer, defval=120, group = "Suivi des Valeurs", inline = "2")
buy2 =input(title="B", type=input.integer, defval=0,  group = "Suivi des Valeurs", inline = "2")
sold2 =input(title="S", type=input.integer, defval=15, group = "Suivi des Valeurs", inline = "2")

var float BuyAlertLevel  = syminfo.ticker == tickers1 ? buy1  : syminfo.ticker == tickers2 ? buy2  : i_BuyAlertLevel
var float SellAlertLevel = syminfo.ticker == tickers1 ? sold1 : syminfo.ticker == tickers2 ? sold2 : i_SellAlertLevel

hline(0, "hline" ,color=color.purple, linestyle=hline.style_solid)
plot(BuyAlertLevel, color=color.green, style=plot.style_line)
plot(SellAlertLevel, color=color.red, style=plot.style_line)

plot(close)

You were confusing the history-referencing operator with how arrays are often indexed in other languages.您将历史引用运算符与 arrays 通常在其他语言中的索引方式混淆了。 See the usrman here for information on arrays in Pine .有关Pine 中 arrays 的信息,请参阅此处的 usrman

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM