[pshaiCmd] EasySuperTrend

0 304 Views No Comments 11 months ago
  • Easy-version of the SuperTrend indicator.

    Command produces signals based on the SuperTrend line.

    • This topic was modified 6 months ago by pshai. Reason: [UPDATE] Added warmup system
    HaasScript Code
    DefineCommand('EasySuperTrend', 'EasySuperTrend indicator by pshai.')
    
    local chartIndex = DefineParameter(NumberType, 'chartIndex', 'The index on which to chart', true, 0, 'Number')
    local name = DefineParameter(StringType, 'name', 'Unique name of the indicator.', false, '', 'Text')
    local interval = DefineParameter(NumberType, 'interval', 'Used interval for price data. Default is 0 and the main interval will be used.', false, 0, 'Number,InputInterval')
    
    DefineIntervalOptimization(interval)
    
    -- Input fields for the indicator.
    InputGroupHeader('EasySuperTrend '..name..' Settings')
    
    -- Parameters
    local atrPeriod = Input('ATR Period Length', 22)
    local atrMult = Input('ATR Multiplier', 3)
    local wicks = Input('Use wicks', false)
    
    -- Data
    local h = HighPrices(interval)
    local l = LowPrices(interval)
    local c = ClosePrices(interval)
    local hl = HLPrices(interval)
    local atr = ATR(h, l, c, atrPeriod) * atrMult
    
    -- Stuff
    local dir = Load('dir', 1)
    local upperId = Load('uid', NewGuid())
    local lowerId = Load('lid', NewGuid())
    local result = SignalNone
    
    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 = SignalSell -- set result
    
            -- Plot
            if offset <= 1 then
                lowerId = NewGuid() -- reset lower id
                Plot(chartIndex, name..' Short Stop', finalUpper, {c=Red, id=upperId})
            end
    
        -- 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 = SignalBuy -- set result
    
            -- Plot
            if offset <= 1 then
                upperId = NewGuid() -- reset upper id
                Plot(chartIndex, name..' Long Stop', finalLower, {c=Green, id=lowerId})
            end
        end
    end
    
    -- Warmup
    if Load('warmup') == nil then
        LogWarning('Warming up SuperTrend...')
    
        for i=100, 1, -1 do
            run(i)
        end
    
        LogWarning('Result after warmup: '..result)
    
        Save('warmup', false)
    else
        run() -- only update current step
    end
    
    
    
    Save('dir', dir)
    Save('uid', upperId)
    Save('lid', lowerId)
    
    DefineOutput(EnumType, 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