def main(): """ Reads in a CSV file specifying the portfolio of stocks and their respective ratios within the portfolio.""" stocks = {} with open(STOCK_RATIO_FILE) as csvfile: reader = csv.reader(csvfile, delimiter=',') for row in reader: ticker, fraction = row[0].strip(), float(row[1].strip()) stocks[ticker] = {} share = Share(ticker) scurr = share.get_currency() if scurr != CURRENCY: xchrate = float(Currency(scurr + CURRENCY).get_rate()) else: xchrate = 1.0 price = float(share.get_price()) * xchrate stocks[ticker]['price'] = price stocks[ticker]['in_currency'] = CURRENCY stocks[ticker]['target_ratio'] = fraction stocks[ticker]['name'] = share.get_name() stocks[ticker]['target_number'] = np.round(fraction * TOTAL_CASH / price) print(json.dumps(stocks, indent=2)) total_value = 0 for stock in stocks: s = stocks[stock] total_value += s['price'] * s['target_number'] print('Total Portfolio Value:') print(total_value)
def getAllStockData(ticker): '''Get a few random tickers.''' stock = Share(ticker) stock.refresh() data = { 'name': stock.get_name(), 'price': stock.get_price(), 'change': stock.get_change(), 'volume': stock.get_volume(), 'prev_close': stock.get_prev_close(), 'open': stock.get_open(), 'avg_daily_volume': stock.get_avg_daily_volume(), 'stock_exchange': stock.get_stock_exchange, 'market_cap': stock.get_market_cap(), 'book_value': stock.get_book_value(), 'ebitda': stock.get_ebitda(), 'dividend_share': stock.get_dividend_share(), 'dividend_yield': stock.get_dividend_yield(), 'earnings_share': stock.get_earnings_share(), 'days_high': stock.get_days_high(), 'days_low': stock.get_days_low(), 'year_high': stock.get_year_high(), 'year_low': stock.get_year_low(), '50day_moving_avg': stock.get_50day_moving_avg(), '200day_moving_avg': stock.get_200day_moving_avg(), 'price_earnings_ratio': stock.get_price_earnings_ratio(), 'price_earnings_growth_ratio': stock.get_price_earnings_growth_ratio(), 'get_price_sales': stock.get_price_sales(), 'get_price_book': stock.get_price_book(), 'get_short_ratio': stock.get_short_ratio(), 'trade_datetime': stock.get_trade_datetime(), 'percent_change_from_year_high': stock.get_percent_change_from_year_high(), 'percent_change_from_year_low': stock.get_percent_change_from_year_low(), 'change_from_year_low': stock.get_change_from_year_low(), 'change_from_year_high': stock.get_change_from_year_high(), 'percent_change_from_200_day_moving_average': stock.get_percent_change_from_200_day_moving_average(), 'change_from_200_day_moving_average': stock.get_change_from_200_day_moving_average(), 'percent_change_from_50_day_moving_average': stock.get_percent_change_from_50_day_moving_average(), 'change_from_50_day_moving_average': stock.get_change_from_50_day_moving_average(), 'EPS_estimate_next_quarter': stock.get_EPS_estimate_next_quarter(), 'EPS_estimate_next_year': stock.get_EPS_estimate_next_year(), 'ex_dividend_date': stock.get_ex_dividend_date(), 'EPS_estimate_current_year': stock.get_EPS_estimate_current_year(), 'price_EPS_estimate_next_year': stock.get_price_EPS_estimate_next_year(), 'price_EPS_estimate_current_year': stock.get_price_EPS_estimate_current_year(), 'one_yr_target_price': stock.get_one_yr_target_price(), 'change_percent_change': stock.get_change_percent_change(), 'divended_pay_date': stock.get_dividend_pay_date(), 'currency': stock.get_currency(), 'last_trade_with_time': stock.get_last_trade_with_time(), 'days_range': stock.get_days_range(), 'years_range': stock.get_year_range() } return data
class StockAnalysis: stock_symbol = 'YHOO' stock_company_name = "Yahoo Inc," quote = Share(stock_symbol) name = quote.get_name() stock_company_name = name.split(". ") def __init__(self,symbol): self.stock_symbol = symbol self.quote = Share(self.stock_symbol) self.name = self.quote.get_name() self.stock_company_name = self.name.split(". ") def getStockSymbol(self): return self.stock_symbol def getStockPriceInfo(self,symbol): stock_price = self.quote.get_price() currency = self.quote.get_currency() #name = self.quote.get_name() #stock_company_name = name.split(". ") price_info = {'Symbol':symbol,'Price':stock_price,'Currency':currency,'Name':self.stock_company_name[0]} return price_info def getDividendYieldInfo(self,symbol): dividend_yield = self.quote.get_dividend_yield() dividend_info = {'Symbol':symbol,'DividendYield':dividend_yield,'Unit':'%'} return dividend_info def getPayOurRatio(self,symbol): dividend_per_share = self.quote.get_dividend_share() EPS_current_year = self.quote.get_EPS_estimate_current_year() pay_out_ratio = (float(dividend_per_share) / float(EPS_current_year))*100 pay_out_ratio = float("{0:.2f}".format(pay_out_ratio)) pay_out_info = {'Symbol':symbol,'PayOut Ratio':pay_out_ratio, "Unit":"%",'Name':self.stock_company_name[0]} return pay_out_info def getDeividendDetails(self,symbol): dividend_per_share = self.quote.get_dividend_share() EPS_current_year = self.quote.get_EPS_estimate_current_year() dividend_yield = self.quote.get_dividend_yield() hold_date = self.quote.get_ex_dividend_date() payout_date = self.quote.get_dividend_pay_date() if dividend_per_share is None: dividend_per_share = "0" dividend_yield = "0%" payout_date = "None" hold_date = "None" pay_out_ratio = (float(dividend_per_share) / float(EPS_current_year))*100 pay_out_ratio = str(float("{0:.2f}".format(pay_out_ratio)))+"%" dividend_info = {'Symbol':symbol, 'DividedPerShare':dividend_per_share, 'EPSCurrentYearEstimation': EPS_current_year, 'PayOutRatio':pay_out_ratio,'ExDividedDate':hold_date,'LastPayoutDate':payout_date,'DividendYield':dividend_yield,'Name':self.stock_company_name[0]} return dividend_info def getExDividedDate(self,symbol): hold_date = self.quote.get_ex_dividend_date() payout_date = self.quote.get_dividend_pay_date() data = {'Symbol':symbol,'ExDividedDate':hold_date,'LastPayoutDate':payout_date,'Name':self.stock_company_name[0]} return data def isDividedRateHealthy(self): pass def getDividendDateDetails(self): pass def getNumberOfOutStandingShares(self,symbol): total_market_cap = self.quote.get_market_cap() current_price = self.quote.get_prev_close() if "B" in total_market_cap: total_market_cap = total_market_cap.rstrip("B") total_market_cap = float(total_market_cap)*1000000000 elif "M" in total_market_cap: total_market_cap = total_market_cap.rstrip("M") total_market_cap = float(total_market_cap)*100000000 out_standing_shares = (total_market_cap/float(current_price))/1000000 out_standing_shares = round(out_standing_shares,2) data = {'Symbol':symbol,'OutStandingShares':out_standing_shares,'Name':self.stock_company_name[0]} return data def get200DaysLowPrice(self,symbol): pass def get200DaysHighPrice(self,symbol): pass def get50DaysLowPrice(self,symbol): pass def get50DaysHighPrice(self,symbol): pass def getPERation(self): pass
print "get_percent_change_from_year_low:", tesla.get_percent_change_from_year_low( ) print "get_change_from_year_low:", tesla.get_change_from_year_low() print "get_change_from_year_high:", tesla.get_change_from_year_high() print "get_percent_change_from_200_day_moving_average:", tesla.get_percent_change_from_200_day_moving_average( ) print "get_change_from_200_day_moving_average:", tesla.get_change_from_200_day_moving_average( ) print "get_percent_change_from_50_day_moving_average:", tesla.get_percent_change_from_50_day_moving_average( ) print "get_change_from_50_day_moving_average:", tesla.get_change_from_50_day_moving_average( ) print "get_EPS_estimate_next_quarter:", tesla.get_EPS_estimate_next_quarter() print "get_EPS_estimate_next_year:", tesla.get_EPS_estimate_next_year() print "get_ex_dividend_date:", tesla.get_ex_dividend_date() print "get_EPS_estimate_current_year:", tesla.get_EPS_estimate_current_year() print "get_price_EPS_estimate_next_year:", tesla.get_price_EPS_estimate_next_year( ) print "get_price_EPS_estimate_current_year:", tesla.get_price_EPS_estimate_current_year( ) print "get_one_yr_target_price:", tesla.get_one_yr_target_price() print "get_change_percent_change:", tesla.get_change_percent_change() print "get_dividend_pay_date:", tesla.get_dividend_pay_date() print "get_currency:", tesla.get_currency() print "get_last_trade_with_time:", tesla.get_last_trade_with_time() print "get_days_range:", tesla.get_days_range() print "get_year_range:", tesla.get_year_range() # http://stackoverflow.com/questions/24233385/pulling-yahoo-finance-data-using-python # http://chartapi.finance.yahoo.com/instrument/1.0/TSLA/chartdata;type=quote;range=1y/csv
#!/usr/bin/env python3 # Documentation at https://pypi.python.org/pypi/yahoo-finance from yahoo_finance import Share share_apple = Share("AAPL") print(share_apple.get_name()) print(share_apple.get_price()) print(share_apple.get_trade_datetime()) print(share_apple.get_currency()) print(share_apple.get_200day_moving_avg()) share_apple.refresh() print(share_apple.get_price()) print("---") from yahoo_finance import Currency btc_in_eur = Currency("BTCEUR") print(btc_in_eur.get_rate()) print(btc_in_eur.get_trade_datetime()) btc_in_eur.refresh() print(btc_in_eur.get_rate())