from flask import Flask, request, jsonify from flask_cors import CORS import requests app = Flask(__name__) CORS(app) # enable CORS for frontend ALPHA_VANTAGE_KEY = 'YOUR_API_KEY' @app.route('/api/stocks') def get_stocks(): symbols = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'TSLA', 'JPM', 'JNJ', 'WMT'] stocks = [] for sym in symbols: url = f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={sym}&apikey={ALPHA_VANTAGE_KEY}' resp = requests.get(url).json() quote = resp.get('Global Quote', {}) if quote: stocks.append({ 'symbol': sym, 'name': sym, 'price': float(quote.get('05. price', 0)), 'volume': round(float(quote.get('06. volume', 0)) / 1e6, 2), 'sector': 'Technology' # placeholder }) # Apply filters min_price = request.args.get('minPrice', type=float) max_price = request.args.get('maxPrice', type=float) min_volume = request.args.get('minVolume', type=float) sector = request.args.get('sector') filtered = [s for s in stocks if (min_price is None or s['price'] >= min_price) and (max_price is None or s['price'] <= max_price) and (min_volume is None or s['volume'] >= min_volume) and (sector is None or s['sector'] == sector)] return jsonify(filtered) if __name__ == '__main__': app.run(debug=True, port=3000)
Share on Google Plus

About ATG NEWS

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment