Skip to content

Commit

Permalink
Merge pull request #539 from bounswe/feature/BE-US-stocks
Browse files Browse the repository at this point in the history
Us stocks
  • Loading branch information
hikasap authored Dec 16, 2024
2 parents 3c9490f + 4915e19 commit eacc84b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
python3 manage.py migrate --noinput &&
python3 manage.py collectstatic --noinput &&
python3 manage.py update_currencies &&
python3 manage.py update_US_stocks &&
python3 manage.py update_stocks &&
python3 manage.py generate_posts &&
python3 manage.py update_indices &&
Expand Down
55 changes: 55 additions & 0 deletions backend/marketfeed/management/commands/update_US_stocks.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit eacc84b

Please # to comment.