Tools, Techniques, and Training for Nirvana Platforms |
OmniTrader, VisualTrader, OmniFunds & OmniVest |
| ||
| ||
|
|
1 Timothy 6:6-7 (NKJV) ... Now godliness with contentment is great gain. For we brought nothing into this world, and it is certain we can carry nothing out. |
John Ehlers' Forward Reverse EMA | |||
|
RossKovacs |
| ||
Veteran Posts: 131 Location: USA: GA, Snellville | In a recent podcast (Better System Trader), John Ehlers described and posted the EasyLanguage code for a new indicator - the Forward Reverse EMA. He has previously written about the basic concept - lag of an EMA can be reduced or eliminated by smoothing twice, first in the normal forward left to right calculation, then a second time in a reverse right to left calculation. You can hear a more full discussion of the concept in the podcast. Attached are two documents, one with Ehlers' original code in EasyLanguage, and a second with my translation of the code into OmniLanguage. Any comments on improving the OL code are welcome. The OL code is similar to the original code in that it only has one value (0.1) for the EMA smoothing constant. The indicator produces an oscillator that looks similar to TRIX, not a moving average type of indicator that would be plotted on price. Edited by RossKovacs 7/1/2017 4:06 PM Attachments ForwardReverseEMA.txt (0KB - 10 downloads)ForwardReverseEMA_OL.rtf (36KB - 11 downloads) | ||
JimDean |
| ||
Owner/Admin Posts: 3925 Location: USA: GA, Lawrenceville | Technically, smoothing in the normal "forward" direction is an infinite series. I've published a lot of proof-calcs over the past 15 years stressing the importance for ena's to have proper warmup. To get approx 3 sig figures precision you need 3x periods of history. To get 5 sigfigs precision (i.e. +/- 1 cent in a $500 stock) you need approx 5x periods warmup (i.e. 100 bars for a 20-period ema). The calc you posted should "start" at bar 99, if you want to model ema(20) accurately on the far left side of the chart. And, the method of doing the reverse calc seems to be worth only one-ish sigfig, if I'm following the logic correctly. So, the smoothing is way different (~4 sig figs or more) in forward vs reverse. That's apples and oranges: sigfigs say that if you add 5 to "5 million" the legit answer is still "5 million". All that notwithstanding, maybe the author knows that info and just doesn't care. Imho, if the sigfigs were handled properly (way more complex code, esp if periods is an input), then the net result conceptually would be sort of like a "Triangular" MA - the TMA is center-weighted (google it). I'd like to see the math that proves the FR EMA has less lag, before diving in to code a robust version. | ||
RossKovacs |
| ||
Veteran Posts: 131 Location: USA: GA, Snellville | I was looking for suggestions on the OL coding and translation from EasyLanguage; it is not in my job description to defend Ehlers' work. I understand the significant figures comment in terms of getting precise values that will not change for a given indicator. I believe your comment on significant figures could apply to most indicators that Nirvana has produced; does the comment imply that N's trading systems will fail from a lack of significant figures? I favor precision whenever it is effective and efficient. However, this is different than getting entry or exit signals that would not change. It is not clear to me that calculating to 5 significant figures will improve the win/loss ratio or other trading statistics in actual trading. I would expect that it would depend on the way the entry and exit rules are written. If it is a simple system with one rule, then I would expect changes. Most systems that traders use have multiple rules with multiple indicators; precision to 5 significant figures of each calculated value would get lost as the number of rules increases. Ehlers mentions in the podcast that his code could be extended to get more significant figures by extending the truncated infinite series. For example, the code for reverse EMA as written RE1 = CC*EMA(19) + EMA(19)[1] RE2 = CC^2*RE1 + RE1[1] RE3 = CC^4*RE2 + RE2[1] RE4 = CC^8*RE3 + RE3[1] RE5 = CC^16*RE4 + RE4[1] RE6 = CC^32*RE5 + RE5[1] RE7 = CC^64*RE6 + RE6[1] RE8 = CC^128*RE7 + RE7[1] could be extended with RE9 = CC^256*RE8 + RE8[1] RE10 = CC^512*RE9 + RE9[1] etc. However, he says in the podcast that he is comfortable for trading with calculated values within +/- 2%. Ehlers has a website, Stockspotter.com, where he publishes all of his trades; anyone willing to look can check if his +/- 2% assumption is impacting his trading signals. As to the TMA comment, this is an oscillator, not a moving average. If I am understanding the code correctly, then subtracting the reverse EMA from the forward EMA would not be center weighting the indicator. Edited by RossKovacs 7/2/2017 9:08 AM | ||
JimDean |
| ||
Owner/Admin Posts: 3925 Location: USA: GA, Lawrenceville | I was just making a comment about coding infinite-series indicators, such as EMA, ATR, MACD, etc. I misunderstood your post - I thought it was "double smoothing" (i.e. Like an Ma of an Ma) rather than "subtracting". Thanks for clarification. As such, it makes sense that it would be an oscillator, and would be totally unrelated to TMA. Also, it sounds a lot more like an "Ehlers tool" that way ;-) Sorry about my confusion. As to accuracy of N plugins, no comment or implication was intended. I was referring to the "pure math" rather than any "required accuracy". Flavor to taste. | ||
RossKovacs |
| ||
Veteran Posts: 131 Location: USA: GA, Snellville | Attached is version 2. It allows a user to choose the number of bars for the indicator calculation and the data array (open, high, low, close). I extended the truncated infinite series and the handling of initial values on the far left of the chart. The calculation should be good for 4 significant digits. I have not used the indicator in a system (yet). It looks like TRIX, so I do not know if it will be any better than using Nirvana's (or your own) version of TRIX. Attachments ForwardReverseEMA_OL_v2.rtf (40KB - 16 downloads) | ||
JimDean |
| ||
Owner/Admin Posts: 3925 Location: USA: GA, Lawrenceville | Looks a lot better! Would be great to see a snapshot with some suggestions how it might be used. Final suggestion: try adding DA #5 = guts = (H+L+2*(O+C))/6 | ||
JimDean |
| ||
Owner/Admin Posts: 3925 Location: USA: GA, Lawrenceville | Also, you might add a third Param to choose how many RE# tiers to apply, using if/then, which will show effect of adding extra terms. Maybe have total options be as many as 50 or so? I.e. (Depth = the new param): RE1 = formula RE2 = iif(Depth >= 2, formula, RE1) RE3 = iif(Depth >= 3, formula, RE2) RE4 = iif(Depth >= 4, formula, RE3) … RE50 = iif(Depth >= 50, formula, RE49) ( this can't be done with deeply nested if/then's since OL won't permit really deep nesting ) | ||
MelDickover |
| ||
Veteran Posts: 155 Location: USA: FL, Bradenton | Ross, I don't think your translation is right. I believe EMA is a variable in Ehlers Tradestation code, not a function. EMA = AA*Close + CC*EMA[1] is a recursive calculation of an EMA variable. Where did ema(19) come from? Did not see that in the Tradestation code. Here is my try at a translation. The alpha (default .1) used at the end to calculate emaFR must change with the period of the EMA - it is is some sense a function of the period. #Indicator 'frEMA #param "alpha", .1,.1, 1 ' use this to change period #param "PlotBars", 260,0,512 Dim a, b, fr, emaFR as Single Dim r1, r2, r3, r4, r5, r6, r7, r8 as Single Dim warmUp as integer Dim BgnPlot as integer if Bar = 0 then warmup = 99 if Plotbars = 0 or PlotBars>(SymbolData.Numrec-1-2*warmup) then BgnPlot = 2*warmup else BgnPlot = Symboldata.numRec-1-PlotBars end if b = 1-alpha elseif bar>20 and bar <= BgnPlot-warmup then fr = alpha*Close + b*fr[1] elseif bar>BgnPlot-warmUp then 'Forward EMA fr = alpha*Close + b*fr[1] 'means ema(19)? 'fr = ema(19) gives different value 'I think EMA in tradestation code is just a variable 'Truncated infinite series for Reverse EMA r1 = b^(2^0)*fr + fr[1] r2 = b^(2^1)*r1 + r1[1] r3 = b^(2^2)*r2 + r2[1] r4 = b^(2^3)*r3 + r3[1] r5 = b^(2^4)*r4 + r4[1] r6 = b^(2^5)*r5 + r5[1] r7 = b^(2^6)*r6 + r6[1] r8 = b^(2^7)*r7 + r7[1] 'Calculate Forward Reverse EMA oscillator ' alpha is specific to a period ' diff periods require different alphas emaFR = fr - alpha*r8 if bar>BgnPlot then if emaFR>emaFR[1] then Plot("FRema", emaFR, green) else Plot("FRema", emaFR, red) end if Plot("zline", 0, black) End If End If Return emaFR Mel Attachments frEMA.txt (1KB - 10 downloads) | ||
MelDickover |
| ||
Veteran Posts: 155 Location: USA: FL, Bradenton | Formula for ema: ema = price*alpha+ema[1]*(1-alpha) where alpha = 2/(n+1) and n is the period so, you can modify the formula to input period instead of alpha by computing alpha by that formula. Note that if the period is 19, alpha = 2/(19+1) = .1, the default in Ehlers formula. Mel | ||
RossKovacs |
| ||
Veteran Posts: 131 Location: USA: GA, Snellville | Mel, some responses - I believe EMA is a variable in Ehlers Tradestation code, not a function. <- I agree Where did ema(19) come from? <- that was a quick way to get the 0.1 smoothing coefficient in version 1, I eliminated it in version 2 Here is my try at a translation. The alpha (default .1) used at the end to calculate emaFR must change with the period of the EMA - it is is some sense a function of the period. <- I agree, version 2 has this with AA = alpha = 2/(n+1) Version 2 has the warmUp periods and PlotBars as a function of the periods selected ( 3 * Period ). This could be increased by anyone that wants more warm up to 10 * Period or whatever value the user prefers. | ||
RossKovacs |
| ||
Veteran Posts: 131 Location: USA: GA, Snellville | Jim, an interesting use of iif to get around OL limitations. I'll have to play with it to see if it works with Ehlers' code. | ||
RossKovacs |
| ||
Veteran Posts: 131 Location: USA: GA, Snellville | Jim, the attached doc has snapshots showing the Forward Reverse EMA and N's TRIX on the S&P 500. Small values (<20) seem to give similar results for both indicators. Large values (>20), the Forward Reverse EMA seems to be more responsive. Not sure if that will make a difference in any trading system. Attachments Snapshots of Forward-Reverse EMA and TRIX.docx (97KB - 30 downloads) | ||
RossKovacs |
| ||
Veteran Posts: 131 Location: USA: GA, Snellville | Attached are versions 3 and 3A. Version 3 uses some of Jim's and Mel's ideas and does a better job of calculating initial values. Version 3A uses Jim's idea to add a "Depth" parameter to control how far the truncated infinite series is allowed to go. Adding more "depth" to the infinite series increases the lag and smoothness of the indicator for a given selection of periods. However, the same thing can be done by increasing the periods in calculating the indicator. Thank you, Jim and Mel, for your comments. While it was a good coding exercise, I think I have beaten this horse to death; version 3 and 3A are the end for me. I do not think that this indicator is much better than TRIX. Attachments ForwardReverseEMA_OL_v3.rtf (42KB - 13 downloads)ForwardReverseEMA_OL_v3A.rtf (57KB - 15 downloads) | ||
SalimHira |
| ||
Veteran Posts: 183 Location: USA: MD, Columbia | Hello Ross and All, I am attempting to turn this code into a "System", with a twist of signals not coming from > 0 or < 0, but wherever frEMA turns, signal can be above or below zero as frEMA floats. Please advise. In addition, I am getting red/green signals on each bar as shown in attached file. Thanks. - Salim (frEMAsystem.png) Attachments frEMAsystem.png (18KB - 6 downloads) | ||
RossKovacs |
| ||
Veteran Posts: 131 Location: USA: GA, Snellville | Hello Salim, The code that you added is part of the Plot section. I do not know if OT will execute Entry signals inside of the Plot command. My suggestion: 1. copy the code shown in the attachment, leave out the Plot command, and place the copy without the Plot command higher in the code of the system 2. of course you would need to add the condition of signals not coming from > 0 or < 0, e.g., If frEMA >= frEMA[1] AND frEMA < 0 Then | ||
|
Owner of site: Jim Dean -- Forum content is confidential, and may not be distributed without written permission. | |