(df, n, n_ADX)
return out(SETTINGS, df, result)
Average Directional Movement Index
Average Directional Movement Index
Average Directional Movement Index
def ADX(df, n, n_ADX):
    """
    Average Directional Movement Index
    """
    i = 0
    UpI = []
    DoI = []
    while i + 1 <= len(df) - 1:  # df.index[-1]:
        UpMove = df.iat[i + 1, df.columns.get_loc('High')] - df.iat[i, df.columns.get_loc('High')]
        DoMove = df.iat[i, df.columns.get_loc('Low')] - df.iat[i + 1, df.columns.get_loc('Low')]
        if UpMove > DoMove and UpMove > 0:
            UpD = UpMove
        else:
            UpD = 0
        UpI.append(UpD)
        if DoMove > UpMove and DoMove > 0:
            DoD = DoMove
        else:
            DoD = 0
        DoI.append(DoD)
        i = i + 1
    i = 0
    TR_l = [0]
    while i < len(df) - 1:  # df.index[-1]:
        TR = max(df.iat[i + 1, df.columns.get_loc('High')], df.iat[i, df.columns.get_loc('Close')]) - min(df.iat[i + 1, df.columns.get_loc('Low')], df.iat[i, df.columns.get_loc('Close')])
        TR_l.append(TR)
        i = i + 1
    TR_s = pd.Series(TR_l)
    ATR = pd.Series(TR_s.ewm(span=n, min_periods=n).mean())
    UpI = pd.Series(UpI)
    DoI = pd.Series(DoI)
    PosDI = pd.Series(UpI.ewm(span=n, min_periods=n - 1).mean() / ATR)
    NegDI = pd.Series(DoI.ewm(span=n, min_periods=n - 1).mean() / ATR)
    temp = abs(PosDI - NegDI) / (PosDI + NegDI)
    result = pd.Series(temp.ewm(span=n_ADX, min_periods=n_ADX - 1).mean(), name='ADX_' + str(n) + '_' + str(n_ADX))
    return out(SETTINGS, df, result)
def ADX ( df , n , n_ADX ) : i = 0 UpI = [ ] DoI = [ ] while i + 1 <= len ( df ) - 1 : # df.index[-1]: UpMove = df . iat [ i + 1 , df . columns . get_loc ( 'High' ) ] - df . iat [ i , df . columns . get_loc ( 'High' ) ] DoMove = df . iat [ i , df . columns . get_loc ( 'Low' ) ] - df . iat [ i + 1 , df . columns . get_loc ( 'Low' ) ] if UpMove > DoMove and UpMove > 0 : UpD = UpMove else : UpD = 0 UpI . append ( UpD ) if DoMove > UpMove and DoMove > 0 : DoD = DoMove else : DoD = 0 DoI . append ( DoD ) i = i + 1 i = 0 TR_l = [ 0 ] while i < len ( df ) - 1 : # df.index[-1]: TR = max ( df . iat [ i + 1 , df . columns . get_loc ( 'High' ) ] , df . iat [ i , df . columns . get_loc ( 'Close' ) ] ) - min ( df . iat [ i + 1 , df . columns . get_loc ( 'Low' ) ] , df . iat [ i , df . columns . get_loc ( 'Close' ) ] ) TR_l . append ( TR ) i = i + 1 TR_s = pd . Series ( TR_l ) ATR = pd . Series ( TR_s . ewm ( span = n , min_periods = n ) . mean ( ) ) UpI = pd . Series ( UpI ) DoI = pd . Series ( DoI ) PosDI = pd . Series ( UpI . ewm ( span = n , min_periods = n - 1 ) . mean ( ) / ATR ) NegDI = pd . Series ( DoI . ewm ( span = n , min_periods = n - 1 ) . mean ( ) / ATR ) temp = abs ( PosDI - NegDI ) / ( PosDI + NegDI ) result = pd . Series ( temp . ewm ( span = n_ADX , min_periods = n_ADX - 1 ) . mean ( ) , name = 'ADX_' + str ( n ) + '_' + str ( n_ADX ) ) return out ( SETTINGS , df , result )