如何重写此脚本,使得以“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() 不正确。我被困住了。
首先,这是一个警告,而不是错误。
您在
三元运算符中使用
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