importpandasaspd
data={'date':[1/3/2000,1/4/2000,1/5/2000,1/6/2000,1/7/2000,1/10/2000,1/11/2000,1/12/2000,1/13/2000,1/14/2000,1/18/2000,1/19/2000,1/20/2000,1/21/2000,1/24/2000,1/25/2000,1/26/2000,1/27/2000,1/28/2000,1/31/2000,2/1/2000,2/2/2000,2/3/2000,2/4/2000,2/7/2000,2/8/2000,2/9/2000,2/10/2000,2/11/2000,2/14/2000,2/15/2000,2/16/2000,2/17/2000,2/18/2000,2/22/2000,2/23/2000,2/24/2000,2/25/2000,2/28/2000,2/29/2000],'close':[308.3,315.3,314.4,307.5,309.8,313.4,310.7,324.2,332.5,348.8,351.1,348.2,348.7,343.5,343,343.3,342.4,343,334.4,334.6,336,333.8,331.6,332.8,335.9,341.2,338.4,342.1,343.2,339.5,346.9,342,339.6,337.4,335,330.8,331.3,331.1,332.6,335.1]}df=pd.DataFrame(data)## Create columns to compare price to day before and day afterdf['prev_close']=df['close'].shift(1)df['next_close']=df['close'].shift(-1)## BOOLEAN TO RETURN IF PRICE IS LOWER THAN PREVIOUS AND NEXT DAYdf['high_high']=((df['prev_close'])>df['close'])&((df['next_close'])>df['close'])## BOOLEAN TO RETURN TRUE IF PRICE IS GREATER THAN PREVIOUS AND NEXT DAYdf['low_low']=((df['prev_close'])df['comp_price']## DESIGNATE FIRST INSTANCE OF BUY SIGNAL AS DAY TO OPEN POSITIONdf['open_pos']=(df['buy_sig']==1)&(df['buy_sig'].shift(1)!=1)df['take_signal']=(df['buy_sig']==1)&(df['open_pos']==True)df['open_pos_price']=df['close'].where(df['take_signal']==True)df['open_pos_price'].fillna(method='pad',inplace=True)## CREATE SELL SIGNALdf['sell_sig']=df['close']0)&(df['open_pos_price']>0))