Hi Tomasz and everyone,
Could you pleases help with calculating half life for mean reverting time series? An ready to use function would be great.
You need to be more specific about what you need. Do you need that for entire price series or a subset or moving window or....? How to ask a good question
Thanks! I want something equivelent to below R code:
Defination1
Here is code in R language
##########
# Calculate half life of mean reversion
y < audnzd
y.lag < lag(y, 1)
delta.y < diff(y)
df < cbind(y, y.lag, delta.y)
df < df[1 ,] #remove first row with NAs
regress.results < lm(delta.y ~ y.lag, data = df)
lambda < summary(regress.results)$coefficients[2]
half.life < log(2)/lambda
I don't think anyone has ready to use code for that and the article with definition you quoted isn't the most clear thing to read. I am also not "R"language specialist but from what I see it calculates linear regression of deltas of "Y" (differences in price) versus "X" (delayed price). I am not sure if coefficients[2] refers to slope or intercept.
Anyway as far as AmiBroker is considered:

delayed data series can be obtained using
Ref()
http://www.amibroker.com/f?ref 
deltas/differences can be calculated by simply subtracting
X  Ref( X, 1 )

LinearRegression is available in AmiBroker http://www.amibroker.com/f?linearregslope and http://www.amibroker.com/f?linearregintercept

Logarithm of course is available too http://www.amibroker.com/guide/afl/log.html
So building blocks are there.
Thanks Tomasz. I believe I need a Ordinary Least Squares regression function. Does AFL provide a way to calculate it?
LinearReg* functions use least squares algorithm.
Hi Tomasz, after research, I found my question is:
How to regress with price as dependent Variable and price ref(price, 1) as independent variable, in order to find Regression slop.
LinearReg function takes bar index as independent variable, so you would rather need to do that as shown in post below. As a possible easy (copypaste) solution you can use AmiPy plugin AmiPy plugin  Python integration together with this code:
https://quant.stackexchange.com/questions/25086/calculatinghalflifeofmeanrevertingserieswithpython
Also you can code just from scratch like below. Code uses ALL bars and produces single value of intercept / slope for all bars.
X = /* independent var */;
Y = /* dependent var */;
bars = BarCount  1; // use all bars
// calculate mean values
mX = LastValue( MA( X, bars ) );
mY = LastValue( MA( Y, bars ) );
// differences x  mean(x) and y  mean(y)
dX = X  mX;
dY = Y  mY;
// sums of squared differences
sumX2 = LastValue( Sum( dX * dX, bars ) );
sumXY = LastValue( Sum( dX * dY, bars ) );
slope = sumXY / sumX2;
intercept = mY  slope * mX;
You are owe some, sir.