如何消除此错误消息? 10:16 警告“operator ?:”函数的“expr0”参数接受“bool”参数

问题描述 投票:0回答:1

如何重写此脚本,使得以“barsInDelay :=”开头的行不会触发以下错误消息:“11:38:51 AM warning at 10:16

expr0
函数的
operator ?:
参数接受'bool' 参数。为了避免潜在的意外结果,请将 'bool' 值或表达式传递给此参数。”

//@version=5

contractsPerOrder = 5
strategy("Delayed Alerts", overlay=true, initial_capital = 20000, use_bar_magnifier = true, default_qty_value = contractsPerOrder)

i_delayBars = input(1, "Delay in Bars")  // How many lower timeframe bars must pass before sending alert

var int barsInDelay = 0  // Initialize the barsInDelay variable

barsInDelay := request.security(syminfo.tickerid, "1", close[1]) ? barsInDelay + 1 : 0  // Count the number of bars in the delay timeframe

delayElapsed = barsInDelay >= i_delayBars // Check if the i_delayBars have elapsed since the last alert

buyCondition = close > ta.sma(close, 20) and close[1] > close[2] and close[2] > close[3]
if buyCondition and delayElapsed
    buyAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "buy", "price": ' + str.tostring(close) + ', "quantity": ' + str.tostring(contractsPerOrder) + '}'
    strategy.entry("Buy", strategy.long, alert_message = buyAlertMessage)

if delayElapsed
    sellAlertMessage = '{"ticker": "' + syminfo.ticker + '", "action": "sell", "price": "{{close}}", "quantity": ' + str.tostring(contractsPerOrder) + '}'
    strategy.exit("Sell", profit = 5*5, loss = 10*4)

我不知道如何消除警告消息。我已经尝试过!= na,这会引发新的警告; != na() 不正确。我被困住了。

pine-script pine-script-v5 tradingview-api algorithmic-trading
1个回答
0
投票

首先,这是一个警告,而不是错误。

您在

三元运算符
中使用 request.security(syminfo.tickerid, "1", close[1]),就好像它返回
bool

condition ? valueWhenConditionIsTrue : valueWhenConditionIsFalse

这没有任何意义,因为你从中获得了价格价值。

如果您确实想消除错误,则应该将该表达式转换为

bool

barsInDelay := bool(request.security(syminfo.tickerid, "1", close[1])) ? barsInDelay + 1 : 0  // Count the number of bars in the delay timeframe
© www.soinside.com 2019 - 2024. All rights reserved.