def getRevenueEstimate(company, fileName=pathConfig.cache+'revenueEstimate.csv'): parName1 = ''.join([company, '-low']) parName2 = ''.join([company, '-growth']) if Path(fileName).is_file() and isColumnExist([parName1, parName2], fileName): return getDF([parName1, parName2], fileName) else: url = ''.join(['https://query1.finance.yahoo.com/v10/finance/quoteSummary/', company, '?modules=earningsTrend']) urlName = 'Yahoo Finance QuoteSummary API' content = fetchUrlWithLog(url, requestRetrySession, urlName) forcast1Quarter = None forcast1Year = None try: trendDict = json.loads( content)["quoteSummary"]["result"][0]["earningsTrend"]["trend"] forcast1Year = trendDict[2]["revenueEstimate"] forcast2Year = trendDict[3]["revenueEstimate"] except Exception as x: print('JSON Parse failed when getting estimate revenue :(', x.__class__.__name__) return pd.DataFrame() else: forcast1YearDF = pd.DataFrame( forcast1Year).loc['raw'].rename('1 Year') forcast2YearDF = pd.DataFrame( forcast2Year).loc['raw'].rename('2 Year') DF = pd.concat([forcast1YearDF, forcast2YearDF], axis=1) NewDF = DF.loc[['low', 'growth']].rename( {'low': parName1, 'growth': parName2}, axis='index') return saveDFtoFile(NewDF, [parName1, parName2], fileName)
def getMyStock(company, fileName=pathConfig.cache+'myStock.csv'): sheetName = "Stock" sheetTabName = 'MyStock' def getName(bidArr): result = [] i = 1 for ele in bidArr: result.append(ele+'-'+str(i)) i = i + 1 return result def getMyStockSheet(sheetTabName, data): bidArr = [ele for ele in data[4] if re.match("Bid/BidDate-*", ele)] columnList = [] columnList.extend(['Company', 'Own Shares']) columnList.extend(getName(bidArr)) df = pd.DataFrame(data[6:, 1:], columns=columnList) return df.set_index('Company') if Path(fileName).is_file() and isColumnExist(company, fileName): return getDF(company, fileName) else: data = getDataFromGoogleSheet(sheetName, sheetTabName) df = getMyStockSheet(sheetTabName, np.array(data)) return saveDFtoFile(df, company, fileName)
def getTreasuriesYield(fileName=pathConfig.cache+'treasuriesYield.csv'): parName = ''.join(['treasuriesYield']) if Path(fileName).is_file() and isColumnExist(parName, fileName): # if file exist read from file return getDF(parName, fileName) else: # if file not exist call api and save in file df = quandl.get("ML/AAAEY").sort_index(ascending=False) treasuriesYieldDF = df.iloc[:2].T.loc[['BAMLC0A1CAAAEY']].rename( {'BAMLC0A1CAAAEY': parName}, axis='index') return saveDFtoFile(treasuriesYieldDF, parName, fileName)
def getCompanyAndIndustryInfo(company, fileName=pathConfig.cache+'company.csv'): sheetName = "Stock" sheetTabName = 'Company' def getCompanySheet(sheetTabName, data): df = pd.DataFrame(data[1:], columns=data[0]) return df.set_index('Name') if Path(fileName).is_file() and isColumnExist(company, fileName): return getDF(company, fileName) else: data = getDataFromGoogleSheet(sheetName, sheetTabName) df = getCompanySheet(data) return saveDFtoFile(df, company, fileName)
def getStockPrice(company, fileName=pathConfig.cache+'stockPrice.csv'): parName1 = ''.join([company, '-amount']) parName2 = ''.join([company, '-date']) if Path(fileName).is_file() and isColumnExist([parName1, parName2], fileName): # if file exist read from file return getDF([parName1, parName2], fileName) else: # if file not exist call api and save in file url = ''.join(['https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=', company, '&apikey=', alphaVantageAPIKey, '&datatype=csv']) urlName = 'Alphavantage' content = fetchUrlWithLog(url, requestRetrySession, urlName) df = pd.read_csv(io.StringIO(content.decode('utf-8'))) time.sleep(20) # DF = pd.DataFrame(dividendDict).rename( # {'amount': parName1, 'date': parName2}, axis='index') # priceDF = df.T.loc[['timestamp', 'close']].rename( # {'timestamp': 'timestamp', 'close': company}, axis='index') priceDF = df.T.loc[['timestamp', 'close']].rename( {'close': parName1, 'timestamp': parName2}, axis='index') return saveDFtoFile(priceDF, company, fileName)
def getMyDividendRecord(myStockDF, company, fileName=pathConfig.cache+'myDividendRecord.csv'): parName1 = ''.join([company, '-amount']) parName2 = ''.join([company, '-date']) firstBidTime = getBidDate(myStockDF) firstBidUnixTimestamp = getUnixTimeStamp(firstBidTime) if Path(fileName).is_file() and isColumnExist([parName1, parName2], fileName): return getDF([parName1, parName2], fileName) else: url = ''.join(['https://query1.finance.yahoo.com/v8/finance/chart/', company, '?period1=', firstBidUnixTimestamp, '&period2=9999999999&interval=1d&includePrePost=false&events=div%2Csplit']) urlName = 'Yahoo Finance Chart API' content = fetchUrlWithLog(url, requestRetrySession, urlName) try: dividendDict = json.loads( content)["chart"]["result"][0]["events"]["dividends"] except Exception as x: print('JSON Parse failed when getting dividend :(', x.__class__.__name__) return pd.DataFrame() else: DF = pd.DataFrame(dividendDict).rename( {'amount': parName1, 'date': parName2}, axis='index') return saveDFtoFile(DF, [parName1, parName2], fileName)
def getDF(self, key, name): return getDF(key, name)