ProRealCode - Trading & Coding with ProRealTime™
“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:
var 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 curve
var Y = -count; // the trend line
Sx = 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
//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 settings
Data = customclose
////////////////////////////////////////////////////////////////////////////////////
//S=Short
SumSX = 0 // the price curve
SumSXX = 0
SumSXY = 0
SumSYY = 0
SumSY = 0 // the trend line
if barindex>SPeriod then
for k=0 to SPeriod-1 do
tprice = Data[k]
SumSX = SumSX+(k+1)
SumSXX = SumSXX+((k+1)*(k+1))
SumSXY = SumSXY+((k+1)*tprice)
SumSYY = SumSYY+(tprice*tprice)
SumSY = SumSY+tprice
next
if (SPeriod*SumSXX-SumSX*SumSX) > 0 and (SPeriod*SumSYY-SumSY*SumSY > 0) then
iRsqS = (SPeriod*SumSXY-SumSX*SumSY)/sqrt((SPeriod*SumSXX-SumSX*SumSX)*(SPeriod*SumSYY-SumSY*SumSY))
endif
endif
////////////////////////////////////////////////////////////////////////////////////
//L = Long
SumLX = 0 // the price curve
SumLXX = 0
SumLXY = 0
SumLYY = 0
SumLY = 0 // the trend line
if barindex>LPeriod then
for k=0 to LPeriod-1 do
tprice = Data[k]
SumLX = SumLX+(k+1)
SumLXX = SumLXX+((k+1)*(k+1))
SumLXY = SumLXY+((k+1)*tprice)
SumLYY = SumLYY+(tprice*tprice)
SumLY = SumLY+tprice
next
if (LPeriod*SumLXX-SumLX*SumLX) > 0 and (LPeriod*SumLYY-SumLY*SumLY > 0) then
iRsqL = (LPeriod*SumLXY-SumLX*SumLY)/sqrt((LPeriod*SumLXX-SumLX*SumLX)*(LPeriod*SumLYY-SumLY*SumLY))
endif
endif
return 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"
Does 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.
length=20
Sx = 0
Sy = 0
Sxx = 0
Sxy = 0
Syy = 0
data=customclose
for count = 0 to Length-1 do
X = Data[count] // the price curve
Y = -count // the trend line
Sx = Sx + X
Sy = Sy + Y
Sxx = Sxx + X*X
Sxy = Sxy + X*Y
Syy = Syy + Y*Y
next
if(Length*Sxx-Sx*Sx > 0 and Length*Syy-Sy*Sy > 0) then
cti= (Length*Sxy-Sx*Sy)/sqrt((Length*Sxx-Sx*Sx)*(Length*Syy-Sy*Sy))
endif
return 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,
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1, 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 THEN
BUY 1 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
ignored, ignored, ignored, indicator4, ignored, ignored = CALL "Kase Dev Stop Lisse+SAR+4.5/6"
c3 = (close >= indicator4)
IF c3 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5, 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 THEN
SELLSHORT 1 PERPOINT AT MARKET
ENDIF
// Conditions to exit short positions
ignored, ignored, ignored, indicator8, ignored, ignored = CALL "Kase Dev Stop Lisse+SAR+4.5/6"
c6 = (close CROSSES OVER indicator8)
IF c6 THEN
EXITSHORT AT MARKET
ENDIF
Ehlers-CTI-0.5-Dev-3.6-€-Daily-1-x-Capital-£13.5k-35.8-Win-70-GainLoss-2.2-DDown-9.8-July2015-01052020.png
Ehlers-CTI-0.5-Dev-3.6-CHF-Daily-1-x-Capital-£13.4k-34.5-Win-71-GainLoss-2.5-DDown-4.5-July2015-01052020.png
Ehlers-CTI-0.5-Dev-3.6-Y-Daily-1-x-Capital-£13.3k-33.8-Win-68-GainLoss-1.99-DDown-13.0-July2015-01052020.png
Ehlers-CTI-0.5-Dev-3.6-£-Daily-1-x-Capital-£15.3k-53.6-Win-68-GainLoss-2.16-DDown-10.2-July2015-01052020.png
//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
///////////////////////////////////////////////////////////////////////////////////////////////////
//Short
SumSx = 0 // the price curve
SumSy = 0
SumSxx = 0
SumSxy = 0
SumSyy = 0 // the trend line
data=customclose
for count = 0 to SPeriod-1 do
X = Data[count] // the price curve
Y = -count // the trend line
SumSx = SumSx + X
SumSy = SumSy + Y
SumSxx = SumSxx + X*X
SumSxy = SumSxy + X*Y
SumSyy = SumSyy + Y*Y
next
if(SPeriod*SumSxx-SumSx*SumSx > 0 and SPeriod*SumSyy-SumSy*SumSy > 0) then
CTIShort = (SPeriod*SumSxy-SumSx*SumSy)/Sqrt((SPeriod*SumSxx-SumSx*SumSx)*(SPeriod*SumSyy-SumSy*SumSy))
endif
///////////////////////////////////////////////////////////////////////////////////////////////////
//Long
SumLx = 0
SumLy = 0
SumLxx = 0
SumLxy = 0
SumLyy = 0
for count = 0 to LPeriod-1 do
X = Data[count] // the price curve
Y = -count // the trend line
SumLx = SumLx + X
SumLy = SumLy + Y
SumLxx = SumLxx + X*X
SumLxy = SumLxy + X*Y
SumLyy = SumLyy + Y*Y
next
if(LPeriod*SumLxx-SumLx*SumLx > 0 and LPeriod*SumLyy-SumLy*SumLy > 0) then
CTILong = (LPeriod*SumLxy-SumLx*SumLy)/Sqrt((LPeriod*SumLxx-SumLx*SumLx)*(LPeriod*SumLyy-SumLy*SumLy))
endif
return 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"
// 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 settings
defparam calculateonlastbars = 100
// SHORT
Sx = 0
Sy = 0
Sxx = 0
Sxy = 0
Syy = 0
data=customclose
for count = 0 to Length -1 do
X = Data[count] // the price curve
Y = -count // the trend line
Sx = Sx + X
Sy = Sy + Y
Sxx = Sxx + X*X
Sxy = Sxy + X*Y
Syy = Syy + Y*Y
next
if(Length*Sxx-Sx*Sx > 0 and Length*Syy-Sy*Sy > 0) then
ctiS= (Length*Sxy-Sx*Sy)/sqrt((Length*Sxx-Sx*Sx)*(Length*Syy-Sy*Sy))
endif
//////////////////////////////////////////////////////////////
// LONG
Sx = 0
Sy = 0
Sxx = 0
Sxy = 0
Syy = 0
data=customclose
for count = 0 to LengthL -1 do
X = Data[count] // the price curve
Y = -count // the trend line
Sx = Sx + X
Sy = Sy + Y
Sxx = Sxx + X*X
Sxy = Sxy + X*Y
Syy = Syy + Y*Y
next
if(LengthL*Sxx-Sx*Sx > 0 and LengthL*Syy-Sy*Sy > 0) then
ctiL= (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 "
Very Latest John Ehler's Correlation Trend Indicator
This topic contains 31 replies,
has 4 voices, and was last updated by supertiti
5 years, 10 months ago.
| Forum: | ProBuilder: Indicators & Custom Tools |
| Language: | English |
| Started: | 04/28/2020 |
| Status: | Active |
| Attachments: | 21 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.