使用熊猫来检查股票价格的差距?

时间:2019-08-14 07:17:58

标签: python python-3.x pandas

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

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

(.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

...

这是为了检查是否存在差距,以及是否在接下来的5天之内填补了差距。谢谢!

1 个答案:

答案 0 :(得分: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?'] = 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且其索引存储在ind中的所有元素,条件1被检查,条件2被循环检查。在这种情况下,只有一个,但是当有多个时,您可以这样做。

我想我了解您的要求,如果第二个条件不完全符合您的要求,您可以对其进行检查并调整必要的值。