time

问题描述 投票:0回答:1

我有一个数据源,它给了我以下数据框。pricehistory:

+---------------------+------------+------------+------------+------------+----------+------+
|         time        |   close    |    high    |    low     |    open    |  volume  | red  |
+---------------------+------------+------------+------------+------------+----------+------+
|                     |            |            |            |            |          |      |
| 2020-01-02 10:14:00 | 321.336177 | 321.505186 | 321.286468 | 321.505186 | 311601.0 | True |
| 2020-01-02 11:16:00 | 321.430623 | 321.465419 | 321.395827 | 321.465419 | 42678.0  | True |
| 2020-01-02 11:17:00 | 321.425652 | 321.445536 | 321.375944 | 321.440565 | 39827.0  | True |
| 2020-01-02 11:33:00 | 321.137343 | 321.261614 | 321.137343 | 321.261614 | 102805.0 | True |
| 2020-01-02 12:11:00 | 321.256643 | 321.266585 | 321.241731 | 321.266585 | 25629.0  | True |
| 2020-01-02 12:12:00 | 321.246701 | 321.266585 | 321.231789 | 321.266585 | 40869.0  | True |
| 2020-01-02 13:26:00 | 321.226818 | 321.266585 | 321.226818 | 321.261614 | 44011.0  | True |
| 2020-01-03 10:18:00 | 320.839091 | 320.958392 | 320.828155 | 320.958392 | 103351.0 | True |
| 2020-01-03 10:49:00 | 320.988217 | 321.077692 | 320.988217 | 321.057809 | 84492.0  | True |
| etc...              | etc...     | etc...     | etc...     | etc...     | etc...   | etc. |
+---------------------+------------+------------+------------+------------+----------+------+

产出: pricehistory.dtypes:

close     float64
high      float64
low       float64
open      float64
volume    float64
red          bool
dtype: object

产出: pricehistory.index.dtype:dtype('<M8[ns]')

注意:这个数据框很大,每一行都是1分钟的数据,而且跨度长达数月,所以有很多时间框架需要迭代。

我想使用一些特定的标准,这些标准将成为新数据框架中的列。

  1. 整个数据框架中每天的最高价格和时间(分钟)。
  2. 首次出现4个下降趋势的分钟。open < close 当天

到目前为止,我还不清楚如何从时间(datetimeindex值)和高价中拉出 pricehistory.

对于上面的(1),我使用的是 pd.DataFrame(pricehistory.high.groupby(pd.Grouper(freq='D')).max()) 这给了我。

+------------+------------+
|    time    |    high    |
+------------+------------+
|            |            |
| 2020-01-02 | 322.956677 |
| 2020-01-03 | 321.753729 |
| 2020-01-04 | NaN        |
| 2020-01-05 | NaN        |
| 2020-01-06 | 321.843204 |
| etc...     | etc...     |
+------------+------------+

但这行不通,因为它只给我一天的时间,而不是精确到分钟,并且使用... min 作为 Grouper freq不工作,因为这样的话,就只是每个min的最大值,也就是 high.

期望的结果(注:包括分钟)。

+---------------------+------------+
|    time             |    high    |
+---------------------+------------+
|                     |            |
| 2020-01-02 9:31:00  | 322.956677 |
| 2020-01-03 10:13:11 | 321.753729 |
| 2020-01-04 15:33:12 | 320.991231 |
| 2020-01-06 12:01:23 | 321.843204 |
| etc...              | etc...     |
+---------------------+------------+

对于上述(2),我使用的是以下方法。

pricehistory['red'] = pricehistory['close'].lt(pricehistory['open'])

要在 pricehistory 其中显示了我们是否有4个红色的分钟连续。

然后,使用 new_pricehistory = pricehistory.loc[pricehistory[::-1].rolling(4)['red'].sum().eq(4)]这就给出了一个新的数据框架,其中只包含一行中出现4个红色分钟的行,最好是只出现第一次的行,而不是所有的行。

目前的输出。

+---------------------+------------+------------+------------+------------+--------+------+
|        time         |   close    |    high    |    low     |    open    | volume | red  |
+---------------------+------------+------------+------------+------------+--------+------+
|                     |            |            |            |            |        |      |
| 2020-01-02 10:14:00 | 321.336177 | 321.505186 | 321.286468 | 321.505186 | 311601 | TRUE |
| 2020-01-03 10:18:00 | 320.839091 | 320.958392 | 320.828155 | 320.958392 | 103351 | TRUE |
| 2020-01-06 10:49:00 | 320.520956 | 320.570665 | 320.501073 | 320.550781 |  71901 | TRUE |
+---------------------+------------+------------+------------+------------+--------+------+

谢谢你!

python pandas finance stock
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.