Skip to content

Instantly share code, notes, and snippets.

@vedranmarkulj
Last active February 25, 2018 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vedranmarkulj/ee4159b06a88eaa414a7bdf8010d494b to your computer and use it in GitHub Desktop.
Save vedranmarkulj/ee4159b06a88eaa414a7bdf8010d494b to your computer and use it in GitHub Desktop.
get_main_df.py
from get_historical_data import AlphaVantage
import pandas as pd
import json
def get_main_data_frame(symbol):
#
# Get historical data as json
#
historical_data_daily = AlphaVantage(symbol).daily()
#
# Find and list all relevant keys in the original historical data response
#
list_keys = []
list_historical_data__daily = []
for key in historical_data_daily['Time Series (Daily)']:
list_keys.append(key)
for k in list_keys:
#
# Extract relevant data from original historical data response
#
data = historical_data_daily['Time Series (Daily)'][k]
price_open = data['1. open']
price_high = data['2. high']
price_low = data['3. low']
price_close = data['4. close']
dict_data = dict([
(u'date', k),
(u'symbol', symbol),
(u'price_open', price_open),
(u'price_high', price_high),
(u'price_low', price_low),
(u'price_close', price_close),
])
list_historical_data__daily.append(dict_data)
#
# convert data to data frame
#
df = pd.read_json(json.dumps(list_historical_data__daily))
#
# prepare data, and add calculated fields to data frame
#
df['date_str'] = df['date']
df.set_index('date', inplace=True)
df.sort_index(inplace=True)
df['price_close_lag'] = df['price_close'].shift(1)
df['price_close_lead'] = df['price_close'].shift(-1)
df.insert(0, 'date_id', range(1, 1 + len(df)))
#
# returns data frame
#
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment