[pshaiCmd] Optimal & Cumulative OBV
stableDescription
This custom command version of the OBV is a lot more optimal than the built-in command, as it keeps the previous values in memory and only calculates the latest value when called.
HaasScript
DefineCommand('OBV', 'Optimal and cumulative version of OBV indicator by pshai')
local c = DefineParameter(ListNumberType, 'prices', 'Price data for OBV', true, ClosePrices(), 'ClosePrices, OpenPrices, HLPrices, HLCPrices, OHLCPrices')
local v = DefineParameter(ListNumberType, 'volumes', 'Volume data for OBV', true, GetVolume(), 'GetVolume')
local isOK = true
if #c < 2 or #v < 2 then
LogError('The [prices] or [volumes] array does not contain enough elements (less than 2).')
isOK = false
end
local obv = Load('obv', 0)
local obva = Load('obva', {})
local init = Load('init', true)
local function calcOBV(c, v, i)
if not isOK then
-- skip
return
end
if c[i] > c[i+1] then
obv = obv + ArrayGet(v, i)
elseif c[i] < c[i+1] then
obv = obv - ArrayGet(v, i)
end
obva = ArrayUnshift(obva, obv)
if #obva > 200 then
obva = Grab(obva, 0, 200)
end
end
if init then
-- warmup, calculate first values based on historical data
for i = #c-1, 1, -1 do
calcOBV(c, v, i)
end
init = false
else
-- here we only calculate current value
calcOBV(c, v, 1)
end
Save('init', init)
Save('obv', obv)
Save('obva', obva)
DefineOutput(ListNumberType, obva, 'OBV values array.', 'Plot')
0 Comments
Sign in to leave a comment.
No comments yet. Be the first!