def getQuotes(self, companyLabel): # TODO: if NO CHANGE: # return nothing print 'Fetching Quotes', companyLabel results = ystockquote.get_historical_prices(companyLabel, '20100101', '20130301') div = ystockquote.get_dividend_yield(companyLabel) print 'Fetched Quotes' if str(div) == 'N/A': div = 0 else: div = float(div) * .01 del results[0] # column names quotes = [] for i,entry in enumerate(results): # second column is opening price if i+1 >= len(results) or self.isFirstWorkDay(entry[0], results[i+1][0]): gain = float(entry[1+2]) * (1+div) quotes.append(gain) return quotes
def get_current_dividened_yield(self): cur_div = Decimal(0) cur_div = ystockquote.get_dividend_yield(self.symbol) try: cur_div = Decimal(cur_div) except: pass return cur_div
def update(stock1): stock1.lastprice = ystockquote.get_price(stock1) stock1.volume = ystockquote.get_volume(stock1) price_change = ystockquote.get_change(stock1) market_cap = ystockquote.get_market_cap(stock1) get_high = ystockquote.get_52_week_high(stock1) get_low = ystockquote.get_52_week_low(stock1) pb_ratio = ystockquote.get_price_book_ratio(stock1) ebitda = ystockquote.get_ebitda(stock1) dividend = ystockquote.get_dividend_yield(stock1) stock1.save()
def test_get_all(self): symbol = 'GOOG' all_info = ystockquote.get_all(symbol) self.assertIsInstance(all_info, dict) self.assertEqual(all_info['dividend_yield'], ystockquote.get_dividend_yield(symbol)) self.assertEqual(all_info['fifty_sma'], ystockquote.get_50_sma(symbol)) self.assertEqual(all_info['company_name'], ystockquote.get_company_name(symbol)) self.assertEqual(all_info['book_value'], ystockquote.get_book_value(symbol))
def update(stock1): stock1.lastprice = ystockquote.get_price(stock1) stock1.volume = ystockquote.get_volume(stock1) stock1.price_change = float(ystockquote.get_change(stock1)) stock1.market_cap = ystockquote.get_market_cap(stock1) stock1.get_high = ystockquote.get_52_week_high(stock1) stock1.get_low = ystockquote.get_52_week_low(stock1) stock1.pb_ratio = ystockquote.get_price_book_ratio(stock1) stock1.pe_ratio = ystockquote.get_price_earnings_ratio(stock1) stock1.dividend = ystockquote.get_dividend_yield(stock1) stock1.peg = get_peg(stock1) stock1.revenue = get_revenue(stock1) stock1.a_gain = get_annualized_gain(stock1) stock1.save()
def _main(): for s in ["NA.TO", "XBB.TO", "NOU.V", "AP-UN.TO", "BRK-A", "AAPL"]: print("=============================================") print("s: {}".format(s)) print("get_name: {}".format(ysq.get_name(s))) print("get_price: {}".format(ysq.get_price(s))) print("get_volume: {}".format(ysq.get_volume(s))) print("get_stock_exchange: {}".format(ysq.get_stock_exchange(s))) print("get_market_cap: {}".format(ysq.get_market_cap(s))) print("get_dividend_yield: {}".format(ysq.get_dividend_yield(s))) print("get_price_earnings_ratio: {}".format( ysq.get_price_earnings_ratio(s))) print("get_52_week_low: {}".format(ysq.get_52_week_low(s))) print("get_52_week_high: {}".format(ysq.get_52_week_high(s))) print("get_currency: {}".format(ysq.get_currency(s)))
def check_db(path): # Create data base: db = sdm.StockDBMgr('./stock_db/' + path, startdate, enddate) # db.update_all_symbols() inv = [] symbolList = db.get_all_symbols() print(len(symbolList)) print(symbolList) for s in symbolList: if False: print("symbol:{}, yield:{}, name:{}".format( s, sq.get_dividend_yield(s), sq.get_name(s))) if not db.validate_symbol_data(s): inv.append(s) continue # Only applies if recent download... if True: df = db.get_symbol_data(s) t = startdate if df is not None and len(df) > 0: # t = r[-1].date t = df.iloc[-1].name.date() else: inv.append(s) continue if (today - t) > datetime.timedelta(4): inv.append(s) print("%s: len = %d" % (s, len(df))) print("Invalid list:") for s in inv: print(s)
def test_get_dividend_yield(self): value = ystockquote.get_dividend_yield(self.symbol) self.assertIsInstance(value, str)
##Some of the methods available name = ystockquote.get_company_name('GOOG') mktcap = ystockquote.get_market_cap('GOOG') print ("Name: %s , Market Cap %s" %(name,mktcap)) ystockquote.get_200_sma() ystockquote.get_50_sma() ystockquote.get_annualized_gain() ystockquote.get_1_year_target() ystockquote.get_52_week_high() ystockquote.get_52_week_low() ystockquote.get_change_200_sma() ystockquote.get_52_week_range() ystockquote.get_book_value() ystockquote.get_eps_estimate_current_year() ystockquote.get_eps() ystockquote.get_ticker_trend() ystockquote.get_short_ratio() ystockquote.get_revenue() ystockquote.get_average_daily_volume() ystockquote.get_float_shares() ystockquote.get_dividend_yield() ystockquote.get_notes() ystockquote.get_pe() ystockquote.get_all() print (ystockquote.get_all('GOOG'))
def individual_stock(request, stock_id): stock1 = get_object_or_404(Stock, pk=stock_id) # Stock Information per Stock stock1.lastprice = ystockquote.get_price(stock1) stock1.volume = ystockquote.get_volume(stock1) price_change = ystockquote.get_change(stock1) market_cap = ystockquote.get_market_cap(stock1) get_high = ystockquote.get_52_week_high(stock1) get_low = ystockquote.get_52_week_low(stock1) pb_ratio = ystockquote.get_price_book_ratio(stock1) ebitda = ystockquote.get_ebitda(stock1) dividend = ystockquote.get_dividend_yield(stock1) # Graph # Last known weekday current_day = weekday().isoformat() # Retrieve live data YYYY-MM-DD historical_price = ystockquote.get_historical_prices(stock1, '2010-01-24', current_day) correct_order = sorted(historical_price) stock_prices = [] dates = [] for values in correct_order: stock_prices.append(historical_price[values]['Adj Close']) dates.append(values) # Convert to Float for p in range(len(stock_prices)): stock_prices[p] = float(stock_prices[p]) # Convert to Datetime Format dates_objects = [] for d in dates: dates_objects.append(datetime.strptime(d,'%Y-%m-%d')) source = ColumnDataSource(data=dict(x=dates_objects,y=stock_prices, time=dates)) # Tools hover = HoverTool(tooltips=[('Stock Price','@y'),('time','@time'),], mode='vline') crosshair = CrosshairTool(dimensions=['height']) TOOLS = [hover, crosshair] plot = figure(x_axis_type="datetime", responsive = True ,plot_height=250, tools = TOOLS, toolbar_location=None) plot.line('x','y',source=source) first_date = dates[0] last_date = dates[-1] callback = CustomJS(args=dict(x_range=plot.x_range), code=""" var start = cb_obj.get("value"); x_range.set("start", start); x_range.set("end", start+2); """) slider = vform(Slider(start=0, end=100, title="Start Date", callback=callback)) widget_script, widget_div = components(slider) script, div = components(plot) stock1.save() context = {'stock':stock1, 'hprice': stock_prices, 'widget_script': widget_script, 'widget_div': widget_div, 'the_script': script, 'the_div':div, 'thedate': dates_objects, 'dates':dates } return render(request, 'stocktracker/individual.html', context)
def individual_stock(request, stock_id): stock1 = get_object_or_404(Stock, pk=stock_id) # Stock Information per Stock stock1.lastprice = ystockquote.get_price(stock1) stock1.volume = ystockquote.get_volume(stock1) price_change = ystockquote.get_change(stock1) market_cap = ystockquote.get_market_cap(stock1) get_high = ystockquote.get_52_week_high(stock1) get_low = ystockquote.get_52_week_low(stock1) pb_ratio = ystockquote.get_price_book_ratio(stock1) ebitda = ystockquote.get_ebitda(stock1) dividend = ystockquote.get_dividend_yield(stock1) # Graph # Last known weekday current_day = weekday().isoformat() # Retrieve live data YYYY-MM-DD historical_price = ystockquote.get_historical_prices( stock1, '2010-01-24', current_day) correct_order = sorted(historical_price) stock_prices = [] dates = [] for values in correct_order: stock_prices.append(historical_price[values]['Adj Close']) dates.append(values) # Convert to Float for p in range(len(stock_prices)): stock_prices[p] = float(stock_prices[p]) # Convert to Datetime Format dates_objects = [] for d in dates: dates_objects.append(datetime.strptime(d, '%Y-%m-%d')) source = ColumnDataSource( data=dict(x=dates_objects, y=stock_prices, time=dates)) # Tools hover = HoverTool(tooltips=[ ('Stock Price', '@y'), ('time', '@time'), ], mode='vline') crosshair = CrosshairTool(dimensions=['height']) TOOLS = [hover, crosshair] plot = figure(x_axis_type="datetime", responsive=True, plot_height=250, tools=TOOLS, toolbar_location=None) plot.line('x', 'y', source=source) first_date = dates[0] last_date = dates[-1] callback = CustomJS(args=dict(x_range=plot.x_range), code=""" var start = cb_obj.get("value"); x_range.set("start", start); x_range.set("end", start+2); """) slider = vform( Slider(start=0, end=100, title="Start Date", callback=callback)) widget_script, widget_div = components(slider) script, div = components(plot) stock1.save() context = { 'stock': stock1, 'hprice': stock_prices, 'widget_script': widget_script, 'widget_div': widget_div, 'the_script': script, 'the_div': div, 'thedate': dates_objects, 'dates': dates } return render(request, 'stocktracker/individual.html', context)
def yahoo_play(): # db = sdm.StockDBMgr('./stock_db/tsx', startdate, enddate) # for s in db.get_all_symbols(): for s in ['SPY', 'NA.TO', 'XBB.TO', 'AP-UN.TO', 'AAPL', 'XOM']: print(s, ysq.get_dividend_yield(s))
def test_ysq_api_dividend(s): assert 0 <= ysq.get_dividend_yield(s) < 100
def data_type(): print "From the list above, enter the data type you'd like to see" data = raw_input("> ") print "What stock would you like to see data on?" stock = raw_input("> ") if data == 'price' or data == 'Price': print ystockquote.get_price(stock) elif data == 'change' or data == 'Change': print ystockquote.get_change(stock) elif data == 'Volume' or data == 'volume': print ystockquote.get_avg_daily_vol(stock) elif data == 'exchange' or data == 'Exchange': print ystockquote.get_stock_exchange(stock) elif data == 'market cap' or data == 'Market cap' or data == 'Market Cap': print ystockquote.get_market_cap(stock) elif data == 'book value' or data == 'Book value' or data == 'Book Value': print ystockquote.get_book_value(stock) elif data == 'eps' or data == 'EPS': print ystockquote.get_earnings_per_share(stock) elif data == 'short ratio' or data == 'Short Ratio': print ystockquote.get_short_ratio(stock) elif data == 'dividend yield' or data == 'Dividend Yield' or data == 'div yield': print ystockquote.get_dividend_yield(stock) elif data == 'ebitda' or data == 'EBITDA': print ystockquote.get_ebitda(stock) else: print 'Sorry, we don\'t have that data type' quit()