def getMarketData(): tickers = 'AAPL' financials = YahooFinancials(tickers) # company_stock_price = financials.get_stock_price_data() #gets stock price information historical_stock_prices_data = financials.get_historical_price_data( '2015-11-21', '2020-11-21', 'daily') #gets historical daily stock price of company # get_Div_data(historical_stock_prices_data[tickers]) get_stock_price_data(historical_stock_prices_data[tickers]) # company_balance_sheet_data_qt = financials.get_financial_stmts('quarterly', 'balance') #get balance sheet # company_income_statement_data_qt = financials.get_financial_stmts('quarterly', 'income') #get income statement company_key_statistics_data = financials.get_key_statistics_data( ) #includes profit margins, forward eps, yearly change etc. # get_forward_pe(company_key_statistics_data[tickers]) # get_trailing_eps(company_key_statistics_data[tickers]) # get_foward_eps(company_key_statistics_data[tickers]) # get_ytdReturn(company_key_statistics_data[tickers]) company_earnings_data = financials.get_stock_earnings_data( ) #historical eps only for 1 year span # get_earnings_data(company_earnings_data[tickers]) company_dividend_yield = financials.get_dividend_yield( ) #current dividends yield company_dividend = financials.get_dividend_rate() #current dividends rate company_avg_div_yield_1year = financials.get_annual_avg_div_yield( ) #average 1 year div yield company_avg_div_yield_5year = financials.get_five_yr_avg_div_yield( ) #average 5 year div yield company_eps = financials.get_earnings_per_share() #current eps company_pe = financials.get_pe_ratio() #current pe ratio company_beta = financials.get_beta() #current beta company_current_stock_price = financials.get_current_price( ) #current stock price company_revenue = financials.get_total_revenue() #current company revenue company_operating_income = financials.get_operating_income( ) #current company operating income company_net_income = financials.get_net_income() #current net income company_yearly_high = financials.get_yearly_high() #get yearly high company_yearly_low = financials.get_yearly_low() #get yearly low company_moving_50 = financials.get_50day_moving_avg( ) #50 day moving average of stock company_moving_200 = financials.get_200day_moving_avg( ) #200 day moving average of stock
def analyze_stock(pstock): days = 1000 now = datetime.datetime.now() year_before = now - datetime.timedelta(days=days) stock = pstock.stock yf = YahooFinancials(stock.name) stock_analysis = StockAnalysis(yf) # update important fields stock.last_price = convert_number(yf.get_current_price()) stock.price_sales_ratio = convert_number(yf.get_price_to_sales()) stock.dividend_yield = convert_number(yf.get_annual_avg_div_yield()) # historical_prices = yf.get_historical_price_data(stock.name, # year_before.strftime('%Y%m%d'), now.strftime('%Y%m%d'), 'daily') #[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos'] # history = [] # historical_prices = historical_prices[1:] # has_history = True # for p in historical_prices: # # error index out of range here # if(len(p) == 1): # has_history = False # break # close_v = float(p[4]) # date = datetime.datetime.strptime(p[0],'%Y-%m-%d') # history.append({"date":date, "close_value":close_v}) # price_trends = [] # volatilities = [ # {'days':100}, # {'days':300}, # {'days':600}, # ] # if has_history: # # today first # history = sorted(history, key=lambda p: p['date']) # assert history[1]['date'] > history[0]['date'] # for v in volatilities: # if len(history) < v['days']: # v['days'] = len(history) # v['volatility'] = round(calculate_historical_volatility(history[-v['days']:]), 2) # v['start_date'] = history[-v['days']:][0]['date'] # stock_analysis.volatility = volatilities[1]['volatility'] # stock.volatility = stock_analysis.volatility # start = 0 # interval = int(len(history) / 5.0) # while len(history) > (start + interval) and interval > 0: # trend = calculate_historical_price_trend(history[start:start+interval]) # price_trends.append(trend) # start = start + interval # #if len(history) < (start + interval): # # interval = len(history) - start - 1 # trend = calculate_historical_price_trend(history) # price_trends.append(trend) # stock_analysis.trend = trend stock.value_score = stock_analysis.value_score_analysis() stock.save() return { #'price_trends':price_trends, 'stock':stock, #'volatilities':volatilities, 'stock_analysis':stock_analysis, 'days':days, #'history': history }