pythongoogle-colaboratoryta-lib

Using Ta-lib in Google Colab


I use this code in Google Colab:

import pandas as pd
import yfinance as yf
import talib as ta

data = yf.download("GOOG")
data['RSI'] = ta.RSI(data["Close"], timeperiod=10)

And this is the output:

[*********************100%***********************]  1 of 1 completed
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-e3c5cad38926> in <cell line: 6>()
      4 
      5 data = yf.download("GOOG")
----> 6 data['RSI'] = ta.RSI(data["Close"], timeperiod=10)

/usr/local/lib/python3.10/dist-packages/talib/__init__.py in wrapper(*args, **kwargs)
     25 
     26             if index is None:
---> 27                 return func(*args, **kwargs)
     28 
     29             # Use Series' float64 values if pandas, else use values as passed

TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got DataFrame)

What is wrong with my code? I have used ta-lib before. But now I can't use it. Why?


Solution

    1. first I attempted by data["Close"].to_numpy(): This line converts the Series data["Close"] to a array using the .to_numpy() method. This provides the correct input type for the ta.RSI function.
    # Convert the Pandas Series to a NumPy array before passing it to ta.RSI
    data['RSI'] = ta.RSI(data["Close"].to_numpy(), timeperiod=10)
    

    However, I faced:

    Exception: input array has wrong dimensions

    1. I've added .flatten() to the array obtained from data["Close"].values. This ensures the array is strictly 1-dimensional, even if it was initially shaped as (n, 1) or any other shape that is not a simple 1D array. This should resolve the "wrong dimensions" error by providing the expected input format to the ta.RSI function.
    import pandas as pd
    import yfinance as yf
    import talib as ta
    import numpy as np      # Import numpy
    
    data = yf.download("GOOG")
    
    # Convert the Pandas Series to a NumPy array and ensure it's 1-dimensional before passing it to ta.RSI
    # Use .values to get the underlying NumPy array and flatten to ensure it's 1D
    
    
    # Creating Technical Indicators using Ta-Lib (RSI)
    data['RSI'] = ta.RSI(data["Close"].values.flatten(), timeperiod=10) 
    # [*********************100%***********************]  1 of 1 completed
    

    Let's plot created technical indicators by Relative Strength Index (RSI):

    import matplotlib.pyplot as plt
    
    data['RSI'].plot(figsize=(20,8),marker='x', label='RSI', alpha=0.5)
    plt.legend()
    plt.title('Relative Strength Index (RSI)', size=20)
    plt.show()
    

    img


    Installation of package on GoogleColab medium:

    !wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
    !tar -xzvf ta-lib-0.4.0-src.tar.gz
    %cd ta-lib
    !./configure --prefix=/usr
    !make
    !make install
    !pip install Ta-Lib
    
    # Verify the installation: Check if TA-Lib is actually installed and print the version.
    !pip show TA-Lib  
    import talib
    print(talib.__version__)