【问题标题】:How can I find the change in trend in pandas dataframe column如何在熊猫数据框列中找到趋势变化
【发布时间】:2020-08-08 10:49:47
【问题描述】:
                             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列有下降趋势,突然有上升趋势,如何判断是否有一系列下降趋势和趋势变化。

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

【问题讨论】:

  • 如果您要确定趋势的增加或减少,最好在图表中可视化数据。
  • @TanmayaMeher - 我正在尝试用这个进行算法交易。这就是原因。感谢您抽出时间回复。

标签: python pandas stock


【解决方案1】:

您要确定derivative (dy / 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

【讨论】:

    【解决方案2】:

    一切都取决于您的模型

    一种方法

    df['chg'] = df['openclose'] -df['openclose'].shift(1)
    df['chg']>0
    
    0    False
    1    False
    2     True
    3     True
    4     True
    

    其他:

    • 可以跨越一些移动平均线
    • 可以检测异常值
    • 可以在负数减少时保留
    • 可以使用statisticslike z-score方法
    • 可以有Time Series analysis(趋势相关)
    • 可以有 technical analysis 等等。
    • 可以有适当的财务建模方法

    【讨论】:

      【解决方案3】:

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

      • idx_是极值点所在的索引。
      • 类型,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
      

      【讨论】:

        猜你喜欢
        • 2021-11-10
        • 2020-06-03
        • 1970-01-01
        • 1970-01-01
        • 2016-08-10
        • 2017-06-04
        • 1970-01-01
        • 1970-01-01
        • 2017-05-28
        相关资源
        最近更新 更多