[pshaiCmd] SuperTrend

0 453 Views No Comments 11 months ago
  • SuperTrend indicator.

    Command uses ClosePrices, HLPrices and High/LowPrices built-in, so no price data cannot be inputted.

    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)

    * SuperTrend line values

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

    Example of usage:

    local c = ClosePrices()
    local st = CC_SuperTrend(22, 3)
    local upperId = Load('uid', NewGuid())
    local lowerId = Load('lid', NewGuid())
    if st < c then
        upperId = NewGuid() -- reset upper id
        Plot(0, 'Long Stop', st, {c=Green, id=lowerId})
        -- DoBuy()
        lowerId = NewGuid() -- reset lower id
        Plot(0, 'Short Stop', st, {c=Red, id=upperId})
        -- DoSell()
    Save('uid', upperId)
    Save('lid', lowerId)
    • This topic was modified 11 months, 1 week ago by pshai.
    • This topic was modified 11 months, 1 week ago by pshai.
    HaasScript Code
    DefineCommand('SuperTrend', 'SuperTrend indicator by pshai.')
    -- Parameters
    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 h = HighPrices()
    local l = LowPrices()
    local c = ClosePrices()
    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
        -- 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
            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
            Save('lower', finalLower)
            result = finalLower -- set result
    -- Warmup
    if Load('warmup') == nil then
        LogWarning('Warming up SuperTrend...')
        for i=100, 1, -1 do
        LogWarning('Warmup completed.')
        Save('warmup', false)
        run() -- only update current step
    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