示例#1
0
    def parseQuote(json):
        items = ""
        stockList = []
        try:
            items = json.items()
        except Exception as ex:
            print("invalid JSON")
            return stockList

        for (k, v) in items:
            curStock = Stock(k)
            try:
                curStock.symbol = v['symbol'].upper()
                curStock.currentPrice = v["lastPrice"]
                curStock.exchange = str(v["exchangeName"]).upper()
            except:
                try:
                    curStock.currentPrice = v["lastprice"]
                    curStock.exchange = v["exchangename"].upper()
                except:
                    print("Can't parse json quote for: " + curStock.symbol)
            if (curStock.isValid()):
                stockList.append(curStock)

        return stockList
def filter_undervalued_stocks(api: fh.Client) -> None:
    """Find which column contains the tickers of the file and loop through
    that column of the DataFrame object returned from the read_from_excel
    function and perform API calls on each stock."""
    global filtered_count
    while True:
        try:
            t = StockQueue.get(block=False)
        except Empty:
            break
        data = fixed_delay(api.company_basic_financials,
                           symbol=t,
                           metric='all')
        stock_metrics = insert_metrics(data)
        if stock_metrics != {} and match_conditions(stock_metrics) == True:
            peak_price = data['metric']['52WeekHigh']
            last_price = fixed_delay(api.quote, symbol=t)['l']
            if (last_price / peak_price) <= criteria['CHP']:
                valued_stock = Stock(symbol=t)
                valued_stock.c_price = last_price
                stock_profile = fixed_delay(api.company_profile2, symbol=t)
                if stock_profile != {}:
                    valued_stock.name = stock_profile['name']
                    valued_stock.exchange = stock_profile['exchange']
                    valued_stock.industry = stock_profile['finnhubIndustry']
                    valued_stock.web_url = stock_profile['weburl']
                    valued_stock.market_cap = stock_profile[
                        'marketCapitalization'] * 10**6
                Filtered.put(valued_stock)
                filtered_count += 1
                logging.info(
                    f"Filtered {filtered_count} stocks. Symbol is {t}")
 def _purchaseStock(self, ticker, price=0):
     stock = Stock(ticker)
     stock.symbol = ticker
     stock.currentPrice = price
     stock.purchasePrice = price
     stock.exchange = "UNKNOWN"
     self.currentHoldings.append(stock)