async def get_quote(self, ticket: str) -> Stock: url = f"https://query1.finance.yahoo.com/v11/finance/quoteSummary/{ticket}" params = {"modules": "summaryDetail,price"} response = await self._query(url, params) if "error" in response: raise StonkyException( f"Could not get stock information for {ticket}" ) try: summary_data = response["quoteSummary"]["result"][0][ "summaryDetail" ] price_data = response["quoteSummary"]["result"][0]["price"] stock = Stock( ticket=ticket, currency_code=price_data["currency"], amount_bid=summary_data["bid"].get("raw", 0.0), amount_ask=summary_data["ask"].get("raw", 0.0), amount_low=summary_data["dayLow"].get("raw", 0.0), amount_high=summary_data["dayHigh"].get("raw", 0.0), amount_prev_close=summary_data["previousClose"].get("raw", 0.0), delta_amount=price_data["regularMarketChange"].get("raw", 0.0), delta_percent=price_data["regularMarketChangePercent"].get( "raw", 0.0 ), market_price=price_data["regularMarketPrice"].get("raw", 0.0), volume=summary_data["volume"].get("raw", 0.0), ) except TypeError: raise StonkyException( f"Could not parse stock information for {ticket}" ) return stock
def get_quote(self, ticket: tuple) -> Stock: url = f"https://query1.finance.yahoo.com/v11/finance/quoteSummary/{ticket[0]}" params = {"modules": "summaryDetail,price"} try: response = self._query(url, params) except Exception as e: print("Error while calling api.py get_quote()!") print(e, type(e)) sys.exit(1) #summary_data = response["quoteSummary"]["result"][0]["summaryDetail"] price_data = response["quoteSummary"]["result"][0]["price"] #print(ticket[0]) #print(price_data) return Stock( ticket=ticket[0], name=ticket[1], currency_code=price_data["currency"], #amount_bid=summary_data["bid"].get("raw", 0.0), #amount_ask=summary_data["ask"].get("raw", 0.0), amount_now=price_data["regularMarketPrice"].get("raw", 0.0), #amount_low=summary_data["dayLow"].get("raw", 0.0), #amount_high=summary_data["dayHigh"].get("raw", 0.0), #amount_prev_close=summary_data["previousClose"].get("raw", 0.0), amount_prev_close=price_data["regularMarketPreviousClose"].get( "raw", 0.0), delta_amount=price_data["regularMarketChange"].get("raw", 0.0), delta_percent=price_data["regularMarketChangePercent"].get( "raw", 0.0), volume=price_data["regularMarketVolume"]["raw"], )
def test_ticker_tape_up(self): stock = Stock( ticket="AMD", delta_amount=10.123, delta_percent=0.156, volume=100.123, amount_bid=200.553, ) assert stock.ticker_tape == "AMD 100.12 @ 200.55 ▲ +10.12 +15.60%"
def test_ticker_tape__zero(): stock = Stock( ticket="AMD", delta_amount=0.0, delta_percent=0.156, volume=100.123, amount_bid=200.553, ) assert stock.ticker_tape == "AMD 100.12 @ 200.55 ▬ +0.00 +15.60%"
async def get_quote(self, ticket: str) -> Stock: url = f"https://query1.finance.yahoo.com/v11/finance/quoteSummary/{ticket}" params = {"modules": "summaryDetail,price"} response = await self._query(url, params) if "error" in response: raise RuntimeError(f"Could not get stock information for {ticket}") try: price_data = response["quoteSummary"]["result"][0]["price"] stock = Stock( ticket=ticket, currency=price_data["currency"], current_amount=price_data["regularMarketPrice"].get("raw", 0.0), delta_amount=price_data["regularMarketChange"].get("raw", 0.0), delta_percent=price_data["regularMarketChangePercent"].get("raw", 0.0), market_price=price_data["regularMarketPrice"].get("raw", 0.0), volume=price_data["regularMarketVolume"].get("raw", 0.0), ) except (TypeError, KeyError): raise RuntimeError(f"Could not get stock information for '{ticket}'") return stock
def get_quote(self, ticket: str) -> Stock: url = f"https://query1.finance.yahoo.com/v11/finance/quoteSummary/{ticket}" params = {"modules": "summaryDetail,price"} response = self._query(url, params) summary_data = response["quoteSummary"]["result"][0]["summaryDetail"] price_data = response["quoteSummary"]["result"][0]["price"] return Stock( ticket=ticket, currency_code=price_data["currency"], amount_bid=summary_data["bid"].get("raw", 0.0), amount_ask=summary_data["ask"].get("raw", 0.0), amount_low=summary_data["dayLow"].get("raw", 0.0), amount_high=summary_data["dayHigh"].get("raw", 0.0), amount_prev_close=summary_data["previousClose"].get("raw", 0.0), delta_amount=price_data["regularMarketChange"].get("raw", 0.0), delta_percent=price_data["regularMarketChangePercent"].get( "raw", 0.0 ), volume=summary_data["volume"]["raw"], )
def test_voume_str__billions__upper(self): stock = Stock(volume=999_999_999_999) assert stock.volume_str == "999.9B"
def test_voume_str__billions__lower(self): stock = Stock(volume=1_000_000_000) assert stock.volume_str == "1B"
def test_volume_str__thousands__upper(self): stock = Stock(volume=999_999) assert stock.volume_str == "999.9K"
def test_volume_str__millions__upper(): stock = Stock(volume=999_999_999) assert stock.volume_str == "999.9M"
def test_profit_and_loss(self): stock = Stock(delta_amount=12.345, delta_percent=0.0678) assert stock.profit_and_loss == "+6.78% +12.35 USD"
def test_colour__green(self): stock = Stock(delta_amount=100) assert stock.colour == "green"
from stonky.enums import CurrencyType from stonky.stock import Stock AMD_STOCK = Stock( ticket="AMD", currency=CurrencyType.USD, amount_bid=452.49, amount_ask=452.67, amount_low=441.19, amount_high=453.1, amount_prev_close=437.5, delta_amount=14.540009, delta_percent=0.033234306, market_price=452.04, volume=41486205, ) AAPL_STOCK = Stock( ticket="AAPL", currency=CurrencyType.USD, amount_bid=81.83, amount_ask=81.89, amount_low=81.54, amount_high=84.0199, amount_prev_close=82.61, delta_amount=-0.7700043, delta_percent=-0.009320957, market_price=81.84, volume=57092710, )
def test_post_init(self): stock = Stock(ticket="amd", currency_code="usd") assert stock.ticket == "AMD" assert stock.currency_code == "USD"
def test_colour__red(self): stock = Stock(delta_amount=-100) assert stock.colour == "red"
def test_colour__yellow(self): stock = Stock(delta_amount=0) assert stock.colour == "yellow"
def test_volume_str__hundreds__lower(self): stock = Stock(volume=0) assert stock.volume_str == ""
def test_post_init(self): stock = Stock(ticket="amd") assert stock.ticket == "AMD"
def test_volume_str__hundreds__upper(self): stock = Stock(volume=999.99) assert stock.volume_str == "999.99"
def test_position(self): stock = Stock(ticket="AAPL", delta_amount=12.345, delta_percent=0.0678) assert stock.position == "AAPL +12.35 +6.78%"
def test_volume_str__thousands__lower(self): stock = Stock(volume=1_000) assert stock.volume_str == "1K"
from stonky.stock import Stock AMD_STOCK = Stock( ticket="AMD", currency="USD", current_amount=452.49, delta_amount=14.540009, delta_percent=0.033234306, market_price=452.04, volume=41486205, ) AAPL_STOCK = Stock( ticket="AAPL", currency="USD", current_amount=81.83, delta_amount=-0.7700043, delta_percent=-0.009320957, market_price=81.84, volume=57092710, ) VGRO_TO_STOCK = Stock( ticket="VGRO.TO", currency="USD", current_amount=27.12, delta_amount=-0.12000084, delta_percent=-0.004402085, market_price=27.14, volume=126570, )
def test_volume_str__millions__lower(): stock = Stock(volume=1_000_000) assert stock.volume_str == "1M"