Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions polybar-scripts/info-stocks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Script: info-stocks

Script that allows you to display the change of a stock on Polybar in the same
day. Uses python3 and **yfinance**.

![example](./screenshots/example.png)

## Dependencies

* **python3**
* **yfinance**

## Configuration

* The script needs to be configured with the text to show of each ticker. 4
emojis are suggested
* The module needs to specify a list of tickers to track. Multiple tickers are
supported.
* The module needs to specify a python3 executable path with yfinance, of
course.

## Module

```ini
[module/stock_tracker]
type = custom/script
exec = path/to/venv/bin/python ~/.config/polybar/src/stock_tracker.py --tickers AAPL MSFT
interval = 60
format = <label>
label = %output%

```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions polybar-scripts/info-stocks/stock_tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh python3

"""
Script to display in Polybar-friendly way information of given stocks. Useful to see yout positions crash in live.

Usage: stock_tracker.py --tickers <ticker1> <ticker2> ..

Created by: Jesús Blázquez
Inspired in: Polystock by Zachary Ashen. His is great I just don't like yahoo_fin
"""

import argparse
import yfinance as yf

def printTicker(ticker: str):
stock = yf.Ticker(ticker)
data = stock.history(period="2d", interval="1d")
prev_close, close = data["Close"].iloc[-2], data["Close"].iloc[-1]
# price = stock.info["regularMarketPrice"]
percent = ((close - prev_close) / prev_close) * 100
# Colors go in the print. An example:
# %{{F#9ece6a}} {ticker}: {percent:.2f}% %{{F-}}
# %{{F#F7768E}} {ticker}: {percent:.2f}% %{{F-}}
# %{{F#7dcfff}} {ticker}: {percent:.2f}% %{{F-}}
if percent > 0:
print(f"#1 {ticker}: {percent:.2f}%", end="")
elif percent < 0:
print(f"#2 {ticker}: {percent:.2f}%", end=" ")
else:
print(f"#3 {ticker}: {percent:.2f}%", end=" ")


parser = argparse.ArgumentParser()
parser.add_argument("--tickers", help="Tickers to display", type=str, nargs="+")
args = parser.parse_args()

if args.tickers is None:
print("No tickers provided")
exit(1)

tickers = args.tickers

for ticker in tickers:
try:
printTicker(ticker)
except Exception as e:
# %{{F#7dcfff}}%{{F-}}
print(f"#4")
exit(1)