[pshaiCmd] SuperTrend Trailing Stop-Loss

0 97 Views 1 Comment 2 weeks ago
  • SuperTrend-based trailing stop-loss safety command. What else… 😉


    if CC_STTSL(50, 1.75) then
        DoExitPosition('STTSL Triggered Exit')

    (In the screenshot’s bottom chart, you see CCI with its SuperTrend_Ext line)

    • This topic was modified 1 week, 3 days ago by pshai. Reason: Added more inputs
    HaasScript Code
    DefineCommand('STTSL', 'SuperTrend Trailing Stop-Loss')
    local atr_len = DefineParameter(NumberType, 'atr_len', 'ATR Period length', true, 20)
    local atr_mult = DefineParameter(NumberType, 'atr_mult', 'ATR Multiplier', true, 2)
    local interval = DefineParameter(NumberType, 'interval', 'Interval for data', false, 0, 'InputInterval, Number')
    local position_id = DefineParameter(StringType, 'position_id', 'Position identifier', false, '', 'PositionContainer')
    local market_id = DefineParameter(StringType, 'market_id', 'Market identifier', false, PriceMarket(), 'PriceMarket, InputAccountMarket, InputMarket, CreateMarket')
    local position = PositionContainer(position_id)
    local direction = GetPositionDirection()
    local prev_id = Load('pid', position.positionId)
    local result = false
    if position.positionId != prev_id then
        Save('upper', -1) -- "reset" upper line
        Save('lower', -1) -- "reset" lower line
    if direction != NoPosition then
        -- Data
        local h = HighPrices(interval, true, market_id)
        local l = LowPrices(interval, true, market_id)
        local c = ClosePrices(interval, true, market_id)
        local hl = (h+l) / 2
        local atr = ATR(h, l, c, atr_len) * atr_mult
        -- Update short side
        if direction == PositionShort then
            local upper = ArrayGet(hl, 1) + ArrayGet(atr, 1)
            local upperPrev = Load('upper', upper) -- load previous
            if upperPrev < 0 then upperPrev = upper end
            local finalUpper = Min(upper, upperPrev) -- take min
            Plot(0, 'Short Stop', finalUpper, {c = Green, id = position.positionId})
            -- if close cuts through upper, trigger signal
            if (c >= finalUpper) then
                result = true
            Save('upper', finalUpper)
        -- update long side
        elseif direction == PositionLong then
            local lower = ArrayGet(hl, 1) - ArrayGet(atr, 1)
            local lowerPrev = Load('lower', lower) -- load previous
            if lowerPrev < 0 then lowerPrev = lower end
            local finalLower = Max(lower, lowerPrev) -- take max
            Plot(0, 'Long Stop', finalLower, {c = Red, id = position.positionId})
            -- if close cuts through lower, trigger signal
            if (c <= finalLower) then
                result = true
            Save('lower', finalLower)
    Save('pid', position.positionId)
    DefineOutput(BooleanType, result, 'Returns true if stop was triggered')
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free