Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Technical Indicators in Python

Hello,

Because I couldn't make TA-Lib work I decided to create a Technical Indicators Python module and test my newbie Python skills.

You can find it here:
http://sourceforge.net/projects/py-tech-ind/
or
https://github.com/jcrmatos/technical_indicators

For now there are RSI, SMA, EMA, BB, Bollinger bandwidth and %B.
When I can I will add more.
If anyone wishes to contribute with new code or corrections/suggestions, feel free.

Best regards,

JM

4 responses

Zipline (the Python library that Quantopian is built on) has support for TA-Lib now. Not sure if the support is in Quantopian yet, but if not it will be soon. Here's an example of using TA-Lib in Zipline:

https://github.com/quantopian/zipline/blob/master/zipline/examples/dual_ema_talib.py

Hi Joao and Ben, TA-Lib support just landed in Quantopian: https://www.quantopian.com/posts/new-feature-ta-lib-support

Hope that helps.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Readable code Joao and your RSI vals close to TradingView.
Thx I appreciate the fact that you posted it.

Hi,
I know this is a bit old or not needed (due to TA-Lib) now, but I think the RSI calculation in the code given at the top is not correct. For anybody who's still interested...

The correct answer to the example in the comment should be:
[ 70.02141328 69.57446809 69.37901499 80.26607539 73.39901478 59.90338164 62.61261261 60. 48.47775176 53.87840671
48.95238095 43.86281588 37.6744186 32.20910624 32.66331658
38.0794702 31.70391061 25.066313 30.17902813]

And the modified RSI function to produce above result is:

def rsi(prices, period=14):

num_prices = len(prices)

if num_prices < period:  
    # show error message  
    raise SystemExit('Error: num_prices < period')

# this could be named gains/losses to save time/memory in the future  
changes = prices[1:] - prices[:-1]  
#num_changes = len(changes)

rsi_range = num_prices - period

rsis = np.zeros(rsi_range)

gains = np.array(changes)  
# assign 0 to all negative values  
masked_gains = gains < 0  
gains[masked_gains] = 0

losses = np.array(changes)  
# assign 0 to all positive values  
masked_losses = losses > 0  
losses[masked_losses] = 0

for idx in range(0, rsi_range):  

    avg_gain = np.mean(gains[idx:(idx+period)])  
    avg_loss = np.mean(losses[idx:(idx+period)])

    if avg_loss == 0:  
        rsis[idx] = 100  
    else:  
        rs = avg_gain / (-avg_loss)  
        rsis[idx] = 100 - (100 / (1 + rs))

return rsis