如何使用 pandas 找到股票的平均方向运动? [英] How to find Average directional movement for stocks using Pandas?

查看:41
本文介绍了如何使用 pandas 找到股票的平均方向运动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OHLCV数据的数据框.我想知道是否有人知道任何教程或使用熊猫找到ADX(平均方向运动)的任何方式?

I have a dataframe of OHLCV data. I would like to know if anyone knows any tutorial or any way of finding ADX(Average directional movement ) using pandas?

import pandas as pd 
import yfinance as yf
import matplotlib.pyplot as plt
import datetime as dt 
import  numpy as nm 


start=dt.datetime.today()-dt.timedelta(59)
end=dt.datetime.today()

df=pd.DataFrame(yf.download("MSFT", start=start, end=end))

平均方向指数或ADX是构成J.Welles Wilder,Jr.开发的技术交易系统的五个指标中的主要技术指标,并使用构成交易系统的其他指标进行计算.ADX主要用作动量或趋势强度的指标,但整个ADX系统也用作方向指标.
通过将两个连续的低点之间的差与它们各自的高点之间的差进行比较来计算方向运动.

The average directional index, or ADX, is the primary technical indicator among the five indicators that make up a technical trading system developed by J. Welles Wilder, Jr. and is calculated using the other indicators that make up the trading system. The ADX is primarily used as an indicator of momentum, or trend strength, but the total ADX system is also used as a directional indicator.
Directional movement is calculated by comparing the difference between two consecutive lows with the difference between their respective highs.

对于ADX的excel计算,这是一个非常不错的视频:

For the excel calculation of ADX this is a really good video:

https://www.youtube.com/watch?v=LKDJQLrXedg&; t = 387s

推荐答案

数学取自这里.

def ADX(df):

    def getCDM(df):
        dmpos = df["High"][-1] - df["High"][-2]
        dmneg = df["Low"][-2] - df["Low"][-1]
        if dmpos > dmneg:
            return dmpos
        else:
            return dmneg 

    def getDMnTR(df):
        DMpos = []
        DMneg = []
        TRarr = []
        n = round(len(df)/14)
        idx = n
        while n <= (len(df)):
            dmpos = df["High"][n-1] - df["High"][n-2]
            dmneg = df["Low"][n-2] - df["Low"][n-1]
                
            DMpos.append(dmpos)
            DMneg.append(dmneg)
        
            a1 = df["High"][n-1] - df["High"][n-2]
            a2 = df["High"][n-1] - df["Close"][n-2]
            a3 = df["Low"][n-1] - df["Close"][n-2]
            TRarr.append(max(a1,a2,a3))

            n = idx + n
    
        return DMpos, DMneg, TRarr

    def getDI(df):
        DMpos, DMneg, TR = getDMnTR(df)
        CDM = getCDM(df)
        POSsmooth = (sum(DMpos) - sum(DMpos)/len(DMpos) + CDM)
        NEGsmooth = (sum(DMneg) - sum(DMneg)/len(DMneg) + CDM)
        
        DIpos = (POSsmooth / (sum(TR)/len(TR))) *100
        DIneg = (NEGsmooth / (sum(TR)/len(TR))) *100

        return DIpos, DIneg

    def getADX(df):
        DIpos, DIneg = getDI(df)

        dx = (abs(DIpos- DIneg) / abs(DIpos + DIneg)) * 100
        
       
        ADX = dx/14
        return ADX

    return(getADX(df))

print(ADX(df))

这篇关于如何使用 pandas 找到股票的平均方向运动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆