Very Latest John Ehler's Correlation Trend Indicator
Forums › ProRealTime English forum › ProBuilder support › Very Latest John Ehler's Correlation Trend Indicator
- This topic has 31 replies, 4 voices, and was last updated 4 years ago by supertiti.
-
-
04/28/2020 at 3:19 PM #128654
I thought this might be worth converting and make a good addition to the indicator library if someone would like to covert the code? It’s John Ehler’s Correlation Trend Indicator from the Stocks & Commodities 05/2020 edition.
“The basic idea of the Correlation Trend Indicator (CTI) is quite simple. The ideal trend curve is a straight upwards line. So the CTI just measures the correlation of the price curve with this ideal trend line. Ehlers provided TradeStation code. This is the indicator:
Ehler's Correlation Trend Indicator123456789101112131415var CTI (vars Data, int Length){int count;var Sx = 0, Sy = 0, Sxx = 0, Sxy = 0, Syy = 0;for(count = 0; count < Length; count++) {var X = Data[count]; // the price curvevar Y = -count; // the trend lineSx = Sx + X; Sy = Sy + Y;Sxx = Sxx + X*X; Sxy = Sxy + X*Y; Syy = Syy + Y*Y;}if(Length*Sxx-Sx*Sx > 0 && Length*Syy-Sy*Sy > 0)return (Length*Sxy-Sx*Sy)/sqrt((Length*Sxx-Sx*Sx)*(Length*Syy-Sy*Sy));else return 0;}X represents the price curve, Y the trend line, and correlation is measured with the Spearman algorithm. The trend line is supposed to linearly rise with count, but I’m using the negative count here because the Data series is stored backwards, with the most recent values at the beginning.
This is how the Correlation Trend Indicator (CTI) looks when applied to SPY (red = 10 days period, blue = 40 days): Pls see image.
We can see that the lines reproduce rather well the price curve trend. And we can also see that the blue line, the 40-days trend, is not just a smoothed version of the red 10-days trend – it looks entirely different. This is an interesting feature of a trend indicator – it separates long-term and short-term trend perfectly.”
The article goes on to say that it might be difficult profitably trading on CTI zero crossovers but I would regard, judging form the indicator screenshot, that the -0.8 and +0.8 crossings are more useful for determining future price direction.
https://financial-hacker.com/petra-on-programming-a-unique-trend-indicator/#more-3424
04/28/2020 at 3:45 PM #128674Seems very similar to: R-Squared correlation coefficient – R²
04/29/2020 at 5:14 PM #128851At first I was so close @Nicolas!
I tried first with my left hand indicator (pls see image) but couldn’t get it to scale -1 to 1 in the top indicator.
So I built another (right hand side indicator and bottom chart using Ehler’s version of R2) and eventually got my Long R2 to stop flatlining along the zero axis by having to make a distinction between the two groups of SumsX’s, SumXY’s etc. I relabelled all the SumX’s, SumXX’s etc by changing them to SumLX (L for Long) and SumY to SumSY etc. Phew.
So… is Ehler’s R2 mathematics equations doing the exact same thing as PRC’s R2?
If it’s correct, maybe it’s one for the library?
Cheers,
Ehler's Correlation Trend Indicator (CTI)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465//Ehler's Correlation Trend Indicator (CTI)//PRC_R-Squared coefficient | indicator//19.09.2018//Nicolas @ www.prorealcode.com / Bard//Sharing ProRealTime knowledge// --- settings//SPeriod = 10 //Add in Variables//LPeriod = 40 //Add in Variables// --- end of settingsData = customclose//////////////////////////////////////////////////////////////////////////////////////S=ShortSumSX = 0 // the price curveSumSXX = 0SumSXY = 0SumSYY = 0SumSY = 0 // the trend lineif barindex>SPeriod thenfor k=0 to SPeriod-1 dotprice = Data[k]SumSX = SumSX+(k+1)SumSXX = SumSXX+((k+1)*(k+1))SumSXY = SumSXY+((k+1)*tprice)SumSYY = SumSYY+(tprice*tprice)SumSY = SumSY+tpricenextif (SPeriod*SumSXX-SumSX*SumSX) > 0 and (SPeriod*SumSYY-SumSY*SumSY > 0) theniRsqS = (SPeriod*SumSXY-SumSX*SumSY)/sqrt((SPeriod*SumSXX-SumSX*SumSX)*(SPeriod*SumSYY-SumSY*SumSY))endifendif//////////////////////////////////////////////////////////////////////////////////////L = LongSumLX = 0 // the price curveSumLXX = 0SumLXY = 0SumLYY = 0SumLY = 0 // the trend lineif barindex>LPeriod thenfor k=0 to LPeriod-1 dotprice = Data[k]SumLX = SumLX+(k+1)SumLXX = SumLXX+((k+1)*(k+1))SumLXY = SumLXY+((k+1)*tprice)SumLYY = SumLYY+(tprice*tprice)SumLY = SumLY+tpricenextif (LPeriod*SumLXX-SumLX*SumLX) > 0 and (LPeriod*SumLYY-SumLY*SumLY > 0) theniRsqL = (LPeriod*SumLXY-SumLX*SumLY)/sqrt((LPeriod*SumLXX-SumLX*SumLX)*(LPeriod*SumLYY-SumLY*SumLY))endifendifreturn iRsqS coloured (255,0,0) style (line,2) as "Short R² Correlation Trend", iRsqL coloured (0,0,255) style (line,2) as "Long R² Correlation Trend", 0 style (dottedline,2)as "0"04/29/2020 at 5:29 PM #128856I don’t think the SPeriod and LPeriod Variables are making any difference to the indicators red and blue R2 lines when they’re increased or decreased?
Ps// Please crop off the blue top bar in the (last, 4th) final photo if it’s used for the library and please delete this comment. Thanks @Nicolas.
04/29/2020 at 8:21 PM #128877Bard – If you think that you have created an indicator worthy of going in the library then you need to click on the ‘+’ on the top bar and submit it yourself. Add images, ITF file a description and include the code in the post. This saves Nicolas a lot of work – and he is a very busy person! Please check how other library posts are formatted and this will give you an idea of what is required.
1 user thanked author for this post.
04/29/2020 at 10:13 PM #128893Thanks Vonasi, I hoped someone would give the indicator a look before adding to make sure the code is correct (it looks correct from the chart but I am not a coder)?
Here I’ve patched together the PRC R2 indicator with some of the code from John Ehler’s Correlation Trend indicator. Both codes are available, Ehler’s at the very top of this thread and PRC R2 via a url link. Does mine look correct and can you fix the period variable not having any impact on the red or blue line (you can only change the period via the code in the indicator)? Cheers.04/29/2020 at 10:21 PM #128895Does mine look correct?
You’ll have to wait for Nicolas the TradeStation to PRT guru to confirm that you have replicated the code correctly and then submit it to the library.
04/30/2020 at 8:43 AM #128934Thanks for the work done Bard. Without diggin too much into your code, I think that there might be some differences, but I can’t spot them.
Here is the 1:1 conversion from the Tradestation code you shared:
123456789101112131415161718192021222324length=20Sx = 0Sy = 0Sxx = 0Sxy = 0Syy = 0data=customclosefor count = 0 to Length-1 doX = Data[count] // the price curveY = -count // the trend lineSx = Sx + XSy = Sy + YSxx = Sxx + X*XSxy = Sxy + X*YSyy = Syy + Y*Ynextif(Length*Sxx-Sx*Sx > 0 and Length*Syy-Sy*Sy > 0) thencti= (Length*Sxy-Sx*Sy)/sqrt((Length*Sxx-Sx*Sx)*(Length*Syy-Sy*Sy))endifreturn cti1 user thanked author for this post.
05/02/2020 at 11:45 AM #129372That’s great Nicolas, not sure it’s the right way up!? I thought maybe mine (short name, bottom indicator) was the inverse when on the £/$, when the R2 reading was high there was no trend but now on Brent Crude I think mine looks right (bottom) and yours is inverse. What do you think? Pls see second screenshot and ignore first screenshot.
What change to the equation would flip them to the “correct” way as in your code, because I thought I altered the PRC R2 code accurately?
I’ve added a long = 40 and short period =10 in variables for easy control.
Nearly there! Cheers,
05/02/2020 at 12:23 PM #129384And S&P 500: Now it clearly looks like yours is the right way round (how could it not be), judging from the blue 40 day trend correlation from Nov 2019 onwards on the S&P 500 But then note how mine seems to correctly represent the crash on the far right and yours doesn’t, both you 10 and 40 period CTI’s are low and “non trending.”
This is slightly confusing to me why this is happening when the trends are so clearly seen in the candlesticks? (Your code top, mine bottom).
1 user thanked author for this post.
05/02/2020 at 2:58 PM #129427This indicator is not giving you the trend direction, but the fact that the price is similar or not to a straight line (a linear regression), calculated over a given period.
1 user thanked author for this post.
05/03/2020 at 10:58 AM #129545I appreciate that Nicolas, but the cases where the market is clearly trending in one direction or another with little noise don’t always seem to be reflected in the CTI?
For example in the S&P 500 above, your top CTI 40 day blue indicator picks up those two stable long uptrends (beginning and end of the chart) because the straight line correlation is strong yet completely misses the albeit only slightly less straight lined Covid19 market plunge, whereas my incorrect inverse (bottom) blue CTI picks it up. What is wrong with my equation, it was copied from PRT’s R2?
Then at the start of that market rout, my 10 day red CTI picks up the 7 day straight line plunge perfectly but yours doesn’t?
It’s the behaviour of your red line that first confused me. It completely misses the start of one of the biggest market plunges in history and that near perfect 7 day “linear” plunge!? How come?Cheers,
05/03/2020 at 1:18 PM #129552I put a 5 day CTI in there (along with the 10 and 40 period) to see if it would forecast the market rout, it didn’t but guess what? I spotted something that does suggest a strong link with trend direction and an exploitable relation: The cross of the 5 and 10 day CTI when it was below the -0.5 level or for shorts above the 0.5 level. Quite consistent! Pls see pics.
Please try this on the major currencies like the €/$, $/CHF, $/Yen, £/$ and AUD/$ for the last 5 years with a 3.8 spread:
Be interesting to see what Machine Learning does with those 0.5 thresholds or CTI periods?“Ehler’s CTI + Dev Stop”
1234567891011121314151617181920212223242526272829303132333435363738// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// Conditions to enter long positionsindicator1, indicator2, ignored, ignored, ignored, ignored = CALL "Ehler's Correlation Trend R2X3"[5, 10, 40](close)c1 = (indicator1 CROSSES OVER indicator2)indicator3, ignored, ignored, ignored, ignored, ignored = CALL "Ehler's Correlation Trend R2X3"[5, 10, 40](close)c2 = (indicator3 < -0.5)IF c1 AND c2 THENBUY 1 PERPOINT AT MARKETENDIF// Conditions to exit long positionsignored, ignored, ignored, indicator4, ignored, ignored = CALL "Kase Dev Stop Lisse+SAR+4.5/6"c3 = (close >= indicator4)IF c3 THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator5, indicator6, ignored, ignored, ignored, ignored = CALL "Ehler's Correlation Trend R2X3"[5, 10, 40](close)c4 = (indicator5 CROSSES UNDER indicator6)indicator7, ignored, ignored, ignored, ignored, ignored = CALL "Ehler's Correlation Trend R2X3"[5, 10, 40](close)c5 = (indicator7 > 0.5)IF c4 AND c5 THENSELLSHORT 1 PERPOINT AT MARKETENDIF// Conditions to exit short positionsignored, ignored, ignored, indicator8, ignored, ignored = CALL "Kase Dev Stop Lisse+SAR+4.5/6"c6 = (close CROSSES OVER indicator8)IF c6 THENEXITSHORT AT MARKETENDIF05/03/2020 at 1:32 PM #129559Here’s the Ehler’s CTI + Dev Stop System itf, easier than posting this as I hope it will include the Kase Dev Stop SAR version I used.
Also here is the Double Ehler’s CTI, I’ll add it to the library when I get more time today:
Ehler's Correlation Trend R212345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364//Ehler's Correlation Trend Indicator (CTI)//PRC_R-Squared coefficient | indicator//19.09.2018//Updated to double Ehler's R2 03/05/2020//Nicolas @ www.prorealcode.com//Sharing ProRealTime knowledge// --- settings//LPeriod =40 //Add in Variables//SPeriod =10 //Add in Variables// --- end of settings//LPeriod=40//SPeriod=10/////////////////////////////////////////////////////////////////////////////////////////////////////ShortSumSx = 0 // the price curveSumSy = 0SumSxx = 0SumSxy = 0SumSyy = 0 // the trend linedata=customclosefor count = 0 to SPeriod-1 doX = Data[count] // the price curveY = -count // the trend lineSumSx = SumSx + XSumSy = SumSy + YSumSxx = SumSxx + X*XSumSxy = SumSxy + X*YSumSyy = SumSyy + Y*Ynextif(SPeriod*SumSxx-SumSx*SumSx > 0 and SPeriod*SumSyy-SumSy*SumSy > 0) thenCTIShort = (SPeriod*SumSxy-SumSx*SumSy)/Sqrt((SPeriod*SumSxx-SumSx*SumSx)*(SPeriod*SumSyy-SumSy*SumSy))endif/////////////////////////////////////////////////////////////////////////////////////////////////////LongSumLx = 0SumLy = 0SumLxx = 0SumLxy = 0SumLyy = 0for count = 0 to LPeriod-1 doX = Data[count] // the price curveY = -count // the trend lineSumLx = SumLx + XSumLy = SumLy + YSumLxx = SumLxx + X*XSumLxy = SumLxy + X*YSumLyy = SumLyy + Y*Ynextif(LPeriod*SumLxx-SumLx*SumLx > 0 and LPeriod*SumLyy-SumLy*SumLy > 0) thenCTILong = (LPeriod*SumLxy-SumLx*SumLy)/Sqrt((LPeriod*SumLxx-SumLx*SumLx)*(LPeriod*SumLyy-SumLy*SumLy))endifreturn CTIShort coloured (255,0,0) style (line,2) as "CTI Short R² Trend", CTILong coloured (0,0,255) style (line,2) as "CTI Long R² Trend", 0 style (dottedline,2)as "0", -1 as "-1", 1 as "1"05/03/2020 at 2:20 PM #129577Hola Nico and Bard,
Can we have the same index with scale from 0 to 100 for apairing with others index ?
I use 5 and 30
attached graph of Cac40
CTI NicoBard12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758// CTI NICO d'après BARD adapté par DID 02.05.2020//Ehler's Correlation Trend Indicator (CTI) //PRC_R-Squared coefficient | indicator//19.09.2018 //Nicolas @ www.prorealcode.com / Bard //Sharing ProRealTime knowledge// --- settings//SPeriod length = 10 perso = 5//Add in Variables//LPeriod lengthL = 40 perso = 30 //Add in Variables// --- end of settingsdefparam calculateonlastbars = 100// SHORTSx = 0Sy = 0Sxx = 0Sxy = 0Syy = 0data=customclosefor count = 0 to Length -1 doX = Data[count] // the price curveY = -count // the trend lineSx = Sx + XSy = Sy + YSxx = Sxx + X*XSxy = Sxy + X*YSyy = Syy + Y*Ynextif(Length*Sxx-Sx*Sx > 0 and Length*Syy-Sy*Sy > 0) thenctiS= (Length*Sxy-Sx*Sy)/sqrt((Length*Sxx-Sx*Sx)*(Length*Syy-Sy*Sy))endif//////////////////////////////////////////////////////////////// LONGSx = 0Sy = 0Sxx = 0Sxy = 0Syy = 0data=customclosefor count = 0 to LengthL -1 doX = Data[count] // the price curveY = -count // the trend lineSx = Sx + XSy = Sy + YSxx = Sxx + X*XSxy = Sxy + X*YSyy = Syy + Y*Ynextif(LengthL*Sxx-Sx*Sx > 0 and LengthL*Syy-Sy*Sy > 0) thenctiL= (LengthL*Sxy-Sx*Sy)/sqrt((LengthL*Sxx-Sx*Sx)*(LengthL*Syy-Sy*Sy))endif//////////////////////////////////////////////////////////////return ctiS style (line,3) as " ctiS " , ctiS style (point,5) as " ctiS " , ctiL style (line,3)as " criL " ,ctiL style (point,5)as " criL " , 0 as " zero " , 1 as " UN " , -1 as " MOINS UN " -
AuthorPosts