[pshaiCmd] SuperInput

2 51 Views 3 Comments 3 weeks ago
  • Creates an input field and monitors changes in value.

    A handy way to monitor values and/or optimize calculations.

    Requires:
    https://www.haasscripts.com/t/pshaicmd-superinputsettings/

    Example:

    EnableHighSpeedUpdates()
    --
    local settings = CC_SuperInputSettings('some default value', 'a tool-tip', 'a group!', 2)
    local someVar = CC_SuperInput('Input', 'Some Input', settings)
    --
    if someVar.FieldChanged then
        LogWarning('some setting was changed, recalculate AI...')
    else
        local value = someVar.FieldValue -- get value from input field
    end
    • This topic was modified 2 weeks, 5 days ago by pshai.
    HaasScript Code
    DefineCommand('SuperInput', 'Creates an input field and monitors changes in value')
    
    local supportedInputs = {}
    supportedInputs['Input'] = Input
    supportedInputs['InputAccount'] = InputAccount
    supportedInputs['InputAccountMarket'] = InputAccountMarket
    supportedInputs['InputCdlTypes'] = InputCdlTypes
    supportedInputs['InputConstant'] = InputConstant
    supportedInputs['InputInterval'] = InputInterval
    supportedInputs['InputLrTypes'] = InputLrTypes
    supportedInputs['InputMaTypes'] = InputMaTypes
    supportedInputs['InputMarket'] = InputMarket
    supportedInputs['InputOptions'] = InputOptions
    supportedInputs['InputOrderType'] = InputOrderType
    supportedInputs['InputPriceSource'] = InputPriceSource
    supportedInputs['InputPriceSourceMarket'] = InputPriceSourceMarket
    supportedInputs['InputSignalManagement'] = InputSignalManagement
    supportedInputs['InputSignalTypes'] = InputSignalTypes
    supportedInputs['InputTable'] = InputTable
    
    local print = function(prefix, type, msg)
        msg = '[' .. prefix .. '] ' .. msg
        
        if type == 0 then
            Log(msg)
        elseif type == 1 then
            LogWarning(msg)
        elseif type == 2 then
            LogError(msg)
        end
    end
    
    local stringFormat = function(msg, ...)
        local args = {...}
        local count = #args
    
        for i = 1, count do
            local arg = args[i]
            local id = '{'..i..'}'
    
            if arg != nil and arg != '' and StringContains(msg, id) then
                local pos = StringIndexOf(msg, id)
                local temp1 = SubString(msg, 0, pos)
                local temp2 = SubString(msg, pos+4, #msg)
                
                if temp1 != nil then
                    msg = temp1 .. arg
                end
                
                    if temp2 != nil and #temp2 > 0 and temp2 != '' then
                    msg = temp1 .. arg .. temp2
                end
            end
        end
    
        return msg
    end
    
    local cmd_name = DefineParameter(StringType, 'cmd_name', 'Input command name', true, 'InputLrTypes')
    local label = DefineParameter(StringType, 'label', 'Input label', true, 'Input label text', 'Text, StringJoin, SessionGet')
    local settings = DefineParameter(ListDynamicType, 'settings', 'Settings for input. For example, {defaultValue = "default"}', true, {defaultValue = LR_Intercept, tooltip = 'a tool tip', group = 'a group!', logType = '-v-m'}, 'NewList, NewArray, SessionGet')
    
    local input_value = supportedInputs[cmd_name] (label, {defaultValue = settings.defaultValue, tooltip = settings.tooltip, group = settings.group})
    local prev_value = Load('prev_value', input_value)
    local input_changed = input_value != prev_value
    
    -- save new value
    Save('prev_value', input_value)
    
    if settings.logType != nil and settings.logType != '' then
        local verbose = false
        local log = false
    
        if StringContains(settings.logType, '-v') then
            verbose = true 
        end
    
        if StringContains(settings.logType, '-m') then
            log = true
        end
    
        local prefix = cmd_name .. ' - ' .. label
    
        if log then
            print(prefix, 0, stringFormat('input_value: {1} (prev: {2})', input_value, prev_value))
        end
        
        if verbose and input_changed then
            print(prefix, 1, stringFormat('input value "' .. label .. '" changed to "' .. input_value .. '"'))
        end
    end
    
    local obj = {
            FieldValue = input_value,
            FieldChanged = input_changed
        }
    
    DefineOutput(ListDynamicType, obj)
    
    DefineOutputIndex(1, DynamicType, 'FieldValue', 'Value from the input field', '')
    DefineOutputIndex(2, BooleanType, 'FieldChanged', 'True if input value has been changed on-the-fly, false if not', '')
    • #1667
      Firetron
      Basic
      Up
      0
      Down

      Can you provide some more details or use cases of how to benefit?

    • #1698
      pshai
      Admin
      Up
      0
      Down

      The idea was to be able to make scripts more optimized. There could be something you need to calculate based on some inputs you get, but is slowing down your script execution. But if those inputs stay the same (like they probably will for quite some time), it could be better to re-calculate those heavy-values _only_ when you change the values.

      Hope this helps.

      • #1704
        Kobalt
        Basic
        Up
        0
        Down

        Is there a way to check if a CC or calculation in some part of your script is slowing down execution or resources used per command dependency.
        To find things SuperInputs would improve, or to avoid in general or any other way avoid sluggish execution?

        Maybe there are common bad practices an optimisation check script could detect.
        [These are more inspirational than straight questions. I assume something like this doesn’t yet exist. Unless I’m over complicating this and theres a check list or simple way to do this..:)]

        Your example makes me curious what might benefit here, can you give an obvious/notorious example? (Interval, VWAP reset loopback those kind of things?)
        And SuperInput works in running HaasBots so can maybe help people running 50+ scripts?

        • This reply was modified 1 day, 9 hours ago by Kobalt.
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free