[pshaiCmd] Volume Weighted Average Price (VWAP)

stable
By pshai in Trend Published June 2020 👁 2,092 views 💬 0 comments

Description

Volume Weighted Average Price (VWAP) custom command. This command stores the cumulative information as it goes and limits the amount of data returned to 250 ticks. The first call for the command runs a warmup so you do not have to wait for a higher data count! NOTE: If you need to store more or less than 250 ticks of results, change the value assigned for [dataLimit] variable. UPDATE: Added [resetInterval] parameter. Set this to 1440 minutes and you will get daily resets. The reset is timed to exact minutes/hours/days etc.
HaasScript
DefineCommand('VWAP', 'Volume Weighted Average Price')

local interval = DefineParameter(NumberType, 'interval', 'Interval for the price data. Set this to zero to use [Main Interval] setting.', false, 0, 'InputInterval, Number')
local resetInterval = DefineParameter(NumberType, 'resetInterval', 'Interval in minutes for resetting the cumulative values. If this value is zero, reset function is ignored.', false, 0, 'InputInterval, Input, Number')

DefineIntervalOptimization(interval)

local roundTime = function(time, span)
    return time - (time % (span * 60))
end

local checkReset = function(isInit, ttime)
    local time = roundTime(not ttime and Time() or ttime, resetInterval)
    
    if isInit then
        Save('rt', time)
    elseif time > Load('rt', time) then
        Save('rt', time)
        return true
    end
    return false
end

local dataLimit = 250
local hlc = HLCPrices(interval)
local v = GetVolume(interval)

local cumTyp = Load('ct', 0)
local cumVol = Load('cv', 0)
local result = Load('r', {})

if Load('init') == nil then
    LogWarning('Running a warmup for VWAP...')
    
    local len = #hlc
    local time_step = CurrentInterval() * 60
    local time = Time() - (len * time_step)
    local res = 0

    checkReset(true) -- initialize
    
    for i=len, 1, -1 do
        local p = ArrayGet(hlc, i)
        local vol = ArrayGet(v, i)
        
        if checkReset(false, time) then
            cumTyp = 0
            cumVol = 0
        else
            cumTyp = cumTyp + p * vol
            cumVol = cumVol + vol
        end

        if cumVol > 0 then
            res = ArrayUnshift(result, cumTyp / cumVol)
        else
            res = ArrayUnshift(result, p)
        end

        result = res
        time = time + time_step
    end

    
    Save('init', false)
else
    local p = ArrayGet(hlc, 1)
    local vol = ArrayGet(v, 1)

    if checkReset() then
        cumTyp = 0
        cumVol = 0
    end
    
    cumTyp = cumTyp + p * vol
    cumVol = cumVol + vol

    if cumVol > 0 then
        result = ArrayUnshift(result, cumTyp / cumVol)
    else
        result = ArrayUnshift(result, p)
    end
end

if #result > dataLimit then
    result = Grab(result, 0, dataLimit)
end


Save('ct', cumTyp)
Save('cv', cumVol)
Save('r', result)

DefineOutput(ListNumberType, result, 'Array containing VWAP values.', 'Plot')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!