├── .gitignore ├── README.md ├── requirements.txt └── src ├── api.py └── chart_source.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | .dist/ 3 | src/__pycache__/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NehalH/TickerChart/76eb0c485e03c8121b4c51c6906c0c029ad7c2b3/README.md -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aniso8601==9.0.1 2 | appdirs==1.4.4 3 | beautifulsoup4==4.12.2 4 | blinker==1.6.2 5 | certifi==2023.5.7 6 | charset-normalizer==3.2.0 7 | click==8.1.6 8 | colorama==0.4.6 9 | Flask==2.3.2 10 | Flask-RESTful==0.3.10 11 | frozendict==2.3.8 12 | html5lib==1.1 13 | idna==3.4 14 | importlib-metadata==6.8.0 15 | itsdangerous==2.1.2 16 | Jinja2==3.1.2 17 | lxml==4.9.3 18 | MarkupSafe==2.1.3 19 | multitasking==0.0.11 20 | numpy==1.25.1 21 | pandas==2.0.3 22 | python-dateutil==2.8.2 23 | pytz==2023.3 24 | requests==2.31.0 25 | six==1.16.0 26 | soupsieve==2.4.1 27 | tzdata==2023.3 28 | urllib3==2.0.4 29 | webencodings==0.5.1 30 | Werkzeug==2.3.6 31 | yfinance==0.2.26 32 | zipp==3.16.2 33 | -------------------------------------------------------------------------------- /src/api.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request 2 | from flask_restful import Resource, Api 3 | from chart_source import get_stock_data 4 | 5 | app = Flask(__name__) 6 | api = Api(app) 7 | 8 | class Chart(Resource): 9 | @staticmethod 10 | def parse_request(): 11 | args = request.args 12 | ticker = args.get('ticker', type=str) 13 | period = args.get('period', type=str) 14 | interval = args.get('interval', type=str) 15 | return ticker, period, interval 16 | 17 | def get(self): 18 | ticker, period, interval = Chart.parse_request() 19 | if not bool(ticker): 20 | return { 21 | 'message':'No query parameters were passed for the API call. Refer documentation.', 22 | 'doc':'https://github.com/NehalH/TickerChart/blob/main/README.md#how-to-use' 23 | } 24 | 25 | data = get_stock_data(ticker, period, interval) 26 | if bool(data): 27 | return { 28 | 'data': data, 29 | }, 200 30 | else: 31 | return { 32 | 'message': f'The ticker {ticker} was not found.' 33 | }, 404 34 | 35 | api.add_resource(Chart, "/chart") 36 | 37 | if __name__ == "__main__": 38 | app.run(debug=True) -------------------------------------------------------------------------------- /src/chart_source.py: -------------------------------------------------------------------------------- 1 | import yfinance as yf 2 | 3 | def get_stock_data(ticker, period, interval): 4 | 5 | ticker += '.NS' 6 | 7 | # Validating the interval value 8 | valid_intervals = ['1m', '2m', '5m', '15m', '30m', '60m', '1h', '1d', '1wk', '1mo', '3mo'] 9 | if interval not in valid_intervals: 10 | raise ValueError("Invalid interval value. Allowed intervals are: " + ", ".join(valid_intervals)) 11 | 12 | # Validating the period value 13 | valid_periods = ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max'] 14 | if period not in valid_periods: 15 | raise ValueError("Invalid period value. Allowed periods are: " + ", ".join(valid_periods)) 16 | 17 | # Interval required 1 minute 18 | data = yf.download(tickers=ticker, period=period, interval=interval) 19 | 20 | # Convert the data to a dictionary 21 | data_dict = data.to_dict(orient='records') 22 | 23 | return data_dict 24 | --------------------------------------------------------------------------------