Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stock Ticker
#1
This is purely visual, and uses random numbers as proof of concept.  Though, the update function is fairly easy to spot, so if you want to actually check live stock prices, that shouldn't be hard for you to do.

When it runs, it checks the available resolutions your monitor can support, so it makes itself as wide as is possible, at the top of the screen, and after a little initialization, starts scrolling quotes.

import pygame
import random
import time

WHITE = (255, 255, 255)
BLACK = (  0,   0,   0)
RED =   (255,   0,   0)
GREEN = (  0, 255,   0)

""" 
	TickerItem cares about WHAT it looks like, not WHERE it is.
	The TickerGroup handles positioning.
"""
class TickerItem(object):
	font = None
	
	@classmethod
	def get_font(cls):
		if not cls.font:
			pygame.font.init()
			cls.font = pygame.font.SysFont("lucidafax", 16, True)
		return cls.font
	
	def __init__(self, ticker):
		self.symbol = ticker[0]
		self.value = self.initial_value = round(ticker[1], 2)
		self.last_update = 0
		self.update()
		self.inital_value = self.value
		
	def render_text(self):
		text = "{0} [{1:.2f}]".format(self.symbol, self.value)
		font = self.get_font()
		color = WHITE
		if self.value > self.initial_value:
			color = GREEN
		elif self.value < self.initial_value:
			color = RED
		self.surface = font.render(text, True, color, BLACK)
		self.text_size = font.size(text)
		
	def update(self):
		# only update the value once every 10 seconds
		if time.time() - self.last_update > 10:
			self.value += (random.random() * random.choice([1, -1]))
			self.value = round(self.value, 2)
			self.render_text()
			self.last_update = time.time()

""" 
	Maintains a list of TickerItems, and their positions in space.
	TickerGroup handles moving the TickerItems around,
		but doesn't care about what they look like or what they say.
"""
class TickerGroup(object):
	def __init__(self, screen, tickers):
		self.distance = 10
		self.screen = screen
		self.tickers = []
		# uncomment if you want the ticker to be blank when it starts,
		# and for all stocks to creep in, instead of being started full of tickers
		xpos = 5 # self.screen.get_width()
		for ticker in tickers:
			tcker = TickerItem(ticker)
			pos = [xpos, 0]
			self.tickers.append({
				"ticker": tcker,
				"position": pos
			})
			xpos += tcker.text_size[0] + self.distance
		self.last = xpos
	
	def update(self):
		for ticker in self.tickers:
			ticker['ticker'].update()
			ticker['position'][0] -= 1
			text_size = ticker['ticker'].text_size[0]
			if ticker['position'][0] + text_size < 0:
				ticker['position'][0] = self.last
			self.last = ticker['position'][0] + text_size + self.distance
			self.screen.blit(ticker['ticker'].surface, ticker['position'])

if __name__ == '__main__':
	# set the window to be at the top of the screen
	import os
	os.environ['SDL_VIDEO_WINDOW_POS'] = "0,0"

	pygame.init()
	# find the current resolution, and use that as our width so we stretch across the screen
	sizes = pygame.display.list_modes()
	screen_size = (sizes[0][0], 20)
	screen = pygame.display.set_mode(screen_size, pygame.NOFRAME)
	
	clock = pygame.time.Clock()
	
	ticker = TickerGroup(screen, [
		["SPY", 200],
		["AAPL", 115],
		["AXP", 63],
		["BA", 130],
		["BAC", 15],
		["CAT", 82],
		["CSCO", 30],
		["CVX", 98],
		["DD", 66],
		["GE", 30],
		["IBM", 153],
		["MCD", 116],
		["MMM", 175],
		["MSFT", 57],
		["PG", 87],
		["UNH", 139],
		["UTX", 102],
		["V", 82],
		["VZ", 52],
		["XOM", 83]
	])
	
	running = True
	while running:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				running = False
			if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
				running = False
		screen.fill(BLACK)
		
		ticker.update()
		
		pygame.display.flip()
		clock.tick(60)
	pygame.quit()

Attached Files

Thumbnail(s)
   


Messages In This Thread
Stock Ticker - by nilamo - Sep-26-2016, 08:58 PM
RE: Stock Ticker - by Larz60+ - Sep-26-2016, 09:27 PM
RE: Stock Ticker - by nilamo - Sep-26-2016, 09:32 PM
RE: Stock Ticker - by Skaperen - Sep-27-2016, 06:52 AM
RE: Stock Ticker - by nilamo - Sep-27-2016, 07:00 PM
RE: Stock Ticker - by Larz60+ - Sep-30-2016, 04:43 AM
RE: Stock Ticker - by nilamo - Sep-30-2016, 02:37 PM
RE: Stock Ticker - by Larz60+ - Sep-30-2016, 02:55 PM
RE: Stock Ticker - by Skaperen - Oct-02-2016, 12:35 AM
RE: Stock Ticker - by Larz60+ - Sep-30-2016, 03:55 PM
RE: Stock Ticker - by Larz60+ - Oct-02-2016, 03:23 AM
RE: Stock Ticker - by Larz60+ - Oct-03-2016, 11:17 AM
RE: Stock Ticker - by metulburr - Oct-03-2016, 11:19 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020