Wondering if any experienced pandas users can point me along the way? For the following code, python doesn't accept that df is defined. Output -> "NameError: name 'df' is not defined"
It seems like maybe there is a merge and/or replace function required to setup df, but what I've tested has not been successful.
Thankful for all feedback and/or suggestions!
TICKERS = ['A', 'AA', 'AAPL', 'ABNB', 'ADBE','AMAT', 'AMD', 'AMC', 'AMGN', 'AMZN']
LOOK_BACK_PERIOD = 100
def last_business_day():
test_date = date.today()
diff = 1
if test_date.weekday() == 0:
diff = 3
elif test_date.weekday() == 6:
diff = 2
else:
diff = 1
res = test_date - timedelta(days=diff)
return str(res)
def get_symbol_prices(symbol, start_date, end_date):
session = requests.Session()
request = f"https://financialmodelingprep.com/api/v3/historical-price-full/{symbol}\
?apikey=YOURAPI\
&from={start_date}&to={end_date}".replace(" ", "")
r = session.get(request)
if r.status_code == requests.codes.ok:
df = pd.io.json.read_json(r.text)
if not df.empty:
df = pd.DataFrame(df['historical'].to_list())
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date').sort_index()
return df
def prepare_data(symbol, look_back_period):
start_date = date.today() - timedelta(days=look_back_period)
end_date = date.today()
prices_df = get_symbol_prices(symbol=symbol, start_date=start_date, end_date=end_date)
prices_df = prices_df[['open', 'high', 'low', 'close', 'volume']]
# {missing merge function here???}
df.index.name = 'datetime'
df['symbol'] = symbol
return df
def get_final_df(tickers, look_back_period):
df = pd.DataFrame()
for symbol in tickers:
df = df.append(prepare_data(symbol=symbol, look_back_period=look_back_period))
# {missing replace function here???}
return df.loc[last_business_day()]
def main():
historical_df = get_final_df(tickers=TICKERS, look_back_period=LOOK_BACK_PERIOD)
output_folder = 'E:/'
file_name = 'HISTORICALPORTFOLIO.csv'
historical_df.to_csv(os.path.join(output_folder, file_name))
if __name__ == '__main__':
main()