def fetch_price(producer, symbol):
    """
    helper function to get stock data and send to kafka
    :param producer: instance of kafka producer
    :param symbol: symbol of specific stock
    :return: None
    """
    logger.debug('Start to fetch stock price for %s', symbol)
    try:
        ticker_info = Ticker(symbol).info
        price = ticker_info.get('preMarketPrice')
        last_trade_time = datetime.utcfromtimestamp(
            ticker_info.get('preMarketTime')).strftime("%Y-%m-%dT%H:%M:%SZ")
        print(last_trade_time)
        payload = (
            '[{"StockSymbol":"%s","LastTradePrice":%s,"LastTradeDateTime":"%s"}]'
            % (symbol, price, last_trade_time)).encode('utf-8')

        logger.debug('Retrieved stock info %s', payload)

        producer.send(topic=topic_name,
                      value=payload,
                      timestamp_ms=int(time.time()))
        logger.debug('Sent stock price for %s to kafka', symbol)
    except KafkaTimeoutError as timeout_error:
        logger.warning(
            'Failed to send stock price for %s to kafka, cauase by %s',
            (symbol, timeout_error))
    except Exception:
        logger.warning('Failed to get stock price for %s', symbol)
Пример #2
0
def main():
    gran = [('2y', '60m'), ('60d', '15m'), ('30d', '5m'), ('7d', '1m')]

    config = ConfigParser()
    config.read('/opt/market/market.ini')
    stocks = config.get('general', 'stocks')

    db = "postgres://[email protected]:5432/stocks"
    session = market.session.Session(db)

    for stock in stocks.split():
        print(stock)

        if session.check_table(stock):
            continue

        dfs = list()
        ticker = Ticker(stock)

        for period, interval in gran:
            df = ticker.history(period=period, interval=interval)
            dfs.append(df)

        df = pd.concat(dfs)
        df = market.df.deduplicate(df)

        session.push_df(dataframe=df, name=stock)
Пример #3
0
def historicaltest():
    if request.method == 'GET':
        obj = ''
        try:
            obj = Ticker('msft')
        except Exception as ex:
            print(ex)

        val = ''
        try:
            val = obj.history(period='max', interval='1d')
            val = val.reset_index()
        except Exception as ex:
            print(ex)

        data = dict()
        data['date'] = val['Date'].astype(str).tolist()
        data['open'] = val['Open'].tolist()
        data['high'] = val['High'].tolist()
        data['low'] = val['Low'].tolist()
        data['close'] = val['Close'].tolist()
        data['volume'] = val['Volume'].tolist()

        for i in range(0, len(data['date'])):
            year = data['date'][i][:4]
            month = data['date'][i][5:7]
            day = data['date'][i][8:]
            dt = datetime.datetime(int(year), int(month), int(day))
            timestamp = int(dt.replace(tzinfo=timezone.utc).timestamp())
            data['date'][i] = timestamp

        outputdata = jsonify(data)
        outputdata.headers.add("Access-Control-Allow-Origin", "*")

        return outputdata
Пример #4
0
def getStock(ticker, time):
    # get current prices
    c = Ticker(ticker)
    t = c.history(time)
    ans = {}
    # return based on change
    a = t.to_dict()["Open"].keys()
    days = [str(i.to_pydatetime()).split(" ")[0] for i in a]
    try:
        c = c.info
        mc = intword(c['marketCap'], format="%0.3f")
        inf = ticker + ": " + c["longName"] + "!Sector: " + \
            c['sector'] + "!Market Cap: $" + mc
    except:
        inf = ticker[1:] + " Index"
    if t["Close"][-1] > t["Close"][-2]:
        ans["price"] = "{: .2f}".format(t["Close"][-1])
        ans["change"] = "+{:.2f}%".format(
            (1 - (t["Close"][-2] / t["Close"][-1])) * 100)
        ans["change"] += " " * (7 - len(ans["change"])) + "⬆"
        ans["history"] = inf + "|" + str(list(zip(days, t["Close"])))
    else:
        ans["price"] = "{: .2f}".format(t["Close"][-1])
        ans["change"] = "-{:.2f}%".format(
            ((t["Close"][-2] / t["Close"][-1]) - 1) * 100)
        ans["change"] += " " * (7 - len(ans["change"])) + "⬇"
        ans["history"] = inf + "|" + str(list(zip(days, t["Close"])))
    print(ticker)
    return ans
Пример #5
0
def build_moex_df(first_month, last_month):
    moex = Ticker('IMOEX.ME').history(start=first_month[0], end=last_month[1],
                                      interval='1mo')
    moex = moex.reset_index().drop(columns=['Date', 'Volume',
                                            'Dividends', 'Stock Splits'])
    moex.rename(columns=lambda x: 'MOEX_%s' % x.lower(), inplace=True)
    moex['date_block_num'] = np.arange(2, 35)
    return moex
def calculate_previous_ema(ticker: yf.Ticker,
                           time_period="1mo",
                           days_previous=1) -> float:
    time_period_days = len(ticker.history(period=time_period))
    stock_history = ticker.history(period=time_period)
    return stock_history.iloc[time_period_days - days_previous - 1].to_dict(
    )['Close'] * (2.5 / (1 + time_period_days)) + calculate_sma(
        ticker, time_period) * (1 - (2.5 / (1 + time_period_days)))
Пример #7
0
def download_hist(stock_to_download):
    try:
        ticker_obj = Ticker(stock_to_download)
        hist = ticker_obj.history(period="max")
        hist.to_csv("data/stocks/" + stock_to_download + ".csv")
    except:
        error = sys.exc_info()[0]
        logger.debug("Unable to download historical stock data for: %s %s",
                     stock_to_download, str(error))
        raise
Пример #8
0
    def __get_history(self):
        from yfinance import Ticker
        from pandas import DataFrame
        from datetime import datetime, timedelta, date

        # Get stock data from yfinance module from 60 days before 01/01 to today
        stock_data = Ticker(self.stock)
        df = DataFrame(stock_data.history(start=str(datetime.now().date().replace(month=1, day=1) - timedelta(60)),
                                          end=str(date.today())))

        return df
Пример #9
0
    def run(self):
        company = Ticker(self.ticker)

        if (company == None):
            return
        # Get prices using yfinance package.
        quotes = company.history(period="max")

        # Save results to csv.
        with self.output().open("w") as out_file:
            quotes.to_csv(out_file, index=False, compression="gzip")
Пример #10
0
    def _set_info_data(self, yf_ticker: yf.Ticker) -> None:
        info = yf_ticker.get_info()

        # Check if this is an equity. If not, raise error, since only equity is implemented.
        if not info['quoteType'] in self.supported_quote_type:
            raise Exception(
                f'"{self.symbol}" is "quoteType" == "{info["quoteType"]}". This "quoteType" is not implemented, '
                f'yet.')

        self.recommendation = info[
            'recommendationKey'] if 'recommendationKey' in info else 'N/A'
        self.volume = info['volume']
        prices_to_convert = {
            'current_price':
            info['preMarketPrice']
            if info['preMarketPrice'] else info['regularMarketPrice'],
            'price_52w_high':
            info['fiftyTwoWeekHigh'],
            'price_52w_low':
            info['fiftyTwoWeekLow']
        }

        if info['quoteType'] == 'EQUITY':
            self.percent_change_52w = info['52WeekChange']

        prices_converted = self._convert_to_local_currency(prices_to_convert)
        self.current_price = prices_converted['current_price']
        self.price_52w_high = prices_converted['price_52w_high']
        self.price_52w_low = prices_converted['price_52w_low']
        self.market_capitalization = self.volume * self.current_price
Пример #11
0
def get_stock_info_from_cache(symbol):
    "Return element from cache if available, if not create"
    if STOCK_INFO_CACHE.get(symbol) != None:
        return STOCK_INFO_CACHE.get(symbol)
    stock = Ticker(symbol)
    #stock.refresh()
    STOCK_INFO_CACHE.add(symbol, stock)
    return stock
Пример #12
0
def previous_high(ticker: yf.Ticker, time_period: str) -> float:
    high = 0
    stock_history = ticker.history(time_period)
    for i in range(0, len(stock_history) - 2):
        temp_high = stock_history.iloc[i].to_dict()['High']
        if temp_high > high:
            high = temp_high
    return high
Пример #13
0
def companyinfo():
    if request.method == 'POST':
        stock = ''
        try:
            stock = str(json.loads(request.data)["stock"])
        except Exception as ex:
            print(ex)

        try:
            stock = str(request.form["stock"])
        except Exception as ex:
            print(ex)

        obj = ''
        try:
            obj = Ticker(stock)
        except Exception as ex:
            print(ex)

        val = ''
        try:
            val = obj.info
        except Exception as ex:
            print(ex)

        data = dict()
        try:
            data["PEGratio"] = val["pegRatio"]
            data["futurePEGratio"] = val["pegRatio"]
        except Exception as ex:
            print(ex)
        try:
            data["futurePEratio"] = val["forwardPE"]
            data["PEratio"] = val["forwardPE"]
        except Exception as ex:
            print(ex)
        try:
            data["beta"] = val["beta"]
        except Exception as ex:
            print(ex)

        try:
            val = si.get_quote_table(stock)
        except Exception as ex:
            print(ex)
        try:
            data["PEratio"] = val["PE Ratio (TTM)"]
        except Exception as ex:
            print(ex)

        outputdata = jsonify(data)

        outputdata.headers.add("Access-Control-Allow-Origin", "*")
        outputdata.headers.add("Access-Control-Allow-Headers", "*")
        outputdata.headers.add("Access-Control-Allow-Methods", "*")

        return outputdata
Пример #14
0
def main():
    config = ConfigParser()
    config.read('/opt/market/market.ini')
    stocks = config.get('general', 'stocks')

    db = "postgres://[email protected]:5432/stocks"
    session = market.session.Session(db)

    for stock in stocks.split():
        print(stock)

        if not session.check_table(stock):
            continue

        ticker = Ticker(stock)
        df = ticker.history(period='15m', interval='1m')

        session.push_df(dataframe=df, name=stock)
Пример #15
0
def historical():
    if request.method == 'POST':
        data = json.loads(request.data)
        stock = str(data['stock'])
        period = str(data['period'])
        interval = str(data['interval'])

        obj = ''
        try:
            obj = Ticker(stock)
        except Exception as ex:
            print(ex)

        val = ''
        try:
            val = obj.history(period=period, interval=interval)
            val = val.reset_index()
        except Exception as ex:
            print(ex)

        data = dict()
        data['date'] = val['Date'].astype(str).tolist()
        data['open'] = val['Open'].tolist()
        data['high'] = val['High'].tolist()
        data['low'] = val['Low'].tolist()
        data['close'] = val['Close'].tolist()
        data['volume'] = val['Volume'].tolist()

        for i in range(0, len(data['date'])):
            year = data['date'][i][:4]
            month = data['date'][i][5:7]
            day = data['date'][i][8:]
            dt = datetime.datetime(int(year), int(month), int(day))
            timestamp = int(dt.replace(tzinfo=timezone.utc).timestamp())
            data['date'][i] = timestamp

        outputdata = jsonify(data)

        #outputdata.headers.add("Access-Control-Allow-Origin", "*")
        #outputdata.headers.add("Access-Control-Allow-Headers", "*")
        #outputdata.headers.add("Access-Control-Allow-Methods", "*")

        return outputdata
Пример #16
0
class EquityYFinance(Asset):
    def __init__(self, ticker):
        from yfinance import Ticker
        super().__init__(ticker, None)
        self.y = Ticker(ticker)
        self.h = self.y.history(period="max")

    def price(self, t: Date) -> float:
        return self.h.Close[t]

    def coupon(self, t: Date) -> float:
        return self.h.Dividends[t]
Пример #17
0
def calculate_sma(ticker: yf.Ticker,
                  time_period="1mo",
                  interval="1d") -> float:
    stock_history = ticker.history(period=time_period, interval=interval)
    summation = 0
    time_period_days = 0
    for i in range(0, len(stock_history) - 1):
        summation += stock_history.iloc[i].to_dict()['Close']
        time_period_days += 1
    if time_period_days > 0:
        return summation / time_period_days
    return sys.maxsize
Пример #18
0
def get_info_data(tickername):
    """
    :param tickername: краткое название биржевого инструмента
    :return: возвращает список из [тикера, название компании,
     описание компании, ссылка на лого]
    """

    info = {}
    try:
        info = Ticker(tickername).info
    except (IndexError, ):
        info = None
    return info
Пример #19
0
def get_ticker(name: str) -> Ticker:
    ticker = Ticker(name)
    info = ticker.info
    if not info.get("ask"):
        if name[-3:] != ".HE":
            if '.' in name:
                # user probably wrote the name wrong
                return get_ticker(f'{name.replace(".", "-")}')
            # if not, maybe they forgot the helsinki börs (.he)
            return get_ticker(f"{name}.HE")
        return False
    # print(ticker.dividends)

    return ticker
Пример #20
0
    def run(self):
        company = Ticker(self.ticker)

        if (company == None):
            return
        # Get dividends using yfinance package.
        dividends = company.dividends.to_frame().reset_index("Date")

        # Since there may be more than one dividends payment within a year I need to group dividends by year.
        divByYear = (dividends.groupby(
            dividends["Date"].dt.year).sum().reset_index("Date"))

        # Save results to csv.
        with self.output().open("w") as out_file:
            divByYear.to_csv(out_file, index=False, compression="gzip")
Пример #21
0
def processHist(ticker: yf.Ticker):
    hist = {}
    info = {}
    data = ticker.history(period='max', interval='5d')
    data = data.where(pd.notnull(data), None)
    try:
        for item in data.T.iteritems():
            stuff = dict(item[1])
            try:
                hist[item[0].value] = stuff
            except Exception as e:
                hist[item[0]] = stuff
    except Exception as e:
        hist['error'] = e
    return hist
Пример #22
0
    def get_sp500_recommendations(self, stocks=['all'], print_ratings=False):
        import pandas as pd
        from yfinance import Ticker

        # Call the get_sp500_tickers to get a list of tickers
        if stocks == ['all']:
            tickers = self.__get_sp500_tickers()
        else:
            tickers = []
            for stock in stocks:
                tickers.append(Ticker(stock))

        # Get a list of analyst ratings for each ticker in tickers
        length = len(tickers)
        i = 1
        recs = []
        symbols = []
        print('Getting analyst ratings...')
        for ticker in tickers:
            print('\tGetting ratings for ',
                  ticker.ticker,
                  ' (',
                  i,
                  '/',
                  length,
                  ')',
                  sep='')
            symbols.append(ticker.ticker)
            try:
                recs.append(ticker.recommendations)
            except:
                recs.append('No ratings found')
            i += 1
        print('Done!')

        # Get the numerical and average rating for each recommendation
        numerical_ratings, avg_ratings = self.__rating_to_num(
            recs, print_ratings)

        # Create data frame with all calculated info
        sp500_ratings_df = pd.DataFrame()
        sp500_ratings_df['Tickers'] = symbols
        sp500_ratings_df['Analyst Ratings'] = recs
        sp500_ratings_df['Numerical Ratings'] = numerical_ratings
        sp500_ratings_df['Average Rating'] = avg_ratings
        sp500_ratings_df = sp500_ratings_df.sort_values(by='Average Rating')

        return sp500_ratings_df
Пример #23
0
def update_stock_records():
    '''Update all stock records from Yahoo API.'''
    symbols = db.Stocks.distinct('Symbol')
    for sym in symbols:
        try:
            stock = Ticker(sym).history(period='1y')
            db.Stocks.update_one({StockColumn.Symbol.name: sym}, {
                '$set': {
                    StockColumn.Records.name:
                    convert_dataframe_to_document(stock)
                }
            })
        except:
            db.Stocks.update_one({StockColumn.Symbol.name: sym},
                                 {'$set': {
                                     StockColumn.Records.name: []
                                 }})
Пример #24
0
    def yfinance(cls,
                 stock_ticker: str,
                 stock_max: float,
                 stock_min: float,
                 short: bool = True) -> list:
        """Checks whether the current price of the stock has increased or decreased.

        Args:
            stock_ticker: Stock ticker value.
            stock_max: Maximum value after which a notification has to be triggered.
            stock_min: Minimum value below which a notification has to be triggered.
            short: Boolean flag to send a short summary vs long description.

        Returns:
            list:
            A list of configured notification message and the price.
        """
        raw_details = Ticker(ticker=stock_ticker).info
        price = round(float(raw_details['currentPrice']), 2)

        if price < stock_min or price > stock_max:
            if short:
                if price < stock_min:
                    return [
                        f'{stock_ticker} [${price}] is currently less than {int(stock_min)}\n',
                        price
                    ]
                elif price > stock_max:
                    return [
                        f'{stock_ticker} [${price}] is currently more than {int(stock_max)}\n',
                        price
                    ]

            msg = f"The current price of {raw_details.get('longName')} is: ${price}"
            day_list = f"\nToday's change list: {[raw_details.get('previousClose'), raw_details.get('open')]}"

            if price < stock_min:
                return [
                    f'{stock_ticker} is currently less than ${stock_min}\n{msg}{day_list}',
                    price
                ]
            elif price > stock_max:
                return [
                    f'{stock_ticker} is currently more than ${stock_max}\n{msg}{day_list}',
                    price
                ]
Пример #25
0
    def __get_sp500_tickers():
        import requests
        import bs4 as bs
        from yfinance import Ticker

        # Get a list of SP500 tickers from wikipedia
        print('Getting S&P500 tickers...')
        resp = requests.get(
            'http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
        soup = bs.BeautifulSoup(resp.text, 'lxml')
        table = soup.find('table', {'class': 'wikitable sortable'})
        tickers = []
        for row in table.findAll('tr')[1:]:
            ticker = row.findAll('td')[0].text
            tickers.append(Ticker(ticker.strip()))
        print('Done!\n')

        return tickers
Пример #26
0
def update(n_clicks, value, input_stock):
    try:
        if n_clicks is not None:
            ticker = input_stock
        else:
            ticker = 'AAPL'
        df = web.DataReader(ticker, 'yahoo', start, end)
    except RemoteDataError:
        raise PreventUpdate

    fig = go.Figure(go.Candlestick(
        x=df.index,
        open=df['Open'],
        high=df['High'],
        low=df['Low'],
        close=df['Close']
    ))

    fig.update_layout(xaxis_rangeslider_visible='slider' in value)
    return fig, Ticker(ticker).info['shortName']
Пример #27
0
    def __init__(self,
                 symbol: str,
                 start: str = str((datetime.now() - timedelta(days=365))),
                 end: str = str(datetime.now())):
        """
        The constructor for the Stock class.

        Parameters:
            symbol (str): A stock ticker symbol.
            start (str): A date in yyyy-mm-dd format. This data is the beginning of a period of time to query stock data.
            end (str): A date in yyyy-mm-dd format. This data is the beginning of a period of time to query stock data.
        """
        # Enforce capitalization
        self._ticker = symbol.upper().split(" ")[0]

        # Retrieval the financial data
        self._history = None
        self.start_date = start.split(" ")[0]
        self.end_date = end.split(" ")[0]
        self.stock_data = Ticker(self.ticker)
        self.calculate_stats()
def _download_one(ticker,
                  start=None,
                  end=None,
                  auto_adjust=False,
                  back_adjust=False,
                  actions=False,
                  period="max",
                  interval="1d",
                  prepost=False,
                  proxy=None,
                  rounding=False):

    return Ticker(ticker).history(period=period,
                                  interval=interval,
                                  start=start,
                                  end=end,
                                  prepost=prepost,
                                  actions=actions,
                                  auto_adjust=auto_adjust,
                                  back_adjust=back_adjust,
                                  proxy=proxy,
                                  rounding=rounding,
                                  many=True)
from yfinance import Ticker

msft = Ticker("MSFT")

# get stock info
infoData = msft.info
print(infoData)

#
# # show actions (dividends, splits)
# msft.actions
#
# # show dividends
# msft.dividends
#
# # show splits
# msft.splits
#
# # show financials
# msft.financials
# msft.quarterly_financials
#
# # show major holders
# msft.major_holders
#
# # show institutional holders
# msft.institutional_holders
#
# # show balance sheet
# msft.balance_sheet
# msft.quarterly_balance_sheet
Пример #30
0
def get_price(ticker_symbol):
    stock = Ticker(ticker_symbol)
    data = stock.info
    return {'ask': data["ask"], 'bid': data["bid"]}