Exemplo n.º 1
0
def get_stock_info(symbols, moreInfo=False):
	""" 
	Scrape stock info from Yahoo finance
	@Param 'moreInfo': False for getting price, True for getting more information
	@Return 'message': "symbol: Price-number, Open Price-number, Pre Close Price-number, High Price-number, 
						Low price-number"
	"""
	message = ''
	for symbol in symbols:
		try:
			stock = Share(symbol)
			price = stock.get_price()
		except AttributeError:
			price = None
		
		if price == None: # Stock symbol not exists, replace with an alert message
			message += '#' + symbol + ': ' + 'The stock symbol does not exit' + '\n'
		elif moreInfo: 
			message += '#%s: Price-%s, Open Price-%s, Pre Close Price-%s, High Price-%s, Low price-%s\n' \
						% (symbol, price, stock.get_open(), stock.get_prev_close(), stock.get_days_high(), stock.get_days_low())
		else:
			message += '#' + symbol + ': ' + price + '\n'

	alert_message = 'Please type #1 followed by stock symbols to get more information' if moreInfo == True else \
					'Please type #0 followed by stock symbols to get stock price'
	return message if message != '' else alert_message
def find_quote(word):
    """Given an individual symbol, 
    find and return the corresponding financial data

    word -- the symbol for which you're finding the data (ex. "GOOG")
    """
    cleanword=re.sub('[@<>]', '', word)
    share = Share(cleanword)
    price = share.get_price()
    if price != None:
        
        # Extract data
        day_high = share.get_days_high()
        day_low = share.get_days_low()
        market_cap = share.get_market_cap()
        year_high = share.get_year_high()
        year_low = share.get_year_low()
        yoy = calculate_YoY(share)
        
        output_string = ('*Stock*: \'{}\' \n*Current Price*: ${} \n*Day Range*: '
        '${} - ${} \n*52 Wk Range*: ${} - ${} \n*YoY Change*: {}\n*Market Cap*: ' 
        '${}').format(word.upper(), str(price), str(day_low), str(day_high), 
                      str(year_low), str(year_high), str(yoy), str(market_cap))
    else:
        output_string = "Can't find a stock with the symbol \'" + cleanword.upper() + "\'"
    return output_string
def find_quote(word):
    """Given an individual symbol, 
    find and return the corresponding financial data

    word -- the symbol for which you're finding the data (ex. "GOOG")
    """
    cleanword = re.sub('[@<>]', '', word)
    share = Share(cleanword)
    price = share.get_price()
    if price != None:

        # Extract data
        day_high = share.get_days_high()
        day_low = share.get_days_low()
        market_cap = share.get_market_cap()
        year_high = share.get_year_high()
        year_low = share.get_year_low()
        yoy = calculate_YoY(share)

        output_string = (
            '*Stock*: \'{}\' \n*Current Price*: ${} \n*Day Range*: '
            '${} - ${} \n*52 Wk Range*: ${} - ${} \n*YoY Change*: {}\n*Market Cap*: '
            '${}').format(word.upper(), str(price),
                          str(day_low), str(day_high), str(year_low),
                          str(year_high), str(yoy), str(market_cap))
    else:
        output_string = "Can't find a stock with the symbol \'" + cleanword.upper(
        ) + "\'"
    return output_string
Exemplo n.º 4
0
def get_quote(symbol):
    share = Share(symbol)

    if not share.get_price():
        return {}

    change_f = float(share.get_change())
    change_str = '+%.02f' % change_f if change_f >= 0 else '%.02f' % change_f

    change_percent_f = change_f / float(share.get_open()) * 100
    change_percent = '+%.02f' % change_percent_f if change_percent_f >= 0 else '%.02f' % change_percent_f

    return {
        'price': share.get_price(),
        'change': change_str,
        'change_percent': change_percent,
        'open_price': share.get_open(),
        'market_cap': share.get_market_cap(),
        'year_low': share.get_year_low(),
        'year_high': share.get_year_high(),
        'day_low': share.get_days_low(),
        'day_high': share.get_days_high(),
        'volume': share.get_volume(),
        'pe_ratio': share.get_price_earnings_ratio() or '-'
    }
Exemplo n.º 5
0
def stock_search(symbol="YHOO"):
    stock = Share(symbol)
    open_price = stock.get_open()
    close_price = stock.get_prev_close()
    price = stock.get_price()
    high = stock.get_days_high()
    low = stock.get_days_low()
    volume = stock.get_volume()

    return stock, open_price, close_price, price, high, low, volume
def stock_summary(request, symbol=None):
    if symbol == None:
        symbol = request.POST['symbol']

    current_stock = Stock()
    stock = Share(symbol)
    current_stock.symbol = symbol.upper()
    current_stock.price = stock.get_price()
    current_stock.change = stock.get_change()
    current_stock.volume = stock.get_volume()
    current_stock.prev_close = stock.get_prev_close()
    current_stock.stock_open = stock.get_open()
    current_stock.avg_daily_volume = stock.get_avg_daily_volume()
    current_stock.stock_exchange = stock.get_stock_exchange()
    current_stock.market_cap = stock.get_market_cap()
    current_stock.book_value = stock.get_book_value()
    current_stock.ebitda = stock.get_ebitda()
    current_stock.dividend_share = stock.get_dividend_share()
    current_stock.dividend_yield = stock.get_dividend_yield()
    current_stock.earnings_share = stock.get_earnings_share()
    current_stock.days_high = stock.get_days_high()
    current_stock.days_low = stock.get_days_low()
    current_stock.year_high = stock.get_year_high()
    current_stock.year_low = stock.get_year_low()
    current_stock.fifty_day_moving_avg = stock.get_50day_moving_avg()
    current_stock.two_hundred_day_moving_avg = stock.get_200day_moving_avg()
    current_stock.price_earnings_ratio = stock.get_price_earnings_ratio()
    current_stock.price_earnings_growth_ratio = stock.get_price_earnings_growth_ratio()
    current_stock.price_sales = stock.get_price_sales()
    current_stock.price_book = stock.get_price_book()
    current_stock.short_ratio = stock.get_short_ratio()

    date_metrics = []
    url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+symbol+'/chartdata;type=quote;range=1y/csv'
    page = urllib2.urlopen(url).read()
    pagebreaks = page.split('\n')
    for line in pagebreaks:
        items = line.split(',')
        if 'Company-Name:' in line:
            current_stock.company_name = line[13:len(line)]
            current_stock.save()
        if 'values' not in items:
            if len(items)==6:
                hd = HistoricalData(
                    stock_id = Stock.objects.get(id=int(current_stock.id)).id,
                    date = items[0][4:6]+'/'+items[0][6:9]+'/'+items[0][0:4],
                    close = items[1][0:(len(items[1])-2)],
                    high = items[2][0:(len(items[2])-2)],
                    price_open = items[3][0:(len(items[3])-2)],
                    low = items[4][0:(len(items[4])-2)],
                    volume = items[5][0:-6]+","+items[5][-6:-3]+","+items[5][-3:len(items[5])])
                hd.save()
                date_metrics.append(hd)
    del date_metrics[0]
    return render(request, "stock_summary.html", {'current_stock': current_stock, 'date_metrics': date_metrics})
Exemplo n.º 7
0
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
Exemplo n.º 8
0
    def process_tweet(self, text):
        organizations = self.extract_orgs(text)
        print(organizations)
        if not organizations:
            print('unable to match a company in this tweet')
            return
        if len(organizations) > 1:
            print(
                'tweet is about more than one company, unsure how to proceed')
            return

        sentiment_scores = self.sentiment_analyzer.polarity_scores(text)
        print(sentiment_scores)
        for k in sorted(sentiment_scores):
            print('{0}: {1}, '.format(k, sentiment_scores[k]), end='')
        print()  # flush new line

        # lookup current price, daily high and low
        stock = Share(self.companies[organizations[0]])
        price = float(stock.get_price())
        high = float(stock.get_days_high())
        low = float(stock.get_days_low())

        if sentiment_scores['neg'] > 0.75:
            # negative sentiment on the stock, short it
            position = amount_to_trade(PORTFOLIO_SIZE, RISK_PERCENTAGE, high,
                                       low, price, 'Short')

        elif sentiment_scores['pos'] > 0.5:
            # positive or neutral sentiment on the stock, buy it
            position = amount_to_trade(PORTFOLIO_SIZE, RISK_PERCENTAGE, high,
                                       low, price, 'Buy')

        else:
            position = (0, 0, 'No Order')

        print(
            f'Execute Trade: {position[2]} {position[0]} of {self.companies[organizations[0]]}, set stop loss at {position[1]}'
        )

        questrade_symbol = self.questrade_client.get_symbol(
            self.companies[organizations[0]])
        print(questrade_symbol)
        if questrade_symbol:
            order = Order(os.getenv('QUESTRADE_ACCOUNT_NUMBER'),
                          questrade_symbol['symbolId'],
                          position[0],
                          None,
                          stop_price=position[1],
                          action=position[2])
            trade = self.questrade_client.create_order(order)
            print(trade)
Exemplo n.º 9
0
def getCurrent(ticker):
    """
	Return the most recent stock data through yahoo_finance.
	"""
    stock = Share(ticker)
    stock.refresh()  #refresh the query
    open_ = stock.get_open()
    volume = stock.get_volume()
    high = stock.get_days_high()
    price = stock.get_price()
    time = stock.get_trade_datetime()
    low = stock.get_days_low()
    return (open_, high, volume, price, time, low)
Exemplo n.º 10
0
def strategy1():
    print "strategy1"
    # the purpose of this strategy is to make sure the stock is progressing
    # upwords and has been gradually increasing. I don't want a stock that
    # isn't a somewhat straight line
    # potentials = set()
    potentials = []
    f = open("todays-gainers.txt", "r")
    for line in f:
        name = line.replace('\n', '')
        # print name

        stock = Share(name)
        # print "open: "+stock.get_open()
        # print "high: "+stock.get_days_high()
        # print "low: "+stock.get_days_low()
        # print "curr: "+stock.get_price()
        if stock.get_open() is not None:
            oo = stock.get_open()
            hh = stock.get_days_high()
            cc = stock.get_price()
            if type(oo) is str and type(hh) is str and type(cc) is str:
                o = float(oo)
                h = float(hh)
                # l = float(stock.get_days_low())
                c = float(cc)
                delta_open_cur = c - o
                delta_high_cur = abs(h - c)
                # delta_low_open = abs(l-o)
                if delta_open_cur != 0.0 and delta_high_cur != 0.0:
                    difference_high = delta_high_cur / delta_open_cur * 100.0
                else:
                    difference_high = 0
                # difference_low = delta_low_open/delta_open_cur*100.0
                if difference_high < 35 and difference_high > -1:
                    # print "LOOK AT THIS ONE"
                    potentials.append(name)
                    print name
                    change = (1.0 - (o / c)) * 100.0
                    print change
                    print "---------------"
                # print "DIFFERENCE:  "+str(difference_high)
                # print "DIFFERENCE LOW:  "+str(difference_low)

    # message = "Strategy 1:\n"
    # message += "These are potentials\n"
    # message += str(potentials)
    # sendMessage(message)
    # printPotentials(potentials)
    return potentials
def rec(p):
    yahoo = Share(p)
    a = yahoo.get_prev_close()
    b = yahoo.get_year_high()
    c = yahoo.get_year_low()
    d = yahoo.get_open()
    e = yahoo.get_ebitda()
    f = yahoo.get_market_cap()
    g = yahoo.get_avg_daily_volume()
    h = yahoo.get_dividend_yield()
    i = yahoo.get_earnings_share()
    j = yahoo.get_days_low()
    k = yahoo.get_days_high()
    l = yahoo.get_50day_moving_avg()
    m = yahoo.get_200day_moving_avg()
    n = yahoo.get_price_earnings_ratio()
    o = yahoo.get_price_earnings_growth_ratio()
    print p
    print "Previous Close: ", a
    print "Year High", b
    print "Year Low", c
    print "Open:", d
    print "EBIDTA", e
    print "Market Cap", f
    print "Average Daily Volume", g
    print "Dividend Yield", h
    print "Earnings per share", i
    print "Days Range:", j, "-", k
    print "50 Days Moving Average", l
    print "200 Days Moving Average", m
    print "Price Earnings Ratio", n
    print "Price Earnings Growth Ratio", o

    import MySQLdb
    db = MySQLdb.connect(host="127.0.0.1",
                         user="******",
                         passwd="1111",
                         db="stocks",
                         local_infile=1)
    cur = db.cursor()
    cur.execute(
        """
	            INSERT INTO stockapp_info (symbol, prev_close, year_high, year_low, open_price , ebidta, market_cap, avg_daily_vol , dividend_yield, eps , days_low ,days_high, moving_avg_50, moving_avg_200, price_earnings_ratio, price_earnings_growth_ratio)
	            VALUES
	                (%s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s)

	        """, (p, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o))
    db.commit()
    cur.close()
Exemplo n.º 12
0
def get_new_stops(symbol, current_stop, spread, start_date):
    share = Share(symbol)
    day_high = float(share.get_days_high())
    day_low = float(share.get_days_low())
    newdate = (datetime.datetime.strptime(start_date,"%Y-%M-%d") + datetime.timedelta(days=1)).strftime("%Y-%M-%d")
    prices = share.get_historical(start_date, today)
    days_high=max([float(y['High']) for y in prices] + [day_high])
    days_low=min([float(y['Low']) for y in prices] + [day_low])

    print symbol
    if days_low < current_stop:
        print "%s - stop activated! , days_low-%s" % (symbol,days_low)
        print days_low
    elif (days_high - spread) > current_stop:
        print "%s - new stop: %s" % (symbol, days_high - spread)
Exemplo n.º 13
0
 def yahoo_aktuell(self):
     #symbol = '^NDX'
     # symbol = 'FB'
     wp = Share(self.wertpapier.ric)
     open = wp.get_open()
     high = wp.get_days_high()
     low = wp.get_days_low()
     close = wp.get_price()
     time = wp.get_trade_datetime()
     # MEZ:  +1 Stunde
     #  MESZ: +2 Stunden
     #  Close KO:  Uhrzeit: 2017-04-04 20:00:00 UTC+0000   => 22 Uhr
     #  Close NDX: Uhrzeit: 2017-04-04 21:15:00 UTC+0000   => 23:15 Uhr
     print('Symbol:', self.wertpapier.ric, 'Kursdaten:', open, high, low,
           close)
     print('Uhrzeit:', time)
Exemplo n.º 14
0
def main():

    PATH_OF_DATA = 'data'
    error_log_file = open('error.log', 'a')

    index_lists = [f[:-4] for f in listdir(PATH_OF_DATA) if f[-4:] == '.csv']

    skipFlag = True if len(sys.argv) > 1 else False
    tillFlag = True if len(sys.argv) > 2 else False
    for stock_index in index_lists:
        if skipFlag:
            if stock_index != sys.argv[1]:
                continue
            else:
                skipFlag = False
        if tillFlag:
            if stock_index == sys.argv[2]:
                break

        filename = join(PATH_OF_DATA, stock_index + '.csv')
        if isfile(filename):  # 如果已經有檔案,就讀出最後一行然後插入在後面
            lastline = get_last_row(filename)
            print 'lastline = ', lastline
            try:
                st = Share(stock_index + '.tw')

                if not time_after(lastline[0], st.get_trade_datetime()[:10]):
                    print 'time : ', st.get_trade_datetime()[:10]
                    fo = open(filename, 'ab')
                    cw = csv.writer(fo, delimiter=',')

                    # 更新當天資料
                    cw.writerow([
                        st.get_trade_datetime()[:10],
                        st.get_open(),
                        st.get_days_high(),
                        st.get_days_low(),
                        st.get_price(),
                        st.get_volume(), '0.0'
                    ])
                    print "更新一筆!"
                else:
                    print "不需要更新"

            except:
                print stock_index, "update error!"
                error_log_file.write('%s, Update Error\n' % (stock_index))
Exemplo n.º 15
0
def post():
	## Update stock info
	try:
		stock_list = Stock.objects.all()
		for stock in stock_list:
			try:
				s = Share(stock.symbol)
				stock.price = s.get_price()
				stock.open_price = s.get_open()
				stock.pre_close_price = s.get_prev_close()
				stock.high_price = s.get_days_high()
				stock.low_price = s.get_days_low()
				stock.save()
			except Exception, e:
				pass
	except Exception, e:
		pass
 def get_today_stock_data(ticker):
     try:
         s = Share(ticker)
         data = {
             'Open': float(s.get_open()),
             'Close': float(s.get_prev_close()),
             'High': float(s.get_days_high()),
             'Low': float(s.get_days_low()),
             'Volume': int(s.get_volume()),
             'Date': datetime.date.today(),
         }
         return data
     except YQLQueryError:
         logger.error("Daily data not found for {}".format(ticker))
     except Exception as e:
         logger.error("Unexpected error occurred: {}".format(e))
     return {}
 def get_today_stock_data(ticker):
     try:
         s = Share(ticker)
         data = {
             'Open': float(s.get_open()),
             'Close': float(s.get_prev_close()),
             'High': float(s.get_days_high()),
             'Low': float(s.get_days_low()),
             'Volume': int(s.get_volume()),
             'Date': datetime.date.today(),
         }
         return data
     except YQLQueryError:
         logger.error("Daily data not found for {}".format(ticker))
     except Exception as e:
         logger.error("Unexpected error occurred: {}".format(e))
     return {}
Exemplo n.º 18
0
def RealTime(filepath,instrument):
	
	# Download CSV from yahoo if the CSV does not already exist.
	if not isfile(filepath):
		download_daily_bars(instrument,2016,filepath)
	
	# Get Live Results from Yahoo Database and append to CSV
	s = Share(instrument)
	date = datetime.today().strftime('%Y-%m-%d')
	O,H,L,C,V,AC = (s.get_open(), s.get_days_high(), s.get_days_low(), s.get_price(), 
					s.get_volume(),s.get_price())

	#Date,Open,High,Low,Close,Volume,Adj Close
	field = [date,O,H,L,C,V,AC]
	
	with open(filepath,'a') as csvfile:
		old = csv.writer(csvfile)
		add = old.writerow(field)
def rec(p):
	yahoo = Share(p)
	a=yahoo.get_prev_close()
	b=yahoo.get_year_high()
	c=yahoo.get_year_low()
	d=yahoo.get_open()
	e=yahoo.get_ebitda()
	f=yahoo.get_market_cap()
	g=yahoo.get_avg_daily_volume()
	h=yahoo.get_dividend_yield()
	i=yahoo.get_earnings_share()
	j=yahoo.get_days_low()
	k=yahoo.get_days_high()
	l=yahoo.get_50day_moving_avg()
	m=yahoo.get_200day_moving_avg()
	n=yahoo.get_price_earnings_ratio()
	o=yahoo.get_price_earnings_growth_ratio()
	print p
	print "Previous Close: ",a
	print "Year High",b
	print "Year Low",c
	print "Open:",d
	print "EBIDTA",e 
	print "Market Cap",f
	print "Average Daily Volume",g 
	print "Dividend Yield",h
	print "Earnings per share",i 
	print "Days Range:", j ,"-",k
	print "50 Days Moving Average",l 
	print "200 Days Moving Average",m
	print"Price Earnings Ratio", n
	print"Price Earnings Growth Ratio",o

	import MySQLdb
	db = MySQLdb.connect(host="127.0.0.1", user="******",passwd="1111", db="stocks",local_infile = 1)  
	cur=db.cursor()
	cur.execute ("""
	            INSERT INTO stockapp_info (symbol, prev_close, year_high, year_low, open_price , ebidta, market_cap, avg_daily_vol , dividend_yield, eps , days_low ,days_high, moving_avg_50, moving_avg_200, price_earnings_ratio, price_earnings_growth_ratio)
	            VALUES
	                (%s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s)

	        """, (p,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o))
	db.commit() 
	cur.close()
Exemplo n.º 20
0
def sp500info(): #gets the S&P 500 price, ticker, changes, etc.
	sp500ticker = '^GSPC' 
	sp500desc = "S&P 500 Index: "
	sp500index = Share(sp500ticker)
	#sp500price = sp500index.get_price()
	sp500price = scraper(sp500ticker)
	#sp500change = sp500index.get_change()
	sp500open = sp500index.get_open()
	#print sp500index.get_50day_moving_avg()
	#print sp500index.get_200day_moving_avg()
	#print sp500ma50, sp500ma200
	sp500price = str(sp500price).strip('[]')
	sp500price = sp500price.replace(",", "")
	#print(repr(sp500price)) #scrub the ticker for hidden values; troubleshooting
	sp500change = float(sp500price) - float(sp500open)
	sp500dayhigh = sp500index.get_days_high()
	sp500percentchange = getPercentChange(sp500index, sp500change, sp500open)

	return sp500percentchange, sp500dayhigh, sp500open, sp500change, sp500price, sp500index, sp500desc
Exemplo n.º 21
0
def main():

    PATH_OF_DATA = 'data'
    error_log_file = open('error.log', 'a')

    index_lists = [ f[:-4] for f in listdir(PATH_OF_DATA) if f[-4:] == '.csv' ]

    skipFlag = True if len(sys.argv) > 1 else False
    tillFlag = True if len(sys.argv) > 2 else False
    for stock_index in index_lists:
        if skipFlag:
            if stock_index != sys.argv[1]:
                continue
            else:
                skipFlag = False
        if tillFlag:
            if stock_index == sys.argv[2]:
                break

        filename = join(PATH_OF_DATA, stock_index+'.csv')
        if isfile(filename):# 如果已經有檔案,就讀出最後一行然後插入在後面
            lastline = get_last_row(filename)
            print 'lastline = ', lastline
            try:
                st = Share(stock_index+'.tw')

                if not time_after(lastline[0], st.get_trade_datetime()[:10]):
                    print 'time : ', st.get_trade_datetime()[:10]
                    fo = open(filename, 'ab')
                    cw = csv.writer(fo, delimiter=',')

                    # 更新當天資料
                    cw.writerow([st.get_trade_datetime()[:10], st.get_open(), st.get_days_high(),
                                     st.get_days_low(), st.get_price(), st.get_volume(), '0.0'])
                    print "更新一筆!"
                else:
                    print "不需要更新"

            except:
                print stock_index, "update error!"
                error_log_file.write('%s, Update Error\n' % (stock_index))
Exemplo n.º 22
0
 def fetch_current_data(self, sym):
     ts = get_latest_trading_date(get_cur_time())
     if ts in self.datasets_daily[sym].keys() or ts > self.endDate:
         return
     try:
         sdata = Share(sym)
         gquote = gQuotes(sym)
     except:
         # live with the fact that data from the most recent day is missing
         return
     self.datasets_daily[sym][ts] = t = {}
     t['Date']  = '3000-01-01' #debugging purposes, so we know this is current. This won't be saved to file
     t['High']  = float(sdata.get_days_high())
     t['Low']   = float(sdata.get_days_low())
     t['Open']  = float(sdata.get_open())
     # t['Close'] = sdata.get_price()
     t['Close'] = float(gquote[0]['LastTradePrice']) # use google data for latest 'Close', which is more accurate
     t['Volume'] = float(sdata.get_volume())
     for k in t.keys():
         if t[k] == None:
             raise Exception('missing most recent daily', sym)
Exemplo n.º 23
0
def getStock(ticker):
    stock = Share(ticker)
    #name = stock.get_name()
    po = str(stock.get_open())
    pn =  str(stock.get_price())
    pcl =  str(stock.get_prev_close())
    pc = str(stock.get_percent_change())
    low = str(stock.get_days_low())
    high = str(stock.get_days_high())
    h = datetime.now().strftime("%I")
    p = datetime.now().strftime("%p")
    x = ticker.upper() + "\nCurrent: " + pn +"\nClose: " + pcl + "\nChange: "+ pc + "\nLow: " + low + "\nHigh: " + high
    y = ticker.upper() + "\nCurrent: " + pn + "\nOpen: " + po + "\nChange: " + pc + "\nLow: " + low + "\nHigh: " + high
    
    if h <= "4" and p == "PM":
        return x
    elif h >= "4" and p == "PM":
        return y
    elif h <= "8" and p == "AM":
        return y
    else:
        return x
Exemplo n.º 24
0
    def postStockHistory(self) -> None:
        for stockID in self.stocksToMonitor:
            timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            currentDate = timestamp[:10]

            # Get the average MySQL query
            avgQuery = StockQueries.getAverageQuery("stock_activity_price",
                                                    "stock_activity",
                                                    "stock_activity_date",
                                                    currentDate)
            # Get the average value
            average = self.sd.getAverageValue(avgQuery)
            if average == None:
                average = -1
            yahoo = Share(stockID)

            # Get the schema of the stock_history table and format it as a comma delimited
            # string for use in the MySQL query.
            stockHistoryAttributes = self.tableDict.get("stock_history")
            attributesString = self._attrsToCommaDeliminatedString(
                stockHistoryAttributes)

            try:
                # Format as a comma delimted string for use in the SQL query
                attributesToInsert = self._attrsToCommaDeliminatedString([
                    '"' + stockID + '"', '"' + currentDate + '"',
                    yahoo.get_open(), average,
                    yahoo.get_price(),
                    yahoo.get_days_high(),
                    yahoo.get_days_low()
                ])
            except YQLQueryError:
                logging.warning("Yahoo finance is currently unavailable.")

            insQuery = StockQueries.getInsertQuery("stock_history",
                                                   attributesString,
                                                   attributesToInsert)
            self.sd.runQuery(insQuery)
Exemplo n.º 25
0
def history():
    # If a user goes to the display page and a session is not active
    if session.get('active') != True:
        sym = 'SPY'
        session['active'] = True
        session['sym'] = sym
    else:
        sym = session['sym'] # if a session is active leave the sym alone

    share = Share(sym)
    historical = share.get_historical('2016-03-13', '2016-04-15')
    canvas_list = []
    for day in historical:
        canvas_list.append([int(day['Date'][:4]),
                            int(day['Date'][5:7]) - 1,
                            int(day['Date'][-2:]),
                            float(day['Open']),
                            float(day['High']),
                            float(day['Low']),
                            float(day['Close'])
                            ])
    info = share.get_info()
    open = share.get_open()
    high = share.get_days_high()
    low = share.get_days_low()
    price = share.get_price()
    canvas_list.append([int(info['end'][:4]),
                        int(info['end'][5:7]) - 1,
                        int(info['end'][-2:]),
                        float(open),
                        float(high),
                        float(low),
                        float(price)
                        ])

    return render_template('history.html',
                            canvas_list=canvas_list,
                            sym=sym)
Exemplo n.º 26
0
def getHistoricalData(ticker, daysAgo):
	try:
		yahooHoo = Share(ticker)
	except ValueError:
		print "Couldn't 'Share("+str(ticker)+")'"
	data=yahooHoo.get_historical(dateDaysAgo(daysAgo), today())
	try:
		empty={}
		element={'Volume': yahooHoo.get_volume(), 'Symbol': ticker, 'High': yahooHoo.get_days_high(), 'Low': yahooHoo.get_days_low(), 'Open': yahooHoo.get_open(), 'Close': yahooHoo.get_price(), 'Date': yahooHoo.get_trade_datetime()[0:10]}
		data.append(element)
	except ValueError:
		print "LALALA"
	
	if len(data) > 0:
		data = sorted(data, key=lambda k: k['Date'], reverse=False) #Sort by Date of json element

		#try:
		#	if data[-1]['Date'] != element['Date']:
		#		data.append(element)
		#except ValueError:
		#	print "DOH"
		#data = sorted(data, key=lambda k: k['Date'], reverse=False) #Sort by Date of json element
	
	return data
Exemplo n.º 27
0
class stockdata:
    def __init__(self, symb, online=1):
        self._symb = symb
        # <class 'yahoo_finance.Share'>
        if (online):
            try:
                self.body = Share(symb)
            except:
                self.body = np.nan
                print("Share error:", self._symb)

    def get_price(self):
        '''
        get today's price
        if market closed, the price is the close price
        Returns
        -------
        price : str
        '''
        price = self.body.get_price()
        return price

    def get_days_high(self):
        '''
        get today's high price
        Returns
        -------
        dayhigh : str
        '''
        dayhigh = self.body.get_days_high()
        return dayhigh

    def get_days_low(self):
        '''
        get today's low price
        Returns
        -------
        daylow : str
        '''
        daylow = self.body.get_days_low()
        return daylow

    def get_historical(self, start_date, end_date):
        '''
        Parameters
        ----------
        start_date : str
            'year-month-day', for example '2015-10-01'
        end_date : str

        Returns
        -------
        historical : dlist, dict list
            historical          : <class 'list'>
            historical[0]       : <class 'dict'>
            historical[0][High] : <class 'str'>
            for example:
            [{'Adj_Close': '75.620003',
              'Close': '75.620003',
              'Date': '2015-10-23',
              'High': '75.760002',
              'Low': '72.839996',
              'Open': '73.980003',
              'Symbol': 'BABA',
              'Volume': '22342100'},
             {}, ... {}]
        '''
        historical = self.body.get_historical(start_date, end_date)
        return historical

    def save_historical_file(self, start_date, end_date):
        '''
        save historical data to excel file in form of DataFrame
        the file name is 'get_historical_SYMB_ENDATE.xlsx', for example, 'get_historical_YHOO_2015-10-24.xlsx'
        Parameters
        ----------
        start_date : str
            'year-month-day', for example '2015-10-01'
        end_date : str

        Returns
        -------
        len : int
            length of historical
        '''
        try:
            historical = self.body.get_historical(start_date, end_date)
        except:
            print("get_historical error:", self._symb, start_date, end_date)
            return 0

        length = len(historical)
        if (length == 0):
            #print("len is 0:", self._symb)
            return 0

        # check data
        try:
            high = float(historical[0]['High'])
        except:
            print("get_high error:", self._symb)
            return 0

        df = pd.DataFrame(historical)
        file_name = './data/' + self._symb + '_historical_' + end_date + '.xlsx'
        df.to_excel(file_name)
        return length

    def read_historical_file(self, end_date):
        '''
        read historical data from file
        the file name is 'get_historical_SYMB_ENDATE.xlsx', for example, 'get_historical_YHOO_2015-10-24.xlsx'
        Parameters
        ----------
        end_date : str
            'year-month-day', for example '2015-10-01'

        Returns
        -------
        df : DataFrame of Pandas
            df                  : <class 'pandas.core.frame.DataFrame'>
            df[:1]              : <class 'pandas.core.frame.DataFrame'>
            df.High(df['High']) : <class 'pandas.core.series.Series'>
            df.loc[0]           : <class 'pandas.core.indexing._LocIndexer'>
            df.iloc[0]          : <class 'pandas.core.indexing._iLocIndexer'>
            for example:
                Adj_Close      Close        Date       High        Low       Open Symbol    Volume
            0   75.620003  75.620003  2015-10-23  75.760002  72.839996  73.980003   BABA  22342100
            1   70.989998  70.989998  2015-10-22  71.629997  70.070000  70.160004   BABA  10691400
            2   69.480003  69.480003  2015-10-21  71.790001  68.889999  71.790001   BABA  16800200
        '''
        file_name = './data/' + self._symb + '_historical_' + end_date + '.xlsx'
#        file_name = './data/get_historical_' + self._symb + '_' + end_date + '.xlsx'
        try:
            df = pd.read_excel(file_name)
        except FileNotFoundError as e:
            df = pd.DataFrame({[]})
            print(e, self._symb)
        self._df = df
        return df

    # get date through row
    def fget_date(self, row):
        val = self._df.Date[row]
        return val

    def fget_close(self, row):
        val = self._df.Close[row]
        return val

    def fget_high(self, row):
        val = self._df.High[row]
        return val

    def fget_low(self, row):
        val = self._df.Low[row]
        return val

    # get date through row && col
    def fget_data(self, col, row):
#        val = self._df[col][row]
#        return val
        return 0
Exemplo n.º 28
0
            continue
        stock = Share(ticker)
        stock.refresh()
        change = (float(stock.get_price()) - float(
            stock.get_prev_close())) / float(stock.get_prev_close())
        change = round(change * 100.0, 2)
        if change > 0.0:
            change = '+' + str(change)
        else:
            change = str(change)

        line = ticker.ljust(7)
        line += stock.get_price().ljust(9)+ change.ljust(8)+ stock.get_volume().ljust(11) + \
            str(round(float(stock.get_volume())/float(stock.get_avg_daily_volume())*100.0)).ljust(8) +\
            stock.get_open().ljust(10)+ \
            stock.get_days_low().ljust(10)+ \
            stock.get_days_high().ljust(10)+ \
            stock.get_year_low().ljust(10)+ \
            stock.get_year_high().ljust(10)
        line = line + str(stock.get_market_cap()).ljust(11) + \
            str(stock.get_price_earnings_ratio()).ljust(8)+\
            stock.get_50day_moving_avg().ljust(10) +\
            stock.get_200day_moving_avg().ljust(10)
        print(line)
    except Exception as e:
        print("Exception error:", str(e))
        traceback.print_exc()
    i += 1

#you get get a spy.txt and then filter everything by yourself
Exemplo n.º 29
0
def main():
    # 1. get the time
    day = Time.get_utc_day()
    hours_mins = Time.get_utc_hours_minutes()

    # 1. Get all the list of stocks
    stocks = base.managers.stock_manager.get_many()

    # 2. go through stock and update the desired values
    for stock in stocks:
        ticker = stock.get('ticker')

        try:
            # 2.1 Get the info from the yahoo API
            updated_stock = Share(ticker)
        except:
            print "-->Failed to update: %s with Yahoo API" % ticker
            continue

        price = updated_stock.get_price()
        open = updated_stock.get_open()
        days_high = updated_stock.get_days_high()
        days_low = updated_stock.get_days_low()
        year_high = updated_stock.get_year_high()
        year_low = updated_stock.get_year_low()
        volume = updated_stock.get_volume()
        market_cap = updated_stock.get_market_cap()
        pe_ratio = updated_stock.get_price_earnings_ratio()
        div_yield = updated_stock.get_dividend_yield()
        change = updated_stock.get_change()
        change_percent = updated_stock.data_set.get('ChangeinPercent')

        # 2.2 Get the stock body
        stock_body = stock.get('body')

        stock_price = {hours_mins: price}

        if stock_body:
            # 1. Get the stock info for the day:
            stock_info = stock_body.get(day)

            if stock_info:
                stock_price = stock_info.get('price')
                stock_price.update({hours_mins: price})
        else:
            stock_body = {}

        # 2.2.4 update the stock info dict
        stock_info = {'price': stock_price}
        stock_info.update({'open': open})
        stock_info.update({'days_high': days_high})
        stock_info.update({'days_low': days_low})
        stock_info.update({'year_high': year_high})
        stock_info.update({'year_low': year_low})
        stock_info.update({'volume': volume})
        stock_info.update({'market_cap': market_cap})
        stock_info.update({'pe_ratio': pe_ratio})
        stock_info.update({'div_yield': div_yield})
        stock_info.update({'change': change})
        stock_info.update({'change_percent': change_percent})

        # update the stock body
        stock_body.update({day: stock_info})

        stock.body = stock_body

        # 3. update the stock in the DB
        try:
            base.managers.stock_manager.update_one(stock)
        except:
            print "-->Failed to update: %s in DB" % ticker
            continue
Exemplo n.º 30
0
# Determine functionality

from yahoo_finance import Share
tesla = Share('TSLA')
print tesla.get_price()
print tesla.get_market_cap()

print "get_book_value:", tesla.get_book_value()
print "get_ebitda:", tesla.get_ebitda()
print "get_dividend_share:", tesla.get_dividend_share()
print "get_dividend_yield:", tesla.get_dividend_yield()
print "get_earnings_share:", tesla.get_earnings_share()
print "get_days_high:", tesla.get_days_high()
print "get_days_low:", tesla.get_days_low()
print "get_year_high:", tesla.get_year_high()
print "get_year_low:", tesla.get_year_low()
print "get_50day_moving_avg:", tesla.get_50day_moving_avg()
print "get_200day_moving_avg:", tesla.get_200day_moving_avg()
print "get_price_earnings_ratio:", tesla.get_price_earnings_ratio()
print "get_price_earnings_growth_ratio:", tesla.get_price_earnings_growth_ratio(
)
print "get_price_sales:", tesla.get_price_sales()
print "get_price_book:", tesla.get_price_book()
print "get_short_ratio:", tesla.get_short_ratio()
print "get_trade_datetime:", tesla.get_trade_datetime()
# "a:", print tesla.get_historical(start_date, end_date)
# "a:", print tesla.get_info()
print "get_name:", tesla.get_name()
print "refresh:", tesla.refresh()
print "get_percent_change_from_year_high:", tesla.get_percent_change_from_year_high(
)
Exemplo n.º 31
0
                print("Stock: %12s  TimeStamp: %20s" % (stock, t))

                # sample: yahoo = Share('YHOO')
                # print t, yahoo.get_open(), yahoo.get_price(), yahoo.get_days_high(), yahoo.get_days_low()

                # update timestamp
                t = time.strftime('%Y-%m-%dT%H:%M:%S')

                # get yahoo finacne information
                shareinfo = Share(stock)

                # print share information
                print(
                    "lc: %d price: %s open: %s dayhigh: %s daylow: %s volume: %s"
                    % (x, shareinfo.get_price(), shareinfo.get_open(),
                       shareinfo.get_days_high(), shareinfo.get_days_low(),
                       shareinfo.get_volume()))

                # line space
                print

                # write data to csv file
                writer.writerow({
                    'ts': t,
                    'symbol': stock,
                    'price': shareinfo.get_price(),
                    'open': shareinfo.get_open(),
                    'days_high': shareinfo.get_days_high(),
                    'days_low': shareinfo.get_days_low(),
                    'volume': shareinfo.get_volume()
                })
Exemplo n.º 32
0
config = {
    'user': '******',
    'password': '******',
    'host': '192.168.XXX.XXX',
    'port': '3306',
    'database': 'dbname',
    'raise_on_warnings': True,
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
for YTicker in tables:
    x = str(YTicker[0])
    Symbol = x.replace("_", ".")
    print(Symbol)
    yahoo = Share(Symbol)
    Date = time.strftime("%Y-%m-%d")
    Adj_Close = yahoo.get_price()
    High = yahoo.get_days_high()
    Low = yahoo.get_days_low()
    Volume = yahoo.get_volume()
    add_value = (
        "INSERT INTO " + YTicker[0] +
        " (Symbol, Date, Adj_Close, High, Low, Volume) VALUES (%s, %s, %s, %s, %s, %s)"
    )
    data = (Symbol, Date, Adj_Close, High, Low, Volume)
    cursor.execute(add_value, data)
    cnx.commit()
    print(YTicker[0] + " updated... to" + Adj_Close)
Exemplo n.º 33
0
def predict():
    today = dt.date.today()
    if today.isoweekday() == 6:
        pred_date = today - dt.timedelta(1)
    elif today.isoweekday() == 7:
        pred_date = today - dt.timedelta(2)
    else:
        pred_date = today

    pred_day = int(pred_date.day)
    pred_month = int(pred_date.month)
    pred_year = int(pred_date.year)

    # If a user goes to the display page and a session is not active
    if session.get('active') != True:
        sym = 'SPY'
        session['active'] = True
        session['sym'] = sym
    else:
        sym = session['sym'] # if a session is active leave the sym alone

    try:  # Error handling...  Some Stock symbols exist but there is no historical information in Yahoo Finance...
        share = Share(sym)

        historical = share.get_historical(str(today - dt.timedelta(5)), str(today - dt.timedelta(1))) # need to auto input the date...

        canvas_list = []

        info = share.get_info()
        open = share.get_open()
        high = share.get_days_high()
        low = share.get_days_low()
        price = share.get_price()
        canvas_list.append([int(info['end'][:4]),
                            int(info['end'][5:7]) - 1,
                            int(info['end'][-2:]),
                            float(price)
                            ])

        for day in historical:
            canvas_list.append([int(day['Date'][:4]),
                                int(day['Date'][5:7]) - 1,
                                int(day['Date'][-2:]),
                                float(day['Close'])
                                ])

        df_preds = pd.read_csv('data/predictions.csv')
        date = df_preds[df_preds['sym'] == sym]['date'].values[0]
        historical_day = float(share.get_historical(date, date)[0]['Close'])
        year = date[:4]
        month = date[5:7]
        day = date[-2:]

        predicted_value = historical_day + df_preds[df_preds['sym'] == sym]['regressor_mse'].values[0]
        predicted_rmse = df_preds[df_preds['sym'] == sym]['mse'].values[0] ** .5

        box1_low = predicted_value - (predicted_rmse / 2.)
        box1_high = predicted_value + (predicted_rmse / 2.)

        box2_low = predicted_value - (predicted_rmse)
        box2_high = predicted_value + (predicted_rmse)

        return render_template('predict.html',
                                canvas_list=canvas_list,
                                sym=sym,
                                box1_low=box1_low,
                                box1_high=box1_high,
                                box2_low = box2_low,
                                box2_high=box2_high,
                                year=year,
                                month=month,
                                day=day,
                                pred_day=pred_day,
                                pred_month=pred_month,
                                pred_year=pred_year)
    except:
        return redirect("/error")
def main():
    global shareName
    global stream
    global buyPrice
    global shareAmount
    global currentBallance
    global beforePrice
    
    global dayOpen
    global dayHigh
    global dayLow

    
    cls()
    start = 1
    prevPrice = 0
    textNum = 1
    textNumLimit = 6
    sellPriceReached = False
    buyPrice = 131.09
    beforePrice = buyPrice
    shareAmount = 10
    shareName = raw_input("Enter The Share You Would Like To Monitor:\n" + "--"*25 + "\n> ").strip().upper() #YHOO
    alertPrice = buyPrice * 1.20
    riskThreshold = buyPrice / 1.20
    cls()
    waitTime = 1 #Until Starts
    initDatabase()
    UpDownValues = [""]
    
    print ">" + "-"*52 + ":Monitoring_Share_[" + shareName + "]:" + "-"*52 + "<"
    while True:
        try:
            status = most_common(UpDownValues)
            share = Share(shareName)
            price = getPrice(share, "false", None)
            dayOpen = share.get_open()
            dayHigh = share.get_days_high()
            dayLow = share.get_days_low()
            
            if price == prevPrice:
                pass
            else:
                info = getPrice(share, "true", status)
                saveToDatabase(info, 1)
                print info

            if price > prevPrice:
                UpDownValues.append("UP")
            elif price < prevPrice:
                UpDownValues.append("DOWN")
            if len(UpDownValues) > 15: #Risk Threshold to pull out
                UpDownValues.pop()
            
            if float(price) >= float(alertPrice):
                if str(status) == "DOWN":
                    if price < riskThreshold:
                        sellShares(shareAmount/2)
                        #sendMessage("Current Status:\n" + str(sellProfit("phone")) + "\n>" + "---"*5 + "<\nSell Half of Shares.")
                elif str(status) == "UP":
                    if price >= buyPrice:
                        if price >= alertPrice:
                            sellPriceReached = True
                            sellShares(shareAmount)
                if textNum <= textNumLimit:
                    #sendMessage("Current Status:\n" + str(sellProfit("phone")) + "\n>" + "---"*5 + "<\nBen's Monitor.")
                    pass
                else:
                    pass
            if sellPriceReached == True:
                if price < alertPrice - 10:
                    sellShares("All")
                
            prevPrice = price
            xValue = getTime()
            yValue = price
            time.sleep(0.3) #Default: (0.3)
            
        except Exception, e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            if "'str' and 'NoneType'" in str(e):
                raw_input("-"*75 + "\nCurrent Share Status:\n[Error] | Unable to find the share: [" + shareName + "] | Press (Enter) To Go Back To Main Menu.\n" + "> ")
                main()
            elif "object has no attribute" in str(e):
                pass
            elif "Query failed with error" in str(e):
                pass
            elif "Expecting value: line 1 column 1 (char 0)" in str(e):
                pass
            elif "float() argument must be a string or a number" in str(e):
                cls()
                raw_input("[Input Error] | Unable To Find Share(" + shareName + ")\nPress Enter To Retry:\n>------------------------------------------------<\n> ")
                main()
            else:
                print "[Error] " + str(exc_tb.tb_lineno) + " | > " + str(e)
        except KeyboardInterrupt:
            print "\n"*2 + sellProfit("pause")
            chs = raw_input(">"+"---"*16 + "<\n> Main Menu----------------(1)\n> Continue Monitoring------(Any Key)\n>" + "-"*36 + "<\n> ")
            if "2" in chs:
                main()
            else:
                print "\n>" + "-"*36 + ":Monitoring_Share_[" + shareName + "]_Continued:" + "-"*36 + "<"
Exemplo n.º 35
0
    def on_message(self, message):
        print_logger.debug("Received message: %s" % (message))

        if "ValidateTicker" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed ticker validation request")
                self.write_message("ValidationFailed:Malformed")
                return

            ticker = message[1]

            if validate_ticker(ticker):
                self.write_message("ValidationSucceeded:%s" % ticker)
                print_logger.debug("Ticker was valid")
            else:
                self.write_message("ValidationFailed:%s" % ticker)
                print_logger.debug("Ticker was bad")

            return

        elif "GetCompanyName" in message:
            print_logger.debug("You got here")
            message = message.split(":")
            company_ticker = message[1]
            company_name = get_company_title(company_ticker)

            self.write_message("CompanyName:%s" % company_name)

        elif "GetStockData" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Get ticker information
            share_data = Share(ticker)
            price = share_data.get_price()
            percent_change = share_data.get_change()
            previous_close = share_data.get_prev_close()
            open_price = share_data.get_open()
            volume = share_data.get_volume()
            pe_ratio = share_data.get_price_earnings_ratio()
            peg_ratio = share_data.get_price_earnings_growth_ratio()
            market_cap = share_data.get_market_cap()
            book_value = share_data.get_price_book()
            average_volume = share_data.get_avg_daily_volume()
            dividend_share = share_data.get_dividend_share()
            dividend_yield = share_data.get_dividend_yield()
            earnings_per_share = share_data.get_earnings_share()
            ebitda = share_data.get_ebitda()
            fifty_day_ma = share_data.get_50day_moving_avg()
            days_high = share_data.get_days_high()
            days_low = share_data.get_days_low()
            year_high = share_data.get_year_high()
            year_low = share_data.get_year_low()
            two_hundred_day_ma = share_data.get_200day_moving_avg()

            # Build a string to send to the server containing the stock data
            share_string = "price:" + str(price) + "|"\
                         + "percentChange:" + str(percent_change) + "|"\
                         + "previousClose:" + str(previous_close) + "|"\
                         + "openPrice:" + str(open_price) + "|"\
                         + "volume:" + str(volume) + "|"\
                         + "peRatio:" + str(pe_ratio) + "|"\
                         + "pegRatio:" + str(peg_ratio) + "|"\
                         + "marketCap:" + str(market_cap) + "|"\
                         + "bookValue:" + str(book_value) + "|"\
                         + "averageVolume:" + str(average_volume) + "|"\
                         + "dividendShare:" + str(dividend_share) + "|"\
                         + "dividendYield:" + str(dividend_yield) + "|"\
                         + "earningsPerShare:" + str(earnings_per_share) + "|"\
                         + "ebitda:" + str(ebitda) + "|"\
                         + "50DayMa:" + str(fifty_day_ma) + "|"\
                         + "daysHigh:" + str(days_high) + "|"\
                         + "daysLow:" + str(days_low) + "|"\
                         + "yearHigh:" + str(year_high) + "|"\
                         + "yearLow:" + str(year_low) + "|"\
                         + "200DayMa:" + str(two_hundred_day_ma) + "|"

            self.write_message("StockData;%s" % (share_string))
            print_logger.debug("Sending Message: StockData;%s" % (share_string))

        elif "GetCompanyDesc" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            description = update_description_oneoff(ticker)

            self.write_message("CompanyDescription:%s" % str(description))

        elif "GetCompanyDividend" in message and "Record" not in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Grab the dividend data from dividata.com
            dividend_url = "https://dividata.com/stock/%s/dividend" % ticker

            # This should potentially be a
            dividend_data = requests.get(dividend_url)
            dividend_soup = BeautifulSoup(dividend_data.text, 'html5lib')

            if len(dividend_soup.find_all("table")) > 0:
                dividend_soup = dividend_soup.find_all("table")[0]
            else:
                dividend_soup = "<h3>No dividend history found.</h3>"

            # Send this div up to the server
            self.write_message("DividendHistoryData:" + str(dividend_soup))

        elif "GetCompanyDividendRecord" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Get the dividend record html for the table and send it up
            #dividend_record = strip_dividends(ticker, req_proxy)

            #print_logger.debug("Writing message: " + str(dividend_record))
            #self.write_message("DividendRecord:" + str(dividend_record))

        elif "GetBollinger" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Switch into the tmp directory
            old_dir = os.getcwd()
            os.chdir(TEMP_DIR)

            # Update the historical data for the ticker symbol
            YAHOO_FINANCE_HISTORICAL_OBJECT.read_ticker_historical(ticker)

            bands = BollingerBandStrategy(data_storage_dir="%s/historical_stock_data" % TEMP_DIR\
                    , ticker_file="%s/stock_list.txt" % TEMP_DIR, filtered_ticker_file=\
                    "%s/filtered_stock_list.txt" % TEMP_DIR)

            # Save the graph so that we can show it on the website
            bands.save_stock_chart(ticker, "%s" % TEMP_DIR)

            # Also let the server know that we found an answer
            result = bands.test_ticker(ticker)

            if result is not None:
                print_logger.debug("BB:GoodCandidate")
                self.write_message("BB:GoodCandidate")
            else:
                print_logger.debug("BB:BadCandidate")
                self.write_message("BB:BadCandidate")
        elif "CheckRobinhoodLogin" in message:
            print "HELLO WORLD!!! HELLO WORLD!!! HELLO WORLD!!!%s" % ROBINHOOD_INSTANCE
            if ROBINHOOD_INSTANCE.is_logged_in() is True:
                self.write_message("RobinhoodLoggedIn:%s" % ROBINHOOD_INSTANCE.username)
            else:
                self.write_message("RobinhoodNotLoggedIn")
                
        elif "GetPosition" in message:

            ticker = message.replace("GetPosition:", "")

            account_positions = ROBINHOOD_INSTANCE.get_position_history(active=True)
            user_owns_stock = False
            position_string = ""
            for position in account_positions:
                
                # Get data about the position, including current price.  
                position_data = requests.get(position["instrument"])
                position_data = json.loads(position_data._content)
                position.update(position_data)

                if position["symbol"] != ticker:
                    continue

                quote_data = requests.get(position["quote"]);
                quote_data = json.loads(quote_data._content)
                position.update(quote_data)
                
                position_string = json.dumps(position)
                user_owns_stock = True
                
            if user_owns_stock is True:
                self.write_message("Position:%s" % position_string)
            else:
                self.write_message("Position:None")
Exemplo n.º 36
0
		print getbook

	if myargs.dividendshare is True:
		getdiv = stock.get_dividend_share()
		print getdiv

	if myargs.dividendyield is True:
		dividendyield = stock.get_dividend_yield()
		print dividendyield

	if myargs.eps is True:
		eps = stock.get_earnings_share()
		print eps

	if myargs.dayh is True:
		dayhigh = stock.get_days_high()
		print dayhigh

	if myargs.dayl is True:
		daylow = stock.get_days_low()
		print daylow

	if myargs.yearhigh is True:
		yearhigh = stock.get_year_high()
		print yearhigh

	if myargs.yearlow is True:
		yearlow = stock.get_year_low()
		print yearlow

	if myargs.ebitda is True:
    def on_message(self, message):
        print_logger.debug("Received message: %s" % (message))

        self.write_message("Test Message")

        if "ValidateTicker" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed ticker validation request")
                self.write_message("ValidationFailed:Malformed")
                return

            ticker = message[1]

            # The file I have stored didn't end up being a good validation
            # option as it does not contain a complete list of all
            # securities.  I have to acquire the data from yahoo
            # finance anyway, so just use that.  The Share function
            # call will throw a NameError exception if the ticker doesn't exist
            # isValid = current_stock_list.is_valid_stock(ticker)

            isValid = True
            try:
                test = Share(str(ticker))

                if test.get_price() is None:
                    isValid = False
            except NameError:
                isValid = False

            if isValid:
                self.write_message("ValidationSucceeded:%s" % ticker)
                print_logger.debug("Ticker was valid")
            else:
                self.write_message("ValidationFailed:%s" % ticker)
                print_logger.debug("Ticker was bad")

            return

        elif "GetCompanyName" in message:
            print_logger.debug("You got here")
            message = message.split(":")
            company_ticker = message[1]
            company_name = ""
            try:
                company_info="../task_1/google_search_program/cleaned_data/" + company_ticker + "/company_info"
                company_name = " "

                f = open(company_info, "r")
                line = f.readlines()
                company_name = line[0].split(",")
                company_name = company_name[0]
                company_name = company_name.title()

                if '(' not in company_name:
                    company_name = company_name + " (%s)" % company_ticker
            except Exception:
                company_name = get_company_title_proxied(company_ticker)


            self.write_message("CompanyName:%s" % company_name)

        elif "ExecuteQuery" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed input query")
                self.write_message("QueryResult:Error")

            data = current_solr_object.issue_query(str(message[1]))

            data = current_solr_object.recover_links(data)

            final_string = "QueryResult"

            for link in data:
                final_string = final_string + ":" + str(link)

            self.write_message(final_string)

        elif "GetStockData" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Get ticker information
            share_data = Share(ticker)
            price = share_data.get_price()
            percent_change = share_data.get_change()
            previous_close = share_data.get_prev_close()
            open_price = share_data.get_open()
            volume = share_data.get_volume()
            pe_ratio = share_data.get_price_earnings_ratio()
            peg_ratio = share_data.get_price_earnings_growth_ratio()
            market_cap = share_data.get_market_cap()
            book_value = share_data.get_price_book()
            average_volume = share_data.get_avg_daily_volume()
            dividend_share = share_data.get_dividend_share()
            dividend_yield = share_data.get_dividend_yield()
            earnings_per_share = share_data.get_earnings_share()
            ebitda = share_data.get_ebitda()
            fifty_day_ma = share_data.get_50day_moving_avg()
            days_high = share_data.get_days_high()
            days_low = share_data.get_days_low()
            year_high = share_data.get_year_high()
            year_low = share_data.get_year_low()
            two_hundred_day_ma = share_data.get_200day_moving_avg()

            # Build a string to send to the server containing the stock data
            share_string = "price:" + str(price) + "|"\
                         + "percentChange:" + str(percent_change) + "|"\
                         + "previousClose:" + str(previous_close) + "|"\
                         + "openPrice:" + str(open_price) + "|"\
                         + "volume:" + str(volume) + "|"\
                         + "peRatio:" + str(pe_ratio) + "|"\
                         + "pegRatio:" + str(peg_ratio) + "|"\
                         + "marketCap:" + str(market_cap) + "|"\
                         + "bookValue:" + str(book_value) + "|"\
                         + "averageVolume:" + str(average_volume) + "|"\
                         + "dividendShare:" + str(dividend_share) + "|"\
                         + "dividendYield:" + str(dividend_yield) + "|"\
                         + "earningsPerShare:" + str(earnings_per_share) + "|"\
                         + "ebitda:" + str(ebitda) + "|"\
                         + "50DayMa:" + str(fifty_day_ma) + "|"\
                         + "daysHigh:" + str(days_high) + "|"\
                         + "daysLow:" + str(days_low) + "|"\
                         + "yearHigh:" + str(year_high) + "|"\
                         + "yearLow:" + str(year_low) + "|"\
                         + "200DayMa:" + str(two_hundred_day_ma) + "|"

            self.write_message("StockData;%s" % (share_string))
            print_logger.debug("Sending Message: StockData;%s" % (share_string))
        elif "GetCompanyDesc" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Read in the company description
            description = ""
            try:
                f = open("../task_1/google_search_program/cleaned_data/%s/company_description" % str(ticker), "r")
                description = f.read()
            except Exception:
                # If the file does not exist, get the data manually
                description = update_description_oneoff(ticker)

            self.write_message("CompanyDescription:%s" % str(description))
        elif "GetCompanyDividend" in message and "Record" not in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Grab the dividend data from dividata.com
            dividend_url = "https://dividata.com/stock/%s/dividend" % ticker

            # This should potentially be a
            dividend_data = requests.get(dividend_url)
            dividend_soup = BeautifulSoup(dividend_data.text, 'html5lib')

            if len(dividend_soup.find_all("table")) > 0:
                dividend_soup = dividend_soup.find_all("table")[0]
            else:
                dividend_soup = "<h3>No dividend history found.</h3>"

            # Send this div up to the server
            self.write_message("DividendHistoryData:" + str(dividend_soup))
        elif "GetCompanyDividendRecord" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Get the dividend record html for the table and send it up
            dividend_record = strip_dividends(ticker, req_proxy)

            print_logger.debug("Writing message: " + str(dividend_record))
            self.write_message("DividendRecord:" + str(dividend_record))
        elif "GetBollinger" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Get the bollinger band history along with the 5 day moving average
            close, lower_band, five_day_ma = calculate_bands(ticker)

            last_5_days_5_day_ma = []
            last_5_days_bb = []
            last_5_days_close = []
            for i in range(0, 5):
                last_5_days_5_day_ma.append(five_day_ma[i])
                last_5_days_bb.append(lower_band[i])
                last_5_days_close.append(close[i])

            condition_1 = False
            condition_2 = False

            # Condition 1: Has the stock price at close been below the lower bollinger band
            # at market close within the last 5 days
            for i in range(0, 5):
                if last_5_days_close[i] < last_5_days_bb[i]:
                    condition_1 = True

            # Condition 2: Has the current stock price been above the 5 day moving average sometime in the last 3 days
            for i in range(0, 3):
                if last_5_days_close[i] > last_5_days_5_day_ma[i]:
                    condition_2 = True

            if condition_1 is True and condition_2 is True:
                self.write_message("BB:GoodCandidate")
            else:
                self.write_message("BB:BadCandidate")
        elif "GetSentiment" in message:
            message = message.split(":")

            if len(message) != 2:
                print_logger.error("Malformed Message from Client")
                return

            ticker = message[1]

            # Lists of sentiment based words
            good_words = ["buy", "bull", "bullish", "positive", "gain", "gains", "up"]
            bad_words = ["sell", "bear", "bearish", "negative", "loss", "losses", "down"]

            DATA_DIRECTORY = "../task_1/google_search_program/cleaned_data/%s" % ticker.upper()


            positive_file_stats = []
            negative_file_stats = []
            positive_files = 0
            negative_files = 0

            neutral_files = 0

            trump_count = 0

            files_examined = 0

            for root, dirs, files in os.walk(DATA_DIRECTORY):
                path = root.split(os.sep)
                print((len(path) - 1) * '---', os.path.basename(root))

                for file in files:

                    if "article" in file:
                        f = open('/'.join(path) + '/' + file)

                        title = f.readline()

                        article_text = " ".join(f.readlines())

                        if article_text.count("trump") > 0:
                            trump_count = trump_count + 1

                        positive_word_count = 0
                        negative_word_count = 0

                        files_examined = files_examined + 1

                        for word in good_words:
                            if word in article_text:
                                positive_word_count = positive_word_count + article_text.count(word)
                                print "Word: %s, %s" % (word, article_text.count(word))


                        for word in bad_words:
                            if word in article_text:
                                negative_word_count = negative_word_count + article_text.count(word)

                        if positive_word_count > negative_word_count:
                            positive_ratio = float(positive_word_count) / float(negative_word_count + positive_word_count)

                            if positive_ratio > 0.7:
                                positive_files = positive_files + 1

                                positive_file_stats.append((positive_word_count, negative_word_count))
                            else:
                                neutral_files = neutral_files + 1

                        elif positive_word_count == negative_word_count:
                            neutral_files = neutral_files + 1

                        else:
                            negative_ratio = float(negative_word_count) / float(negative_word_count + positive_word_count)

                            if negative_ratio > 0.7:
                                negative_files = negative_files + 1

                                negative_file_stats.append((positive_word_count, negative_word_count))
                            else:
                                neutral_files = neutral_files + 1

            print_logger.debug("Sentiment:" + str(positive_files) + ":" + str(negative_files) +\
                ":" + str(neutral_files) + ":" + str(trump_count) + ":" + str(files_examined))

            self.write_message("Sentiment:" + str(positive_files) + ":" + str(negative_files) +\
                ":" + str(neutral_files) + ":" + str(trump_count) + ":" + str(files_examined))
Exemplo n.º 38
0
def display():
    today = dt.date.today()
    # Is the user coming from the form page?
    try:
        sym = str(request.form['user_input']).strip().upper() or 'SPY'
        session['active'] = True
        session['sym'] = sym
    except:
        # If a user goes to the display page and a session is not active
        if session.get('active') != True:
            sym = 'SPY'
            session['active'] = True
            session['sym'] = sym
        else:
            sym = session['sym'] # if a session is active leave the sym alone

    share = Share(sym)
    if 'start' not in share.get_info():  # is there information related to the stock symbol in Yahoo Finance?
        # if request.method == 'POST':
        #     print "This is a test"
        #     # if request.form['submit'] == 'Submit':
        #     #     print "before change session"
        #         # session['active'] = False
        print "before return"
        return redirect("/error")
        # return render_template('error.html',
        #                         sym=session['sym'])
    else:
        try:  # Error handling...  Some Stock symbols exist but there is no historical and/or daily information in Yahoo Finance...
            quote = float(share.get_price())
            com_name = sym #hand_made_list()[sym][0]
            historical = share.get_historical(str(today - dt.timedelta(31)), str(today - dt.timedelta(1)))
            canvas_list = []
            for day in historical:
                canvas_list.append([int(day['Date'][:4]),
                                    int(day['Date'][5:7]) - 1,
                                    int(day['Date'][-2:]),
                                    float(day['Open']),
                                    float(day['High']),
                                    float(day['Low']),
                                    float(day['Close'])
                                    ])
            info = share.get_info()
            open = share.get_open()
            high = share.get_days_high()
            low = share.get_days_low()
            price = share.get_price()
            canvas_list.append([int(info['end'][:4]),
                                int(info['end'][5:7]) - 1,
                                int(info['end'][-2:]),
                                float(open),
                                float(high),
                                float(low),
                                float(price)
                                ])

            return render_template('display.html',
                                    sym=session['sym'],
                                    com_name=com_name,
                                    quote=quote,
                                    canvas_list=canvas_list)
        except:
            return redirect("/error")
Exemplo n.º 39
0
from yahoo_finance import Share
#yahoo = Share('YHOO')
#yahoo = Share('SPXC')
yahoo = Share('TFM')
#yahoo = Share('INDU')
#INDEXSP
#yahoo = Share('NDX')
print yahoo
print yahoo.get_open()
#'36.60'
print yahoo.get_price()
print yahoo.get_price_earnings_ratio()
print 'get_dividend_share: ',yahoo.get_dividend_share()
print 'get_dividend_yield: ',yahoo.get_dividend_yield()
print 'get_earnings_share: ',yahoo.get_earnings_share()
print 'get_price_earnings_ratio: ',yahoo.get_price_earnings_ratio()
print 'get_price_earnings_growth_ratio: ',yahoo.get_price_earnings_growth_ratio()
print 'get_year_high: ',yahoo.get_year_high()
print 'get_year_low: ',yahoo.get_year_low()
print 'get_days_high: ',yahoo.get_days_high()
print 'get_days_low: ',yahoo.get_days_low()
print 'get_ebitda: ',yahoo.get_ebitda()
print 'get_book_value: ',yahoo.get_book_value()
#'36.84'
#print yahoo.get_trade_datetime()
#'2014-02-05 20:50:00 UTC+0000'
#get_avg_daily_volume()
Exemplo n.º 40
0
        if len(ticker) == 0:
            continue
        stock = Share(ticker)
        stock.refresh()
        change = (float(stock.get_price()) - float(stock.get_prev_close()))/float(stock.get_prev_close()) 
        change = round(change *100.0, 2)
        if change > 0.0:
            change= '+' + str(change)
        else:    
            change =str(change)
          
        line = ticker.ljust(7) 
        line += stock.get_price().ljust(9)+ change.ljust(8)+ stock.get_volume().ljust(11) + \
            str(round(float(stock.get_volume())/float(stock.get_avg_daily_volume())*100.0)).ljust(8) +\
            stock.get_open().ljust(10)+ \
            stock.get_days_low().ljust(10)+ \
            stock.get_days_high().ljust(10)+ \
            stock.get_year_low().ljust(10)+ \
            stock.get_year_high().ljust(10)
        line = line + str(stock.get_market_cap()).ljust(11) + \
            str(stock.get_price_earnings_ratio()).ljust(8)+\
            stock.get_50day_moving_avg().ljust(10) +\
            stock.get_200day_moving_avg().ljust(10) 
        print(line)    
    except Exception as e:
        print("Exception error:", str(e))
        traceback.print_exc()
    i+=1

#you get get a spy.txt and then filter everything by yourself
Exemplo n.º 41
0
# The "extract" phase loops through stocks from a list stored in stocks.py
# daily and stores it in a Pandas dataframe

print("Extracting stock data from Yahoo Finance for the following stocks:")
print(stocks_list)
print("Extracting...")

df = pd.DataFrame(
    columns=["stock", "open", "high", "low", "close", "volume", "date"])

i = 1
for item in stocks_list:
    stock = Share(item)
    df.loc[i] = [
        item,
        stock.get_open(),
        stock.get_days_high(),
        stock.get_days_low(),
        stock.get_prev_close(),
        stock.get_volume(),
        dt.date.today()
    ]
    i += 1

print("Returning generated DataFrame:")
print(df)

# The "load" phase takes the formatted dataframe and loads it into a Postgresql
# database where they will be available for analysis