AlphaTrend Indicator SMOD

stable
By smokyho in Trend Published January 2024 👁 1,125 views 💬 0 comments

Description

Modification of Bunka's port of AlphaTrend by by Kivanc Ozbilgic. https://www.haasscripts.com/t/alphatrend-indicator/ Features: 1. The output now is Signal. 2. Plotting 2 lines to generate signal on crossover event. 3. If AlphaTrend lines cross over and close > lines then SIgnalLong, marked by a green dot. Short is the other way around.
HaasScript
-- AlphaTrend indicator (by author Kivanc Ozbilgic (TV))
-- HaasScript version by Bunka
-- Mod by Smokyho
 
DefineCommand("AlphaTrendSMOD","AlphaTrend Indicator ")
local market = DefineParameter(StringType, "AlphaTrend Market", "AlphaTrendMarket", false, PriceMarket())
local interval = DefineParameter(NumberType,"AlphaTrend interval", "AlphaTrend interval", false, 0)
local source = DefineParameter(ListNumberType, 'source', 'Source data.', true, ClosePrices(interval,true,market), 'ClosePrices, HLPrices, HLCPrices, OHLCPrices')
local AP = DefineParameter(NumberType, "AlphaTrend length", "AlphaTrend length", false, 14)
local coeff = DefineParameter(NumberType, "AlphaTrend Multiplier", "AlphaTrend Multiplier", false, 1)
local novolumedata = DefineParameter(BooleanType, "Change calculation (no volume data)?", "Change calculation (no volume data)?", false, false)
local plot_or_not = DefineParameter(BooleanType, "Draw Plot", "Enable AlphaTrend plot", false, true)

OptimizedForInterval(interval, function()
    local h = HighPrices(interval,true,market)
    local l = LowPrices(interval,true,market)
    local c = ClosePrices(interval,true,market)
    local v = GetVolume(interval,true,market)

    local updateAlphaTrend = function(offset)
        if offset == nil then
            offset = 1
        end
        --Log(offset)

        local atr = SMA(TRANGE(h,l,c), AP)[offset]
        local upT = l[offset] - (atr * coeff)
        local downT = h[offset] + (atr * coeff)

        local AlphaTrend = Load("AlphaTrend",HNC(AP+1, ArrayGet(c,offset)))
        local AlphaTrend = ArrayUnshift(AlphaTrend,IfElseIf(IfElse(novolumedata, RSI(source,AP)[offset] >= 50, MFI(h,l,c,v,AP)[offset] >= 50) 
                            and upT > ArrayGet(AlphaTrend,1), downT < ArrayGet(AlphaTrend,1), upT, downT, ArrayGet(AlphaTrend,1)))
        local AlphaTrend = ArrayPop(AlphaTrend)

        if plot_or_not then 
            local a1 = Plot(0,"AlphaTrend1", AlphaTrend[1], DarkGreen)
            local a2 = Plot(0,"AlphaTrend2", AlphaTrend[2], Red)
            PlotCloud(a1, a2, 21)
        end

        local crossCheck = GetCrossOverUnderSignal(AlphaTrend[1], AlphaTrend[2])
        local alphaCheck = IfElseIf(crossCheck == SignalLong and c > AlphaTrend,
                            crossCheck == SignalShort and c < AlphaTrend,
                            SignalLong, SignalShort, SignalNone)

        local lastSignal = Load('lastSignal', alphaCheck)
        local signalAlpha = alphaCheck != lastSignal and alphaCheck
                            or SignalNone

        if alphaCheck != SignalNone and alphaCheck != lastSignal then 
            Save('lastSignal', alphaCheck) 
        end

        if signalAlpha == SignalLong then 
            PlotShape(0, ShapeCircle, DarkGreen, 3, false) 
        elseif signalAlpha == SignalShort then 
            PlotShape(0, ShapeCircle, Red, 3, true)
        end
        

        DefineOutput(EnumType, signalAlpha, 'The resulting signal from crossover ')
    end

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

        local wm_len = 200

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

        LogWarning('Warmup completed.')

        Save('warmup', false)
    else
        updateAlphaTrend() -- only update current step
    end
    -----

end)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!