[pshaiCmd] VWMA v2 (Optimized)

0 80 Views No Comments 1 month ago
  • Optimized version of CC_VWMA_SE.

    • This topic was modified 1 month ago by pshai. Reason: optimized
    HaasScript Code
    DefineCommand('VWMA_v2', 'Volume-Weighted Moving Average, Script Editor version')
    
    local source = DefineParameter(ListNumberType, 'source', 'Price source', true, ClosePrices(), 'ClosePrices, HLPrices, HLCPrices, OHLCPrices')
    local volume = DefineParameter(ListNumberType, 'volume', 'Volume source', true, GetVolume(), 'GetVolume')
    local period = DefineParameter(NumberType, 'period', 'Period length', true, 20, 'Input, Number')
    
    DefineIntervalOptimization(CurrentInterval(source))
    
    local vwma = Load('vwma', {})
    local base, tmp, value
    local limit = 250
    
    if Load('warmup', true) then
        LogWarning('Warming up VWMA...')
    
        for i=99, 0, -1 do
            tmp = Grab(volume, i, period)
            base = Sum(Grab(source, i, period) * tmp)
            value = base / Sum(tmp)
            vwma = ArrayUnshift(vwma, value)
        end
    
        LogWarning('Completed.')
        Save('warmup', false)
    else
        tmp = Grab(volume, 0, period)
        base = Sum(Grab(source, 0, period) * tmp)
        vwma = ArrayUnshift(vwma, base / Sum(tmp))
    end
    
    if #vwma > limit then
        vwma = Grab(vwma, 0, limit)
    end
    
    Save('vwma', vwma)
    
    DefineOutput(ListDynamicType, vwma, 'VWMA values', 'Plot')
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free