2021-03-30

Split the date and time in data frame itself

I am working on 15 minutes historical OHLC stocks data and calculating few technical indicators like stochastic, VWAP and RSI etc. Followed is my code, and I need help on splitting the date and time right in the data frame itself then I can save it to CSV file.

import requests
import pandas as pd
import arrow
import datetime
window = 10
window20 = 20
window50 = 50
window100 = 100
window200 = 200

ema_time = 8
import numpy as np
import csv

def get_quote_data(symbol='GOOGL', data_range='10d', data_interval='60m'):
    res = requests.get('https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?range={data_range}&interval={data_interval}'.format(**locals()))
    data = res.json()
    body = data['chart']['result'][0]    
    dt = datetime.datetime
    dt = pd.Series(map(lambda x: arrow.get(x).to('US/Central').datetime.replace(tzinfo=None), body['timestamp']), name='Datetime')
    df = pd.DataFrame(body['indicators']['quote'][0], index=dt)
    dg = pd.DataFrame(body['timestamp'])
    
    #calculation for VWAP
    volumeC = df['volume']
    priceC = df['close']
    df = df.assign(VWAP=((volumeC * priceC).cumsum() / volumeC.cumsum()).ffill())
    # calculate stochastic
    df['low5']= df['low'].rolling(5).min()
    df['high5']= df['high'].rolling(5).max()
    
    #k = 100 * (c - l) / (h - l) 
    df['K'] = (df['close']-df['low5'])/(df['high5']-df['low5'])
    
    #calculate RSI
    df['RSI'] = calculate_rsi(df)
    df = df.loc[:, ('open','high','low', 'close','volume','VWAP','RSI','K')]
    df.columns = ['OPEN','HIGH','LOW', 'CLOSE','volume','VWAP', 'RSI', 'K']
    df.dropna(inplace=True)    
       
       
    return df

def calculate_rsi(df):
    delta = df.close.diff()
    window_rsi = 14
    up_days = delta.copy()
    up_days[delta<=0]=0.0
    down_days = abs(delta.copy())
    down_days[delta>0]=0.0
    RS_up = up_days.rolling(window_rsi).mean()
    RS_down = down_days.rolling(window_rsi).mean()
    rsi= 100-100/(1+RS_up/RS_down)
    return rsi



data = get_quote_data('AMZN', '30d', '15m')
print(data)

It prints data like followed. OPEN HIGH LOW CLOSE
Datetime
2021-02-16 12:00:00 3271.580078 3276.655029 3267.879883 3271.580078
2021-02-16 12:15:00 3273.000000 3277.874023 3269.949951 3275.500000
2021-02-16 12:30:00 3273.860107 3278.290039 3272.000000 3273.619873
2021-02-16 12:45:00 3273.149902 3274.327393 3265.510010 3268.120117
2021-02-16 13:00:00 3269.500000 3272.229980 3267.850098 3271.943604
... ... ... ... ...
2021-03-29 13:45:00 3063.350098 3073.989990 3063.350098 3069.439941
2021-03-29 14:00:00 3068.635010 3083.368896 3068.635010 3078.189941
2021-03-29 14:15:00 3079.534912 3082.459961 3075.130127 3080.810059
2021-03-29 14:30:00 3081.500000 3083.021973 3075.939941 3076.739990
2021-03-29 14:45:00 3076.280029 3081.235107 3067.639893 3076.760010

                 volume         VWAP        RSI         K  

Datetime
2021-02-16 12:00:00 67712 3282.279987 38.809164 0.779962
2021-02-16 12:15:00 52424 3282.080086 34.602731 0.887348
2021-02-16 12:30:00 42972 3281.880445 44.879847 0.741838
2021-02-16 12:45:00 34363 3281.625596 38.436315 0.405271
2021-02-16 13:00:00 34606 3281.448318 36.041982 0.503410
... ... ... ... ...
2021-03-29 13:45:00 62228 3105.228131 55.439824 0.294534
2021-03-29 14:00:00 75404 3105.206534 59.610208 0.757640
2021-03-29 14:15:00 53411 3105.192739 56.776731 0.880254
2021-03-29 14:30:00 77107 3105.169530 58.732036 0.689787
2021-03-29 14:45:00 195762 3105.110819 53.152839 0.669866

[766 rows x 8 columns]

I would like to have columns like DATE TIME OPEN HIGH LOW CLOSE VOLUME VWAP RSI K in the data frame itself. I am new to python and struggling to split the date and time RIGHT INTO Dataframe itself.

Please help.

Thanks, DR



from Recent Questions - Stack Overflow https://ift.tt/31scqqy
https://ift.tt/eA8V8J

No comments:

Post a Comment