Swing Trading Safely with Super Trend Signal v1 [Spot Version]

stable
By deemzie in Trading Bots Published March 2021 👁 2,366 views 💬 6 comments

Description

Hello folks! Following a recent demand from a member, I have adapted my palindromic bot STSSTS v1 to Spot positions. Here is the original script: https://www.haasscripts.com/t/swing-trading-safely-with-super-trend-signal-v1/ I did not try it live yet, just did a couple of BT, so use at your own risk and feel free to report on any issue you may encounter and good settings you may find. ~deemzie
HaasScript
-- Swing Trading Safely with Super Trend Signal (STSwSTS)
-- Author: deemzie
-- Thanks to pshai for the MassEve script which inspired the present and for the SuperTrend command
-- Consider donating to support our work!
-- deemzie: ETH/BSC: 0xFcFD51f3eD7BCcb3652631E7118C2c5913A3aa14
-- phsai BTC: 3FRx1EkG4T4izrkaS34xeZHJFK4kQefHKf

HideTradeAmountSettings()

local src_types = {
    'Close',
    'Open',
    'HL',
    'HLC',
    'OHLC'
}
 
InputGroupHeader('Timeframe Settings')
local tf_update = InputInterval('1. Update Timeframe', 60, 'Update timeframe, how frequently does the bot logic update.')
local tf_price = InputInterval('2. Prices Timeframe', 240, 'Prices timeframe to use.')

InputGroupHeader('Position Settings')
local init_amount = Input('1. Initial amount', 100, 'Trade amount to use (in unit of the second coin of the pair)')
local reuse = Input('2. Reuse profits', true, 'Reuse the profits when the bot enters a new position')

InputGroupHeader('Entry and Exit Settings')
local ent_trlg = Input('1. Entry Trailing %', 1, 'Price move down before going Long after the SuperTrend Buy signal')
local sec_tp = Input('2. Safety TP price %', 5, 'Price move up after going Long to secure profit')
local sec_tp_prct = Input('3. Safety TP amount %', 50, 'Portion of the Long to close when the Safety TP is reached')
local tp_prct = Input('4. Partial TP amount %', 15, 'Portion of the Long to close after the SuperTrend TP Sell signal')
local sl = Input('5. Stop Loss %', 20, 'Maximum price movement from entry before triggering the Stop Loss')
 
InputGroupHeader('SuperTrend Settings')
local st_atr_len = Input('1. ATR Length Open', 10, 'SuperTrend ATR period lenth.')
local st_atr_mul = Input('2. ATR Mult Open', 3, 'SuperTrend ATR multiplier.')
local st_atr_len_tp = Input('1. ATR Length TP', 10, 'SuperTrend ATR period lenth.')
local st_atr_mul_tp = Input('2. ATR Mult TP', 2, 'SuperTrend ATR multiplier.')
local st_src_type = InputOptions('3. Price Source Type', src_types[4], src_types, 'Price source used for SuperTrend.')
local st_use_smooth = Input('4. Use Smoothing', false, 'Whether or not use MA smoothing on SuperTrend price source.')
local st_smooth_type = InputMaTypes('5. Smooth Type', SmaType, 'MA type for smoothing, if enabled.')
local st_smooth_len = Input('6. Smooth Length', 10, 'MA length for smoothing, if enabled.')

function init_id (name)
    local id = Load(name)
    if id == nil then
        id = NewGuid()
    end
    return id
end

local amount = Load('amount')
if amount == nil then
    amount = init_amount
    Save('amount', amount)
end
 
-- all trading logic etc happens inside this command's callback.
OptimizedForInterval(
    tf_update, -- update interval
 
    -- the callback function
    function()
        -- get candle prices
        local o = OpenPrices(tf_price)
        local h = HighPrices(tf_price)
        local l = LowPrices(tf_price)
        local c = ClosePrices(tf_price)
 
        local op = OpenPrices(tf_update)
        local hp = HighPrices(tf_update)
        local lp = LowPrices(tf_update)
        local cp = ClosePrices(tf_update)
 
        -- get source prices
        local st_src
        local src
 
        if st_src_type == src_types[1] then
            st_src = c
            src = cp
        elseif st_src_type == src_types[2] then
            st_src = o
            src = op
        elseif st_src_type == src_types[3] then
            st_src = (h+l)/2
            src = (hp+lp)/2
        elseif st_src_type == src_types[4] then
            st_src = (h+l+c)/3
            src = (hp+lp+cp)/3
        elseif st_src_type == src_types[5] then
            st_src = (o+h+l+c)/4
            src = (op+hp+lp+cp)/4
        end
 
        -- apply smoothing if used
        if st_use_smooth then
            st_src = MA(st_src, st_smooth_len, st_smooth_type)
        end
 
        -- super trend
        local st = CC_SuperTrendExt(h, l, st_src, st_atr_len, st_atr_mul)
        local st_tp = CC_SuperTrendExt(h, l, st_src, st_atr_len_tp, st_atr_mul_tp)
        Plot(0, 'st', st)
        Plot(0, 'st tp', st_tp) 
 
        -- go long orders
        local enter_id = init_id('enter_id')
        local enter = OrderContainer(enter_id)

        -- exit long orders
        local sec_id = init_id('sec_id')
        local sec = OrderContainer(sec_id)

        local tp_id = init_id('tp_id')
        local tp = OrderContainer(tp_id)

        -- position
        local pos_id = init_id('pos_id')
 
        local entry = GetPositionEnterPrice(pos_id)
        local pos_amount = GetPositionAmount(pos_id)
        local dir = GetPositionDirection(pos_id)

        -- entry logic
        if st_src > st_tp and st_src > st and dir != PositionBought and not stop then 
            if (enter.isOpen and enter.price < lp * (1 - ent_trlg / 100)) or not enter.isOpen then
                CancelAllOrders(pos_id)
                pos_id = NewGuid()
                enter_id = PlaceBuyOrder(lp * (1 - ent_trlg / 100), amount / (lp * (1 - ent_trlg / 100)), {timeout = 999999, positionId = pos_id})
                sec_id = NewGuid()
                tp_id = NewGuid()
            end
        end

        -- SL logic
        if dir == PositionBought and c < entry * (1 - sl / 100) then
            CancelAllOrders(pos_id)
            PlaceExitPositionOrder(pos_id, {type = MarketOrderType, timeout = 999999})
            if reuse then
                Save('amount', amount + GetBotProfit())
            end
            stop = true
            pos_id = NewGuid()
            dir = NoPosition
            Log('Stop!')
        end

         -- Exit logic
        if dir == PositionBought and st_src < st then
            CancelAllOrders(pos_id)
            Save('amount', amount * (1 + GetPositionROI(pos_id) / 100))
            PlaceExitPositionOrder(pos_id, {type = MarketOrderType, timeout = 999999})
            if reuse then
                Save('amount', amount + GetBotProfit())
            end
            pos_id = NewGuid()
            dir = NoPosition
            Log('Exit!')
        end

        -- Secure TP logic
        if dir == PositionBought and not sec.isOpen and not sec.isFilled and not stop then
            sec_id = PlaceSellOrder(entry * (1 + sec_tp / 100), pos_amount * sec_tp_prct / 100, {timeout = 999999})
        end

         -- TP logic
        if dir == PositionBought and st_src < st_tp and not tp.isOpen and not tp.isFilled and not stop then
            tp_id = PlaceSellOrder(hp, pos_amount * tp_prct / 100, {timeout = 999999})
        end

        -- reset TP
        if dir == PositionBought and st_src > st_tp and tp.isFilled then
            tp_id = NewGuid()
        end
 
        -- reset SL: if SL has been triggered, wait for ST to flip bullish again
        if st_src < st and stop then
            stop = false
        end

        Save('pos_id', pos_id)
        Save('enter_id', enter_id)
        Save('sec_id', sec_id)
        Save('tp_id', tp_id)
    end
)

6 Comments

Sign in to leave a comment.

I
IQGobal about 5 years ago

Thank you!

S
Spike77 about 5 years ago

This bot does work and I would like to post pairs and parameters that work, but it never fully closes its position and restarts itself. Is there something I am missing?

N
narun37 about 5 years ago

Hello, it says

4. 25 May 2021 09:40:31 ERROR: Unknown references: CC_SuperTrendExt3. 25 May 2021 09:40:31 ERROR: Unknown references: CC_SuperTrendExt

Not an issue ?

Thank you !

EDIT : I found the command thx

S
SupMap about 5 years ago

Solved.

R
RobinW almost 5 years ago

WHAT IS THE COMMANDE ?

K
kr3nk1 over 4 years ago

I have same error, whats the solution?