首页 > 解决方案 > 使用 Pandas 检查股票价格的差距?

问题描述

'Gap Fill?'如果列中有a ,我想在列中输入1,'1'并且从今天开始到未来5天'Gap Down?'的最大值,或者是昨天的或。'Highs'(.shift(-5))>='Adj Close'(.shift(-1))

这是我重新创建并提出解决方案的代码:

# Import pandas library 
import pandas as pd 

# initialize list of lists 
data = [[5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [3, 3, 3, 3], [3, 3, 3, 3], [3, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4]
         ] 

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Open', 'High', 'Low', 'Adj Close']) 

# Now apply a new column and put a 1 if today's opened is less than yesterday's Adj Close
df['Gap Down?'] = 0

df.loc[df['Open'] < df['Adj Close'].shift(1), 'Gap Down?'] = 1
df.loc[df['Open'] >= df['Adj Close'].shift(1), 'Gap Down?'] = 0

# print the new dataframe
print(df)

# Now make a new column that puts a 1 if the highest price in the High column from today
# and FORWARD (future) 5 days becomes >= yesterday's close
df['Gap Fill?'] = 0

这当前给出了输出:

    Open  High  Low  Adj Close  Gap Down?
0      5    10    3          4          0
1      5    10    3          4          0
2      5    10    3          4          0
3      5    10    3          4          0
4      5    10    3          4          0
5      5    10    3          4          0
6      5    10    3          4          0
7      5    10    3          4          0
8      5    10    3          4          0     <- Yesterday's Adj Close was 4
9      3     3    3          3          1     <- Today opened at 3 so there's been a gap down
10     3     3    3          3          0
11     3    10    3          4          0     <- Here is when the High went >= 4, and it's within 5 days of the open price in question
12     5    10    3          4          0
13     5    10    3          4          0
14     5    10    3          4          0
15     5    10    3          4          0
16     5    10    3          4          0
17     5    10    3          4          0
18     5    10    3          4          0
19     5    10    3          4          0
20     5    10    3          4          0
21     5    10    3          4          0
22     5    10    3          4          0
23     5    10    3          4          0

我想结束这个:

    Open  High  Low  Adj Close  Gap Down? Gap Fill?
0      5    10    3          4          0         0
1      5    10    3          4          0         0
2      5    10    3          4          0         0
3      5    10    3          4          0         0
4      5    10    3          4          0         0
5      5    10    3          4          0         0
6      5    10    3          4          0         0
7      5    10    3          4          0         0
8      5    10    3          4          0         0
9      3     3    3          3          1         1   <- ... but the 1 should go here
10     3     3    3          3          0         0
11     3    10    3          4          0         0   <- The gap fill happened here two days later...
12     5    10    3          4          0         0
13     5    10    3          4          0         0
14     5    10    3          4          0         0
15     5    10    3          4          0         0
16     5    10    3          4          0         0
17     5    10    3          4          0         0
18     5    10    3          4          0         0
19     5    10    3          4          0         0

...

这是为了检查是否存在缺口,以及是否在接下来的 5 天中的某个时间点被填补。谢谢!

标签: pythonpython-3.xpandas

解决方案


你可以这样做:

# Import pandas library 
import pandas as pd 

# initialize list of lists 
data = [[5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [3, 3, 3, 3], [3, 3, 3, 3], [3, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4],
         [5, 10, 3, 4], [5, 10, 3, 4], [5, 10, 3, 4]
         ] 

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Open', 'High', 'Low', 'Adj Close']) 

# Now apply a new column and put a 1 if today's opened is less than yesterday's Adj Close
df['Gap Down?'] = 0

df.loc[df['Open'] < df['Adj Close'].shift(1), 'Gap Down?'] = 1
df.loc[df['Open'] >= df['Adj Close'].shift(1), 'Gap Down?'] = 0

# print the new dataframe
print(df)

# Now make a new column that puts a 1 if the highest price in the High column from today
# and FORWARD (future) 5 days becomes >= yesterday's close
df['Gap Fill?'] = df['Gap Down?']
#first condition
e=df['Gap Down?'].eq(1)
ind=df[e].index.values.tolist()
i=0
while i<len(ind):
    #cheked of second condition
    if(df.loc[ind[i]:ind[i]+4,'High'].max()>=df.loc[ind[i-1],'Adj Close']):

        df.loc[ind[i],'Gap Fill?'] = 1
    i+=1
print(df)

输出:

     Open  High  Low  Adj Close  Gap Down?
0      5    10    3          4          0
1      5    10    3          4          0
2      5    10    3          4          0
3      5    10    3          4          0
4      5    10    3          4          0
5      5    10    3          4          0
6      5    10    3          4          0
7      5    10    3          4          0
8      5    10    3          4          0
9      3     3    3          3          1
10     3     3    3          3          0
11     3    10    3          4          0
12     5    10    3          4          0
13     5    10    3          4          0
14     5    10    3          4          0
15     5    10    3          4          0
16     5    10    3          4          0
17     5    10    3          4          0
18     5    10    3          4          0
19     5    10    3          4          0
20     5    10    3          4          0
21     5    10    3          4          0
22     5    10    3          4          0
23     5    10    3          4          0
     Open  High  Low  Adj Close  Gap Down?  Gap Fill?
0      5    10    3          4          0          0
1      5    10    3          4          0          0
2      5    10    3          4          0          0
3      5    10    3          4          0          0
4      5    10    3          4          0          0
5      5    10    3          4          0          0
6      5    10    3          4          0          0
7      5    10    3          4          0          0
8      5    10    3          4          0          0
9      3     3    3          3          1          1
10     3     3    3          3          0          0
11     3    10    3          4          0          0
12     5    10    3          4          0          0
13     5    10    3          4          0          0
14     5    10    3          4          0          0
15     5    10    3          4          0          0
16     5    10    3          4          0          0
17     5    10    3          4          0          0
18     5    10    3          4          0          0
19     5    10    3          4          0          0
20     5    10    3          4          0          0
21     5    10    3          4          0          0
22     5    10    3          4          0          0
23     5    10    3          4          0          0

使用e,条件 1 被检查,条件 2 由循环检查所有满足条件 1 且索引存储在 中的元素ind。在这种情况下,只有一个,但这允许您在有多个时执行此操作。

我想我明白你在找什么,如果第二个条件不是你所要求的,你可以检查它并调整必要的值。


推荐阅读