[pshaiCmd] Easy Average Change Oscillator

0 207 Views No Comments 9 months ago
  • Easy version of the ACO indicator custom command.

    HaasScript Code
    -- ==============================================
    -- Average Change Oscillator
    -- Developed by pshai
    -- ----------------------------------------------
    -- Brief intro: ACO is a type of momentum strength indicator that
    -- measures the average price change. It can be interpreted in
    -- several ways and can be beneficial for confirming trends and reversals.
    -- ==============================================
    
    DefineCommand('EasyACO', 'Easy Average Change Oscillator')
    
    local inputs = DefineEasyIndicatorParameters()
    local chartIdx = inputs[1]
    local interval = inputs[2]
    
    local signalOptions = {
        IndicatorDirection = 'Indicator Direction',
        SignalDirection = 'Signal Direction',
        BothDirection = 'Both Direction',
        IndicatorSignalCross = 'Indicator-Signal Cross', 
        IndicatorZeroCross = 'Indicator-Zero Cross',
        SignalZeroCross = 'Signal-Zero Cross',
        BothZeroCross = 'Both-Zero Cross'
    }
    
    -- Parameters
    local period1 = Input('Period #1', 10, 'Smoothing period for the first average', 'EasyACO Settings')
    local period2 = Input('Period #2', 20, 'Smoothing period for the second average', 'EasyACO Settings')
    local signalType = InputOptions('Signal Type', 0, signalOptions, '', 'EasyACO Settings')
    
    
    -- Optimization
    DefineIntervalOptimization(interval)
    
    -- Data
    local source = ClosePrices(interval)
    local lookback = (period1 + period2) + 10 -- speed optimisation part 1
    local source1 = Grab(source, 0, lookback) -- speed optimisation part 2
    local source2 = Offset(source1, 1)
    local temp = Sub(Div(source1, source2), 1.0) -- source1 / source2 - 1.0
    local tempLen = #temp
    local result = {}
    local values
    
    -- Magic
    for i=1, tempLen-period1
    do
        values = Grab(temp, i-1, period1)
        result[i] = Average(values)
    end
    
    -- Results
    local signal = SMA(result, period2)
    local aco = Sub(result, signal)
    
    Log('ACO chartIdx: '..chartIdx)
    
    Plot(chartIdx, '', 0, DarkGray(75)) -- Zero-line
    local acoPlot = Plot(chartIdx, 'ACO', aco, {color=Green, style=Spiked}) -- The ACO line..
    PlotDoubleColor(acoPlot, 0.0, Red, Gray(15)) -- ...with some icing
    Plot(chartIdx, 'Signal', signal, {color=Gray, style=Smooth}) -- The SMA of ACO
    
    
    local result = SignalNone
    
    if signalType == signalOptions.IndicatorDirection then
        if IsRising(aco, 3) then
            result = SignalBuy
        elseif IsFalling(aco, 3) then
            result = SignalSell
        end
    
    elseif signalType == signalOptions.SignalDirection then
        if IsRising(signal, 3) then
            result = SignalBuy
        elseif IsFalling(signal, 3) then
            result = SignalSell
        end
    
    elseif signalType == signalOptions.BothDirection then
        if IsRising(aco, 3) and IsRising(signal, 3) then
            result = SignalBuy
        elseif IsFalling(aco, 3) and IsFalling(signal, 3) then
            result = SignalSell
        end
    
    elseif signalType == signalOptions.IndicatorSignalCross then
        if aco > signal then
            result = SignalBuy
        elseif aco < signal then
            result = SignalSell
        end
    
    elseif signalType == signalOptions.IndicatorZeroCross then
        if aco > 0 then
            result = SignalBuy
        elseif aco < 0 then
            result = SignalSell
        end
    
    elseif signalType == signalOptions.SignalZeroCross then
        if signal > 0 then
            result = SignalBuy
        elseif signal < 0 then
            result = SignalSell
        end
    
    elseif signalType == signalOptions.BothZeroCross then
        if aco > 0 and signal > 0 then
            result = SignalBuy
        elseif aco < 0 and signal < 0 then
            result = SignalSell
        end
    end
    
    
    -- Output
    DefineOutput(EnumType, result, 'Indicator trade signal', 'DoSignal, IndicatorContainer')
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free