Apoorv Kalra
Apoorv Kalra

Reputation: 54

How to build aroon indicator with Python pandas

I am trying to make aroon indicator in python using pandas. However I am getting wrong values... could anyone help as to point where I am going wrong...

import pandas as pd
import Bitmex_OHLC
import numpy as np
import importlib

def aroon():
    importlib.reload(Bitmex_OHLC)
    df_aroon = Bitmex_OHLC.OHLC()
    df_aroon['14L_min'] = df_aroon['low'].rolling(window=14,min_periods=0).min()
    df_aroon['14H_max'] = df_aroon['high'].rolling(window=14,min_periods = 0).max()
    df_aroon['ind'] = range(0,len(df_aroon))
    # recent_high = df_aroon.iloc[-1]["25d High"]
    df_aroon['high_ind'] = df_aroon['ind'].where(df_aroon["14H_max"]==df_aroon['high']).fillna(method = 'ffill')
    df_aroon['low_ind'] = df_aroon['ind'].where(df_aroon["14L_min"] == df_aroon['low']).fillna(method = 'ffill')
    df_aroon['since_high'] = df_aroon['ind']-df_aroon['high_ind']
    df_aroon['since_low'] = df_aroon['ind'] - df_aroon['low_ind']
    df_aroon['up'] = (((14 - df_aroon['since_high'])/14) *100)
    df_aroon['down'] = (((14 - df_aroon['since_low']) / 14) * 100)
    return (df_aroon)

print(aroon().tail())

The value of (down) column should have been always positive and (since_low) column should have been less than 14.

Any help will be appreciated.. Thanx

https://dpaste.de/kJJW Error enter image description here code enter image description here

Upvotes: 1

Views: 4664

Answers (3)

theAfricanQuant
theAfricanQuant

Reputation: 63

I was looking for the same indicator too then found this piece of code that I modified

def aroon(data, lb=25):

    df = data.copy()
    df['up'] = 100 * df.High.rolling(lb + 1).apply(lambda x: x.argmax()) / lb
    df['dn'] = 100 * df.Low.rolling(lb + 1).apply(lambda x: x.argmin()) / lb

    return df['up'], df['dn'] 

Here is my image 1

Upvotes: 5

Luca
Luca

Reputation: 169

This version should be faster:

def aroon (ohlc, period = 14):
    data = np.array(ohlc[['high', 'low']])
    size = len(data)
    out_up = np.zeros(size)
    out_down = np.zeros(size)
    for i in range(period - 1, size):
        window = np.flip(data[i + 1 - period:i + 1],axis=0)
        out_up[i] = ((period - window[:,0].argmax()) / period)
        out_down[i] = ((period - window[:,1].argmin()) / period)
    
return out_up, out_down

df_aroon['up'], df_aroon['down'] = aroon(df_aroon)

With very big dataframes, you can try compiling the code with numba to get even faster:

from numba import jit

@jit(nopython=True)
def aroon (data, period = 14):
    size = len(data)
    out_up = np.zeros(size)
    out_down = np.zeros(size)
    for i in range(period - 1, size):
        window = np.flip(data[i + 1 - period:i + 1,0])
        out_up[i] = ((period - window.argmax()) / period)
        window = np.flip(data[i + 1 - period:i + 1,1])
        out_down[i] = ((period - window.argmin()) / period)
return out_up, out_down

df_aroon['up'], df_aroon['down'] = aroon(np.array(df_aroon[['high', 'low']]))

Upvotes: 0

David Nairn
David Nairn

Reputation: 21

Hi I am a newbie looking to use quantopian Have an interest in using Aroon.Found this in my search. Looks simpler than your code and believe it uses ta-lib. https://github.com/FreddieWitherden/ta/blob/master/ta.py

Upvotes: 2

Related Questions