-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoin_ticker.py
More file actions
85 lines (63 loc) · 1.85 KB
/
bitcoin_ticker.py
File metadata and controls
85 lines (63 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Show the current price of Bitcoin with color coded text.
Color meanings:
- Green text: Sell
- Blue text: HODL
- Gold text: Buy and HODL
"""
from datetime import datetime, timedelta
import requests
import time
from contextlib import suppress
from copy import deepcopy
from money import Money # pip install money
# Normal Price targets
BUY_PRICE = 30_000
SELL_PRICE = 72_000
# 3/11/24 - Sold BTC @ $72k - Buy back in if it goes steadily higher
# 3/9/24 - BTC hit $70k and is holding at $68k, need more fine grain targets
# SELL_PRICE = 70_000
# BUY_PRICE = 67_999
BLACK = 0x00000
BLUE = 0x0000FF
GOLD = 0xFFD700
GREEN = 0x00FF00
RED = 0xFF0000
WHITE = 0xFFFFFF
def get_color(price):
# Buy Color
if price <= BUY_PRICE:
color = GOLD
# Hodl color
elif BUY_PRICE < price < SELL_PRICE:
color = BLUE
# Sell color
elif price >= SELL_PRICE:
color = GREEN
else:
color = WHITE
return color
def get_btc_price():
while True:
with suppress(
requests.exceptions.ConnectionError, requests.exceptions.JSONDecodeError
):
response = requests.get("https://api.coincap.io/v2/assets/bitcoin")
price = response.json()["data"]["priceUsd"]
break
m_price = Money(price, "USD").format("en_US")
# print(f"BTC {m_price} {start.strftime('%m/%d/%Y %I:%M %p')}")
return m_price, get_color(float(price))
if __name__ == "__main__":
# clear panels
display.set_all(BLACK)
# init
start = datetime.now()
price, color = get_btc_price()
wait_time = timedelta(minutes=0.5)
# main loop
while True:
display.scroll_text(f"BTC {price}", speed=1.5, colour=color)
if (now := datetime.now()) >= start + wait_time:
start = deepcopy(now)
price, color = get_btc_price()
time.sleep(2)