Pine Script Iterator returning float values

Viewed 23

I'm trying to develop and test the following script against ADAPERP one-minute timeframe.

In the updateMA() function, I have a for loop with iterator _group. I've commented out the array functions within the loop, as these through a pine internal compilation error. The problem seems to be that the _group iterator is a float value - you can see this in the label during execution. I've also tried a while loop, and if you uncomment the math.round function for this you can see that _group is converted to integer, but the resulting calcs for _from and _to variables still don't add up.

I have to be missing something here but just can't see it.

Any help would be appreciated.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © aliveZebra28942

//@version=5
strategy("MA Trend", overlay=true, margin_long=100, margin_short=100)

var MA_maxLength            =   200
var MA_maturityLength       =   MA_maxLength * 3
var MA                      =   array.new_float(7, 0.0)
var MA_order                =   array.new_float(6, 0.0)
var MA_slope                =   array.new_float(6, 0.0)
var MA_gap                  =   array.new_float(6, 0.0)
var MA_orderGroup           =   array.new_float(3, 0.0)
var MA_slopeGroup           =   array.new_float(3, 0.0)
var MA_gapGroup             =   array.new_float(3, 0.0)
MA_disp                     =   barstate.islast 

calcMAattributes(int _index) => 
    _current                =   nz(array.get(MA, _index))
    _faster                 =   nz(array.get(MA, _index-1))
    _slope                  =   (_current - _current[1]) / _current[1]
    _gap                    =   (_faster - _current) / _current
    _order                  =   _faster > _current ? 1 : _faster < _current ? -1 : 0
    array.set(MA_order, _index-1, _order)
    array.set(MA_slope, _index-1, _slope)
    array.set(MA_gap, _index-1, _gap)    

    [_order, _slope, _gap]

updateMA() =>
    var _maturity           =   0
    MA1                     =   ta.ema(close, 5)
    MA2                     =   ta.ema(close, 30)
    MA3                     =   ta.ema(close, 50)
    MA4                     =   ta.ema(close, 75)
    MA5                     =   ta.ema(close, 100)
    MA6                     =   ta.ema(close, 150)
    MA7                     =   ta.ema(close, 200)
    array.set(MA, 0, MA1)
    array.set(MA, 1, MA2)
    array.set(MA, 2, MA3)
    array.set(MA, 3, MA4)
    array.set(MA, 4, MA5)
    array.set(MA, 5, MA6)
    array.set(MA, 6, MA7)
    if MA7 > 0
        _maturity       +=  1

    if _maturity > MA_maturityLength    
        // calc order, slope, and gap for each MA and store in attribute arrays
        for i = 1 to 6 by 1
            calcMAattributes(i)
    
    // calc order, slope, and gap for each MA GROUP and store in attribute group arrays
    //_group                  =   0
    //while _group <= 2
        for _group = 0 to 2 by 1        
            // round iterator, as pine seems to have for/while bug where iterator becomes a float ?
            //_group              :=  math.round(_group)
            _inc                =   _group == 0 ? 0.001 : _group == 1 ? 0.005 : 0.01 //(_group * 0.02) + 0.02 
            _from               =   _group*2 
            _to                 =   _from + 2
            if MA_disp
                label.new(x=bar_index, y=high + 0.01 + _inc, text="group:" + str.tostring(_group) + ",inc:" + str.tostring(_inc) + ", from:" + str.tostring(_from) + ", to:" + str.tostring(_to), color=color.aqua)

            //_orderGroupTot      =   array.sum(array.slice(MA_order, _from, _to)) 
            //_slopeGroupTot      =   array.sum(array.slice(MA_slope, _from, _to)) 
            //_gapGroupTot        =   array.sum(array.slice(MA_gap, _from, _to))    
            //array.set(MA_orderGroup, _group, _orderGroupTot) 
            //array.set(MA_slopeGroup, _group, _slopeGroupTot)   
            //array.set(MA_gapGroup, _group, _gapGroupTot)     
            //_group              +=  1
            _maturity    

MA_maturity                 =   updateMA()
1 Answers

After looking at this for hours I found the problem a few minutes after posting.

function calcMAattributes(i) returns a tuple which I didn't include in the function call. No idea why this would screw up the iterator variable contents, but there you go. Iremoved the tuple from the function and the loop performs as expected now.

Related