[pshaiCmd] SuperTrend Extended (UPDATED)

stable
By pshai in Trend Published October 2019 👁 4,678 views 💬 5 comments

Description

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: * h, l, and c : Price data used for ATR. * src : [NEW] source prices for the baseline (can be a moving average for example). * atrPeriod : ATR period length (for example 22). * atrMult : ATR multiplier (for example 3). * [priceCut] : [NEW] Whether or not it is price that flips the ST. If false, src will be the flipper. * [wicks] : Whether to take wicks into account or not (default is false). * [id] : [NEW] Optional unique ID in case you run this command in a loop and need more than one line. Output: * SuperTrend line values **NOTE**: The command does not handle any plotting nor does it produce any signals! Example of usage:
local maSrc = InputSourcePrice('MA Source', HLPriceSource)
local maType = InputMaTypes('MA Type', SmaType)
local maLen = Input('MA Length', 10)
local atrLen = Input('ATR Length', 10)
local atrMul = Input('ATR Multiplier', 3)
local priceCut = Input('Price Flips ST?', false, 'If false, EMA flips the ST line.')
local wicks = Input('Use Wicks?', false, 'If true, price wicks will flip the ST line. Doesn\'t work if price cut is enabled.')

local h = HighPrices()
local l = LowPrices()
local c = ClosePrices()

local baseline = MA(SourcePrices(maSrc), maLen, maType)

local st = CC_SuperTrendExt(h, l, c, baseline, atrLen, atrMul, priceCut, wicks, 'MySuperTrend')
 
-- Plot baseline EMA
Plot(0, 'EMA', baseline)
 
local upperId = Load('uid', NewGuid())
local lowerId = Load('lid', NewGuid())

if st < baseline 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)
**UPDATED**: Added new input parameters and did some optimizations and other fixes.
HaasScript
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 src = DefineParameter(ListNumberType, 'source', 'Source prices', true, ClosePrices())
local atrPeriod = DefineParameter(NumberType, 'atrPeriod', 'ATR period length', true, 22)
local atrMult = DefineParameter(NumberType, 'atrMult', 'ATR multiplier', true, 3)
local priceCut = DefineParameter(BooleanType, 'priceCut', 'If true, price (H, L and C) will be the ones flipping ST line. If false, the source will work as the flipper.', false, false)
local wicks = DefineParameter(BooleanType, 'wicks', 'Take wicks into account or not', false, false)
local id = DefineParameter(StringType, 'id', 'Optional unique id', false, '')

-- Data
local hl = src --(h + l) / 2
local atr = ATR(h, l, c, atrPeriod) * atrMult

-- Stuff
local dir = Load('dir'..id, 0)
local result

local run = function(offset)
    if offset == nil then
        offset = 1
    end

    -- define which direction to start
    if dir == 0 then
        local start = Load('start'..id, offset)
        local upper = ArrayGet(hl, start) + ArrayGet(atr, start)
        local lower = ArrayGet(hl, start) - ArrayGet(atr, start)

        if c[offset] > upper then
            dir = 1
        elseif c[offset] &lt; lower then
            dir = -1
        end

        result = hl[start]

    -- Update short side
    elseif dir == -1 then
        local upper = ArrayGet(hl, offset) + ArrayGet(atr, offset)
        local upperPrev = Load('upper'..id, upper) -- load previous
        local finalUpper = Min(upper, upperPrev) -- take min

        -- if close/high/src cuts through upper, we switch to other side
        local flip = priceCut
                and ((c[offset] >= finalUpper) or (wicks and h[offset] >= finalUpper))
                or (hl[offset] >= finalUpper)

        if flip then
            dir = 1
            Save('lower'..id, ArrayGet(hl, offset) - ArrayGet(atr, offset)) -- "reset" lower line
        end

        Save('upper'..id, finalUpper)
        result = finalUpper -- set result

    -- update long side
    elseif dir == 1 then
        local lower = ArrayGet(hl, offset) - ArrayGet(atr, offset)
        local lowerPrev = Load('lower'..id, lower) -- load previous
        local finalLower = Max(lower, lowerPrev) -- take max

        -- if close/high/src cuts through lower, we switch to other side
        local flip = priceCut
                and (c[offset] &lt;= finalLower or (wicks and l[offset] &lt;= finalLower))
                or (hl[offset] &lt;= finalLower)

        if flip then
            dir = -1
            Save('upper'..id, ArrayGet(hl, offset) + ArrayGet(atr, offset)) -- "reset" upper line
        end

        Save('lower'..id, finalLower)
        result = finalLower -- set result
    end
end

-- Warmup
if Load('warmup'..id) == nil then
    LogWarning('Warming up SuperTrend...')

    local wm_len = ArrayGet(Min(500, #src, #c, #atr), 1)

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

    LogWarning('Warmup completed.')

    Save('warmup'..id, false)
else
    run() -- only update current step
end

Save('dir'..id, dir)

DefineOutput(ListNumberType, result, 'SuperTrend output values')

5 Comments

Sign in to leave a comment.

B
BBRNFX about 5 years ago

hi, i need the steps on How to install. Thanks a bunch

P
pshai over 4 years ago

Simply create a new script, select its type as "COMMAND", and then copy-paste the script from the lower codebox on this page. The upper codebox inside the description is just an example for how to use this command once it is properly installed.

L
Lamacorn over 4 years ago

Thank you!

V
VxLCLi over 3 years ago

KING please extend Super trend on CCI for trend detection or adaptive it base on market moving speed

D
Denu over 3 years ago

Hi, are you available for hire? I'm looking for someone who can help me with the arbitrage bot