Skip to content

Commit

Permalink
Updates to database load, removing 2 new stocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesetzer committed Nov 13, 2023
1 parent e7ac811 commit 16cb469
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
4 changes: 1 addition & 3 deletions dataload/DJIAStockList.csv
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ WBA,"Walgreens Boots Alliance, Inc."
XOM,Exxon Mobil Corporation
PFE,Pfizer Inc.
AMGN,"Amgen, Inc."
RTX,Raytheon
AXP,American Express Company
WMT,Walmart Inc.
RTX,Raytheon
63 changes: 48 additions & 15 deletions home/management/commands/load_database.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
# home/management/commands/load_db_with_stocks.py
from django.core.management.base import BaseCommand
from home.views import load_db_with_stocks, load_db_with_recommendations
from home.models import Stock
from django.conf import settings
import os
import csv

class Command(BaseCommand):
help = 'Load initial stock and recommendation data into the database.'
help = 'Load stocks from DJIAStockList.csv and update the database.'

def handle(self, *args, **options):
# Call the function to load stocks
try:
self.stdout.write('Loading stocks...')
load_db_with_stocks()
self.stdout.write(self.style.SUCCESS('Stocks have been loaded successfully.'))
except Exception as e:
self.stdout.write(self.style.ERROR(f'An error occurred while loading stocks: {e}'))
# Define the path to your CSV file
csv_file_path = os.path.join(settings.BASE_DIR, 'dataload', 'DJIAStockList.csv')

# Get the current list of stocks from the database
existing_stocks = set(Stock.objects.values_list('ticker', flat=True))

# Counter for statistics
created_count = 0
updated_count = 0
removed_count = 0

# Call the function to load recommendations
try:
csvfilename = os.path.join(settings.BASE_DIR, 'dataload', 'COP_DJIA_Total_Dataset.csv')
self.stdout.write('Loading recommendations...')
load_db_with_recommendations(csvfilename)
self.stdout.write(self.style.SUCCESS('Recommendations have been loaded successfully.'))
# Use Python's built-in CSV reader to process the file
with open(csv_file_path, mode='r', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)

for row in reader:
ticker = row['ticker']
company = row['company']

# Create a new stock entry or update the existing one
stock, created = Stock.objects.update_or_create(
ticker=ticker, # look up by ticker
defaults={'company': company} # fields to update
)

# Update the counters
if created:
created_count += 1
else:
updated_count += 1

# Remove the ticker from the existing stocks set
if ticker in existing_stocks:
existing_stocks.remove(ticker)

# Remove stocks that are no longer in the CSV file
removed_count = Stock.objects.filter(ticker__in=existing_stocks).delete()[0]

# Print summary of the operation
self.stdout.write(self.style.SUCCESS(
f'Operation completed. {created_count} stocks created, {updated_count} stocks updated, {removed_count} stocks removed.'
))

except Exception as e:
self.stdout.write(self.style.ERROR(f'An error occurred while loading recommendations: {e}'))
# If an error occurs during the process, print it to the console
self.stdout.write(self.style.ERROR(f'An error occurred during the stock loading process: {e}'))

0 comments on commit 16cb469

Please sign in to comment.