Hi Nicolas,
The code above does appear to be come from a paper written by Ehler’s: http://www.mesasoftware.com/papers/FRAMA.pdf
But I think there is a newer version. I found this code below for Ehler’s FRAMA which has the fast and slow moving averages setting.
#'FRactal Adaptive Moving Average
#'@param HLC an HLC price series
#'@param n a lookback period
#'@param FC a fast constant--aka lowest n for an EMA
#'@param SC a slow constant--aka lowest n for an EMA
#'@param triggerLag a number of days by which to lag the original computation
#'@return an xts containing the FRAMA and the trigger
#'@references
#'\cr\url{http://www.mesasoftware.com/Papers/FRAMA.pdf}
#'\cr\url{http://etfhq.com/blog/2010/09/30/fractal-adaptive-moving-average-frama/}
#'@export
"FRAMA" <- function(HLC, n=20, FC=1, SC=200, triggerLag=1, ...) {
price <- ehlersPriceMethod(HLC, ...)
if (n%%2==1) n=n-1 #n must be even
N3 <- (runMax(Hi(HLC), n)-runMin(Lo(HLC), n))/n
N1 <- (runMax(Hi(HLC), n/2)-runMin(Lo(HLC), n/2))/(n/2)
lagSeries <- lag(HLC, n/2)
N2 <- (runMax(Hi(lagSeries), n/2)-runMin(Lo(lagSeries), n/2))/(n/2)
dimen <- (log(N1+N2)-log(N3))/log(2)
w <- log(2/(SC+1))
oldAlpha <- exp(w*(dimen-1))
oldN <- (2-oldAlpha)/oldAlpha
newN <- ((SC-FC)*(oldN-1)/(SC-1))+FC
alpha <- 2/(newN+1)
alpha[which(alpha > 1)] <- 1
alpha[which(alpha < w)] <- w
alphaComplement <- 1-alpha
initializationIndex <- index(alpha[is.na(alpha)])
alpha[is.na(alpha)] <- 1; alphaComplement[is.na(alphaComplement)] <- 0
FRAMA <- rep(0, length(price))
FRAMA[1] <- price[1]
FRAMA <- computeFRAMA(alpha, alphaComplement, FRAMA, price)
FRAMA <- xts(FRAMA, order.by=index(price))
FRAMA[initializationIndex] <- alpha[initializationIndex] <- NA
trigger <- lag(FRAMA, triggerLag)
out <- cbind(FRAMA=FRAMA, trigger=trigger)
return(out)
}
There is also metatrader code using log values of the EMA: https://www.mql5.com/en/code/72
FRAMA(i) = A(i) * Price(i) + (1 - A(i)) * FRAMA(i-1)
where:
FRAMA(i) - current value of FRAMA;
Price(i) - current price;
FRAMA(i-1) - previous value of FRAMA;
A(i) - current factor of exponential smoothing.
Exponential smoothing factor is calculated according to the below formula:
A(i) = EXP(-4.6 * (D(i) - 1))
where:
D(i) - current fractal dimension;
EXP() - mathematical function of exponent.
Fractal dimension of a straight line is equal to one. It is seen from the formula that if D = 1, then A = EXP(-4.6 *(1-1)) = EXP(0) = 1. Thus if price changes in straight lines, exponential smoothing is not used, because in such a case the formula looks like this:
FRAMA(i) = 1 * Price(i) + (1 - i) * FRAMA(i-1) = Price(i)
I.e. the indicator exactly follows the price.
The fractal dimension of a plane is equal to two. From the formula we get that if D = 2, then the smoothing factor A = EXP(-4.6*(2-1)) = EXP(-4.6) = 0.01. Such a small value of the exponential smoothing factor is obtained at moments when price makes a strong saw-toothed movement. Such a strong slow-down corresponds to approximately 200-period simple moving average.
Formula of fractal dimension:
D = (LOG(N1 + N2) - LOG(N3))/LOG(2)
It is calculated based on the additional formula:
N(Length,i) = (HighestPrice(i) - LowestPrice(i))/Length
where:
HighestPrice(i) - current maximal value for Length periods;
LowestPrice(i) - current minimal value for Length periods;
Values N1, N2 and N3 are respectively equal to:
N1(i) = N(Length,i)
N2(i) = N(Length,i + Length)
N3(i) = N(2 * Length,i)
Is is possible to code these into PRT code (or incorporate this with your code) to compare the effectiveness of the FC and SC with the Log version?
Thanks,
Cheers
Brad