pine-scripttradingview-apialgorithmic-trading

Can't the multiple-value return of a function be attributed to multiple global variables? If not, what's the best alternative?


The following doesn't work. How to fix it or what's the best alternative?

var float avgBuyPrice = na
var float safetyTradeSize = na
var float safetyTradePrice = na

recalculate() =>
   _avgBuyPrice = strategy.position_avg_price
   _safetyTradeSize = strategy.position_size * 2
   _safetyTradePrice = _avgBuyPrice * (1 - safetyTrade)
   [_avgBuyPrice, _safetyTradeSize, _safetyTradePrice]

if (condition)
   [avgBuyPrice, safetyTradeSize, safetyTradePrice] := recalculate()

Solution

  • No, this is not yet possible.

    As a workaround, store the values in temp variables and assign them to the global variables on the next line.

    if (condition)
        [temp_avgBuyPrice, temp_safetyTradeSize, temp_safetyTradePrice] = recalculate()
        avgBuyPrice := temp_avgBuyPrice, safetyTradeSize := temp_safetyTradeSize, safetyTradePrice := temp_safetyTradePrice