Exemple #1
0
 def check_buy(self):
     #one-time buy
     if self.owned_shares != 0:
         return False
     share = Share(self.code)
     avg50 = self.get_short_moving_avg()
     avg200 = self.get_long_moving_avg()
     if avg50 == -1.0 or avg200 == -1.0:
         self.log('Short or Long moving average cannot be obtained due to yahoo API error')
         return 0
     
     current_price_u = share.get_price()
     if current_price_u is None:
         self.log('Current Price is None for Stock')
         return 0
     
     current_price = float(current_price_u)
     if avg50 > avg200:
         #trend change, buy
         buy_count = self.how_many_shares_to_buy(current_price)
         if buy_count != 0:
             if buy(self, buy_count, current_price) == True:
                 self.owned_shares = buy_count
                 self.bought_value = float(current_price * self.owned_shares)
         return self.owned_shares
     return 0
Exemple #2
0
def fetch_data(dt_from, dt_to, sym):
    min_dt_from = dt.datetime(2014, 1, 1)
    assert(dt_from >= min_dt_from)
    r = TStockHistory.find_one(
        {'Symbol': sym}, projection={'Date': 1}, sort=[('Date', pymongo.DESCENDING)])
    if not r:
        fetch_dt_from = min_dt_from
    elif r['Date'] < dt_to:
        fetch_dt_from = r['Date'] + dt.timedelta(days=1)
        if fetch_dt_from > dt_to:
            fetch_dt_from = None
    else:
        fetch_dt_from = None
    if fetch_dt_from:
        f = '%d-%d-%d' % (
            fetch_dt_from.year, fetch_dt_from.month, fetch_dt_from.day)
        t = '%d-%d-%d' % (dt_to.year, dt_to.month, dt_to.day)
        print('fetch %s from network...' % (sym))
        share = Share(sym)
        docs = share.get_historical(f, t)
        if docs:
            for r in docs:
                for k in ['Adj_Close', 'High', 'Low', 'Close', 'Volume']:
                    r[k] = np.float(r[k])
                r['Date'] = dateutil.parser.parse(r['Date'])
            TStockHistory.insert_many(docs)
    data = TStockHistory.find(
        {'Symbol': sym, 'Date': {'$gte': dt_from, '$lte': dt_to}})
    rs = filter(lambda x: 'placeholder' not in x, [u for u in data])
    return rs
def main():
    start_date = '2007-01-01'
    end_date = '2015-08-31'
    plotly_name = 'username'
    plotly_api_key = 'api-key'

    # get_historical is part of the yahoo-finance module
    nflx = Share('nflx')
    nflx_prices = nflx.get_historical(start_date, end_date)

    # how you can just extract the dates only
    nflx_dates = [x['Date'] for x in nflx_prices]

    # nflx_prices is currently sorted by dates because thats how the module gave them
    sorted_by_price = sorted(nflx_prices, key=lambda x: x['Adj_Close'], reverse=True)

    # say you wanted multiple stock prices
    ticker_symbols = ['hrl', 'tsn', 'gis', 'k']
    foods = {}

    if do_plot:
        plot_stock(plotly_name, plotly_api_key, nflx_prices, 'nflx')

    for symbol in ticker_symbols:
        foods[symbol] = Share(symbol).get_historical(start_date, end_date)
        foods[symbol].sort(key=lambda x: x['Date'])
        dates = [x['Date'] for x in foods[symbol]]
        prices = [x['Adj_Close'] for x in foods[symbol]]
        if do_plot:
            add_trace(dates, prices, symbol)
def getStockData(theTicker):
    global startDate
    print("Getting Data for ... " + theTicker)
    stock = Share(theTicker)
    print(startDate)
    data = stock.get_historical(startDate, DateNow)
    for d in data:
        tmp = []
        volume = int(d['Volume'])
        adjclose = float(d['Adj_Close'])
        high = float(d['High'])
        low = float(d['Low'])
        close = float(d['Close'])
        date = d['Date']
        open = float(d['Open'])
        # newDate = datetime.strptime(date, "%Y-%m-%d")
        tmp.append(date)
        tmp.append(open)
        tmp.append(high)
        tmp.append(low)
        tmp.append(close)
        tmp.append(adjclose)
        tmp.append(volume)
        givenStock.append(tmp)
    return givenStock
Exemple #5
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
Exemple #6
0
  def lookup_by_symbol(symbol='', service='y'):
    """
    TODO : Not calling this method anywhere now; think about
    this method's purpose again
    """
    query_symbol = Symbol.symbol_dict.get(symbol)

    if query_symbol is None:
      try:
        if service == 'g':
          g_get_quotes(symbol)
          query_symbol  = Symbol(g_symbol=symbol)
        elif service == 'y':
          print 'creating Share instance to check whether valid symbol'
          y_obj = Share(symbol)
          if 'start' not in y_obj.get_info():
            raise Exception('Given symbol doesn\'t exist in Yahoo finance')
          query_symbol  = Symbol(y_symbol=symbol)
          query_symbol.yahoo_handle = y_obj
      except urllib2.HTTPError:
        print('{symbol} is invalid'.format(symbol=symbol))
      except Exception as e:
        print(e.args)

    return query_symbol
def stock_Prices():
    # exit if the output already existed
    # if os.path.isfile('./input/stockPrices.json'):
    #     sys.exit("Prices data already existed!")

    priceSet = {}
    fin = open('./input/tickerList.csv')
    for num, line in enumerate(fin):
        line = line.strip().split(',')
        ticker, name, exchange, MarketCap = line
        if 1:
            print(num, ticker)
            yahoo = Share(ticker)
            time.sleep(np.random.poisson(3))
            prices = yahoo.get_historical('2005-01-01', '2020-01-01')
            priceDt = {}
            for i in range(len(prices)):
                date = ''.join(prices[i]['Date'].split('-'))
                priceDt[date] = round(log(float(prices[i]['Close']) / float(prices[i]['Open'])), 6)
            priceSet[ticker] = priceDt
        #except:
            #continue

    with open('./input/stockPrices.json', 'w') as outfile:
        json.dump(priceSet, outfile, indent=4)
Exemple #8
0
 def SelectStock(self):
     wb = Workbook()
     ws = wb.active
     with open('export.csv',newline='') as f:
         reader = csv.reader(f)
         index = 2
         title = ["代號","名稱","股價","本益比","EPS"]
         for x in range(0,5,1):
             ws.cell(row=1,column=x+1,value=title[x])
         for row in reader:
             if row!=[]:
                 try:
                     if int(row[0]) < 10000:
                     #    print(row[2])
                         if "-" not in (row[2]) and float(row[2]) < 20:
                          #   print(row)
                             Query = Share(row[0]+'.TW')
                             #print(float(Query.get_earnings_share()))     
                             #print(float(Query.get_open()))
                             #print("******************")
                             if float(Query.get_open()) >= 20 and float(Query.get_open()) <= 90 and float(Query.get_earnings_share())>1.2:
                                 print(row)
                                 ws.cell(row=index , column=1 , value = row[0])
                                 ws.cell(row=index , column=2 , value = row[1])
                                 ws.cell(row=index , column=3 , value = Query.get_open())
                                 ws.cell(row=index , column=4 , value = row[2])
                                 ws.cell(row=index , column=5 , value = Query.get_earnings_share())
                                 index = index + 1
             
                                 
                 except:
                     pass    
         wb.save("Final.xlsx")        
Exemple #9
0
    def check_sell(self):
        #UNCOMMENT WHEN USING ACTUAL DATA
        if self.owned_shares == 0:
            return False
        
        share = Share(self.code)

        current_price_u = share.get_price()
        if current_price_u is None:
            self.log('Current Price is None for Stock')
            return 0
        
        current_price = float(current_price_u)
        
        #UNCOMMENT WHEN USING ACTUAL DATA
        if self.bought_value < current_price:
            return False
        
        avg50 = self.get_short_moving_avg()
        avg200 = self.get_long_moving_avg()
        if avg50 < avg200:
            #trend change, buy
            sell(self, self.owned_shares,current_price)
            return True
        return False
	def make_csv(self,name,now_time):
		if os.path.exists(self.DIR_NAME+name+".csv"):
			print "Already have a %s.csv"%name
			return

		print "making %s.csv....." %name
		from yahoo_finance import Share
		share=Share(name)

		info=share.get_info()		
		if "start" not in info:
			print "Cant make a %s.csv"%name
			return -1
		
		start=info["start"]	
		
		#wrong date
		if re.match("\d{4}-\d{2}-\d{2}",start)== None:
			print "invalid date cant make a file"
			return -1
		
		obj=share.get_historical(start,now_time)
		
		filename=name+".csv"
		fieldnames=("Date","High","Low","Volume" )
		headers = dict( (n,n) for n in fieldnames )
		f=open(self.DIR_NAME+filename,"w")
		writer=csv.DictWriter(f,fieldnames)	
		writer.writerow(headers)
		for o in obj:
			row=""
			writer.writerow({"Date":o["Date"],"High":o["High"],"Low":o["Low"],"Volume":o["Volume"]})
			
		f.close()
Exemple #11
0
def combine(onedayago, twodaysago, threedaysago, fivedaysago, oneweekago, companyname):
    print "Analyzing data from the past few days..."
    dates = [onedayago, twodaysago, threedaysago, fivedaysago, oneweekago]
    for i in dates:
        i == i.format('YYYY-MM-DD')


    # Just gets the info and puts it into programmer friendly names
    def getclosing(date, company):
        # Thanks to stackoverflow user 'TessellatingHeckler' for helping me out with this next function! At the time dictionaries were a foreign concept to me.
        readings = company.get_historical(date, date)
        for reading in readings:
            close = reading['Close']
            return close

    company = Share(companyname)
    closingonedayago = getclosing(str(dates[0]), company)
    closingtwodaysago = getclosing(str(dates[1]), company)
    closingthreedaysago = getclosing(str(dates[2]), company)
    closingfivedaysago = getclosing(str(dates[3]), company)
    closingoneweekago = getclosing(str(dates[4]), company)
    twohundredavg = company.get_200day_moving_avg()
    fiftyavg = company.get_50day_moving_avg()
    today = company.get_price()
    decision(today, closingonedayago, closingtwodaysago, closingthreedaysago, closingfivedaysago, closingoneweekago, twohundredavg, fiftyavg)
Exemple #12
0
def get_data(symbol='^GSPC'):
    symbol1 = symbol
    if symbol1[0] == '^':
        symbol1 = symbol[1:]
   
    fileName = 'c:/data/%s.csv' % symbol1
    keys = ['Date', 'Close']  # only save these fields
    
    gspc = Share(symbol)
    if os.path.isfile(fileName):
        result = read_data(fileName)
        oneday = timedelta(1)
        yesterday = datetime.today().date() - oneday
        latest = datetime.strptime(result[0]['Date'], '%Y-%m-%d').date()+oneday
        if latest < yesterday:
            print('Extending from %s to %s ...' % (str(latest), str(yesterday)))
            result = gspc.get_historical(str(latest), str(yesterday)) + result
            write_data(fileName, result, keys)
        else:
            print('No need to update')
    else:
        result = gspc.get_historical('1976-02-01', '2016-02-21')
        write_data(fileName, result, keys)

    return result
Exemple #13
0
def download_prices_to_today(ticker, prices):
    """Download prices till today.
    :param str ticker:
    :param [dict] prices: prices sorte by date
    """
    try:
        share = Share(ticker)

        # get from_date
        if not prices:
            from_date = datetime.strptime('2015-08-03', '%Y-%m-%d')
        else:
            last_date = datetime.strptime(prices[-1]['Date'], '%Y-%m-%d')
            from_date = (last_date + timedelta(days=1))

        # get to_date
        to_date = datetime.today()
        day_of_week = to_date.weekday()
        if day_of_week == 6:
            to_date = to_date + timedelta(days=-2) # if sunday
        elif day_of_week == 5:
            to_date = to_date + timedelta(days=-1) # if saturday

        # skip if already update-to-date
        if from_date >= to_date:
            return
        str_from_date = from_date.strftime('%Y-%m-%d')
        str_to_date = to_date.strftime('%Y-%m-%d')
        eprint('downloading prices', ticker, str_from_date, str_to_date)
        data = share.get_historical(str_from_date, str_to_date)
        return data
    except Exception as ex:
        eprint(ex)
def start_stock():
    # Busca as informações do pregão

    data_corrente = start_data()
    data_exec = data_corrente[0:4] + '-' + data_corrente[4:6] + '-' + data_corrente[6:8]
    lista_acoes = cria_lista()
    #lista_acoes = ['PETR3.SA', 'VALE3.SA', 'BBAS3.SA']
    nome_arq = 'pregao' + str(data_exec) + '.txt'
    arquivo = open(nome_arq, 'w')
    texto = ''
    informacao = None

    for acao in lista_acoes:
        try:
            yahoo = Share(str(acao))
            informacao = yahoo.get_historical(data_exec,data_exec)
        except:
            pass
        if informacao :
            simbolo = informacao[0]['Symbol'].split('.SA', 1)[0]
            try:
                fechamento = informacao[0]['Close']
            except KeyError:
                fechamento = ''
            try:
                volume = informacao[0]['Volume']
            except KeyError:
                volume = ''
            texto = simbolo + ';' + fechamento + ';' + volume + "\n"
            arquivo.writelines(texto)

    arquivo.close()
    return "Registros salvos no arquivo: " + nome_arq
Exemple #15
0
def getPrice(bodyList, from_number):
    retStr = ""
    #interate over bodyList
    for ticker in bodyList:
        quote = Share(str(ticker))
        price = quote.get_price()
        #construct return string with price ticker if found
        #appended the comma and space in the front, will remove preceeding comma after loop
        if price:
            retStr += ", " + ticker + " Price: " + price
        else:
            retStr += ", ticker not found"
        
        #update last_lookup field so the "more info" feature works
        conn = mysql.connect()
        cur = conn.cursor()
        q = '''INSERT INTO last_lookup (phone, ticker)
                   VALUES (%s, %s)
                   ON DUPLICATE KEY UPDATE
                   ticker = VALUES(ticker)
            '''
        try:
            cur.execute(q,(from_number,ticker))
            conn.commit()
        except:
            conn.rollback()
    
    #strip preceeding comma and space
    retStr = retStr[2:]
    return retStr
Exemple #16
0
def update_stock(dial, stock):
    stk = Share(stock)
    prev_close_price = float(stk.get_prev_close())
    stk_data = json.loads(json.dumps(getQuotes(stock), indent=2))[0]
    stk_price = float(stk_data['LastTradePrice'])
    NIMBUS.set_dial_value(dial, percentage(stk_price, prev_close_price),
                          "%s:%.2f" % (stock, stk_price))
Exemple #17
0
    def data_frame_companies_amount(list_companies,porcentaje_volumnen_diario,max_var_pvd):

        # Es una funcion que toma una lista de empresas (codificadas por yahoo.finance), el porcentaje (tanto por uno)
        # sobre el volumen diario a considerar y la maxima variacion en porcentaje (tanto por uno) sobre este porcentaje.



        list_index=["ask5","ask4","ask3","ask2","ask1","bid1","bid2","bid3","bid4","bid5"]
        companies_amount=pd.DataFrame(index=list_index)
        for i in list_companies:

            company = Share(i)
            volume=company.get_volume()
            cantidad_per_ba=float(company.get_avg_daily_volume())*porcentaje_volumnen_diario
            num=cantidad_per_ba*max_var_pvd # variacion maxima respecto al 1% del volumen diario
            vec_list_amount=[]

            for h in range(0,len(list_index)):

                vec_list_amount.append(round(cantidad_per_ba+np.random.randint(-num,num,1)))

            vec_amount=np.array(vec_list_amount)
            companies_amount[i]=vec_amount   

        return companies_amount
Exemple #18
0
def create_files():
        for i in range(0,TIME_IN_MIN*60/TIME_BETWEEN_ITERATIONS):
	    reader=csv.DictReader(open('Yahoo_symbols.csv','rb'))
     	    for sym in reader:

			company=sym["COMPANY"]
			symbol=sym["SYMBOL"]
			while(1):
			   try:
				share_name=Share(symbol)
				if share_name:
					break
			   except:
				time.sleep(1)

			filename = "./Q4_files/"+company+".csv"
			try:
				file=open(filename,"a")
			except:
				file=open(filename,"w")
				file.write("Time,Price")

			timestamp = share_name.get_trade_datetime()
			price = share_name.get_price()
			writer = csv.DictWriter(file, fieldnames=["Time","Price"], delimiter=",", lineterminator="\n")
			writer.writerow({"Time":timestamp ,"Price":price})		
	    time.sleep(TIME_BETWEEN_ITERATIONS)	

        file.close()
def get_stock_price_and_volume(symbol, start_date, end_date, dates):
	share = Share(symbol)
	hist_data = share.get_historical(start_date, end_date)
	hist_data.reverse()
	volume = []
	price = []
	i = 0
	for d in dates:
		if i < len(hist_data):
			if (hist_data[i]['Date'] == d):
				# Weekday
				price.append(hist_data[i]['Close'])
				volume.append(hist_data[i]['Volume'])
				i += 1
			else:
				# Weekend
				price.append(0)
				volume.append(0)
		else:
			# Get the current price and volume instead from historical data
			price.append(share.get_price())
			volume.append(share.get_volume())
	if len(dates) != len(volume) and len(dates) != len(price):
		print 'Dates and volume and/or price lists are not of same lenght!'
	return [price, volume]
Exemple #20
0
    def data_frame_companies_prices(list_companies,a,b):

        # Es una funcion que toma una lista de empresas (codificadas por yahoo.finance) y valor minimo y maximo
        # de la distribucion uniforme que sigue la diferencia entre el bid y el ask para las empresas.



        list_index=["ask5","ask4","ask3","ask2","ask1","bid1","bid2","bid3","bid4","bid5"]
        companies_prices=pd.DataFrame(index=list_index)

        for i in list_companies:

            company = Share(i)
            open_price=company.get_price()
            open_price=float(open_price)
            a=float(a)
            b=float(b)
            random1=(np.random.rand(1)+(a/b-a))/(1.0/b-a)

            first_ask_price=open_price+(round(random1,2))/2.0
            ask_array=np.array([first_ask_price+0.04,first_ask_price+0.03,first_ask_price+0.02,first_ask_price+0.01,first_ask_price])

            first_bid_price=open_price-(round(random1,2))/2.0
            bid_array=np.array([first_bid_price+0.04,first_bid_price+0.03,first_bid_price+0.02,first_bid_price+0.01,first_bid_price])

            ask_bid_array=np.concatenate((ask_array,bid_array))

            companies_prices[i]=ask_bid_array

        return companies_prices
def retrieveQuoteFromYahoo(symbol,start,end):        
    share = Share(symbol)  
    quoteList = share.get_historical(start,end)
    quoteDict = {}
    for quote in quoteList:
        quoteDict[quote['Date']] = float(quote['Adj_Close'])        
    return quoteDict
def get_stock_df(ticker,start_date,end_date):
    """Get stock dataframe"""
    share = Share(ticker)
    share_hist = share.get_historical(start_date,end_date)
    len_share_hist = len(share_hist)
    dates = ['']*len_share_hist
    open = [0.]*len_share_hist
    close = [0.]*len_share_hist
    high = [0.]*len_share_hist
    low = [0.]*len_share_hist
    volume = [0.]*len_share_hist
    adj_close = [0.]*len_share_hist
    for i in range(len_share_hist):
        dates[i] = share_hist[i][DATE_STR]
        open[i] = float(share_hist[i][OPEN_STR])
        close[i] = float(share_hist[i][CLOSE_STR])
        adj_close[i] = float(share_hist[i][ADJ_CLOSE_STR])
        high[i] = float(share_hist[i][HIGH_STR])
        low[i] = float(share_hist[i][LOW_STR])
        volume[i] = float(share_hist[i][VOLUME_STR])
    df = pd.DataFrame(open, index = pd.to_datetime(dates), columns=[OPEN_STR])
    df[CLOSE_STR] = close
    df[ADJ_CLOSE_STR] = adj_close
    df[HIGH_STR] = high
    df[LOW_STR] = low
    df[VOLUME_STR] = volume
    df.index.name = DATE_STR
    return df.sort_index()
Exemple #23
0
 def get_historical(self, num_of_days):
   if self._id.startswith('6'):
     stk_id = self._id + '.ss'
   else:
     stk_id = self._id + '.sz'
   stk = Share(stk_id)
   start = datetime.strftime(datetime.today() - timedelta(days=num_of_days+1),
                             '%Y-%m-%d')
   end   = datetime.strftime(datetime.today() - timedelta(days=1),
                             '%Y-%m-%d')
   hist = stk.get_historical(start, end)
   high    = []
   low     = []
   opening = []
   close   = []
   vol     = []
   date    = []
   for d in hist:
     if d.has_key('High'):
       high.append(float(d['High']))
       low.append(float(d['Low']))
       opening.append(float(d['Open']))
       close.append(float(d['Close']))
       vol.append(float(d['Volume']))
       date.append(d['Date'])
   return high, low, opening, close, vol, date
def get_data(stock,start,end):
    data = Share(stock)
    try:
        data = pd.DataFrame(data.get_historical(start_date=start,end_date=end))
    except Exception as e:
        f = open('log.txt',mode='a')
        f.write(stock+'\n')
        f.write(str(e)+'\n')
        return pd.DataFrame()

    try:
        data.index = data.Date
    except Exception as e:
        f = open('log.txt', mode='a')
        f.write(stock+'\n')
        f.write(str(e)+'\n')
        return pd.DataFrame()

    data = data.drop(['Date','Symbol'],axis=1)
    data = data.sort_index()
    for i in data.columns:
        data[i] = data[i].astype(np.float)
    #data['Adj_Open'] = 0
    #data['Adj_High'] = 0
    #data['Adj_Low'] = 0
    #for i in range(len(data)):
    #    k = data['Adj_Close'][i] / data['Close'][i]
    #    data.loc[i:i+1,'Adj_Open'] = k*data['Open'][i]
    #    data.loc[i:i + 1, 'Adj_High'] = k * data['High'][i]
    #    data.loc[i:i + 1, 'Adj_Low'] = k * data['Low'][i]
    data['Symbol'] = stock
    return data
Exemple #25
0
def sell():
    if 'username' in session:
        # Get symbol name
        s = request.args.get('symbol')
        # recreate users stock information
        if not models.new_stock:
            models.Users.reload_user()
        # symbol name is valid
        if s is not None and s != '':
            symbol = Share(s)
            if symbol.get_price() is None:
                return render_template('apology.html', message=['Something went wrong. Please try again.'])
            else:
                # Get user's stock info
                holding = models.Stockholding.objects.get(username=session['username'], symbol=s)
                amount_to_add = int(holding.shares) * float(symbol.get_price())
                # add value of shares to user's cash
                models.Users.objects(username=session['username']).update(inc__cash=amount_to_add)
                # log transaction
                transaction = models.Transaction(username=session['username'], date=time.strftime("%d/%m/%Y"), \
                                            type=models.Transaction.SELL, symbol=s, shares=holding.shares)
                transaction.save()
                holding.delete() # Remove stock
                flash('Stock sold successfully', 'text-success')
                models.Users.reload_user()
                print models.new_stock
                return redirect('portfolio')
        return render_template('sell.html', stocks=models.new_stock)
    return render_template('apology.html', message=['Log In to see this page'])
Exemple #26
0
def get_data():

    filename = 'data/symbols.csv'

    data = []

    with open(filename, 'r') as csvfile:

        reader = csv.reader(csvfile, delimiter=',')

        for row in reader:


            symbol  = row[0]
            print ("Getting share for %s" % symbol)

            name    = row[1]
            sector  = row[2]

            share = Share(symbol)
            _open = share.get_open()

            if _open:
                data.append({
                    'id'        : symbol,
                    'symbol'    : symbol,
                    'name'      : name,
                    'sector'    : sector,
                    'open'      : _open
                })


    return data
Exemple #27
0
def report_current_from_yahoo(symbol):
    yahoo = Share(symbol)
    price_cur = yahoo.get_price()
    eps = yahoo.get_earnings_share()
    pe = yahoo.get_price_earnings_ratio()

    print 'yahoo: price=%s eps=%s, pe=%s'%(price_cur, eps, pe)
    def generateData(self):
        global StartingDate
        global DataSize
        i = datetime.datetime.now()
        EndingDate = '%s-%s-%s' % (i.year,i.month,i.day)
        stock = Share(Stock_name)
        data = stock.get_historical(StartingDate,EndingDate)
        file = open('stock_data', 'w')
        closes = [c['Close'] for c in data]
        opens = [o['Open'] for o in data]
        oArray = []
        cArray = []

        for c in closes:
            cArray.append(c)

        for o in opens:
            oArray.append(o)

        for x in range(len(data)-2):
            #  %Difference, Next Day %Difference, Money Made Holding for a Day
            file.write(str((float(cArray[x])-float(oArray[x+1]))/100) + ' ' + str((float(cArray[x+1]) - float(oArray[x+2]))/100) + ' ' + str((float(oArray[x]) - float(oArray[x+1]))) + '\n')

            self.dayChange.append((float(cArray[x])-float(oArray[x+1]))/100)
            self.nextDayChange.append((float(cArray[x+1]) - float(oArray[x+2]))/100)
            self.profit.append(float(oArray[x]) - float(oArray[x+1]))
        #Makes sure the population size is
        DataSize = len(self.dayChange)
        file.close()
def getData(startDate, endDate, period):
    tickerSymbols = []
    sp500 = finsymbols.get_sp500_symbols()
    nasdaq = finsymbols.get_nasdaq_symbols()
    amex = finsymbols.get_amex_symbols()
    nyse = finsymbols.get_nyse_symbols()
    for companies in amex:
        tickerSymbols.append(companies.get("symbol"))
    print tickerSymbols
    return
    # start a period before the startdate to obtain the data
    print startDate, " ", endDate
    startDate = startDate - timedelta(days=period)
    print startDate

    data = {}
    for company in tickerSymbols:
        print company
        c = Share(company)
        histData = c.get_historical(str(startDate), str(endDate))
        histData = histData[::-1]  # reverse data so its easier to use
        data[company] = histData

    addTools(data, period)

    with open("Data/Amex_data.json", "w") as fp:
        json.dump(data, fp, indent=2)

    return data
	def getDifferencePercentage(self):
	 	closing = []
	 	shareName = Share(self.company)
	 	self.getTimeStamp()
	 	startDate=''
	 	endDate=''
	 	hist=''
	 	for t in self.times:
	 		todayTimeStamp = t
	 		yesterdayTimeStamp = t-86400
	 		startDate = str(datetime.datetime.fromtimestamp(todayTimeStamp).strftime('%Y-%m-%d'))
	 		yesterdayDate=str(datetime.datetime.fromtimestamp(yesterdayTimeStamp).strftime('%Y-%m-%d'))
	 		todayHist = shareName.get_historical(startDate, startDate)
	 		yesterdayHist = shareName.get_historical(yesterdayDate,yesterdayDate)
	 		while(len(todayHist)==0):
	 			todayTimeStamp = todayTimeStamp+86400
	 			startDate = str(datetime.datetime.fromtimestamp(todayTimeStamp).strftime('%Y-%m-%d'))
	 			todayHist = shareName.get_historical(startDate, startDate)
	 		while(len(yesterdayHist)==0):
	 			yesterdayTimeStamp= yesterdayTimeStamp-86400
				yesterdayDate=str(datetime.datetime.fromtimestamp(yesterdayTimeStamp).strftime('%Y-%m-%d'))
	 			yesterdayHist = shareName.get_historical(yesterdayDate,yesterdayDate)
	 			
	 		closingPriceToday = float(todayHist[0]['Close'])
	 		closingPriceYesterday = float(yesterdayHist[0]['Close'])
	 		difference = (float(closingPriceYesterday) - float(closingPriceToday))*100.0/float(closingPriceYesterday)
	 		diff2 = float(format(difference, '.3f'))
	 		closing.append(diff2)
	 	self.differencePercentage = closing
	 	return closing
Exemple #31
0
from yahoo_finance import Share
import csv

goog = Share('GOOG')
data = goog.get_historical('2012-01-01', '2017-03-17')
write = []
write = [[d["Date"], d["Close"]] for d in data]

write = sorted(write, key=lambda x: x[0])
write.insert(0, ["date", "price"])
with open("prices.csv", "w+") as f:
    writer = csv.writer(f)
    writer.writerows(write)
def getData(startDate, endDate, stock):
    yahoo = Share(stock)
    yahoo.get_open()
    return json.dumps(yahoo.get_historical(startDate, endDate))
import string
import time

# file to add valid tickers to
stockFile = open('stockFile.txt', 'w')

# just keeps track of the start time, so the time taken to process all possibilities can be printed at the end
start = time.time()

#will serve as "root" for each next layer to add new letters onto
list = [""]

# will test alphabetical combinations up to 5 characters long
for x in range(0, 5):
    # for storing values to later become list for the next "layer" of letters
    temp = []
    for e in list:
        for letter in string.ascii_uppercase:
            s = (e + letter)
            temp.append(s)
            price = Share(s).get_price()
            if (price != None and float(price) >= 5.0):
                # makes it clear that it is in fact working
                print(s + ": " + price)
                stockFile.write(s + "\n")
    list = temp

# prints how many seconds it took to complete the task
duration = time.time() - start
print(duration)
Exemple #34
0
class Symbol(object):

    def __init__(self, symbol, s_date=None, e_date=None):
        self.log=Logging()
        self.name=symbol
        self.created=datetime.datetime.utcnow()
        self.log.info("created {}".format(self.name))
        try:
            self.share=Share(symbol)
        except:
            self.log.error("platform is offline or not connecting")

        if s_date and e_date:
            self.begin=s_date
            self.end=e_date
            try:
                self.share=Share(symbol)
                self.data=self.share.get_historical(self.begin, self.end)
                self.log.refresh("{} data collected".format(self.name))
            except:
                self.log.error("platform is offline or not connecting")
    
    def refresh_data(self, s_date=None, e_date=None):
        if s_date and e_date:
            try:
                share=Share(self.name)
                self.begin=s_date
                self.end=e_date
                share.get_historical(s_date, e_date)
                self.log.refresh("{} data collected".format(self.name))
            except:
                self.log.error("platform is offline or not connecting")

    def market_cap(self):
        try:
            self.market_cap = self.share.get_market_cap()
            self.log.info("{} market cap refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")
        
    def earnings_per_share(self):
        try:
            self.eps = self.share.get_earnings_share()
            self.log.info("{} eps refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")

    def moving_average_50(self):
        try:
            self.moving_average_50 = self.share.get_50day_moving_average()
            self.log.info("{} 50 day moving ave refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")

    #implement TODO
    def nday_moving_average(self, n):
        try:
            self.moving_average_n = None
            self.log.info("{} {} day moving ave refreshed".format(self.name, n))
        except:
            self.log.error("platform is offline or not connecting")

    def price_earnings_ratio(self):
        try:
            self.price_to_earnings = self.share.get_price_earnings_ratio()
            self.log.info("{} price to earnings refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")


    def book_value(self):
        try:
            self.book = self.share.get_price_book()
            self.log.info("{} book value refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")

    def year_high(self):
        try:
            self.year_high = self.share.get_change_from_year_high()
            self.log.info("{} year high change refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")

    def year_low(self):
        try:
            self.year_low = self.share.get_change_from_year_low()
            self.log.info("{} year low change refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")

    def target_price(self):
        try:
            self.year_target = self.share.get_change_from_year_high()
            self.log.info("{} year target change refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")
    
    def year_range(self):
        try:
            self.year_range = self.share.get_change_from_year_high()
            self.log.info("{} year range change refreshed".format(self.name))
        except:
            self.log.error("platform is offline or not connecting")
Exemple #35
0
 def getCurrentPrice(self, stockName):
     stock = Share(stockName)
     return stock.get_price()
STEP = 10000  # Step limitation in an episode, this is actually a bug in my code. This parameter should be equal or larger than the training data size, so here I revised into a large number. However, you may still tune this parameter by yourself, it won't occur any error.
TEST = 10  # Test episode

GAMMA = 0.9  # discount factor
BUFFER_SIZE = 10000  # the replay buffer size
BATCH_SIZE = 32  # minibatch size
INITIAL_EPSILON = 0.8  # initial large, in order to explore more
FINAL_EPSILON = 0.1  # explore less, greedy most time
DAY_LENGTH = 10  # the total days for a training data, also the dim of features
FEATURE_NUM = 8  # Currently: close price, volume, K, D, RSI9, MA5, MA20, MA5-MA20
START = "2011-01-01"  # Training data start date
TRAIN_END = "2015-12-31"  # Training data end date
_ID = 2330  # By default, TSMC (2330)

# Get stock data from yahoo finance
stock = Share(str(_ID) + '.TW')
today = datetime.date.today()
stock_data = stock.get_historical(START, str(today))  # Total data downloaded
print("Historical data since", START, ": ",
      len(stock_data))  # Total length of raw data
stock_data.reverse()  # Reverse the data since it starts from the latest data.

#----------Remove the data with 0 volume---------#
i = 0
while (i < len(stock_data)):
    if (int(stock_data[i].get('Volume')) <= 0):
        stock_data.remove(stock_data[i])
        i = -1
    i += 1
#----------Remove the data with 0 volume---------#
print("Remove the datas with zero volume, total data ", len(stock_data))
Exemple #37
0
def get_Share(symbol):
    stock = Share(clean_stock_search(symbol))
    return stock
Exemple #38
0
sp900_chg = {}
for stock in sp900:
    sys.stdout.write(stock['symbol'])
    try:
        if stock['symbol'] not in google_bad_list and '.' in stock['symbol']:
            equity_parsed = getQuotes(stock['symbol'])[0]
            sp900_all[stock['symbol']] = float(equity_parsed['ChangePercent'])
            sys.stdout.write(" {}\n".format(
                float(equity_parsed['ChangePercent'])))
            if DEBUG:
                sys.stdout.write("{}\t{}\t{}\t{}%".format(
                    stock['symbol'], stock['company'],
                    equity_parsed['LastTradePrice'],
                    equity_parsed['ChangePercent']))
        else:
            equity = Share(stock['symbol'])
            sp900_all[stock['symbol']] = float(
                equity.get_percent_change().strip('%')) / 100
            sys.stdout.write(" {}\n".format(
                float(equity.get_percent_change().strip('%')) / 100))
            if DEBUG:
                sys.stdout.write("{}\t{}\t{}\t{}".format(
                    stock['symbol'], equity.get_name(), equity.get_price(),
                    equity.get_percent_change()))
    except:
        pass

for ticker in sp900_all:
    if sp900_all[ticker] > 0.03 or sp900_all[ticker] < -0.03:
        sp900_chg[ticker] = sp900_all[ticker]
Exemple #39
0
import csv

#apple = Share('AAPL')
#print(apple.get_open())
#print(apple.get_price())
#print(apple.get_trade_datetime())

start = datetime.datetime(2016, 1, 1)
end = datetime.datetime(2016, 8, 1)

stocks = []
with open('companyList.csv') as csvfile:
    stockreader = csv.reader(csvfile)
    for row in stockreader:
        print(str(row)[2:][:-2].upper())
        x = Share(str(row)[2:][:-2].upper())
        print(x.get_price())
        #stocks.append(str(row)[2:][:-2].upper())

#for stock in stocks:
#    q = web.get_quote_google(stock)
#    print(q)

#amazon = web.DataReader("AAPL", "yahoo", start, end)
#close = amazon["Close"]
#close.plot()
#print(type(amazon))
#print(amazon)
#pylab.rcParams['figure.figsize'] = (15,9)

#plt.show()
Exemple #40
0
conn = sqlite3.connect('stocks.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS stocks (
                       stock_date datetime, ticker text, open real, close real, high real, low real, volume long, PRIMARY KEY (stock_date, ticker))''')

# - get today data
now = datetime.datetime.now()
today = "{0}-{1}-{2}".format(now.year,str(now.month).zfill(2),str(now.day).zfill(2))

with open("../data/nasdaq107list.csv", "r") as f:
    content = f.readlines()

for ticker in content:
    ticker = ticker.rstrip('\n')
    try:
        yahoo= Share(ticker)
    except AttributeError:
        pass

    stock = (yahoo.get_historical('2017-03-17', today))

    for st in stock :
        row= []
        print (st)
        row.append(st['Date'])
        row.append(st['Symbol'])
        row.append(st['Open'])
        row.append(st['Close'])
        row.append(st['High'])
        row.append(st['Low'])
        row.append(st['Volume'])
Exemple #41
0
import json
import os
import flask
from flask import Flask, Response, render_template, send_from_directory, send_file

# Needed for the demo, but perhaps not in actual use
import itertools
import random
import time
from yahoo_finance import Share
yahoo = Share('AAPL')


# Server sent events
class ServerSentEvent:
    FIELDS = ('event', 'data', 'id')

    def __init__(self, data, event=None, event_id=None):
        self.data = data
        self.event = event
        self.id = event_id

    def encode(self):
        if not self.data:
            return ""
        ret = []
        for field in self.FIELDS:
            entry = getattr(self, field)
            if entry is not None:
                ret.extend(
                    ["%s: %s" % (field, line) for line in entry.split("\n")])
from yahoo_finance import Share
yahoo = Share('YHOO')
print (yahoo.get_open())
Exemple #43
0
import sqlite3
from yahoo_finance import Share
#yahoo = Share('GOOG')
company_list = ['GOOG', 'YHOO', 'AMZN', 'AAPL', 'FB', 'INTC', 'IBM', 'MSFT', 'TWTR', 'HPQ']
li=[]
for i in range(len(company_list)):
	comp=Share(company_list[i])
	li.append(comp.get_historical('2016-04-25', '2017-04-26'))

print li[1][1]
#comp=Share(company_list[1])
#li.append(comp.get_historical('2014-04-25', '2015-04-25'))

conn = sqlite3.connect('PyFolio.sqlite3')
cur = conn.cursor()
cur.execute('''DROP TABLE historical_data_10stocks''')
cur.execute('''CREATE TABLE historical_data_10stocks
       (ID INTEGER PRIMARY KEY AUTOINCREMENT,
       Symbol		VARCHAR,
       Time         DATE    NOT NULL,
       Open         REAL     NOT NULL,
       Close        REAL,
       High         REAL,
       Low          REAL,
       Volume 		BIGINT   
       );''')

#print len(li[i])
for i in range(len(company_list)):
	for j in range(len(li[i])):
		cur.execute("INSERT INTO historical_data_10stocks (Symbol,Time,Open,Close,High,Low,Volume) \
Exemple #44
0
def portfolio(request):
    '''The main method for all the user functionality'''
    user_id = request.user.id
    if request.method == 'POST':
        which_form = request.POST.get('which-form', '').strip()

        if which_form == 'find-stock':
            symbol = request.POST.get(
                'stock', '').strip().split(' ')[0].strip().upper()
            if symbol != '':
                portfolio_stock = portfolio_stocks(user_id)
                money = portfolio_stock['money']
                porfolio = portfolio_stock['portfolio_info']

                return render(
                    request, 'StockFolio/portfolio.html', {
                        'stock': get_current_info(['' + symbol]),
                        'news': get_news_feed(symbol),
                        'portfolio': porfolio,
                        'stock_rows': plot(user_id),
                        'symbols': json.dumps(STOCK_SYMBOLS),
                        'money': money
                    })

        elif which_form == 'download-historical':
            return download_historical(
                request.POST.get('stock-symbol', '').strip())

        elif which_form == 'buy-stock':
            symbol = request.POST.get('stock-symbol', '').strip()
            StockPortfolio.buy(user_id, symbol,
                               request.POST.get('shares', '').strip(),
                               request.POST.get('cost-per-share', '').strip())
            portfolio_stock = portfolio_stocks(user_id)
            money = portfolio_stock['money']
            porfolio = portfolio_stock['portfolio_info']
            return render(
                request, 'StockFolio/portfolio.html', {
                    'stock': get_current_info(['' + symbol]),
                    'news': get_news_feed(symbol),
                    'portfolio': porfolio,
                    'portfolio_rows': plot(user_id),
                    'symbols': json.dumps(STOCK_SYMBOLS),
                    'money': money
                })

        elif which_form == 'recommend':
            symbol = request.POST.get('stock-symbol', '').strip()
            portfolio_stock = portfolio_stocks(user_id)
            money = portfolio_stock['money']
            porfolio = portfolio_stock['portfolio_info']
            reco_svm = []
            my_stocks = []
            conn = sqlite3.connect('PyFolio.sqlite3')
            cur = conn.cursor()
            sql = ''' SELECT svm_prediction FROM stockapp_prediction_svm_all WHERE symbol LIKE "%''' + symbol + '''%" '''
            sql2 = ''' SELECT ANN_prediction FROM stockapp_prediction_svm_all WHERE symbol LIKE "%''' + symbol + '''%" '''
            sql3 = ''' SELECT bayesian_prediction FROM stockapp_prediction_svm_all WHERE symbol LIKE "%''' + symbol + '''%" '''

            cur.execute(sql)
            result = cur.fetchall()

            cur.execute(sql2)
            reco_ann = cur.fetchall()

            cur.execute(sql3)
            reco_bay = cur.fetchall()

            for i in range(30):
                reco_svm.append(result[i][0])

            stock_price = float(Share(symbol).get_price())
            for i in range(len(portfolio_stock['portfolio_info'])):
                my_stocks.append(
                    portfolio_stock['portfolio_info'][i]['symbol'])

            x, y = 0, 0

            if float(reco_ann[0][0]) > float(stock_price) + 5 or float(
                    reco_svm[29]) > 1.05 * float(stock_price):
                x, y = 0, 0
                if float(reco_ann[0][0]) > float(stock_price) + 5 and float(
                        reco_ann[0][0]) <= 1.1 * float(stock_price):
                    x = 1
                elif float(reco_svm[29]) > 1.05 * float(stock_price) and float(
                        reco_svm[29]) <= 1.1 * float(stock_price):
                    y = 1

                if float(reco_ann[0][0]) > float(stock_price) + 5 and float(
                        reco_svm[29]) > 1.05 * float(stock_price):
                    x, y = 1, 1

                if (x == 1 and y == 0):
                    message = "Buy for Short term gains"

                elif (y == 1 and x == 0):
                    message = "Buy for Long term gains"

                elif (y == 1 and x == 1):
                    message = "Buy for short and long term gains"

            if symbol in my_stocks:
                if float(reco_ann[0][0]) < float(stock_price) + 5 and float(
                        reco_ann[0]
                    [0]) > float(stock_price) - 5 and float(
                        reco_svm[29]) <= 1.05 * float(stock_price) and float(
                            reco_svm[29]) > 0.95 * float(stock_price):
                    x, y = 0, 0
                    if float(
                            reco_ann[0][0]) < float(stock_price) + 5 and float(
                                reco_ann[0][0]) > float(stock_price) - 5:
                        x = 1
                    elif float(reco_svm[29]
                               ) <= 1.05 * float(stock_price) and float(
                                   reco_svm[29]) > 0.95 * float(stock_price):
                        y = 1

                    if float(reco_ann[0][0]) < float(stock_price) + 5 and float(
                            reco_ann[0][0]) > float(stock_price) - 5 and float(
                                reco_svm[29]
                            ) <= 1.05 * float(stock_price) and float(
                                reco_svm[29]) > 0.95 * float(stock_price):
                        x, y = 1, 1

                    if (x == 1 and y == 0):
                        message = "HOLD"

                    elif (y == 1 and x == 0):
                        message = "HOLD"

                    elif (y == 1 and x == 1):
                        message = "HOLD"
            else:
                message = "Stock not in your portfolio"

            if symbol in my_stocks:
                if float(reco_ann[0][0]) < float(stock_price) - 5 and float(
                        reco_ann[0]
                    [0]) >= 0.90 * float(stock_price) or float(
                        reco_svm[29]) < 0.95 * float(stock_price) and float(
                            reco_svm[29]) > 0.90 * float(stock_price):
                    x, y = 0, 0
                    if float(
                            reco_ann[0][0]) > float(stock_price) - 5 and float(
                                reco_ann[0][0]) > 0.90 * float(stock_price):
                        x = 1
                    elif float(reco_svm[29]
                               ) > 0.95 * float(stock_price) and float(
                                   reco_svm[29]) > 0.90 * float(stock_price):
                        y = 1

                    if float(
                            reco_ann[0][0]) > float(stock_price) - 5 and float(
                                reco_ann[0]
                                [0]) >= 0.90 * float(stock_price) and float(
                                    reco_svm[29]
                                ) > 0.95 * float(stock_price) and float(
                                    reco_svm[29]) > 0.90 * float(stock_price):
                        x, y = 1, 1

                    if (x == 1 and y == 0):
                        message = "SELL"

                    elif (y == 1 and x == 0):
                        message = "SELL"

                    elif (y == 1 and x == 1):
                        message = "SELL"
            else:
                message = "Stock not in your portfolio"

            ann_pred = "Tomorrow's predicted price: " + str(reco_ann[0][0])

            return render(
                request, 'StockFolio/portfolio.html', {
                    'stock': get_current_info(['' + symbol]),
                    'news': get_news_feed(symbol),
                    'portfolio': porfolio,
                    'portfolio_rows': plot(user_id),
                    'symbols': json.dumps(STOCK_SYMBOLS),
                    'money': money,
                    'value_reco': message,
                    'ann_pred': ann_pred
                })

        elif which_form == 'buy-sell':
            symbol = request.POST.get('stock-symbol', '').strip()
            if request.POST.get('buy-stock'):
                StockPortfolio.buy(
                    user_id, symbol,
                    request.POST.get('shares', '').strip(),
                    request.POST.get('cost-per-share', '').strip())
            elif request.POST.get('sell-stock'):
                StockPortfolio.sell(
                    user_id, symbol,
                    request.POST.get('shares', '').strip(),
                    request.POST.get('cost-per-share', '').strip())
            portfolio_stock = portfolio_stocks(user_id)
            money = portfolio_stock['money']
            porfolio = portfolio_stock['portfolio_info']
            return render(
                request, 'StockFolio/portfolio.html', {
                    'stock': get_current_info(['' + symbol]),
                    'news': get_news_feed(symbol),
                    'portfolio': porfolio,
                    'portfolio_rows': plot(user_id),
                    'symbols': json.dumps(STOCK_SYMBOLS),
                    'money': money
                })

    portfolio_stock = portfolio_stocks(user_id)
    money = portfolio_stock['money']
    porfolio = portfolio_stock['portfolio_info']

    return render(
        request, 'StockFolio/portfolio.html', {
            'portfolio': porfolio,
            'portfolio_rows': plot(user_id),
            'symbols': json.dumps(STOCK_SYMBOLS),
            'money': money
        })
Exemple #45
0
import time
import sys
from yahoo_finance import Share
from pprint import pprint
from datetime import datetime, timedelta

#connet into firehose with boto3
firehose_client = boto3.client('firehose', region_name='us-east-1')
date = datetime.now()  #today date
print("Ex) US Dallar Index: DX-Y.NYB | Apple: aapl | Google: googl"
      )  #print example for symbols in market
# symbol1 = raw_input("#1 Stock/Curreny: ")                                 #raw_input symbol1
# symbol2 = raw_input("#2 Stock/Curreny: ")                                 #raw_input symbol2
symbol1 = sys.argv[1]  #first argment (symbol1)
symbol2 = sys.argv[2]  #second argment (symbol2)
symbol1 = Share(symbol1)  #symbol1 with yahoo api
symbol2 = Share(symbol2)  #symbol2 with yahoo api
#days_before = int(raw_input("Previous Data(Days from today): "))           #type date before today for historical data; 0 is current data
days_before = int(sys.argv[3])


def yahoo_his_years(symbols, date, days_before):
    '''
    INPUT: historical price for symbols in seleted period of time (raw_input or sys.argv) .
    OUTPUT: symbols price in str format inserted in S3 using Kinesis.
    '''
    counter = 0
    date = date - timedelta(days=days_before)
    for day in range(days_before):
        if date.strftime('%Y-%m-%d') <= datetime.now().strftime('%Y-%m-%d'):
            try:
Exemple #46
0
from yahoo_finance import Share
from pprint import pprint
import matplotlib.pyplot as plt
import numpy as np
import subprocess

yahoo = Share('YHOO')
print(yahoo.get_open())
print(yahoo.get_price())
print(yahoo.get_trade_datetime())

yahoo.refresh()
print('refresh')
print(yahoo.get_price())

dadosHistoricos = yahoo.get_historical('2017-06-06', '2017-06-06')

pprint(dadosHistoricos.__len__())


def getDadosHistoricos():
    pprint(dadosHistoricos)
    print('teste')


getDadosHistoricos()
Exemple #47
0
def getPercentChange(Sym):
	ticker = Share(Sym)
	return ticker.get_percent_change()
			index = Codes.index(Code)
			print Names[index]
			break
#function for search by company	
#try for max regex match	
else:
	while True:
		Company_name = raw_input('Enter the name of the company\t')
		if Company_name not in Names:
			print "The Name Entered is not correct or not in our Database"
			Flag = raw_input('If you want to retry press 4 or press 1 to search by name')
			if int(Flag) == 4:
				continue
			if int(Flag) == 1:
				break	
share = Share(Code)

Start_Date = raw_input('Enter the Start Date in YYYY/MM/DD')

End_Date = raw_input('Enter the End Date in YYYY/MM/DD')

Stocks = share.get_historical(Start_Date,End_Date)

Opening = []

Closing = []

Low = []

High = []
Exemple #49
0
import time
# pull data every day at 15:55 [Sydney], 13:55 [Perth]
from datetime import datetime
from threading import Timer

# x=datetime.today()
# y=x.replace(day=x.day+1, hour=13, minute=55, second=0, microsecond=0)
# delta_t=y-x

# secs=delta_t.seconds+1

# t = Timer(secs, stocks)
# t.start()
# companies | sort by industries
# banks
cba = Share("CBA.ax")
nab = Share("NAB.ax")
anz = Share("ANZ.ax")
wbc = Share("WBC.ax") # westpac
# retail
wow = Share("WOW.ax")
jbh = Share("JBH.ax") # jb hifi
wes = Share("WES.ax") # west farmer 
# misc
asx = Share("ASX.ax")
bhp = Share("BHP.ax")
ccl = Share("CCL.ax") # coca cola
dmp = Share("DMP.ax") # domino pizza
price = [cba.get_price(), nab.get_price(), anz.get_price(), wbc.get_price(), wow.get_price(), jbh.get_price(), wes.get_price(), asx.get_price(), bhp.get_price(), ccl.get_price(), dmp.get_price()]
volume = [cba.get_volume(), nab.get_volume(), anz.get_volume(), wbc.get_volume(), wow.get_volume(), jbh.get_volume(), wes.get_volume(), asx.get_volume(), bhp.get_volume(), ccl.get_volume(), dmp.get_volume()]
change = [cba.get_change(), nab.get_change(), anz.get_change(), wbc.get_change(), wow.get_change(), jbh.get_change(), wes.get_change(), asx.get_change(), bhp.get_change(), ccl.get_change(), dmp.get_change()]
Exemple #50
0
def getCurrentPrice(Sym):
	ticker = Share(Sym)
	return ticker.get_price()
        url = urllib2.urlopen("http://finance.yahoo.com/q?s=" + stock)
        summary = url.read()

        (monthurl, date) = GetOptionsMonth(stock)

        url = urllib2.urlopen(monthurl)
        futureoptions = url.read()

        if len(options) < 180000:
            print("\nThis stock does not appear to exist.")
            #buyvaluelist.append("\nThis stock does not appear to exist.")
        else:
            title = Title(summary)

            #This one uses a different method
            yahoo = Share(stock)
            stockp = float(yahoo.get_price())

            spricelist = GetAllSprices(futureoptions, stock)
            print spricelist

            #trims dupicates from list
            a = len(spricelist)
            spricelist.reverse()
            while len(spricelist) > a / 2:
                spricelist.remove(spricelist[0])
            spricelist.reverse()

            #finds strike price nearest stock price
            while stockp > spricelist[i]:
                i += 1
Exemple #52
0
from yahoo_finance import Share
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from io import BytesIO

apple = Share('AAPL')
print(apple.get_name())

end = datetime.now()
start = end - timedelta(days=20)

start = start.strftime('%Y-%m-%d')
end = end.strftime('%Y-%m-%d')

data = apple.get_historical(start, end)
data = [point['Close'] for point in data]

plt.plot(data)
plt.axis('off')
plt.savefig('stocks.svg', bbox_inches='tight', format='svg', transparent=True)

Exemple #53
0
def stock(symbol):
    stock = get_Share(symbol)
    if stock.data_set['Open'] == None:
        # flash("Couldn't find that stock. Try another symbol.")
        stock = None
        return redirect(url_for('stocks'))
    else:
        # you wrote a function for these two lines, replace here!
        stock = set_stock_data(Share(symbol))
        write_stock_to_db(stock)
        ### ^^
    title = stock.name
    loggedin_user = get_user()
    user, allplayers, leaders = get_leaderboard(loggedin_user)

    form = StockSearchForm(request.form)
    tradeform = TradeForm(request.form)
    stocks = Stock.query.order_by(desc(Stock.view_count)).limit(10).all()

    if user != None:
        portfolio = user.portfolio
        portfolio.prettycash = pretty_numbers(portfolio.cash)
        # This is to show many shares much of that particular stock a user has in his/her position.
        positions = portfolio.positions
        position = portfolio.positions.filter_by(symbol=symbol).first()
        if position:
            position.prettysharecount = pretty_ints(position.sharecount)
    else:
        portfolio = None
        position = None
        positions = None

    if request.method == 'POST' and tradeform.validate():
        share_amount = tradeform.amount.data
        buy_or_sell = tradeform.buy_or_sell.data
        if stock.get_price() == None:
            # If it's POST and valid, but there's no such stock
            flash("Couldn't find stock matching " + symbol.upper() +
                  ". Try another symbol.")
            return redirect(url_for('stocks'))
        else:
            # if it's POSTed, validated, and there is a real stock
            trade(stock, share_amount, buy_or_sell, user, portfolio, positions)
            return redirect(url_for('user'))

    elif request.method == 'POST' and not tradeform.validate():
        flash("Invalid share amount; please try again.")
        return redirect(url_for('stocks'))

    if request.method == 'GET':
        start = '2015-09-30'
        end = '2015-10-31'
        prices, dates = prepare_stock_graph(symbol, start, end)
        script, div = build_stock_plot(symbol, dates, prices)

    return render_template('stock.html',
                           form=form,
                           tradeform=tradeform,
                           stock=stock,
                           stocks=stocks,
                           leaders=leaders,
                           title=title,
                           user=user,
                           loggedin_user=loggedin_user,
                           position=position,
                           script=script,
                           div=div)
Exemple #54
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
Exemple #55
0
def set_ETF_data():
    etf_data = []

    for index, etf_symbol in enumerate(settings.ETF_MASTER_LIST):
        etf_dict = {
            'model': 'portfolio.ETF',
            'pk': index + 1,
            'fields': {},
        }

        fund = Share(etf_symbol)

        fields = {
            'name': fund.get_name(),
            'symbol': etf_symbol,
            'last_trade': fund.get_price(),
            'dividend_yield': fund.get_dividend_yield(),
            'absolute_change': fund.get_change(),
            'percentage_change': fund.get_percent_change(),
            'year high': fund.get_year_high(),
            'year low': fund.get_year_low(),
            '50 day moving average': fund.get_50day_moving_avg(),
            '200 day moving average': fund.get_200day_moving_avg(),
            'average_daily_volume': fund.get_avg_daily_volume()
        }

        etf_dict['fields'] = fields
        etf_data.append(etf_dict)
    json_data = json.dumps(etf_data)

    # print(json_data)

    output_dict = [y for y in etf_data if y['fields']['dividend_yield'] > 1]

    output_dict = [
        x for x in output_dict if x['fields']['average_daily_volume'] > 100000
    ]

    output_dict = [
        z for z in output_dict
        if z['fields']['200 day moving average'] < z['fields']['last_trade']
    ]

    sorted_list = sorted(output_dict,
                         key=lambda k: k['fields']['dividend_yield'],
                         reverse=True)

    for etf in sorted_list[:5]:
        ETF.objects.create(
            portfolio=Portfolio.objects.get(pk=1),
            name=etf['fields']['name'],
            symbol=etf['fields']['symbol'],
            investment_style=1,
            last_trade=etf['fields']['last_trade'],
            dividend_yield=etf['fields']['dividend_yield'],
            absolute_change=etf['fields']['absolute_change'],
            percentage_change=etf['fields']['percentage_change'],
            currency='USD',
            last_updated=timezone.now())
Exemple #56
0
def stock_lookup_and_write(symbol):
    stock = set_stock_data(Share(symbol))
    write_stock_to_db(stock)
    return stock
Exemple #57
0
# https://www.toptal.com/machine-learning/s-p-500-automated-trading
from __future__ import division
import graphlab as gl
from datetime import datetime
from yahoo_finance import Share
import urllib2

# download historical prices of S&P 500 index
print('before download')
today = datetime.strftime(datetime.today(), "%Y-%m-%d")
print('after download')
try:
    stock = Share(
        '^GSPC')  # ^GSPC is the Yahoo finance symbol to refer S&P 500 index
    print('Share')
    # we gather historical quotes from 2001-01-01 up to today
    hist_quotes = stock.get_historical('2011-01-01', today)
    # here is how a row looks like
    hist_quotes[0]
    {
        'Adj_Close': '2091.580078',
        'Close': '2091.580078',
        'Date': '2016-04-22',
        'High': '2094.320068',
        'Low': '2081.199951',
        'Open': '2091.48999',
        'Symbol': '%5eGSPC',
        'Volume': '3790580000'
    }
except urllib2.URLError, err:
    print err
Exemple #58
0
from yahoo_finance import Share
import json
from pprint import pprint

IBM = Share('IBM').get_historical('2017-04-15', '2017-05-14')
for item in IBM:
    variation = float(item['Close']) - float(item['Open'])
    date = item['Date']
    print(date + '  --->  open: ' + str(item['Open']) + '  Close: ' +
          str(item['Close']) + '  Variation: ' + str(variation))
Exemple #59
0
if __name__ == '__main__':
    #Variables to Change
    symbol = "NFLX"
    side = 'call'
    exp_start = date(2017, 5, 5)
    exp_end = date(2017, 5, 20)
    strike_low = 145
    strike_high = 160

    ####Calculate Alpha and volatility######
    start_date = datetime.datetime(2016, 5, 5)
    end_date = datetime.datetime(2017, 5, 13)
    dcgr = contGrowthRate(symbol, start_date, end_date)
    alpha = np.mean(dcgr)
    volatility = np.std(dcgr)
    ###########################################

    kde_price_option = build_kde_option_pricer(symbol, start_date, end_date)

    #The three option pricing models you can use is
    # kde_price_option (kernal density estimator)
    # bs_price_option (black scholes)
    # rolldice_price_option (roll dice)
    labels, data = compute_model_error(bs_price_option, symbol, side, exp_start, exp_end, strike_low, strike_high)
   

    options_df = pd.DataFrame.from_records(data, columns=labels)
    print("stock: " + symbol)
    print("price: " + str(Share(symbol).get_price()))
    print(options_df)
Exemple #60
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)