PERCENT Change from the open

Is there any way to get the percentage change from the open on minute data. I did the following, however i think it is over complicated, and it only works in my notebook. Any ideas/help will be appreciative.

def make_data(stock):

x = stock.between_time('13:31','13:31')

stock['date'] = np.nan
stock['date'] = stock.index.date
x['date'] = np.nan
x['date'] = x.index.date

df = pd.merge(stock,x,on = ['date'],right_index= True)

# Select the ones you want
df = df[['price_x','open_price_y']]
df= df.rename(columns={'price_x': 'price', 'open_price_y': 'open'})
df['return'] = np.nan
df['return'] = (df['price']-df['open'])/df['open']
return df

2 responses

@oscar

Try this way:

start_date = '2020-1-1'
end_date = '2020-5-1'

stock = symbols('SPY')
opens = get_pricing(stock, start_date, end_date, fields='open_price')
closes = get_pricing(stock, start_date, end_date, fields='close_price')

direction = (closes - opens)/opens

direction


@Vldimir
Thanks for the code!!!
... this explains why i almost failed algorithms in college lol .