[pshaiCmd] SuperTrend

stable
By pshai in Trend Published October 2019 👁 2,246 views 💬 0 comments

Description

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) 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 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()
else
    lowerId = NewGuid() -- reset lower id
    Plot(0, 'Short Stop', st, {c=Red, id=upperId})
    -- DoSell()
end

Save('uid', upperId)
Save('lid', lowerId)
HaasScript
DefineCommand(&#039;SuperTrend&#039;, &#039;SuperTrend indicator by pshai.&#039;)

-- Parameters
local atrPeriod = DefineParameter(NumberType, &#039;atrPeriod&#039;, &#039;ATR period length&#039;, true, 22)
local atrMult = DefineParameter(NumberType, &#039;atrMult&#039;, &#039;ATR multiplier&#039;, true, 3)
local wicks = DefineParameter(NumberType, &#039;wicks&#039;, &#039;Take wicks into account or not&#039;, 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(&#039;dir&#039;, 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(&#039;upper&#039;, upper) -- load previous
        local finalUpper = Min(upper, upperPrev) -- take min

        -- if close cuts through upper, we switch to other side
        if (c &gt;= finalUpper) or (wicks and h &gt;= finalUpper) then
            dir = 1
            Save(&#039;lower&#039;, ArrayGet(hl, offset) - ArrayGet(atr, offset)) -- &quot;reset&quot; lower line
        end

        Save(&#039;upper&#039;, finalUpper)
        result = finalUpper -- set result

    -- update long side
    elseif dir == 1 then
        local lower = ArrayGet(hl, offset) - ArrayGet(atr, offset)
        local lowerPrev = Load(&#039;lower&#039;, lower) -- load previous
        local finalLower = Max(lower, lowerPrev) -- take max

        -- if close cuts through lower, we switch to other side
        if c &lt;= finalLower or (wicks and l &lt;= finalLower) then
            dir = -1
            Save(&#039;upper&#039;, ArrayGet(hl, offset) + ArrayGet(atr, offset)) -- &quot;reset&quot; upper line
        end

        Save(&#039;lower&#039;, finalLower)
        result = finalLower -- set result
    end
end

-- Warmup
if Load(&#039;warmup&#039;) == nil then
    LogWarning(&#039;Warming up SuperTrend...&#039;)

    for i=100, 1, -1 do
        run(i)
    end

    LogWarning(&#039;Warmup completed.&#039;)

    Save(&#039;warmup&#039;, false)
else
    run() -- only update current step
end


Save(&#039;dir&#039;, dir)

DefineOutput(ListNumberType, result, &#039;SuperTrend output values&#039;)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!