Login Page - Create Account

Support Board


Date/Time: Fri, 07 Feb 2025 22:48:10 +0000



Post From: New Study - Rescaled Range - Hurst Exponent

[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