Skip to content

Instantly share code, notes, and snippets.

@NickBouwhuis
Created January 18, 2023 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickBouwhuis/abd89f2cd4a9cdaa506b68b77e2a2147 to your computer and use it in GitHub Desktop.
Save NickBouwhuis/abd89f2cd4a9cdaa506b68b77e2a2147 to your computer and use it in GitHub Desktop.
import requests
import forex_python.converter
# Define variables
stocks = {"AAPL": 100, "TSLA": 100, "MSFT": 100}
finnhub_token = 'xxx'
lunchmoney_token = 'xxx'
lunchmoney_assetid = 1234
# Initialize the total worth in EUR.
# You can add cash that's not invested but still in your account here
total_worth_eur = 0
# Create a converter object
converter = forex_python.converter.CurrencyRates()
# Iterate through the symbols and multipliers
for symbol, multiplier in stocks.items():
# Make the GET request to the API
response = requests.get(f'https://finnhub.io/api/v1/quote?symbol={symbol}&token={finnhub_token}')
# Get the current price in USD
current_price_usd = response.json()['c']
# Multiply the current price by the multiplier
current_price_usd *= multiplier
# Convert the value from USD to EUR
current_price_eur = converter.convert('USD', 'EUR', current_price_usd)
# Add the converted value to the total worth in EUR
total_worth_eur += current_price_eur
# Prepare the JSON payload with the balance key
payload = {'balance': total_worth_eur}
# Prepare the headers with the API token
headers = {'Authorization': f'Bearer {lunchmoney_token}'}
# Make a PUT request to the Lunchmoney API
response = requests.put(f'https://dev.lunchmoney.app/v1/assets/{lunchmoney_assetid}', json=payload, headers=headers)
# Check the response status code
if response.status_code == 200:
print(f'Succesfully updated asset {lunchmoney_assetid} to {total_worth_eur} EUR')
else:
print(f'An error occurred: {response.text}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment