VWAP Window

beta
By r4stl1n in Trend Published March 2022 👁 1,418 views 💬 2 comments

Description

Simple moving vwap window to use for trend validation Example Usage: https://www.haasscripts.com/t/simple-vwap-window-strategy/
HaasScript
-- [r4stl1n] VWAP Window
--
-- This returns a vwap window
--
--------------------------------
-- You are on your own with this script
-------------------------------- 
-- Note: If you are going to access the historical values 
-- Make sure to validate the historic you are looking for exist 
-- Ex. if retVal[2] != nil
-- ~Bored and programming~
 
-- Define command
DefineCommand('VWAPWindow', 'VWAP Window')
 
-- Define command parameters.
local windowSize = DefineParameter(NumberType, 'windowSize', 'Used windowSize for the vwap', false, 20, 'Number,InputwindowSize')

local sizedPrices = Load("sizedPrices",{})
local historicVwap = Load("historicVwap",{})
local totalSize = Load('totalSize', 0)
local totalVWP = Load('totalVWP',0)

if #sizedPrices == windowSize
then
    local oldestMatch = sizedPrices[windowSize]
    totalSize = totalSize-oldestMatch.volume
    totalVWP = totalVWP - (oldestMatch.volume*oldestMatch.close)
    sizedPrices = ArrayPop(sizedPrices)
end

sizedPrices = ArrayUnshift(sizedPrices,CurrentPrice())

-- Calculate the new values
totalSize = totalSize + sizedPrices[1].volume
totalVWP = totalVWP + (sizedPrices[1].volume * sizedPrices[1].close)

if #historicVwap == windowSize
then
    historicVwap = ArrayPop(historicVwap)
end

historicVwap = ArrayUnshift(historicVwap,totalVWP / totalSize)

-- Check the first element for NaN and replace with close if it exist
if tostring(historicVwap[1]) == tostring(0/0) then
    historicVwap[1] = CurrentPrice().close
end

Save("sizedPrices",sizedPrices)
Save("historicVwap",historicVwap)
Save('totalSize', totalSize)
Save('totalVWP',totalVWP)

--Return the custom signal.
DefineOutput(ListDynamicType, historicVwap, 'Historic VWAP', 'TradeBotContainer, IndicatorContainer, Signal Helpers')

2 Comments

Sign in to leave a comment.

T
Trentacles about 4 years ago

Script has error...log does not show.

T
Trentacles about 4 years ago

1. 08 Apr 2022 03:03:59 ERROR: Unknown references: tostring