Switch-case implementation for HaasScript

0 169 Views No Comments 2 months ago
HTS CLOUDSTABLE
  • 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)
    
    • This topic was modified 2 months ago by pshai.
    • This topic was modified 2 months ago by pshai.
    • This topic was modified 2 months ago by pshai.
    • This topic was modified 2 months ago by pshai.
    HaasScript Code
    Sign in or Register to download for free
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free