src/scrape_fun.py
def retrieve_price_history(stock_of_interest, api_key, save_path):
    """
    Retrieves historical daily closing price of a given ticker from the Alphavantage API.
    Adjusts ticker price and dividend payout accordingly to forward/reverse splits.
    Returns matrix with: date, adjusted closing price, adjusted dividend payout, and split factor
    Checks and appends the prices to local version of ticker history is present.

    :param stock_of_interest: ticker symbol (string)
    :param api_key: API key used to access the Alphavantage server (string)
    :param save_path: Path used to save data (string)
    :return: adjusted daily closing price and dividend payouts of the ticker (DataFrame)
    """

    # Default parameters
    split_multiplier = 1
    data_url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=' \
               + stock_of_interest + '&outputsize=full&apikey=' + api_key

    # Actual data starts at row 5
    history_data = pd.read_json(data_url)['Time Series (Daily)'].iloc[5:].reset_index()
    # Reset index to be datetime format
    history_data['index'] = pd.to_datetime(history_data['index'])
    # Create DataFrame to store data
    my_history = np.zeros((len(history_data), 4), dtype=object)
    # Extracting information we need
    for n in range(len(history_data)):
        # Check if stock forward/reverse split happened the following day (data is present -> past)
        # n > 0 since we are calling 'n - 1'
        if n > 0:
            temp_multiplier = float(history_data.iloc[n - 1, 1]['8. split coefficient'])
            if temp_multiplier != 1:
                split_multiplier = split_multiplier * temp_multiplier
        # Update DataFrame
        my_history[n] = [history_data.iloc[n, 0],
                         round((float(history_data.iloc[n, 1]['4. close']) / split_multiplier), 3),
                         round((float(history_data.iloc[n, 1]['7. dividend amount']) / split_multiplier), 3),
                         round(split_multiplier, 3)]
    # Reverse list such that the oldest price is first
    my_history = my_history[::-1]
    # Removes the last row if that is the current date
    if my_history[-1, 0].date() == pd.to_datetime("today").date():
        my_history = my_history[:-1, :]

    # Convert to DataFrame and store
    my_history_df = pd.DataFrame(data=my_history,
                                 columns=["date", "close", "dividend amount", "adjustment factor"])

    # Create data directory if it doesn't exist
    Path(save_path).mkdir(exist_ok=True)

    try:
        old_data = pd.read_csv(os.path.abspath(os.path.join(save_path, stock_of_interest)) + ".csv")
        old_data["date"] = pd.to_datetime(old_data["date"])
        # Sometimes there are rounding errors when using "read_csv"
        old_data["close"] = round(old_data["close"], 3)
        old_data["dividend amount"] = round(old_data["dividend amount"], 3)
        old_data["adjustment factor"] = round(old_data["adjustment factor"], 3)
        my_history_df = pd.concat([old_data, my_history_df], ignore_index=True).drop_duplicates().reset_index(drop=True)
        if len(my_history_df['date']) != len(set(my_history_df['date'])):
            # Discrepancies are appended to the end of the concatenation
            my_history_df = my_history_df.sort_values(by=["date"])
            print(my_history_df[my_history_df.duplicated(subset=["date"],
                                                         keep=False)])
            raise Exception("Discrepancies between old and new files. This could be due to forward/reverse splits."
                            "Data not updated, please manually fix!")
        else:
            my_history_df.to_csv(path_or_buf=(save_path + stock_of_interest + '.csv'),
                                 index=False)
    except FileNotFoundError:
        print("Local history of ticker '" + stock_of_interest + "' does not exist, created new file.")
        my_history_df.to_csv(path_or_buf=(os.path.abspath(os.path.join(save_path, stock_of_interest)) + ".csv"),
                             index=False)

    return my_history_df

    Retrieves historical daily closing price of a given ticker from the Alphavantage API.
    Adjusts ticker price and dividend payout accordingly to forward/reverse splits.
    Returns matrix with: date, adjusted closing price, adjusted dividend payout, and split factor
    Checks and appends the prices to local version of ticker history is present.

    :param stock_of_interest: ticker symbol (string)
    :param api_key: API key used to access the Alphavantage server (string)
    :param save_path: Path used to save data (string)
    :return: adjusted daily closing price and dividend payouts of the ticker (DataFrame)
    
Retrieves historical daily closing price of a given ticker from the Alphavantage API.
Adjusts ticker price and dividend payout accordingly to forward/reverse splits.
Returns matrix with: date, adjusted closing price, adjusted dividend payout, and split factor
Checks and appends the prices to local version of ticker history is present.
Retrieves historical daily closing price of a given ticker from the Alphavantage API . Adjusts ticker price and dividend payout accordingly to forward / reverse splits . Returns matrix with : date adjusted closing price adjusted dividend payout and split factor Checks and appends the prices to local version of ticker history is present .
def retrieve_price_history(stock_of_interest, api_key, save_path):
    split_multiplier = 1
    data_url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=' \
               + stock_of_interest + '&outputsize=full&apikey=' + api_key
    history_data = pd.read_json(data_url)['Time Series (Daily)'].iloc[5:].reset_index()
    history_data['index'] = pd.to_datetime(history_data['index'])
    my_history = np.zeros((len(history_data), 4), dtype=object)
    for n in range(len(history_data)):
        if n > 0:
            temp_multiplier = float(history_data.iloc[n - 1, 1]['8. split coefficient'])
            if temp_multiplier != 1:
                split_multiplier = split_multiplier * temp_multiplier
        my_history[n] = [history_data.iloc[n, 0],
                         round((float(history_data.iloc[n, 1]['4. close']) / split_multiplier), 3),
                         round((float(history_data.iloc[n, 1]['7. dividend amount']) / split_multiplier), 3),
                         round(split_multiplier, 3)]
    my_history = my_history[::-1]
    if my_history[-1, 0].date() == pd.to_datetime("today").date():
        my_history = my_history[:-1, :]
    my_history_df = pd.DataFrame(data=my_history,
                                 columns=["date", "close", "dividend amount", "adjustment factor"])
    Path(save_path).mkdir(exist_ok=True)
    try:
        old_data = pd.read_csv(os.path.abspath(os.path.join(save_path, stock_of_interest)) + ".csv")
        old_data["date"] = pd.to_datetime(old_data["date"])
        old_data["close"] = round(old_data["close"], 3)
        old_data["dividend amount"] = round(old_data["dividend amount"], 3)
        old_data["adjustment factor"] = round(old_data["adjustment factor"], 3)
        my_history_df = pd.concat([old_data, my_history_df], ignore_index=True).drop_duplicates().reset_index(drop=True)
        if len(my_history_df['date']) != len(set(my_history_df['date'])):
            my_history_df = my_history_df.sort_values(by=["date"])
            print(my_history_df[my_history_df.duplicated(subset=["date"],
                                                         keep=False)])
            raise Exception("Discrepancies between old and new files. This could be due to forward/reverse splits."
                            "Data not updated, please manually fix!")
        else:
            my_history_df.to_csv(path_or_buf=(save_path + stock_of_interest + '.csv'),
                                 index=False)
    except FileNotFoundError:
        print("Local history of ticker '" + stock_of_interest + "' does not exist, created new file.")
        my_history_df.to_csv(path_or_buf=(os.path.abspath(os.path.join(save_path, stock_of_interest)) + ".csv"),
                             index=False)
    return my_history_df
def retrieve_price_history ( stock_of_interest , api_key , save_path ) : split_multiplier = 1 data_url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=' + stock_of_interest + '&outputsize=full&apikey=' + api_key history_data = pd . read_json ( data_url ) [ 'Time Series (Daily)' ] . iloc [ 5 : ] . reset_index ( ) history_data [ 'index' ] = pd . to_datetime ( history_data [ 'index' ] ) my_history = np . zeros ( ( len ( history_data ) , 4 ) , dtype = object ) for n in range ( len ( history_data ) ) : if n > 0 : temp_multiplier = float ( history_data . iloc [ n - 1 , 1 ] [ '8. split coefficient' ] ) if temp_multiplier != 1 : split_multiplier = split_multiplier * temp_multiplier my_history [ n ] = [ history_data . iloc [ n , 0 ] , round ( ( float ( history_data . iloc [ n , 1 ] [ '4. close' ] ) / split_multiplier ) , 3 ) , round ( ( float ( history_data . iloc [ n , 1 ] [ '7. dividend amount' ] ) / split_multiplier ) , 3 ) , round ( split_multiplier , 3 ) ] my_history = my_history [ : : - 1 ] if my_history [ - 1 , 0 ] . date ( ) == pd . to_datetime ( "today" ) . date ( ) : my_history = my_history [ : - 1 , : ] my_history_df = pd . DataFrame ( data = my_history , columns = [ "date" , "close" , "dividend amount" , "adjustment factor" ] ) Path ( save_path ) . mkdir ( exist_ok = True ) try : old_data = pd . read_csv ( os . path . abspath ( os . path . join ( save_path , stock_of_interest ) ) + ".csv" ) old_data [ "date" ] = pd . to_datetime ( old_data [ "date" ] ) old_data [ "close" ] = round ( old_data [ "close" ] , 3 ) old_data [ "dividend amount" ] = round ( old_data [ "dividend amount" ] , 3 ) old_data [ "adjustment factor" ] = round ( old_data [ "adjustment factor" ] , 3 ) my_history_df = pd . concat ( [ old_data , my_history_df ] , ignore_index = True ) . drop_duplicates ( ) . reset_index ( drop = True ) if len ( my_history_df [ 'date' ] ) != len ( set ( my_history_df [ 'date' ] ) ) : my_history_df = my_history_df . sort_values ( by = [ "date" ] ) print ( my_history_df [ my_history_df . duplicated ( subset = [ "date" ] , keep = False ) ] ) raise Exception ( "Discrepancies between old and new files. This could be due to forward/reverse splits." "Data not updated, please manually fix!" ) else : my_history_df . to_csv ( path_or_buf = ( save_path + stock_of_interest + '.csv' ) , index = False ) except FileNotFoundError : print ( "Local history of ticker '" + stock_of_interest + "' does not exist, created new file." ) my_history_df . to_csv ( path_or_buf = ( os . path . abspath ( os . path . join ( save_path , stock_of_interest ) ) + ".csv" ) , index = False ) return my_history_df
Retrieves historical daily closing price of a given ticker from the Alphavantage API.
Retrieves historical daily closing price of a given ticker from the Alphavantage API .
"""
    Retrieves historical daily closing price of a given ticker from the Alphavantage API.
    Adjusts ticker price and dividend payout accordingly to forward/reverse splits.
    Returns matrix with: date, adjusted closing price, adjusted dividend payout, and split factor
    Checks and appends the prices to local version of ticker history is present.

    :param stock_of_interest: ticker symbol (string)
    :param api_key: API key used to access the Alphavantage server (string)
    :param save_path: Path used to save data (string)
    :return: adjusted daily closing price and dividend payouts of the ticker (DataFrame)
    """ # Default parameters
 # Actual data starts at row 5
 # Reset index to be datetime format
 # Create DataFrame to store data
 # Extracting information we need
 # Check if stock forward/reverse split happened the following day (data is present -> past)
 # n > 0 since we are calling 'n - 1'
 # Update DataFrame
 # Reverse list such that the oldest price is first
 # Removes the last row if that is the current date
 # Convert to DataFrame and store
 # Create data directory if it doesn't exist
 # Sometimes there are rounding errors when using "read_csv"
 # Discrepancies are appended to the end of the concatenation

adjusted daily closing price and dividend payouts of the ticker (DataFrame)
adjusted daily closing price and dividend payouts of the ticker ( DataFrame )
stock_of_interest
ticker symbol (string)
ticker symbol ( string )
api_key
API key used to access the Alphavantage server (string)
API key used to access the Alphavantage server ( string )
save_path
Path used to save data (string)
Path used to save data ( string )