From 33be45fa0b4911f6e6802e3d1e321805232c627b Mon Sep 17 00:00:00 2001 From: cemgungor1 Date: Mon, 16 Dec 2024 20:18:03 +0300 Subject: [PATCH] feat: add us stocks --- backend/docker-compose.yaml | 1 + .../management/commands/update_US_stocks.py | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 backend/marketfeed/management/commands/update_US_stocks.py diff --git a/backend/docker-compose.yaml b/backend/docker-compose.yaml index 4cefe877..65522000 100644 --- a/backend/docker-compose.yaml +++ b/backend/docker-compose.yaml @@ -34,6 +34,7 @@ services: python3 manage.py collectstatic --noinput && python3 manage.py update_currencies && python3 manage.py update_stocks && + python3 manage.py update_US_stocks && gunicorn backend.wsgi:application --bind 0.0.0.0:8000' restart: always environment: diff --git a/backend/marketfeed/management/commands/update_US_stocks.py b/backend/marketfeed/management/commands/update_US_stocks.py new file mode 100644 index 00000000..5fabacba --- /dev/null +++ b/backend/marketfeed/management/commands/update_US_stocks.py @@ -0,0 +1,55 @@ +from django.core.management.base import BaseCommand +from marketfeed.models import Stock, Currency +from django.conf import settings +import requests +import pandas as pd + + +class Command(BaseCommand): + help = 'Update or Insert the Turkish stock market stocks to db' + print("update stocks") + + def handle(self, *args, **kwargs): + # Url to fetch stock list + + headers = { + 'authority': 'api.nasdaq.com', + 'accept': 'application/json, text/plain, */*', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36', + 'origin': 'https://www.nasdaq.com', + 'sec-fetch-site': 'same-site', + 'sec-fetch-mode': 'cors', + 'sec-fetch-dest': 'empty', + 'referer': 'https://www.nasdaq.com/', + 'accept-language': 'en-US,en;q=0.9', + } + + parameters = ( + ('tableonly', 'true'), + ('limit', '25'), + ('offset', '0'), + ('download', 'true'), + ) + + + try: + response = requests.get('https://api.nasdaq.com/api/screener/stocks', headers=headers, params=parameters) + data = response.json()['data'] + stocks = pd.DataFrame(data['rows'], columns=data['headers']) + stocks = stocks.drop(columns=['lastsale', 'netchange', 'pctchange', 'marketCap', 'country', 'ipoyear', 'volume','url']) + + currency = Currency.objects.get(code='USD') + + for indx,stock in stocks.iterrows(): + try: + Stock.objects.update_or_create( + symbol=stock['symbol'], + defaults={ + 'name': stock['name'], + 'currency': currency, + } + ) + except Exception as e: + print(e) + except Exception as e: + print(e) \ No newline at end of file