Login Page - Create Account

Support Board


Date/Time: Fri, 03 May 2024 13:59:19 +0000



New Study - Rescaled Range - Hurst Exponent

View Count: 5760

[2017-02-21 02:11:54]
User71961 - Posts: 144
Could this study be added to the list of basic studies? This is a simple method for calcing the Rescaled Range, and thus, the Hurst Exponent...which is a better type of RSI.

https://en.wikipedia.org/wiki/Rescaled_range

Rescaled Range = R/S for a timeseries
When you divide the range (R = Max-Min) by the standard deviation (S) of the input data, that is "rescaling" the range.

If you do a linear regression of the X*Y plot Table
Y = Log(ReScaled Range) = Log(R/S)
X = Log(n) (n is a counter indicating the order of the input bars...for the first observation, n=1 and if there are 20 observations, then the last observation has n=20)
The slope of the regression line of this plot is the Hurst Exponent, and can be plotted over time as a type of RSI indicator.

Variables for the study would be
Length = an integer. This is the number of bars used to calc the statistic, similar to a moving average. (default to 20)
Input Data = {Open, Close, High, Low, OHLC, HLC, etc...}
[2017-02-21 02:18:43]
Sierra Chart Engineering - Posts: 104368
OK we are looking this over. Thank you.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2017-02-23 22:53:26]
User71961 - Posts: 144
i have made an excel spreadsheet that breaks down the "Rescaled Range - Hurst Exponent" study, to make it easier to understand (also shows how to de-trend the dataseries, which is necessary and not broken down in the wikipedia page).

I also noticed that there is an additional parameter that needs to be set, called "Lag"

Lag indicates which Slope (Hurst) value is used to calculate the statistic (so, which of the orange cells) for the entire set of input data, defining the size of the internal rolling data sample, within the larger sample. The various options are highlighted in the attached spreadsheet. (Lag and the corresponding Slope/Hurst Exponent are next to each other).

By definition, "Lag" needs to be greater than 2, and smaller than n, where n = Length = number of input bars used for the study. For best results, Lag = 20% of Length (So, if Length defaults to 20, then Lag should default to 4)


https://drive.google.com/open?id=0B21lW36D6iHnYkFiLXZuSmlRRGc
[2017-02-24 04:09:22]
Sierra Chart Engineering - Posts: 104368
Thank you for this.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2017-03-08 01:49:33]
User71961 - Posts: 144
i've written the code for this in Excel VBA.
Its fine for small number of calcs...but takes a long time for large amounts, like 10,000. I'm sure C++ would be faster

Also, after some thought, i've split the Lag input into 2 (Min Lag, Max Lag), as i think this makes sense.

I don't know C++, which is why i've written this in VBA...hopefully, this should be easy for you guys to implement in C++ for SierraChart as another standard study.

here is the VBA code (InputData is just a column of prices)

Option Base 1 'all arrays default to start at 1, instead of 0

Public Function Hurst(MinLag As Integer, MaxLag As Integer, InputData As Range) As Double
Dim OneBarChanges() As Double
Dim dAvgChange As Double
Dim DetrendedChanges() As Double
Dim DeTrendedTimeSeries() As Double

Dim iRowCount As Integer
Dim iRollingRowCount As Integer
Dim iLagColCount As Integer
Dim iLagCol As Integer
Dim iRow As Integer
Dim iDataRow As Integer
Dim iDataStartRow As Integer
Dim iDataEndRow As Integer
Dim dTotalRange As Double
Dim dTotalSTDev As Double
Dim dAvgRange As Double
Dim dAvgSTDev As Double

Dim LogN() As Double
Dim AvgR() As Double
Dim AvgS() As Double
Dim LogAvg_R_S() As Double
Dim R_Table() As Double
Dim S_Table() As Double
Dim RollingDataGroup() As Double



'get dimension of inputdata, and create arrays
iRowCount = InputData.Rows.Count
dAvgChange = (InputData(iRowCount, 1).Value - InputData(1, 1).Value) / iRowCount

ReDim OneBarChanges(iRowCount)
ReDim DetrendedChanges(iRowCount)
ReDim DeTrendedTimeSeries(iRowCount)
ReDim AvgR(iRowCount)
ReDim AvgS(iRowCount)
ReDim R_Table(iRowCount, MaxLag)
ReDim S_Table(iRowCount, MaxLag)

ReDim LogN(MaxLag - MinLag + 1)
ReDim LogAvg_R_S(MaxLag - MinLag + 1)

DeTrendedTimeSeries(1) = InputData(1, 1).Value


For iDataRow = 2 To iRowCount
OneBarChanges(iDataRow) = InputData(iDataRow, 1).Value - InputData(iDataRow - 1, 1).Value
DetrendedChanges(iDataRow) = OneBarChanges(iDataRow) - dAvgChange
DeTrendedTimeSeries(iDataRow) = DeTrendedTimeSeries(iDataRow - 1) + DetrendedChanges(iDataRow)
Next iDataRow

iLagColCount = 1

'create the staircase tables
For iLagCol = MinLag To MaxLag

ReDim RollingDataGroup(iLagCol)
dTotalRange = 0
dTotalSTDev = 0
iDataEndRow = iRowCount - iLagCol + 1

For iDataStartRow = 1 To iDataEndRow

iRollingRowCount = 1
For iRow = iDataStartRow To iDataStartRow + iLagCol - 1
RollingDataGroup(iRollingRowCount) = DeTrendedTimeSeries(iRow)
iRollingRowCount = iRollingRowCount + 1
Next iRow

R_Table(iDataStartRow, iLagCol) = Application.WorksheetFunction.Max(RollingDataGroup) - Application.WorksheetFunction.Min(RollingDataGroup)
S_Table(iDataStartRow, iLagCol) = Application.WorksheetFunction.StDev(RollingDataGroup)

dTotalRange = dTotalRange + R_Table(iDataStartRow, iLagCol)
dTotalSTDev = dTotalSTDev + S_Table(iDataStartRow, iLagCol)
Next iDataStartRow

AvgR(iLagCol) = dTotalRange / iDataEndRow
AvgS(iLagCol) = dTotalSTDev / iDataEndRow

LogAvg_R_S(iLagColCount) = Log(AvgR(iLagCol) / AvgS(iLagCol))
LogN(iLagColCount) = Log(iLagCol)

iLagColCount = iLagColCount + 1

Next iLagCol

'inputs assembled....now return slope of Log(R/S) vs Log(N) = Hurst Exponent
Hurst = Application.WorksheetFunction.Slope(LogAvg_R_S, LogN)
End Function
[2017-03-08 02:06:44]
User71961 - Posts: 144
an example of inputs might be

input length: 15 bars
input data: Last Price
Min Lag: 3
Max Lag: 5

By definition, MaxLag must be greater than MinLag.
[2017-03-08 05:34:04]
Sierra Chart Engineering - Posts: 104368
We are already working on our own implementation of this study.

It should be ready by tomorrow.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-03-08 05:34:14
[2017-03-13 16:16:47]
User71961 - Posts: 144
i've looked at the Hurst study that you implemented, however, there seems to be some errors...this does not display the Hurst Exponent (i'm not sure what this Hurst study is showing).

The Hurst Exponent shows the logarithmic relationship between the avg rescaled range, and the avg st.dev of the data creating that range, over time, vs the log of the number of observations, for a chosen set of "Lag Periods". A set of data will have multiple Hurst exponents, depending on the Lag values chosen (just like how RSI and Bollinger Bands depend on the input values chosen).

The Hurst exponent, being the avg slope of that logarithmic relationship, should be between 0-1.

1 = the series has internal trend persistence
0.5 = Random Brownian motion
0 = the series has internal mean reversion

(you would expect that for most market data, the Hurst value averages 0.5 = random Brownian motion)

(1) For this study to have value in trading, its important for the study to be calced like a moving average.

(2) I also noticed that MinLag and Max Lag were not optional parameters to be set in the study. These are necessary for the study to have value in Trading.

The Visual Basic code above is the "correct" calculation, which you can use as a benchmark (not for speed...but for accuracy).
To test the VB code, i just copied chart data from SierraChart, pasted into a new excel, copy/pasted the VB code into a VBA code module, and then use the =Hurst function in the excel worksheet to get the values. This is just for testing purposes, to check for the proper calculation...its fine for calcing 20-50 values....but takes a long time to calc thousands (the main speed culprit is the VBA platform itself vs the code....VBA is a very slow processing engine).
[2017-03-13 17:34:37]
Sierra Chart Engineering - Posts: 104368
We will look this over in more detail and we will also add a new Input to the study to have it work in a moving mode where it calculates across a specified Length at each chart bar.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2017-03-20 16:14:40]
SC Support Tom - Posts: 450
Hello Steven. I have been working from your spreadsheet, and I have written a C++ code that duplicates the calculations contained therein. The next step is for me to modify the code so that it produces a moving calculation over a specified length. The only thing I need to know in order to proceed is how the Lag (Column K) fits into the calculation. It does not appear that you used it anywhere. I do not know VBA well enough to understand the code sample that you posted, so it would be best if you could tell me which columns should be affected by the Lag, and how they should be affected.
Date Time Of Last Edit: 2017-03-20 16:15:20
[2017-03-21 20:02:54]
User71961 - Posts: 144
that Lag column was a description of dataset used to calculate the slope column.

In the spreadsheet, the Slope(Hurst) column (col J), only one of those values should be chosen for the data set of 20 prices (in theory).
Kind of like saying, "which moving average should we look at?" This is to be determined by the user, and this is why we make InputLength, MinLag, MaxLag as inputs.
To be clear, the way that spreadsheet was built, MinLag was "hard coded" to = 3, but of course, this should be a variable for the user to choose when the solution is built with code (and its pretty easy to do...you can see the logic in the above VB code...its just a set of nested loops...just make sure to keep the various loop counters in line...i tried to name them meaningfully, instead of just using i, j, k etc..)

N and Lag have the same meaning in this sheet...but since "N" is used by lots of people to mean lots of things...we use "Lag" to give it some meaning here. N=Lag=Rolling Window Size

However, Lag does have meaning. A dataset first has a number of input values. In this case, there are 20 prices. To analyze this input data using Hurst, we are taking a rolling window of prices, within the larger dataset, and looking at how the data changes as the rolling window increases. So, lets say, with an input length of 20 (using standard sierra chart terminology, we have 20 prices), i might set the min lag to 3 and the max lag to 6. What do these lags mean? They are the size of the rolling windows, within the larger dataset, that we use to analyse the entire dataset.

In the tables over to the right, the staircase columns whose rolling ranges are being analyzed. So, Column O (Lag3) is looking at a rolling window of 3 values...so that col is looking at Lag=3 (RollingWindowSize=3) The values in that column are then averaged at the bottom. So, for a Lag (or, rolling window) of 3, we get an avg range of the rolling windows of 3 contiguous prices, and the avg standard deviation of those same sets of 3 prices, for the entire dataset defined by the input length (in this case, 20 prices). We do that again in cols P-->R, but with a different Lag (rolling window size). As the Lag increases (or, as the rolling window gets bigger), we expect the relationship (slope) between Log(avgRange/avgStDev) and Log(RollingWindowSize) to meaningfully define the data. The slope of the plot of Y-Log(R/S) vs X-Log(RollingWindowSize) is the "Hurst Exponent"....using the "rescaled range method".

We want to look at this indicator (Hurst) as a moving average, because trends in the data will come and go. Hurst picks up on whether the data is "trending" "mean reverting" or "random" by the slope of the Log(R/S) vs Log(Lag)...so intuitively, if the range is increasing faster then the stdev, as the rolling window size increases, then the slope will be larger and approach 1.
If the range vs its standard deviation is not increasing as the rolling window size increases, then hurst would define the data as "mean reverting" and slope would approach 0.
If the Data is "random" then the slope of this relationship will approach 0.5

Data in the natural world (in this case, financial price data) will form trends ephemerally (they come and go)....so this is why we want to calc and look at hurst like a moving average. I expect the Hurst value to spike when it picks up a trend...but i also expect those trends to be "short lived", which is why Hurst is kindof like an RSI indicator. Since financial data is not perfect (compared to other data in the natural world like climate data) this method will miss certain trends, and over-report others. Its just one more thing to look at in combo with other indicators.

I hope this helps..and if not, i'd be glad to answer any other questions you might have.

Regarding VB or VBA, i think the code just has a bunch of For / Next Loops, and variables & arrays storing data doing simple sum/avg math (in VB you can "resize" an array using the "redim" statement). There are no other complicated things going on...its just keeping the loops and various loop counters straight. Indenting the code properly will help.

for example
"Dim LogN() As Double" is creating an array variable "LogN" with unknown dimensions (). Those dimensions are set later in the code with a redim statement
InputData is an Excel Range object, but we are just using it as holding a column of data.

There might be a couple extra variables that are assigned data in a loop that are a waste, because that value just gets passed on to another variable immediately afterwards. I think there are a couple of those. I was looking at the step tables in the spreadsheet when i wrote the code, so i recreated everything i saw in the sheet...but there was some repetition that i now realize was a waste. Its not so bad..probably 4-5 lines of variable assignment code could be reduced to 1-2 in a couple of the loops...something like that.
Date Time Of Last Edit: 2017-03-21 20:06:25
[2017-03-22 01:52:39]
User71961 - Posts: 144
regarding the "moving average" aspect of this study...if the input length is 20, then the last 20 price bars = the complete input data set needed to calculate the most recent value of the study. Then, going back 1 bar in time, those 20 price bars = the complete input data set needed to calculate the next value of the study.
So, if there are 100 bars of data in total, and input length=20, then first you use bars 1-->20, then 2-->21, then 3-->22, 4-->23, 5-->24, 6-->25, etc....all the way to 81-->100.
Each rolling set of 20 bars has its own hurst exponent (tells us how trending or mean reverting are those 20 bars)...and we can watch the hurst exponent change over time, just like a simple moving average changes over time.

I expect the hurst study to spend the majority of its time around 0.5 which indicates random brownian motion. However, when we get extreme readings (close to either 0 or 1), that should indicate market instability and transition changes.
[2017-03-22 17:37:57]
Sierra Chart Engineering - Posts: 104368
Thank you. We are looking this over. We will let you know if we have any questions.

Fully understood what you described in post #12.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-03-22 17:39:21
[2017-03-23 16:37:02]
SC Support Tom - Posts: 450
Hi again Steven. Please be advised that I am working exclusively from the spreadsheet, and not the VBA code that you provided. Once I get how MinLag and MaxLag fit into the spreadsheet calculation, I will be able to change my C++ program to a moving calculation with Length, MinLag, and MaxLag as Inputs.

So if I understand Post #11 correctly, in the spreadsheet MinLag = 3 (because the Hurst Exponent calculations begin with n = 3 in Column J), and MaxLag = 7 (because the Hurst Exponent calculations end with n = 7 in Column J). If that is correct, then I can finish coding this in C++.
Date Time Of Last Edit: 2017-03-23 16:37:50
[2017-03-23 16:52:48]
User71961 - Posts: 144
my apologies for my typo earlier...in the spreadsheet, min lag is hard-coded to =2 (not 3), and there are 5 different Hurst values in col J, each with the following Settings

Min Lag=2, Mag Lag=3, Input Length=20
Min Lag=2, Mag Lag=4, Input Length=20
Min Lag=2, Mag Lag=5, Input Length=20
Min Lag=2, Mag Lag=6, Input Length=20
Min Lag=2, Mag Lag=7, Input Length=20

This would be the equivalent of adding the Hurst Study 5 times to a chart, each with the different Settings.
[2017-03-23 23:22:42]
SC Support Tom - Posts: 450
Thank you, this is helpful. I now understand how these inputs should be used.
[2017-05-22 20:50:50]
Sierra Chart Engineering - Posts: 104368
We have completed the development of the Hurst Exponent study. It took a long time because there were many variations of it available. We did some research and this is the one we finally decided on:
https://en.wikipedia.org/wiki/Hurst_exponent

This implementation executes very fast.

We are preparing detailed documentation for it in the Technical Studies Reference and that should be out in the next day or two. The study is available in the latest revision in version 1559.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2020-06-15 01:26:28]
Sierra Chart Engineering - Posts: 104368
This is billable time. You have to be willing to pay for this.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account