0

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()
  • There is no `df`in the scope of `prepare_data`. Did you mean `prices_df`? – Tranbi Jun 12 '22 at 07:41
  • I see what you mean. Something like df = prices_df? – steeltoaster Jun 12 '22 at 08:10
  • why would you assign a new df and not returning prices_df directly? (basically replacing df with prices_df in this function) – Tranbi Jun 12 '22 at 08:12
  • I tried this, but it however only returns price data from last session instead of for entire period. – steeltoaster Jun 12 '22 at 08:44
  • Just a side note... `r.status_code == requests.codes.ok` can be just `if r.ok` and `pd.io.json.read_json(r.text)` should be `pd.read_json(r.text)` – Jon Clements Jun 12 '22 at 09:22
  • I don't understand exactly what you mean but appending should work as expected (it's deprecated though, you should replace it with `pd.concat`) I think the problem is that `get_final_df` returns `df.loc[last_business_day()]`. Maybe you want to return the whole df? – Tranbi Jun 12 '22 at 09:31
  • Yes, that's correct. I'm looking to return whole df, for in this case, price data for 100 days. – steeltoaster Jun 12 '22 at 12:07
  • Thx for amazing support! This middle section now looks like this and is working returning the full set of expected data. '''def get_final_df(symbol_list, 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''' – steeltoaster Jun 12 '22 at 12:37

0 Answers0