Inkycal/inkycal/modules/inkycal_stocks.py

258 lines
9.7 KiB
Python
Raw Normal View History

"""
Stocks Module for Inkycal Project
Version 0.5: Added improved precision by using new priceHint parameter of yfinance
Version 0.4: Added charts
Version 0.3: Added support for web-UI of Inkycal 2.0.0
Version 0.2: Migration to Inkycal 2.0.0
Version 0.1: Migration to Inkycal 2.0.0b
by https://github.com/worstface
"""
import logging
2023-11-20 18:58:56 +01:00
import os
from PIL import Image
2023-11-21 15:18:19 +01:00
from matplotlib import pyplot
2023-11-20 18:58:56 +01:00
from inkycal.custom import write, internet_available
from inkycal.modules.template import inkycal_module
2023-11-20 20:15:16 +01:00
import yfinance as yf
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
logger = logging.getLogger(__name__)
2022-04-02 01:30:17 +02:00
class Stocks(inkycal_module):
name = "Stocks - Displays stock market infos from Yahoo finance"
2022-04-02 01:30:17 +02:00
# required parameters
requires = {
2022-04-02 01:30:17 +02:00
"tickers": {
2022-04-02 01:30:17 +02:00
"label": "You can display any information by using "
"the respective symbols that are used by Yahoo! Finance. "
"Separate multiple symbols with a comma sign e.g. "
"TSLA, U, NVDA, EURUSD=X"
}
}
2022-04-02 01:30:17 +02:00
def __init__(self, config):
super().__init__(config)
config = config['config']
# If tickers is a string from web-ui, convert to a list, else use
# tickers as-is i.e. for tests
if config['tickers'] and isinstance(config['tickers'], str):
self.tickers = config['tickers'].replace(" ", "").split(',') # returns list
else:
self.tickers = config['tickers']
# give an OK message
print(f'{__name__} loaded')
def generate_image(self):
"""Generate image for this module"""
# Define new image size with respect to padding
im_width = int(self.width - (2 * self.padding_left))
im_height = int(self.height - (2 * self.padding_top))
im_size = im_width, im_height
logger.info(f'image size: {im_width} x {im_height} px')
# Create an image for black pixels and one for coloured pixels (required)
im_black = Image.new('RGB', size=im_size, color='white')
im_colour = Image.new('RGB', size=im_size, color='white')
# Create tmp path
2023-11-21 15:18:19 +01:00
tmpPath = 'temp/'
2022-04-02 01:30:17 +02:00
2023-11-21 15:18:19 +01:00
if not os.path.exists(tmpPath):
print(f"Creating tmp directory {tmpPath}")
2023-11-20 18:58:56 +01:00
os.mkdir(tmpPath)
2022-04-02 01:30:17 +02:00
# Check if internet is available
2023-11-21 15:18:19 +01:00
if internet_available():
2022-04-02 01:30:17 +02:00
logger.info('Connection test passed')
else:
raise Exception('Network could not be reached :/')
# Set some parameters for formatting feeds
line_spacing = 1
2023-11-20 20:15:16 +01:00
text_bbox = self.font.getbbox("hg")
2023-11-21 15:18:19 +01:00
line_height = text_bbox[3] + line_spacing
2022-04-02 01:30:17 +02:00
line_width = im_width
2023-11-20 20:15:16 +01:00
max_lines = (im_height // (line_height + line_spacing))
2022-04-02 01:30:17 +02:00
logger.debug(f"max_lines: {max_lines}")
# Calculate padding from top so the lines look centralised
spacing_top = int(im_height % line_height / 2)
# Calculate line_positions
line_positions = [
(0, spacing_top + _ * line_height) for _ in range(max_lines)]
logger.debug(f'line positions: {line_positions}')
parsed_tickers = []
parsed_tickers_colour = []
chartSpace = Image.new('RGBA', (im_width, im_height), "white")
chartSpace_colour = Image.new('RGBA', (im_width, im_height), "white")
tickerCount = range(len(self.tickers))
for _ in tickerCount:
ticker = self.tickers[_]
logger.info(f'preparing data for {ticker}...')
yfTicker = yf.Ticker(ticker)
try:
stockInfo = yfTicker.info
except Exception as exceptionMessage:
logger.warning(f"Failed to get '{ticker}' ticker info: {exceptionMessage}")
try:
stockName = stockInfo['shortName']
except Exception:
stockName = ticker
logger.warning(f"Failed to get '{stockName}' ticker name! Using "
"the ticker symbol as name instead.")
try:
stockCurrency = stockInfo['currency']
if stockCurrency == 'USD':
stockCurrency = '$'
elif stockCurrency == 'EUR':
stockCurrency = ''
except Exception:
stockCurrency = ''
logger.warning(f"Failed to get ticker currency!")
try:
precision = stockInfo['priceHint']
except Exception:
precision = 2
logger.warning(f"Failed to get '{stockName}' ticker price hint! Using "
"default precision of 2 instead.")
stockHistory = yfTicker.history("30d")
stockHistoryLen = len(stockHistory)
logger.info(f'fetched {stockHistoryLen} datapoints ...')
previousQuote = (stockHistory.tail(2)['Close'].iloc[0])
currentQuote = (stockHistory.tail(1)['Close'].iloc[0])
currentHigh = (stockHistory.tail(1)['High'].iloc[0])
currentLow = (stockHistory.tail(1)['Low'].iloc[0])
currentOpen = (stockHistory.tail(1)['Open'].iloc[0])
currentGain = currentQuote - previousQuote
currentGainPercentage = (1 - currentQuote / previousQuote) * -100
firstQuote = stockHistory.tail(stockHistoryLen)['Close'].iloc[0]
logger.info(f'firstQuote {firstQuote} ...')
def floatStr(precision, number):
return "%0.*f" % (precision, number)
def percentageStr(number):
return '({:+.2f}%)'.format(number)
def gainStr(precision, number):
return "%+.*f" % (precision, number)
stockNameLine = '{} ({})'.format(stockName, stockCurrency)
stockCurrentValueLine = '{} {} {}'.format(
floatStr(precision, currentQuote), gainStr(precision, currentGain),
percentageStr(currentGainPercentage))
stockDayValueLine = '1d OHL: {}/{}/{}'.format(
floatStr(precision, currentOpen), floatStr(precision, currentHigh), floatStr(precision, currentLow))
maxQuote = max(stockHistory.High)
minQuote = min(stockHistory.Low)
logger.info(f'high {maxQuote} low {minQuote} ...')
stockMonthValueLine = '{}d OHL: {}/{}/{}'.format(
stockHistoryLen, floatStr(precision, firstQuote), floatStr(precision, maxQuote),
floatStr(precision, minQuote))
logger.info(stockNameLine)
logger.info(stockCurrentValueLine)
logger.info(stockDayValueLine)
logger.info(stockMonthValueLine)
parsed_tickers.append(stockNameLine)
parsed_tickers.append(stockCurrentValueLine)
parsed_tickers.append(stockDayValueLine)
parsed_tickers.append(stockMonthValueLine)
parsed_tickers_colour.append("")
if currentGain < 0:
parsed_tickers_colour.append(stockCurrentValueLine)
else:
parsed_tickers_colour.append("")
if currentOpen > currentQuote:
parsed_tickers_colour.append(stockDayValueLine)
else:
parsed_tickers_colour.append("")
if firstQuote > currentQuote:
parsed_tickers_colour.append(stockMonthValueLine)
else:
parsed_tickers_colour.append("")
2023-11-21 15:18:19 +01:00
if _ < len(tickerCount):
2022-04-02 01:30:17 +02:00
parsed_tickers.append("")
parsed_tickers_colour.append("")
logger.info(f'creating chart data...')
chartData = stockHistory.reset_index()
chartCloseData = chartData.loc[:, 'Close']
chartTimeData = chartData.loc[:, 'Date']
logger.info(f'creating chart plot...')
fig, ax = plt.subplots() # Create a figure containing a single axes.
ax.plot(chartTimeData, chartCloseData, linewidth=8) # Plot some data on the axes.
ax.set_xticklabels([])
ax.set_yticklabels([])
chartPath = tmpPath + ticker + '.png'
logger.info(f'saving chart image to {chartPath}...')
plt.savefig(chartPath)
logger.info(f'chartSpace is...{im_width} {im_height}')
logger.info(f'open chart ...{chartPath}')
chartImage = Image.open(chartPath)
2023-11-21 15:18:19 +01:00
chartImage.thumbnail((int(im_width / 4), int(line_height * 4)), Image.BICUBIC)
pyplot.close()
2022-04-02 01:30:17 +02:00
2023-11-21 15:18:19 +01:00
chartPasteX = im_width - chartImage.width
2022-04-02 01:30:17 +02:00
chartPasteY = line_height * 5 * _
logger.info(f'pasting chart image with index {_} to...{chartPasteX} {chartPasteY}')
if firstQuote > currentQuote:
chartSpace_colour.paste(chartImage, (chartPasteX, chartPasteY))
else:
chartSpace.paste(chartImage, (chartPasteX, chartPasteY))
im_black.paste(chartSpace)
im_colour.paste(chartSpace_colour)
# Write/Draw something on the black image
for _ in range(len(parsed_tickers)):
if _ + 1 > max_lines:
logger.error('Ran out of lines for parsed_ticker_colour')
break
write(im_black, line_positions[_], (line_width, line_height),
parsed_tickers[_], font=self.font, alignment='left')
# Write/Draw something on the colour image
for _ in range(len(parsed_tickers_colour)):
if _ + 1 > max_lines:
logger.error('Ran out of lines for parsed_tickers_colour')
break
write(im_colour, line_positions[_], (line_width, line_height),
parsed_tickers_colour[_], font=self.font, alignment='left')
# Save image of black and colour channel in image-folder
return im_black, im_colour