Help with autocorrelation

Login or Register

Please login or register in order to view and post in the Forum.

7 posts / 0 new
Last post
galtstein
Last seen: 1 year 3 months ago
Joined: 05/02/2012 - 00:00
Help with autocorrelation

Hey Chad,

Happy holidays to you and the rest of the IRT team.

I'm trying to run an autocorrelation.

Autocorrelation is the correlation of an instrument with a delayed copy of itself.

Using the RTY, for example, I'm trying to calculate the correlation coefficient between the current bar and previous bar -- so, in this case, the "delayed copy of itself" is a 1-bar delay, or just the previous bar.

For this 1-bar delay, I created a custom indicator, which is simply: MED.1

I set a V# variable for this custom indicator and then created a custom instrument called RTY.LAG.

The next step is to run a correlation coefficient between RTY and RTY.LAG.

The problem I'm having is there's no output when I add the COCO for RTY and RTY.LAG.

I've attached the chart definition. Can you please take a look and see where I'm going wrong? Thank you!

Chart definition
=====
BEGIN Investor/RT CUSTOM INSTRUMENT DEFINITION for RTY.LAG
CUSTINST: RTY.LAG,1,31,1,User Defined Sum TYPE=SUM
COMPONENT: @RTY#,1,31,3,1,E-MINI RUSSELL 2000 MARCH 2019 MULT=1 PRICE=129 (29)
END Investor/RT CUSTOM INSTRUMENT DEFINITION for RTY.LAG
BEGIN Investor/RT CUSTOM INDICATOR DEFINITION for 1_Bar_Lag
COMMENT=NONE
SOURCE=
PLATFORM=Windows 10 (6.2, Build 9200)
DATE=2018-12-21 07:57
VERSION=13.5.12 (Build #33326 Dec 11 2018 14:42:27)
DATAFEED=IQFEED
NAME=1_Bar_Lag
BARCOUNT=AUTOMATIC
ELEM=MED:Avg. of High+Low
INDICATOR=MED.1
END Investor/RT CUSTOM INDICATOR DEFINITION for 1_Bar_Lag
BEGIN Investor/RT CHART DEFINITION for TEST_Autocorrelation
--
--To Import This Definition:
--1. Select all the text in this file.
--2. COPY to clipboard (Ctrl+C)
--3 Go to Investor/RT and PASTE (Ctrl+V)
--To learn more about importing definitions see www.linnsoft.com/importing.
--
COMMENT=NONE
SOURCE=
PLATFORM=Windows 10 (6.2, Build 9200)
DATE=2018-12-21 07:57
TIMEZONE=Mountain Standard Time (GMT - 7:00)
VERSION=13.5.12 (Build #33326 Dec 11 2018 14:42:27)
DATAFEED=IQFEED ORS=N/A
NAME=TEST_Autocorrelation SIZE=815,1491
PERIODICITY=3,1,0 (1 Minute)
PIXELS=14:0
LOOKAHEAD=0
RMARGIN=155
CFLAGS=524288 CFLAGS2=150145
VIEWPERIOD=1,1,5,3627617400,3628223940 (Last 5 hours)
SUBSESSION=0,30,6
COLORS=10526880,0,7237230,16053492,1973790,8421504,16777215,0,0
DESC=TEST_Autocorrelation: @RTY# (1m), RTY.LAG (1m) 1 Minute, Session #0 07:30-14:00
SESSION OVERRIDE=0 US Stocks: Hours: 07:30 to 14:00
TICKER=@RTY#:AUTO:31 SECTYPE=3 DISPLAY=1 ALIAS=@RTY#
-Tick Data: 5 Days, Intraday: 120 Days, Daily: 1400 Days
TICKER=RTY.LAG:AUTO:31 SECTYPE=16 DISPLAY=1 ALIAS=-
-Tick Data: 5 Days, Intraday: 120 Days, Daily: 1000 Days
PANE #1 PCT=0.236740 PFLAGS=32896 SCALE=17,1320.300000,1340.100000,0.250000,1.000000,0.000000,0,0
INSTRUMENT: @RTY#:AUTO:0 ID#1 PERIODICITY: 3,1,1 TYPE: 3,2,1 COLORS: 8454016,12632256,1,0 FLAGS: 512
VAP_FOOTPRINT: 0,0,2,0,9,0,0,100,1,0,0,4160,50,0,0.1,0,0,0,0,0,Arial,0,
SESSION #0. US Stocks: Open,UnPosted, Hours: 07:30 to 14:00
PANE #2 PCT=0.232859 PFLAGS=128 SCALE=17,1323.000000,1338.800000,0.250000,1.000000,0.000000,0,0
INDICATOR: CI #40 ASSOC: @RTY#:AUTO:0 ID#1 PERIODICITY: 3,1,1 DESC: CI:1_Bar_Lag[@RTY# (1m) 0] MED.1 LABEL: T TXTLBL: F,"AUTO",F RECALC: 0,0 PREFS: 128,255,128,2,2,0,1_Bar_Lag,0,0,3,F,15693672,3,128,128,128,0,0,0,11,70,F,0,F,50,F,F,13001300,1,0,2,7,T,F,0,0,255,1,2,0,33488896,33619712,21672,4.000000,8.000000,12.000000,1,1,F,T,F,F,0,F,F,1020,405,A1,F,
PANE #3 PCT=0.232859 PFLAGS=32896 SCALE=17,1322.600000,1335.600000,0.250000,1.000000,0.000000,0,0
INSTRUMENT: RTY.LAG:AUTO:0 ID#2 PERIODICITY: 3,1,2147483648,0,1,1 TYPE: 3,2,1 COLORS: 8454016,12632256,1,0 FLAGS: 256
VAP_FOOTPRINT: 0,0,0,0,0,0,0,0,0,0,0,4160,0,0,0.1,0,0,0,0,0,\0,0,
SESSION #0. US Stocks: Open,UnPosted, Hours: 07:30 to 14:00
PANE #4 PCT=0.298836 PFLAGS=0 SCALE=17,0.000000,0.000000,0.000000,0.000000,0.000000,0,0
INDICATOR: COCO #79 ASSOC: @RTY#:AUTO:0 ID#1 PERIODICITY: 3,1,1 DESC: COCO[@RTY# (1m) 0] RTY.LAG, M20 LABEL: T TXTLBL: F,"AUTO",F RECALC: 0,0 PREFS: 153,204,255,2,2,0,5,20,3,3061238400,F,RTY.LAG,1,31,
- This chart definition references 1 RTL object (scan/signal/custom indicator)
END Investor/RT CHART DEFINITION for TEST_Autocorrelation

=====

For anybody following along, I've seen this autocorrelation used to define the market mode and help determine if the market is in a mean-reverting/cycling/range bound mode OR if it's in a trending mode.

The general application is:

1 - Calculate autocorrelation
2 - Define the period/frequency (I'm just using a 1-minute as an example)
3 - Define the look-back period (I'm just using the 20-bar default, but lots of room to experiment with this look-back period)
4 - Define a threshold where, if autocorrelation is above, then the market is in trend mode; or, if autocorrelation is below threshold, then market is in mean-reverting/cycling/range-bound mode.

=====

In the chart below, the top pane is @RTY

Second pane is custom indicator: MED.1

Third pane is custom instrument: RTY.LAG (which is just MED.1)

Fourth pane is COCO between @RTY and RTY.LAG -- no output

0
galtstein
Last seen: 1 year 3 months ago
Joined: 05/02/2012 - 00:00
Hey Chad, disregard this.

Hey Chad, disregard this. There's an easier way.

For anyone following along, there's a simpler way to do this with the Two Array Statistic.

Just create a Custom Indicator for current price and another Custom Indicator for a 1-bar lag. Then run a Correlation. Easy peasy.

mitchpa
Last seen: 4 years 10 months ago
Joined: 02/05/2019 - 23:19
Python code

I have a bit of python code I run in the mornings that ranks forex markets based on hourly mean reversion for the last (x) number of days, uses something called the hurst exponent. You could tweak this to look at however many candles.. Makes it easy to do a batch scan rather than individually look at instruments

Only problem is that current mean reversion isn't always indicative of future mean reversion : )

mitchpa
Last seen: 4 years 10 months ago
Joined: 02/05/2019 - 23:19
Stats

I'd be very interested in stats that look at time % spent trending vs. % time spent reverting to the mean

galtstein
Last seen: 1 year 3 months ago
Joined: 05/02/2012 - 00:00
I'd love to see those stats.

I'd love to see those stats. I'd never really thought about measuring trending vs. mean reverting in terms of time.

I was experimenting with autocorrelation as a tool to identify mean reverting and trending modes. I was trying to define when a market would exit mean reverting mode and enter trend mode. And do it in a way that is fast and accurate. This would be super helpful to a) hold a position if it's in the direction of the new trend b) avoid fighting the trend (i.e. STOP mean reverting trading).

Autocorrelation is fast/timely, but I could never find a way to make it work for me to define trend vs. mean reverting.

I've fine-tuned the BandPass indicator in IRT and have gotten it to work really well when a market is in mean reverting mode. It does a good job of accurately and quickly predicting highs and lows. But when the market starts to trend, it falls apart. So, how do you know when the market starts to trend? That's what I was trying to answer with autocorrelation, but haven't come up with anything useful.

mitchpa
Last seen: 4 years 10 months ago
Joined: 02/05/2019 - 23:19
Hurst

Hurst exponent definition is quite simple:. <0.5 = mean reverting, >0.5 = trending..

Right now I ask the code - Am I mean reverting in relation to the past 10 days..? With some decent programming skills (something I lack) you could scan today's hurst, then yesterday's hurst, then yesterday - 1's hurst etc and use that data to plot a study of average days spent <0.5 until a shift to >0.5

galtstein
Last seen: 1 year 3 months ago
Joined: 05/02/2012 - 00:00
Do you apply this on an

Do you apply this on an intraday basis? Or do you apply to longer timeframes like a multi-day, swing trade?

I'm all intraday and I couldn't figure out a way to make the autocorrelation work at the higher frequencies. That said, I've seen some effective implementations of autocorrelation applied to longer time frames and it does a good job of defining trend vs. mean-reverting over weeks and months, even years.

I've studied the Hurst a little bit, but will give it a closer look. Thank you for the inspiration!