예제 #1
0
def getHistoricStockPrices(stock, yahooEarningsDF, daysAroundEarnings=10):
    """
    Get Historic Stock Price data ----
    add daysAroundEarnings Days forward/back - this will be used to count back and plot time
    durationString,The amount of time (or Valid Duration String units) to go back from the request's given end date and time.

    Parameters
    ----------
    stock :             Stock symbol string
    yahooEarningsDF:    a pandas DF of scraped companies and earnings data
    daysAroundEarnings: Number for Number of Days before / after Earnings date

    Returns
    -------
    # time series panda dataframes
    earningsPastStock

        """
    # create time series panda dataframes
    earningsPastStock = pd.DataFrame()

    # get the Stock into yahoofinancials module
    yahoo_financials = YahooFinancials(stock)

    # Bar Size = Daily weekly etc
    barSizeSetting = 'daily'

    # Get Historical price (earningsPastStock) and Implied Volatility (earningsPastImpVol) Data
    for earnData in range(0, len(yahooEarningsDF)):
        # set start and end Date
        # set to format 2018-09-29 // String Dash Separator
        endDateTime = dateUtils.getDateStringDashSeprtors(
            yahooEarningsDF['Earnings_Date'][earnData] +
            datetime.timedelta(days=daysAroundEarnings))
        startDateTime = dateUtils.getDateStringDashSeprtors(
            yahooEarningsDF['Earnings_Date'][earnData] -
            datetime.timedelta(days=daysAroundEarnings))

        # Get stock prices from yahoo_financials
        historical_stock_prices = yahoo_financials.get_historical_price_data(
            startDateTime, endDateTime, barSizeSetting)
        historical_stock_prices = pd.DataFrame(
            historical_stock_prices[stock]['prices'])

        # add Min/Max Moves
        # Keep appending the info
        earningsPastStock = earningsPastStock.append(historical_stock_prices)

    return earningsPastStock
예제 #2
0
def getEarningsForWeek(startday):
    """
    This is the function that is called to start the Yahoo page scraping.

    Parameters
    ----------
    startday : date on which to start earnings week

    Returns
    -------
    anEarningsDF: a DF of companies for the earnings week
    """
    anEarningsDF = pd.DataFrame(
        columns=['Symbol', 'Earnings_Date', 'Company', 'Earnings Call Time'])

    # Week start date
    aStartDay = dateUtils.getDateFromISO8601(startday)

    #Start Monday go to Friday
    for x in range(5):
        aDay = aStartDay + datetime.timedelta(days=x)
        aNewEarningsDF = getEarningsOnDate(
            dateUtils.getDateStringDashSeprtors(aDay))
        try:
            anEarningsDF = anEarningsDF.append(aNewEarningsDF)
        except TypeError:
            print("No Earnings on: ", aDay)
            continue
        print('Working Day: ', aDay)

    return anEarningsDF.reset_index(drop=True)
def getHistoricStockPrices(stock, yahooEarningsDF, daysAroundEarnings=10):
    """
    Get Historic Stock Price data from YahooFinancials----
    add daysAroundEarnings Days forward/back - this will be used to earnDateRow back and plot time
    durationString,The amount of time (or Valid Duration String units) to go back from the
    request's given end date and time.

    Parameters
    ----------
    stock :             Stock symbol string
    yahooEarningsDF:    a pandas DF of scraped companies and earnings data
    daysAroundEarnings: Number for Number of Days before / after Earnings date to use for processing

    Returns
    -------
    # update yahooEarningsDF - panda dataframe - closing prince info and closing $ and % deltas
    yahooEarningsDF

        """

    yahooEarningsDF['EDClosePrice'] = np.nan
    yahooEarningsDF['EDPlus1ClosePrice'] = np.nan
    yahooEarningsDF['EDMinus1ClosePrice'] = np.nan
    yahooEarningsDF['EDPlus4ClosePrice'] = np.nan
    yahooEarningsDF['Plus4MinusED'] = np.nan
    yahooEarningsDF['Plus1MinusED'] = np.nan
    yahooEarningsDF['EDPlus4ClosePriceDiffPercent'] = np.nan
    yahooEarningsDF['EDPlus1ClosePriceDiffPercent'] = np.nan
    yahooEarningsDF['EDCloseToFwd1DayOpen'] = np.nan

    # get the Stock into yahoofinancials module
    yahoo_financials = YahooFinancials(stock)

    # Bar Size = Daily weekly etc
    barSizeSetting = 'daily'

    # Get Historical price data for each Stock past earnings Dates
    for earnDateRow in range(0, len(yahooEarningsDF)):
        # set start and end Date
        # set to format 2018-09-29 // String Dash Separator
        endDateTime = dateUtils.getDateStringDashSeprtors(
            yahooEarningsDF['Earnings_Date'][earnDateRow] +
            datetime.timedelta(days=daysAroundEarnings))
        startDateTime = dateUtils.getDateStringDashSeprtors(
            yahooEarningsDF['Earnings_Date'][earnDateRow] -
            datetime.timedelta(days=daysAroundEarnings))

        # Get historic stock prices from yahoofinancials within daysAroundEarnings timeframe
        historical_stock_prices = yahoo_financials.get_historical_price_data(
            startDateTime, endDateTime, barSizeSetting)
        #create DF from prices
        historical_stock_prices = pd.DataFrame(
            historical_stock_prices[stock]['prices'])

        yahooEarningsDF = getEarningsDayPricing(
            earnDateRow, historical_stock_prices, yahooEarningsDF,
            yahooEarningsDF['Earnings_Date'][earnDateRow])

    # calculate price and persent deltas
    yahooEarningsDF['Plus4MinusED'] = yahooEarningsDF[
        'EDPlus4ClosePrice'] - yahooEarningsDF['EDClosePrice']
    yahooEarningsDF['Plus1MinusED'] = yahooEarningsDF[
        'EDPlus1ClosePrice'] - yahooEarningsDF['EDClosePrice']

    yahooEarningsDF['EDPlus4ClosePriceDiffPercent'] = 1 - (
        yahooEarningsDF['EDClosePrice'] / yahooEarningsDF['EDPlus4ClosePrice'])
    yahooEarningsDF['EDPlus1ClosePriceDiffPercent'] = 1 - (
        yahooEarningsDF['EDClosePrice'] / yahooEarningsDF['EDPlus1ClosePrice'])

    print(
        "-------------------------------------- cat \n Meow------------------------------"
    )

    return yahooEarningsDF
예제 #4
0
def addPastMarketData(stocksPastEarningsDF, daysAroundEarnings = 10):
    """
    Add Market Data to companies in  stocksPastEarningsDF
    #todo remove daysAroundEarnings = 10 // should be 5??
    Parameters
    ----------
    stocksPastEarningsDF: DF of 'Symbol', 'Earnings_Date', 'Company', 'Earnings Call Time'

    Returns
    -------
    updated DF w/ Market Data for companies in earningsDF

    """
# get info from the time of this run
    stocksPastEarningsDF['High'] = np.nan
    stocksPastEarningsDF['Open'] = np.nan
    stocksPastEarningsDF['Volume'] = np.nan
    stocksPastEarningsDF['Low'] = np.nan
    stocksPastEarningsDF['Close'] = np.nan
    # Closing Prices of Interest
    stocksPastEarningsDF['EDClose'] = np.nan          # Earning Day Closing Price
    stocksPastEarningsDF['EDFwd1DayClose'] = np.nan   # Earning Day Forward 1 Day - Closing Price
    stocksPastEarningsDF['EDBak1DayClose'] = np.nan   # Earning Day Back 1 Day - Closing Price
    stocksPastEarningsDF['EDFwd4DayClose'] = np.nan   # Earning Day Forward 4 Days - Closing Price
    # Deltas on Closing Prices of Interest
    stocksPastEarningsDF['EDDiffFwd4Close'] = np.nan # Earning Day Subtract the Forward 4 Days Closing Price
    stocksPastEarningsDF['EDDiffFwd1Close'] = np.nan # Earning Day Subtract the Forward 1 Day Closing Price
    stocksPastEarningsDF['EDFwd1DayClosePercentDelta'] = np.nan # Earning Day % Delta the Forward 1 Day Closing Price
    stocksPastEarningsDF['EDFwd4DayClosePercentDelta'] = np.nan # Earning Day % Delta the Forward 4 Day Closing Price

    stocksPastEarningsDF['EDFwd1DayOpen'] = np.nan # Earning Day Forward 1 Day Open Price
    stocksPastEarningsDF['EDBak1DayOpen'] = np.nan # Earning Day Back 1 Day Open Price

    theStock = stocksPastEarningsDF.Symbol[0]

    lenDF = len(stocksPastEarningsDF)
    if lenDF > 32:
        lenDF = 32
        print('--> Calculating',  lenDF, 'past Qtrs // 8 years - Max')
        pruneDF = True
    else:
        print('--> Calculating', lenDF, 'past Qtrs //', f'{lenDF/4:1.1f}', 'years')
        pruneDF = False

    yahoo_financials = YahooFinancials(theStock)

    for earnDateRow in stocksPastEarningsDF.itertuples():
        #print(earnDateRow.Symbol, ' / theStock: ', theStock,  ' @  ', lenDF, 'earnDateRow.Index: ', earnDateRow.Index, end=", ")

        if lenDF == 0:
            break
        else:
            lenDF = lenDF - 1

        # set start and end Date
        # set to format 2018-09-29 // String Dash Separator
        endDateTime = dateUtils.getDateStringDashSeprtors(earnDateRow.Earnings_Date
                                                          +datetime.timedelta(days=daysAroundEarnings))
        startDateTime = dateUtils.getDateStringDashSeprtors(earnDateRow.Earnings_Date
                                                            -datetime.timedelta(days=daysAroundEarnings))

        # Drop future earnings dates
        if earnDateRow.Earnings_Date + datetime.timedelta(days=daysAroundEarnings) > datetime.datetime.today():
            # todo can append the dictionary to the DF as well.
            stocksPastEarningsDF.at[earnDateRow.Index, 'High']   = np.nan
            stocksPastEarningsDF.at[earnDateRow.Index, 'Open']   = np.nan
            stocksPastEarningsDF.at[earnDateRow.Index, 'Volume'] = np.nan
            stocksPastEarningsDF.at[earnDateRow.Index, 'Low']    = np.nan
            stocksPastEarningsDF.at[earnDateRow.Index, 'Close']  = np.nan
            # stocksPastEarningsDF.at[earnDateRow.Index, 'Last']   = np.nan
            continue

        # Get historic stock prices from yahoofinancials within daysAroundEarnings timeframe
        historical_stock_prices = yahoo_financials.get_historical_price_data(startDateTime, endDateTime, 'daily')
        #print("historical_stock_prices: ", historical_stock_prices)

        try:
            historical_stock_pricesDF = pd.DataFrame(historical_stock_prices[theStock]['prices'])
        except KeyError:
            print('     Stock: ', theStock)
            print('     prices:  ', historical_stock_prices)
            print('         ', KeyError, 'Prices: ', '       setup.addPastMarketData')
            break
        except TypeError:
            print('     Stock: ', theStock)
            print('     prices:  ', historical_stock_prices)
            print('         ', TypeError, 'NoneType', '       setup.addPastMarketData')
            continue

        # recreate index as the 'date' column for price
        historical_stock_pricesDF['date'] = historical_stock_pricesDF['formatted_date'].apply(
            dateUtils.getDateFromISO8601)

        historical_stock_pricesDF = historical_stock_pricesDF.set_index("date", drop=False)
        # historical_stock_prices.index = pd.to_datetime(historical_stock_prices.index)

        try:
            # todo can append the dictionary to the DF as well.
            stocksPastEarningsDF.at[earnDateRow.Index, 'High']   = historical_stock_pricesDF.high[earnDateRow.Earnings_Date.date()]
            stocksPastEarningsDF.at[earnDateRow.Index, 'Open']   = historical_stock_pricesDF.open[earnDateRow.Earnings_Date.date()]
            stocksPastEarningsDF.at[earnDateRow.Index, 'Volume'] = historical_stock_pricesDF.volume[earnDateRow.Earnings_Date.date()]
            stocksPastEarningsDF.at[earnDateRow.Index, 'Low']    = historical_stock_pricesDF.low[earnDateRow.Earnings_Date.date()]
            stocksPastEarningsDF.at[earnDateRow.Index, 'Close']  = historical_stock_pricesDF.close[earnDateRow.Earnings_Date.date()]
            # stocksPastEarningsDF.at[earnDateRow.Index, 'Last']   = historical_stock_pricesDF.adjclose[earnDateRow.Earnings_Date.date()]
        except KeyError:
            print('     Stock: ', theStock)
            print('         ', KeyError, 'Historical Price Issue in:', '       setup.addPastMarketData')

            continue

        stocksPastEarningsDF = getDaysPastEarningsClosePrices(earnDateRow, historical_stock_pricesDF, stocksPastEarningsDF)
        stocksPastEarningsDF = calcPriceDeltas(stocksPastEarningsDF)

    stocksPastEarningsDF = formatForCSVFile(stocksPastEarningsDF, pruneDF)

    return stocksPastEarningsDF