talib.BETA
Calculates the Beta.
talib.BETA(inReal0, inReal1)
talib.BETA(inReal0, inReal1, optInTimePeriod)Examples
Compute the beta of the second exchange's symbol against the first one (the benchmark).
javascript
function main() {
// exchanges[0] is the benchmark, exchanges[1] is the instrument whose beta is calculated
var r0 = exchanges[0].GetRecords()
var r1 = exchanges[1].GetRecords()
if (!r0 || !r1) return
var n = Math.min(r0.length, r1.length)
var c0 = r0.slice(-n).map(function(r) { return r.Close })
var c1 = r1.slice(-n).map(function(r) { return r.Close })
var ret = talib.BETA(c0, c1, 5)
Log(ret)
}
python
import talib
def main():
# exchanges[0] is the benchmark, exchanges[1] is the instrument whose beta is calculated
r0 = exchanges[0].GetRecords()
r1 = exchanges[1].GetRecords()
if not r0 or not r1:
return
n = min(len(r0), len(r1))
ret = talib.BETA(r0.Close[-n:], r1.Close[-n:], 5)
Log(ret)Returns
| Type | Description |
numeric array | Indicator values, the same length as the input; the leading values without enough data are |
Arguments
| Name | Type | Required | Description |
inReal0 | numeric array | Yes | First price series (the benchmark). |
inReal1 | numeric array | Yes | Second price series; it must have the same length as |
optInTimePeriod | number | No | Calculation period. Default |
See Also
Remarks
TA-Lib equivalent: BETA(Array(inReal0),Array(inReal1),Time Period = 5) = Array(outReal).
Takes the bar-by-bar rate of change of inReal0 as the independent variable and that of inReal1 as the dependent variable, runs a linear regression over the latest optInTimePeriod changes and returns the slope. To get the beta of an instrument against a benchmark, pass the benchmark prices as inReal0 and the instrument prices as inReal1.
In JavaScript, both inputs must be numeric arrays (extract a field from the K-line data with map first): if a K-line array is passed, both inputs take the close price of the same K-line data and the result is meaningless; arrays of different lengths raise an error.