[Giankam] Trail Blaze

stable
By StacksMooningWhale in Other Published March 2023 👁 1,503 views 💬 0 comments

Description

The main set of features includes: Three independent trailing types each with their own +/- multipliers: - Standard % change - ATR (aka Supertrend) - IQR (inter-quartile range) These can be used in isolation or summed together. A subsequent pair of direction specific multipliers are also included. Example: local atrLength = Input('atrLength', 8) local atrMult = Input('atrMult', 1) local tslPercent = Input('Trailing %', 0) local triggerLength = Input('triggerLength', 2) local centreLength = Input('centreLength', 8) local iqrLength = Input('iqrLength', 12) local iqrMult = Input('iqrMult', 0) local loMult = Input('loMult', 1) local hiMult = Input('loMult', 1) local filterSum = Input('filterSum', false) local trlBlz = CC_Trail_Blaze(atrLength, atrMult, tslPercent, triggerLength, centreLength, iqrLength,iqrMult, loMult,hiMult, filterSum) if trlBlz.trnd == 1 then PlotShapes( Plot(0, 'up', trlBlz.lostp, {w = 1, c=Aqua(100), s=Smooth, id = NewGuid()}), ShapeCircle,Aqua ) elseif trlBlz.trnd == -1 then PlotShapes( Plot(0, 'down', trlBlz.histp, {w = 1, c=Purple(100), s=Smooth, id = NewGuid()}), ShapeCircle, Purple ) end
HaasScript
-- Author: Giankam


DefineCommand('Trail_Blaze', 'Trail_Blaze')


local atrLength = DefineParameter(NumberType, 'atrLength', 'atrLength', true, 8)
local atrMult = DefineParameter(NumberType, 'atrMult', 'atrMult', true, 1)

local tslPercent = DefineParameter(NumberType, 'Trailing %', 'Trailing %', true, 0)

local triggerLength = DefineParameter(NumberType, 'triggerLength', 'triggerLength', true, 2)

local centreLength = DefineParameter(NumberType, 'centreLength', 'centreLength', true, 8)

local iqrLength = DefineParameter(NumberType, 'iqrLength', 'iqrLength', true, 12)
local iqrMult = DefineParameter(NumberType, 'iqrMult', 'iqrMult', true, 0)

local loMult = DefineParameter(NumberType, 'loMult', 'loMult', true, 0)
local hiMult = DefineParameter(NumberType, 'hiMult', 'hiMult', true, 0)
local filterSum = DefineParameter(BooleanType, 'filterSum', 'filterSum', false, false)
 

local function iqr_array(len, _h, _l , _hlc)

    local function percentile_linear_interpolation(arr, percentile)
    --    # sort the array in ascending order
        local arr_sorted = ArraySort(arr)
        
        --# determine the rank of the percentile value
        local rank = (percentile / 100) * (#arr_sorted - 1)
        
        --# determine the integer and fractional parts of the rank
        local int_rank = Truncate(rank,0)
        local frac_rank = rank - int_rank
        
        --# interpolate between the values at int_rank and int_rank + 1
        local lower_val = ArrayGet(arr_sorted,int_rank)
        local upper_val = ArrayGet(arr_sorted,int_rank + 1)
        local interpolated_val = lower_val + frac_rank * (upper_val - lower_val)
        
        return interpolated_val
    end
    
    local hlArray     =   ArrayConcat(Grab(_h,0,len), Grab(_l,0,len))
    local hlcmArray   =   ArrayConcat(hlArray, Grab(_hlc,0,len))
    local q1          =   percentile_linear_interpolation (hlcmArray, 25)
    local q3          =   percentile_linear_interpolation (hlcmArray, 75)

    return (q3 - q1) / 2
end





local function trail_blaze (srct, srcc, lofac, hifac, _trendPrev, _histpPrev, _lostpPrev)                                      
    local lostop      =   0
    local histop      =   0
    local trend       =   0
    trend  =  srct >_histpPrev  and  1 or (srct < _lostpPrev and  -1  or _trendPrev)
    local lostop00    =   trend ==  1 and _trendPrev == -1
    local histop00    =   trend == -1 and _trendPrev ==  1

    lostop      =  lostop00    and  srcc - lofac  or (srct[1] > _lostpPrev  and Max(srcc - lofac, _lostpPrev)  or srcc - lofac)
    histop      =  histop00    and srcc + hifac   or (srct[1] < _histpPrev   and Min(srcc + hifac, _histpPrev)  or srcc + hifac)

    return {lostp = lostop, histp= histop, trnd = trend}
end






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



local triggerSource = HLPrices()


local centreSource = ClosePrices()


--// PROCESS

local trigger                 =   EMA(c,4)

local centre                  =   EMA(HLPrices(),atrLength)

local tslFactor               =    centre    * tslPercent / 100          
local atrFactor               =   atrMult   * ATR (h, l, c, atrLength)  
local iqrFactor               =   iqrMult   * iqr_array (iqrLength, h, l, hlc)       
 

local sumFactor               =   filterSum       and  WMA(WMA((tslFactor + atrFactor + iqrFactor),2),2)   or atrFactor + iqrFactor + tslFactor  

local loFactor                =   Max (sumFactor, 0)     * loMult
local hiFactor                =   Max (sumFactor, 0)     * hiMult

local trendPrev = Load('trendPrev',0)
local histpPrev = Load('histpPrev',0)
local lostpPrev = Load('lostpPrev',0)


local trend =   trail_blaze (trigger, centre, loFactor, hiFactor, trendPrev, histpPrev, lostpPrev)



Save('trendPrev',trend.trnd)
Save('histpPrev',trend.histp)
Save('lostpPrev',trend.lostp)



DefineOutput(ListDynamicType, trend, 'trend.trnd - trend.histp - trend.lostp', 'Plot')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!