clojure-quant / quanta

quantitative technical analysis in clojure
11 stars 3 forks source link

Lowpass #4

Open awb99 opened 1 year ago

awb99 commented 1 year ago

The Market Meanness Index (MMI) evaluates the self-correlation of the data and indicates its 'trendiness'. Its algorithm and usage can be found in the Black Book.

awb99 commented 1 year ago

The valley function returns true when the last price was below the current price and also below the last-but-one price; the peak function returns true when the last price was above the current and the last-but-one

awb99 commented 1 year ago

ZMA, Ehler’s Zero-Lag Moving Average, an EMA with a correction term for removing lag. This is the source code: var ZMA(var Data,int Period) { var ZMA = series(Data[0]); var a = 2.0/(1+Period); var Ema = EMA(Data,Period); var Error = 1000000; var Gain, GainLimit=5, BestGain=0; for(Gain = -GainLimit; Gain < GainLimit; Gain += 0.1) { ZMA[0] = a(Ema + Gain(Data[0]-ZMA[1])) + (1-a)ZMA[1]; var NewError = Data[0] - ZMA[0]; if(abs(Error) < newError) { Error = abs(newError); BestGain = Gain; } } return ZMA[0] = a(Ema + BestGain(Data[0]-ZMA[1])) + (1-a)ZMA[1]; }

awb99 commented 1 year ago

ALMA, Arnaud Legoux Moving Average, based on a shifted Gaussian distribution (described in this paper): var ALMA(var Data, int Period) { var m = floor(0.85(Period-1)); var s = Period/6.0; var alma = 0., wSum = 0.; int i; for (i = 0; i < Period; i++) { var w = exp(-(i-m)(i-m)/(2ss)); alma += Data[Period-1-i] w; wSum += w; } return alma / wSum; }

awb99 commented 1 year ago

Laguerre, a 4-element Laguerre filter: var Laguerre(var Data, var alpha) { var L = series(Data[0]); L[0] = alphaData[0] + (1-alpha)L[1]; L[2] = -(1-alpha)L[0] + L[1] + (1-alpha)L[2+1]; L[4] = -(1-alpha)L[2] + L[2+1] + (1-alpha)L[4+1]; L[6] = -(1-alpha)L[4] + L[4+1] + (1-alpha)L[6+1]; return (L[0]+2L[2]+2L[4]+L[6])/6; }

awb99 commented 1 year ago

Smooth, John Ehlers’ “Super Smoother”, a 2-pole Butterworth filter combined with a 2-bar SMA that suppresses the Nyquist frequency: var Smooth(var Data,int Period) { var f = (1.414PI) / Period; var a = exp(-f); var c2 = 2acos(f); var c3 = -aa; var c1 = 1 - c2 - c3; var S = series(Data[0]); return S[0] = c1(Data[0]+Data[1])0.5 + c2S[1] + c3S[2]; }

awb99 commented 1 year ago

Decycle, another low-lag indicator by John Ehlers. His decycler is simply the difference between the price and its fluctuation, retrieved with a highpass filter: var Decycle(var* Data,int Period) { return Data[0]-HighPass2(Data,Period); }

awb99 commented 1 year ago

image

awb99 commented 1 year ago

// Market Meanness Index double MMI(double Data,int Length) { double m = Median(Data,Length); int i, nh=0, nl=0; for(i=1; i<Length; i++) { if(Data[i] > m && Data[i] > Data[i-1]) // mind Data order: Data[0] is newest! nl++; else if(Data[i] < m && Data[i] < Data[i-1]) nh++; } return 100.(nl+nh)/(Length-1); }

awb99 commented 1 year ago

This indicator can improve – sometimes even double – the profit expectancy of trend following systems. The Market Meanness Index tells whether the market is currently moving in or out of a “trending” regime. It can this way prevent losses by false signals of trend indicators. It is a purely statistical algorithm and not based on volatility, trends, or cycles of the price curve.

There are already several methods for differentiating trending and nontrending market regimes. Some of them are rumored to really work, at least occasionally. John Ehlers proposed the Hilbert Transform or a Cycle / Trend decomposition, Benoit Mandelbrot the Hurst Exponent. In comparison, the source code of the Market Meanness Index is relatively simple: