Posts

I Used ChatGPT to Write a Python Script to Find the Cheapest Silver Online

26 comments·0 reblogs
pepe.voter
66
0 views
·
min-read

bullion-932218_1280.jpg

Pixabay Image

Happy Friday everyone! I am on my stacking journey, and decided to let ChatGPT write me a Python Script to get the cheapest silver online. This was my first attempt at automation of purchases of silver bars and rounds. I plan on dollar cost averaging rounds and bars, and decided to set some cash aside for silver purchased when automation suggests it.

*Notice- the content below was generated by ChatGPT app on my Android phone on 11/8/2024:

import requests
from bs4 import BeautifulSoup
import pandas as pd

Define a function to get the silver price from JM Bullion

def get_jmbullion_price():
url = 'https://www.jmbullion.com/silver/bars/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')

# Find price from a typical element (this will change based on site layout) 
price = soup.find('span', {'class': 'price-value'}).text.strip() 
return float(price.replace('$', '').replace(',', '')) 

Define a function to get the silver price from Apmex

def get_apmex_price():
url = 'https://www.apmex.com/category/37932/silver'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')

# Look for price on the product page (this will change based on site layout) 
price = soup.find('span', {'class': 'price'}).text.strip() 
return float(price.replace('$', '').replace(',', '')) 

Define a function to get the silver price from Kitco

def get_kitco_price():
url = 'https://www.kitco.com/charts/live_silver.html'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

# Extract the spot price (may vary depending on layout) 
spot_price = soup.find('td', {'class': 'live_silver'}).text.strip() 
return float(spot_price.replace('$', '').replace(',', '')) 

Compare prices from different dealers

def compare_silver_prices():
prices = {
'JM Bullion': get_jmbullion_price(),
'Apmex': get_apmex_price(),
'Kitco': get_kitco_price(),
}

# Convert to DataFrame for easier analysis 
price_df = pd.DataFrame(list(prices.items()), columns=['Dealer', 'Price']) 
print("Silver Price Comparison (in USD per ounce):") 
print(price_df) 

# Find the cheapest price 
cheapest_dealer = price_df.loc[price_df['Price'].idxmin()] 
print(f"\nCheapest Silver: {cheapest_dealer['Dealer']} at ${cheapest_dealer['Price']:.2f} per ounce") 

Main execution

if name == 'main':
compare_silver_prices()

Silver is retailing around $32 per ounce with some ebbs and flows we can attribute to post-election jitters. I am willing to pay 2-4% premiums over spot, and am not eager to buy 100 or 1,000 ounce bars. I decided to put this out there in hopes someone finds it useful.

A basic strategy of dollar cost averaging is easy to implement and involves buying silver or other assets at regular intervals. This can help smooth out returns and weather losses.

Workplace automation has helped me to make money faster for my employer using ChatGPT and other AI programs. I believe that AI is a big part of our future, and want to automate as much as I can. I want to compare prices between dealers online to find the best deals, and this looks promising. What do you think?

Let me know if this helps you. What platform would you put this script on to if you ant to maximize your yield?