- 20 Feb 2025
- Added average purchase prices.
May 21, 2024 at 13:26
Updated: 20 Feb 2025
Linux Mint has a taskbar applet that keeps track of crypto investments, allowing me to keep track of the price of Doge from the corner of my eye. This is fine for what it is, but it’s clearly not informative enough. So I wrote a bot that pings my crypto portfolio into a Slack channel every morning. It goes a little something like this:
crypto_bot.py
import requests
import json
from urllib import request
from slack_lib import send_message_to_slack
def get_crypto_data(cryptos):
url = "https://api.coingecko.com/api/v3/simple/price"
params = {
"ids": ",".join(cryptos),
"vs_currencies": "usd",
"include_24hr_change": "true"
}
response = requests.get(url, params=params)
#print("API Response Status Code:", response.status_code)
#print("API Response Content:", response.content)
if response.status_code == 200:
return response.json()
else:
return None
def format_price(crypto, price):
if crypto in ["bitcoin", "ethereum","stellar","xrp"]:
return f"${price:,.2f}"
elif crypto == "dogecoin":
return f"${price:.3f}"
elif crypto == "shiba-inu":
return f"${price:.6f}"
def main():
cryptos = ["bitcoin", "ethereum", "dogecoin", "shiba-inu", "stellar"]
quantities = {
"bitcoin": 234,
"ethereum": 234,
"dogecoin": 234,
"shiba-inu": 234,
"stellar": 234
"xrp": 234 # note: coingecko doesn't have xrp via API
}
average_purchase_prices = {
"bitcoin": 123,
"ethereum": 123,
"dogecoin": 123,
"shiba-inu": 123,
"stellar": 123
"xrp": 123 # note: coingecko doesn't have xrp via API
}
crypto_data = get_crypto_data(cryptos)
if crypto_data:
total_current_value = 0
total_initial_value = 0
total_percent_change = 0
total_value_change = 0
output = ""
for crypto in cryptos:
price = crypto_data[crypto]['usd']
percent_change = crypto_data[crypto]['usd_24h_change']
current_value = price * quantities[crypto]
initial_value = current_value / (1 + percent_change / 100)
value_change = current_value - initial_value
total_current_value += current_value
total_initial_value += initial_value
total_percent_change += percent_change
total_value_change += value_change
# Calculate delta
average_purchase_price = average_purchase_prices[crypto]
purchase_value = average_purchase_price * quantities[crypto]
delta = current_value - purchase_value
delta_percent = (delta / purchase_value) * 100
formatted_price = format_price(crypto, price)
output += (f"> *{crypto.capitalize()}:* {percent_change:.2f}% {formatted_price} | T: ${current_value:,.2f} D: ${delta:,.2f} ({delta_percent:.1f}%)\n")
overall_percent_change = ((total_current_value - total_initial_value) / total_initial_value) * 100
# output += f"> *Total: {overall_percent_change:.2f}% T: ${total_current_value:,.2f}*"
send_message_to_slack(output, username = "🪙 Crypto Bot 💱")
#print(output)
else:
print("Failed to retrieve data.")
if __name__ == "__main__":
main()