[pshaiCmd] Volume Adjusted Moving Average (VAMA)

stable
By pshai in Trend Published December 2021 👁 1,374 views 💬 1 comments

Description

Volume Adjusted Moving Average "Time-based moving averages make the assumption that all trading days are equal. Important trading days are usually associated with heavier volume. VAMA makes price and volume equal partners when computing the average. Higher volume trading days are more heavily weighted than lower volume days." This command is also optimized, meaning, that after the very first call of this command it only calculates the latest value. NOTE: With CC_HighSpeedPrices the results might not be what was expected! Usage:

local c = ClosePrices()
local v = GetVolume()

local vama1 = CC_VAMA(c, v, 14, 0.67)
local vama2 = CC_VAMA(c, v, 55, 0.67)

Plot(0, 'vama1', vama1, Cyan)
Plot(0, 'vama2', vama2, {c = Orange, w = 3})
~pshai
HaasScript
-- [pshaiCmd] Volume Adjusted Moving Average
-- Author: pshai

DefineCommand('VAMA', 'Volume Adjusted Moving Average')

local c = DefineParameter(ListNumberType, 'price', 'Price data', true, ClosePrices(), 'ClosePrices, HLPrices, HLCPrices, OHLCPrices')
local v = DefineParameter(ListNumberType, 'volume', 'Volume data', true, GetVolume(), 'GetVolume')
local period = DefineParameter(NumberType, 'period', 'Period Length (default: 14)', true, 14, 'Number, Input')
local increment = DefineParameter(NumberType, 'increment', 'Volume Increment (default: 0.67)', false, 0.67, 'Number, Input')
local plot = DefineParameter(BooleanType, 'plot', 'If true, the command will plot the VAMA line', false, true, 'True, False, Input')

local data_cap = 200

    local function calculateVAMA(in_c, in_v, step)
        if not step then step = 0 end

        local c = Grab(in_c, step, period)
        local v = Grab(in_v, step, period)

        local avg_vol = Average(v)
        local vol_inc = avg_vol * increment
        local vol_rat = v / vol_inc
        local cum_sum = Sum(vol_rat * c)
        local cum_div = Sum(vol_rat)
        local vama = cum_sum / cum_div

        return vama
    end

    local realVama = Load('vama:vama', {})

    if #realVama == 0 then
        local len = data_cap
        for i = 1, len do
            realVama[i] = calculateVAMA(c, v, i-1)
        end
    else
        realVama = ArrayUnshift(
            realVama,
            calculateVAMA(c, v)
        )
    end


    Save('vama:vama', Grab(realVama, 0, data_cap))

    if plot then
        Plot(0, 'VAMA('..period..', '..increment..')', realVama, {c = Cyan, w = 2})
    end

DefineOutput(ListNumberType, realVama, 'VAMA values array', 'Plot, IsRising, IsFalling, CrossOver, CrossUnder, IsSmallerThan, IsBiggerThan')

1 Comment

Sign in to leave a comment.

S
Skywalker over 4 years ago

Amazing Pshai! Thanks so much for making this happen!