# Ultimate oscillator formula

Here is another one guys. ultimate oscillator comes as a function in amibroker, and in case you want to use an array beside close for your ultimate oscillator( i.e: O, H, L or if you have whole new and adjusted set of price candles using PlotOHLC function) this might help;

``````r1 = Param("Fast avg", 7, 2, 200, 1 );
r2 = Param("Med avg", 14, 2, 200, 1 );
r3 = Param("Slow avg", 28, 2, 200, 1 );

BP = C - IIf(L < Ref(C , -1), L, Ref(C , -1));
TR = IIf(H > Ref(C , -1), H, Ref(C , -1)) - IIf(L < Ref(C , -1), L, Ref(C , -1));

FSTAVG = Sum(BP, r1) / Sum(TR, r1);
MEDAVG = Sum(BP, r2) / Sum(TR, r2);
SLOAVG = Sum(BP, r3) / Sum(TR, r3);

Plot(100 * ((4 * FSTAVG) + (2 * MEDAVG) + SLOAVG) / (4 + 2 + 1), _DEFAULT_NAME(), ParamColor("Color", colorCycle ), ParamStyle("Style"));
``````
2 Likes

You can use built-in `Ultimate()` oscillator with any array you want, by simply assigning custom values to OHLC. This can be done with any built-in function.

``````// you can override OHLC
// this is local and operates on per-execution temporary copy of OHLC data
// does NOT affect other parts of AmiBroker

Open = ..your values
High = ..your values
Low = ..your values
Close = ..your values

// and any indicator used later will use custom OHLC
x = Ultimate( 7, 14, 28 );// this will use custom values
``````

I advise against re-inventing the wheel and using much slower code than built-in functions. Custom codes like this can be anywhere from 2-10x slower than built-in and may be less accurate. Internal functions operate in double precision and offer superior performance. While 2-10x slow down may not be apparent in indicator that runs in microsecond range, if you use such codes in optimization execution times would quickly multiply by number of steps that goes into millions and the difference in performance would be important.

3 Likes