[pshaiCmd] SuperTrend Extended

0 411 Views No Comments 11 months ago
  • SuperTrend Extended indicator.

    This command version provides inputs for price data, which allows to use this indicator over MACD or RSI data for example.

    Input paramaters:
    * atrPeriod : ATR period length (for example 22)
    * atrMult : ATR multiplier (for example 3)
    * [wicks] : Whether to take wicks into account or not (default is false)

    Output:
    * SuperTrend line values

    **NOTE**: The command does not handle any plotting nor does it produce any signals!

    Example of usage:

    local c = ClosePrices()
    local macd = MACD(c, 12, 26, 9)
    local macdLine = macd[1]
    local st = CC_SuperTrendExt(macdLine, macdLine, macdLine, 22, 3)
    
    -- Plot macd
    Plot(1, 'MACD', macdLine)
    
    local upperId = Load('uid', NewGuid())
    local lowerId = Load('lid', NewGuid())
    
    if st < macdLine then
        upperId = NewGuid() -- reset upper id
        Plot(1, 'Long Stop', st, {c=Green, id=lowerId})
        -- DoBuy()
    else
        lowerId = NewGuid() -- reset lower id
        Plot(1, 'Short Stop', st, {c=Red, id=upperId})
        -- DoSell()
    end
    
    Save('uid', upperId)
    Save('lid', lowerId)
    • This topic was modified 6 months ago by pshai. Reason: [UPDATE] Added warmup system
    HaasScript Code
    DefineCommand('SuperTrendExt', 'SuperTrend indicator (extended) by pshai.')
    
    -- Parameters
    local h = DefineParameter(ListNumberType, 'high', 'High prices', true, HighPrices())
    local l = DefineParameter(ListNumberType, 'low', 'Low prices', true, LowPrices())
    local c = DefineParameter(ListNumberType, 'close', 'Close prices', true, ClosePrices())
    local atrPeriod = DefineParameter(NumberType, 'atrPeriod', 'ATR period length', true, 22)
    local atrMult = DefineParameter(NumberType, 'atrMult', 'ATR multiplier', true, 3)
    local wicks = DefineParameter(NumberType, 'wicks', 'Take wicks into account or not', false, false)
    
    -- Data
    local hl = HLPrices()
    local atr = ATR(h, l, c, atrPeriod) * atrMult
    
    -- Stuff
    local dir = Load('dir', 1)
    local result
    
    local run = function(offset)
        if offset == nil then
            offset = 1
        end
    
        -- Update short side
        if dir == -1 then
            local upper = ArrayGet(hl, offset) + ArrayGet(atr, offset)
            local upperPrev = Load('upper', upper) -- load previous
            local finalUpper = Min(upper, upperPrev) -- take min
    
            -- if close cuts through upper, we switch to other side
            if (c >= finalUpper) or (wicks and h >= finalUpper) then
                dir = 1
                Save('lower', ArrayGet(hl, offset) - ArrayGet(atr, offset)) -- "reset" lower line
            end
    
            Save('upper', finalUpper)
            result = finalUpper -- set result
    
        -- update long side
        elseif dir == 1 then
            local lower = ArrayGet(hl, offset) - ArrayGet(atr, offset)
            local lowerPrev = Load('lower', lower) -- load previous
            local finalLower = Max(lower, lowerPrev) -- take max
    
            -- if close cuts through lower, we switch to other side
            if c <= finalLower or (wicks and l <= finalLower) then
                dir = -1
                Save('upper', ArrayGet(hl, offset) + ArrayGet(atr, offset)) -- "reset" upper line
            end
    
            Save('lower', finalLower)
            result = finalLower -- set result
        end
    end
    
    -- Warmup
    if Load('warmup') == nil then
        LogWarning('Warming up SuperTrend...')
    
        for i=100, 1, -1 do
            run(i)
        end
    
        LogWarning('Warmup completed.')
    
        Save('warmup', false)
    else
        run() -- only update current step
    end
    
    Save('dir', dir)
    
    DefineOutput(ListNumberType, result, 'SuperTrend output values')
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free