如何在dataframe列中找到趋势的变化

2024-10-06 12:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

                             open   high    low     close   volume  openclose
date                        
2020-08-04 09:15:00+05:30   227.00  229.00  226.40  226.70  157982  -0.30
2020-08-04 09:20:00+05:30   226.55  226.80  222.40  223.15  253212  -3.40
2020-08-04 09:25:00+05:30   223.00  223.15  220.15  220.15  236819  -2.85
2020-08-04 09:30:00+05:30   220.15  220.60  217.55  219.90  628153  -0.25
2020-08-04 09:35:00+05:30   219.70  221.80  218.90  221.60  260912  1.90

所以openclose列中有一个下降趋势,突然有一个上升趋势,我如何确定是否有一系列的下降趋势和变化趋势

任何线索或帮助都会有帮助。提前谢谢


Tags: closedateopen趋势lowhighvolume线索
3条回答

可以使用极值点。找到局部最大值和最小值

  • idx_u是极值点所在的指数
  • 类型,0是最小值。1是最大值
  • close是位于极值点时的价格

def get_max_min(df, smoothing=4, window_range=10):
    smooth_df = df['close'].rolling(window=smoothing).mean().dropna()
    local_max = argrelextrema(smooth_df.values, np.greater)[0]
    local_min = argrelextrema(smooth_df.values, np.less)[0]
    price_local_max_dt = []
    for i in local_max:
        if (i > window_range) and (i < len(df) - window_range):
            price_local_max_dt.append(df.iloc[i - window_range:i + window_range]['close'].idxmax())
    price_local_min_dt = []
    for i in local_min:
        if (i > window_range) and (i < len(df) - window_range):
            price_local_min_dt.append(df.iloc[i - window_range:i + window_range]['close'].idxmin())
    maxima = pd.DataFrame(df.loc[price_local_max_dt])
    minima = pd.DataFrame(df.loc[price_local_min_dt])

    max_min = pd.concat([maxima, minima]).sort_index()
    max_min.index.name = 'idx_'
    max_min = max_min.reset_index()
    max_min = max_min[~max_min.idx_.duplicated()]
    max_min = max_min.reset_index(drop=True)

    maxima.index.name = 'idx_'
    maxx = maxima.reset_index()
    maxx = maxx[~maxx.idx_.duplicated()]
    maxx = maxx.assign(type=[1]*len(maxx))
    maxx = maxx[['idx_', 'type', 'close']]

    minima.index.name = 'idx_'
    minn = minima.reset_index()
    minn = minn[~minn.idx_.duplicated()]
    minn = minn.assign(type=[0]*len(minn))
    minn = minn[['idx_', 'type', 'close']]

    resminMax = pd.concat([minn, maxx]).sort_values('idx_')
    resminMax = resminMax.reset_index(drop=True)
    return resminMax

一切都取决于您的车型

一种方法

df['chg'] = df['openclose'] -df['openclose'].shift(1)
df['chg']>0

0    False
1    False
2     True
3     True
4     True

其他:

  • 可以是一些移动平均线的交叉
  • 可以检测异常值
  • 可在负计数减少时保持
  • 可以使用类似于statistics的z-score方法
  • 可能有Time Series analysis(与趋势相关)
  • 可以有technical analysis和更多
  • 可以有适当的财务建模方法

您需要确定derivativedy / dx)并查看它是负值还是正值。如果是积极的,则表示有积极的趋势:

derivative = df['openclose'].diff() / df.index.to_series().diff().dt.total_seconds()
df['trend'] = derivative.gt(0).map({False: -1, True: 1})
                             open    high     low   close  volume  openclose  trend
date                                                                               
2020-08-04 09:15:00+05:30  227.00  229.00  226.40  226.70  157982      -0.30     -1
2020-08-04 09:20:00+05:30  226.55  226.80  222.40  223.15  253212      -3.40     -1
2020-08-04 09:25:00+05:30  223.00  223.15  220.15  220.15  236819      -2.85      1
2020-08-04 09:30:00+05:30  220.15  220.60  217.55  219.90  628153      -0.25      1
2020-08-04 09:35:00+05:30  219.70  221.80  218.90  221.60  260912       1.90      1

相关问题 更多 >