Live Chat Software by Kayako
Knowledgebase
Linear Regression

Overview
Linear regression is a common statistical method used to forecast values using least squares fit.

Interpretation
The TA LinearRegression class contains two functions. The base Function: “Regression” returns a Recordset object containing four Field objects: “R-Squared”, “Forecast”, “Slope” and “Intercept”
Where R-Squared is the coefficient of determination, Forecast is the linear regression forecasted value for the next period, Slope is the slope value for the periods being analyzed and Intercept is the end period’s Y intercept value.

Class:
LinearRegression

Syntax

 Public function RegressionSlop(ByRef Data As Database, ByRef Source As Field, ByVal Periods As Integer) As RecordSet

 Return Type Returns object of type Recordset Default Field Name(s) RSquared, Forecast, Slope, Intercept

Sample

 Public Sub main()         'Variables         Dim _symbolInfo As VTLGeneral.CSymbol=ClientCode.GetSymbolByName("GOLD")         Dim DB As New VTLGeneral.Database()         Dim RecordCount As Integer         Dim m_Recordset As VTLGeneral.RecordSet         Dim _historyData As object()         Dim output As String         Dim Record As Integer         Dim m_Date As VTLGeneral.Field         Dim m_Open As VTLGeneral.Field         Dim m_High As VTLGeneral.Field         Dim m_Low As VTLGeneral.Field         Dim m_Close As VTLGeneral.Field         Dim j As Integer = 0         Dim i As Integer = 0         Dim _recordCount As Integer =ClientCode.Bars(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day)                 m_Recordset = DB.CreateRecord         m_Open = New VTLGeneral.Field         m_High = New VTLGeneral.Field         m_Low = New VTLGeneral.Field         m_Close = New VTLGeneral.Field          DB.RecordCount = _recordCount         RecordCount = _recordCount          'Initialize Recordsets         m_Open.initialize(_recordCount-1, "Open")         m_High.initialize(_recordCount-1, "High")         m_Low.initialize(_recordCount-1, "Low")         m_Close.initialize(_recordCount-1, "Close")         'load high, low ,open and data          _historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY_TYPE.HIS_HIGH, _recordCount)           For i  = 0 To _recordCount-1                 m_High.setValue(i,_historyData(i))          Next          _historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY_TYPE.HIS_LOW, _recordCount)          For i  = 0 To _recordCount-1                 m_Low.setValue(i,_historyData(i))         Next          _historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY_TYPE.HIS_OPEN, _recordCount)          For i  = 0 To _recordCount-1                 m_Open.setValue(i,_historyData(i))         Next          _historyData = ClientCode.GetChartHistory(_symbolInfo.ID, VTLGeneral.ENUM_PERIOD.Day,VTLGeneral.ENUM_HISTORY_TYPE.HIS_CLOSE, _recordCount)          For i  = 0 To _recordCount-1                 m_Close.setValue(i,_historyData(i))         Next           m_Recordset.addField(m_Open)         m_Recordset.addField(m_High)         m_Recordset.addField(m_Low)         m_Recordset.addField(m_Close)          'Regression indicator         Dim _indRecord As New  VTLGeneral.RecordSet()         Dim lr As New VTLGeneral.LinearRegression()         _indRecord = lr.RegressionSLope(DB,m_High,14)          For i = 0 To DB.getRecordCount-1                 output = output  &  CSTR(_indRecord.getValue("Forecast", i) ) & vbcrlf         Next          GUI.MsgDialog(output)   End Sub