For the following code the output returned is such:
The desired arrangement is however with the tickers raised and grouped/aggregated like:
All suggestions and feedback welcome.
Code sample
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[['close']]
df = pd.DataFrame(prices_df)
df.index.name = 'datetime'
df['symbol'] = symbol
return df
def get_final_df(tickers, look_back_period):
df = pd.DataFrame()
for symbol in symbol_list:
df = df.append(prepare_data(symbol=symbol, look_back_period=look_back_period))
return df
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))