Can anyone code John Ehler's FRAMA ?
Forums › ProRealTime English forum › ProBuilder support › Can anyone code John Ehler's FRAMA ?
- This topic has 13 replies, 4 voices, and was last updated 7 years ago by Bard.
-
-
11/24/2016 at 2:11 PM #17097
Hi
I was looking into Ehlers Cycle theories and came across a Moving Averages Study that shows Ehler’s Fractal Adaptive Moving Average (FRAMA) is far superior to the EMA and SMA (although I don’t know how it would stand up to Kaufman’s Adaptive Moving Average) :
http://etfhq.com/blog/2010/10/09/frama-is-it-effective/#Best
Does anyone know how to code it from this Metatrader code? :
https://www.mql5.com/en/code/72
Cheers
Brad11/24/2016 at 2:40 PM #17102Found this old code on a German forum for FRAMA moving average , modified some variables names for recent PRT version :
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061// FRAMAp = 20pri=Customcloselen=pN3=(Highest[len](High)-Lowest[len](Low))/lenmH=HighL=LowFor count=0 To len/2-1If High[count] > mH ThenmH=High[count]EndifIf Low[count] < L ThenL=Low[count]EndifNextN1=(mH-L)/(len/2)HH=High[len/2]LL=Low[len/2]For count=len/2 To len-1If High[count] > HH ThenHH=High[count]EndifIf Low[count] < LL ThenLL=Low[count]EndifNextN2=(HH-LL)/(len/2)If N1 > 0 And N2 > 0 And N3 > 0 ThenDimen=(Log(N1+N2)-Log(N3))/Log(2)Endifalpha=Exp(-4.6*(Dimen-1))If alpha < 0.01 Thenalpha=0.01EndifIf alpha > 1 Thenalpha=1EndifFilt=alpha*pri+(1-alpha)*Filt[1]If Barindex < len+1 ThenFilt=priEndifReturn Filt as "FRAMA"I’ll post it too into the Library with credits for the author of course.
1 user thanked author for this post.
11/24/2016 at 3:04 PM #17108Added to the Library for future reference: FRAMA indicator download
11/24/2016 at 4:23 PM #17116Thanks Nicolas, much appreciated.
Do you know if FRAMA is meant to have a fast (FC) and slow (SC) moving average setting like the Kaufman Adaptive Mov Ave?
On the link above (efthq blog) is mentions in it’s conclusion:“The FRAMA is astoundingly effective as both a fast and a slow moving average and will outperform any SMA or EMA. We selected a modified FRAMA with a “FC” of 4, a “SC” of 300 and a “FRAMA” period of 126 as being the most effective fast FRAMA although the settings for a standard FRAMA will also produce excellent results. For a slower or longer term average the best results are likely to come from a “FC” of 40, a “SC” of 250 and a “FRAMA” period of 252.”
Cheers
Brad
12/05/2016 at 7:36 PM #17842Hi 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.1234567891011121314151617181920212223242526272829303132333435363738#'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 evenN3 <- (runMax(Hi(HLC), n)-runMin(Lo(HLC), n))/nN1 <- (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)/oldAlphanewN <- ((SC-FC)*(oldN-1)/(SC-1))+FCalpha <- 2/(newN+1)alpha[which(alpha > 1)] <- 1alpha[which(alpha < w)] <- walphaComplement <- 1-alphainitializationIndex <- index(alpha[is.na(alpha)])alpha[is.na(alpha)] <- 1; alphaComplement[is.na(alphaComplement)] <- 0FRAMA <- 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] <- NAtrigger <- 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
123456789101112131415161718192021222324252627282930313233343536FRAMA(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))/Lengthwhere: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
Brad12/06/2016 at 5:31 PM #1788312/06/2016 at 10:18 PM #1790712/07/2016 at 11:59 AM #17922Hi Nicholas,
You mean Copyrighted “C”?
This code has the fast and Slow Mov Averages and is logarithmic. Could you delete the other two codes above? Thanks (-:
123456789101112131415161718192021222324252627282930313233study("Fractal Adaptive Moving Average",shorttitle="FRAMA",overlay=true)price = closelen = input(defval=252,minval=1)FC = input(defval=40,minval=1)SC = input(defval=252,minval=1)w = log(2/(SC+1))len1 = len/2H1 = highest(high,len1)L1 = lowest(low,len1)N1 = (H1 - L1)/len1H2 = for i = len1+1 to lenhigh>high[i]?high:high[i]L2 = for i = len1+1 to lenlow>low[i]?low:low[i]N2 = (H2 - L2)/len1H3 = highest(high,len)L3 = lowest(low,len)N3 = (H1 - L1)/lendimen = (log(H1+H2) - log(H3))/log(2)oldalpha = exp(w*(dimen-1))oldN = (2-oldalpha)/oldalphanewN = ((SC-FC)*(oldN-1)/(SC-1))+FCalpha = 2/(newN+1)out = nz(out[1])*(1-alpha) + price*alphaplot(out,title="FRAMA")12/07/2016 at 3:26 PM #17936Nope I meant “R”, the statistical analysis software: https://www.r-project.org/
This last new code is much more readable, I believe it can be converted to probuilder. Let me have a look.
12/08/2016 at 10:05 AM #17985Your last code proposal have some variables that aren’t used in the final calculation of the FRAMA. This prorealtime code is the result of the conversion, it returns a curve, I let you verify its information on your charts 🙂
123456789101112131415161718192021222324252627282930313233343536373839price = closelen = 126 //FRAMA periodFC = 4 //fast constant--aka lowest n for an EMASC = 300 //slow constant--aka lowest n for an EMAif barindex>len+fc+sc thenw = log(2/(SC+1))len1 = len/2H1 = highest[len1](high)//L1 = lowest[len1](low)//N1 = (H1 - L1)/len1for i = len1+1 to len doif high>high[i] thenH2 = high[i]endifnext//for i = len1+1 to len do//if low>low[i] then//L2 = low[i]//endif//next//N2 = (H2 - L2)/len1H3 = highest[len](high)//L3 = lowest[len](low)//N3 = (H1 - L1)/lendimen = (log(H1+H2) - log(H3))/log(2)oldalpha = exp(w*(dimen-1))oldN = (2-oldalpha)/oldalphanewN = ((SC-FC)*(oldN-1)/(SC-1))+FCalpha = 2/(newN+1)out = (out[1])*(1-alpha) + price*alphaendifRETURN out as "modified FRAMA"12/08/2016 at 12:40 PM #1805705/13/2017 at 11:27 PM #35462Dans le code on a du
1234567HH=High[len/2]LL=Low[len/2]For count=len/2 To len-1If High[count] > HH ThenHH=High[count]EndifTout va bien quand len est pair.
Si len est impaire, ça va planter on va essayer d’accéder à Low[décimal].
Du coup il faut faire un ROUND je suppose avant 🙂
genre High[ROUND(count)]
C’est pas super beau mais bon 🙂
05/13/2017 at 11:51 PM #35463Voilà mon code avec les rounds et le FIX sur le LOG
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758// FRAMA - Fractal Adaptive Moving Average by John Ehlers// --- parametersSeries = CustomClose// Period = 20// ---Period = MAX(Period, 1)N3 = (highest[Period](High) - lowest[Period](Low)) / PeriodmH = HighL = LowFOR count = 0 TO (Period / 2) - 1 DOcount = ROUND(count)IF High[count] > mH THENmH = High[count]ENDIFIF Low[count] < L THENL = Low[count]ENDIFNEXTN1 = (mH - L) / (Period / 2)HH = High[ROUND(Period / 2)]LL = Low[ROUND(Period / 2)]FOR count = Period / 2 TO Period - 1 DOcount = ROUND(count)IF High[count] > HH THENHH = High[count]ENDIFIF Low[count] < LL THENLL = Low[count]ENDIFNEXTN2 = (HH - LL) / (Period / 2)IF N1 > 0 AND N2 > 0 AND N3 > 0 THEN// FIX LOGDimen = ((LOG(N1 + N2) / LOG(10)) - (LOG(N3) / LOG(10))) / (LOG(2) / LOG(10))ENDIFalpha = EXP(-4.6 * (Dimen - 1))IF alpha < 0.01 THENalpha = 0.01ELSIF alpha > 1 THENalpha = 1ENDIFIF BarIndex < Period THENFRAMA = SeriesELSEFRAMA = alpha * Series + (1 - alpha) * FRAMA[1]ENDIFRETURN FRAMA1 user thanked author for this post.
05/14/2017 at 5:09 PM #35493 -
AuthorPosts