// Licensed under the Apache License, Version 2.0 // © mihakralj //@version=6 indicator("Accelerator Oscillator (AC)", "AC", overlay=false) //@function Calculates Bill Williams' Accelerator Oscillator //@param fastLength Period for fast MA calculation //@param slowLength Period for slow MA calculation //@returns AC value measuring acceleration/deceleration of market momentum ac(simple int fastLength, simple int slowLength) => if fastLength <= 0 or slowLength <= 0 runtime.error("Lengths must be greater than 0") if fastLength >= slowLength runtime.error("Fast length must be less than slow length") float mp = (high + low) / 2.0 var array fastBuf = array.new_float(fastLength, na) var array slowBuf = array.new_float(slowLength, na) var int fastHead = 0, var int slowHead = 0 var float fastSum = 0.0, var float slowSum = 0.0 var int fastCount = 0, var int slowCount = 0 float fastOldest = array.get(fastBuf, fastHead) if not na(fastOldest) fastSum -= fastOldest fastCount -= 1 if not na(mp) fastSum += mp fastCount += 1 array.set(fastBuf, fastHead, mp) fastHead := (fastHead + 1) % fastLength float slowOldest = array.get(slowBuf, slowHead) if not na(slowOldest) slowSum -= slowOldest slowCount -= 1 if not na(mp) slowSum += mp slowCount += 1 array.set(slowBuf, slowHead, mp) slowHead := (slowHead + 1) % slowLength float fastMA = fastCount > 0 ? fastSum / fastCount : na float slowMA = slowCount > 0 ? slowSum / slowCount : na float ao = fastMA - slowMA var array acBuf = array.new_float(5, na) var int acHead = 0, var float acSum = 0.0, var int acCount = 0 float acOldest = array.get(acBuf, acHead) if not na(acOldest) acSum -= acOldest acCount -= 1 if not na(ao) acSum += ao acCount += 1 array.set(acBuf, acHead, ao) acHead := (acHead + 1) % 5 float aoSMA = acCount > 0 ? acSum / acCount : na ao - aoSMA // ---------- Main loop ---------- // Inputs i_fastLength = input.int(5, "Fast Length", minval=1) i_slowLength = input.int(34, "Slow Length", minval=1) // Calculation ac_value = ac(i_fastLength, i_slowLength) // Plot plot(ac_value, "AC", ac_value >= 0 ? color.green : color.red, linewidth=2)