TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.49k stars 1.74k forks source link

beta function with high low price series or is this a typo? #486

Closed stucash closed 1 month ago

stucash commented 2 years ago

Thanks for the great work!

I noticed that BETA function on your homepage comes with args of high and low, what do they refer to? CAPM beta normally goes with two different stocks' adjusted close or close price series.

Thank you!

mrjbq7 commented 2 years ago

You can read about it here:

https://github.com/TA-Lib/ta-lib/blob/master/src/ta_func/ta_BETA.c#L207

   /** DESCRIPTION OF ALGORITHM:
    *   The Beta 'algorithm' is a measure of a stocks volatility vs from index. The stock prices
    *   are given in inReal0 and the index prices are give in inReal1. The size of these vectors
    *   should be equal. The algorithm is to calculate the change between prices in both vectors
    *   and then 'plot' these changes are points in the Euclidean plane. The x value of the point
    *   is market return and the y value is the security return. The beta value is the slope of a
    *   linear regression through these points. A beta of 1 is simple the line y=x, so the stock
    *   varies percisely with the market. A beta of less than one means the stock varies less than
    *   the market and a beta of more than one means the stock varies more than market. A related
    *   value is the Alpha value (see TA_ALPHA) which is the Y-intercept of the same linear regression.
    */
mrjbq7 commented 2 years ago

We call them real0 and real1:

Help on function BETA in module talib._ta_lib:

BETA(...)
    BETA(real0, real1[, timeperiod=?])

    Beta (Statistic Functions)

    Inputs:
        real0: (any ndarray)
        real1: (any ndarray)
    Parameters:
        timeperiod: 5
    Outputs:
        real
mrjbq7 commented 2 years ago

Looks like a bug in the docs, for some reason it says high/low.

stucash commented 2 years ago

that's what I read in code as well (when I ran into an exception with the args passed in). Thanks a lot!

mpaccione commented 2 years ago

@mrjbq7 Hi John I agree that the docs need to be updated. Unfortunately I believe I have the right setup but am getting nan's as the output.

# get beta weight
@app.route('/dev/api/equity/beta', methods=["POST"])
@cross_origin(origin="*")
def get_beta():
    req = request.get_json()
    timeperiod = 5 # default 5
    indices = ['^GSPC','^DJI','^IXIC']
    named_indices = ['SPY500', 'DJI', 'NASDAQ']

    end_date = date.today()
    start_date = end_date - timedelta(days=timeperiod)
    req_prices = si.get_data(req['symbol'], start_date, end_date)
    arr = []
    i = 0

    for index in indices:
        index_prices = si.get_data(index, start_date, end_date)
        index_prices_array = index_prices['adjclose'].to_numpy()
        req_prices_array = req_prices['adjclose'].to_numpy()
        beta = talib.BETA(req_prices_array, index_prices_array, timeperiod)
        betaObj = {}
        betaObj[named_indices[i]] = beta[-1]
        arr.append(betaObj)
        i += 1

    return jsonify(arr)
mrjbq7 commented 2 years ago

Do you have an example?

NaN in the beginning are required by look back period, NaN in the middle are because your data has NaN in the middle.

On Jan 16, 2022, at 10:26 AM, Michael Paccione @.***> wrote:

 @mrjbq7 Hi John I agree that the docs need to be updated. Unfortunately I believe I have the right setup but am getting nan's as the output.

get beta weight

@app.route('/dev/api/equity/beta', methods=["POST"]) @cross_origin(origin="*") def get_beta(): req = request.get_json() timeperiod = 5 # default 5 indices = ['^GSPC','^DJI','^IXIC'] named_indices = ['SPY500', 'DJI', 'NASDAQ']

end_date = date.today()
start_date = end_date - timedelta(days=timeperiod)
req_prices = si.get_data(req['symbol'], start_date, end_date)
arr = []
i = 0

for index in indices:
    index_prices = si.get_data(index, start_date, end_date)
    index_prices_array = index_prices['adjclose'].to_numpy()
    req_prices_array = req_prices['adjclose'].to_numpy()
    beta = talib.BETA(req_prices_array, index_prices_array, timeperiod)
    betaObj = {}
    betaObj[named_indices[i]] = beta[-1]
    arr.append(betaObj)
    i += 1

return jsonify(arr)
```

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you were mentioned.

mpaccione commented 2 years ago

I believe it was a time period problem. I don't understand what the timeperiod param is in talib. Is this supposed to be days? The higher the timeperiod I put in the more nans I receive in the function.

alexandercommon commented 1 month ago

This is a ridiculous error, and should be fixed. High and low clearly make no sense here. It seems likely the author copied and pasted the correl definition and simply changed the name to beta.

mrjbq7 commented 1 month ago

I reviewed the docs and updated the web pages, the function docstrings all were fine.