Switch-case implementation for HaasScript
stableDescription
Any potential updates or changes can be found in HaasOnline Discord server.
Here's my implementation for a switch-case system.
Usage:
local c = ClosePrices()
local len = 20
local indicator = 'sma'
local values = switch({
select = indicator,
run_funcs = true,
cases = {
['sma'] = || SMA(c, len),
['ema'] = || EMA(c, len),
['dema'] = || DEMA(c, len)
}
})
Plot(0, indicator, values)
If run_funcs = false then you will get the function itself as the return value, and in the case of this example, would run it like this in the plot or wherever: values().
A better example of run_funcs = false would be this:
local c = ClosePrices()
local len = 20
local indicator = 'sma'
local cmd = switch({
select = indicator,
-- run_funcs = false, <- if not defined, defaults to false
cases = {
['sma'] = SMA,
['ema'] = EMA,
['dema'] = DEMA
}
})
local values = cmd(c, len)
Plot(1, indicator, values)
HaasScript
-- Switch-case implementation for HaasScript
-- Author: pshai
function switch(args)
local t = args.select
local rf = args.run_funcs or false
for c, r in pairs(args.cases) do
if t == c then
if rf and GetType(r) == FunctionDataType then
return r()
end
return r
end
end
end
0 Comments
Sign in to leave a comment.
No comments yet. Be the first!