Question: Can someone fix all errors and make this code work in jupyter notebook? api.py : from flask import Flask, jsonify, request from bs import calculate_black_scholes
Can someone fix all errors and make this code work in jupyter notebook?
api.py :

from flask import Flask, jsonify, request from bs import calculate_black_scholes app = Flask(__name__) # Example data for testing purposes option_data = { "AAPL": { "last_price": 150.0, "rate": 0.02, "div": 0.01, "vol": 0.2, "expiry": 1.0, } } @app.route('/hw5/black-scholes/', methods=['GET']) def calculate_option_price(ticker): if ticker not in option_data: return jsonify({"error": "Ticker not found"}), 404 is_call = request.args.get('iscall', type=bool) strike = request.args.get('strike', type=float) rate = request.args.get('rate', type=float) div = request.args.get('div', type=float) vol = request.args.get('vol', type=float) expiry = request.args.get('expiry', type=float) date = request.args.get('date', type=str) if date: # Use the provided date for pricing if available # In a real-world scenario, you would fetch historical data for the given date # For simplicity, we'll use the last known closing price for this example last_price = option_data[ticker]["last_price"] else: last_price = option_data[ticker]["last_price"] try: option_price, greeks = calculate_black_scholes( is_call=is_call, last_price=last_price, strike=strike, rate=rate, div=div, vol=vol, expiry=expiry ) return jsonify({"option_price": option_price, "greeks": greeks}), 200 except ValueError as e: return jsonify({"error": str(e)}), 400 if __name__ == '__main__': app.run(debug=True) bs.py :
import math def calculate_black_scholes(is_call, last_price, strike, rate, div, vol, expiry): # Calculate d1 and d2 d1 = (math.log(last_price / strike) + (rate - div + 0.5 * vol ** 2) * expiry) / (vol * math.sqrt(expiry)) d2 = d1 - vol * math.sqrt(expiry) # Calculate Black-Scholes option price if is_call: option_price = last_price * math.exp(-div * expiry) * norm_cdf(d1) - strike * math.exp(-rate * expiry) * norm_cdf(d2) else: option_price = strike * math.exp(-rate * expiry) * norm_cdf(-d2) - last_price * math.exp(-div * expiry) * norm_cdf(-d1) # Calculate option Greeks delta = math.exp(-div * expiry) * norm_cdf(d1) if is_call else -math.exp(-div * expiry) * norm_cdf(-d1) gamma = math.exp(-div * expiry) * norm_pdf(d1) / (last_price * vol * math.sqrt(expiry)) theta = -(last_price * vol * math.exp(-div * expiry) * norm_pdf(d1)) / (2 * math.sqrt(expiry)) \ - rate * strike * math.exp(-rate * expiry) * norm_cdf(d2) if is_call else \ -(last_price * vol * math.exp(-div * expiry) * norm_pdf(d1)) / (2 * math.sqrt(expiry)) \ + rate * strike * math.exp(-rate * expiry) * norm_cdf(-d2) rho = expiry * strike * math.exp(-rate * expiry) * norm_cdf(d2) if is_call else \ -expiry * strike * math.exp(-rate * expiry) * norm_cdf(-d2) return option_price, {"delta": delta, "gamma": gamma, "theta": theta, "rho": rho} def norm_cdf(x): # Cumulative distribution function for the standard normal distribution return 0.5 * (1 + math.erf(x / math.sqrt(2))) def norm_pdf(x): # Probability density function for the standard normal distribution return (1 / math.sqrt(2 * math.pi)) * math.exp(-0.5 * x ** 2)
Error(s) occured :

If I change the line :
@app.route('/hw5/black-scholes/
to
@app.route('/', methods=['GET'])
it still says no ticker.
Can someone edit this and make it work without the ticker string file
Also you are welcome to make necessaary changes.
fron flask import Flask, jsonify, request from bs inport calculate_black_scholes app=Flask(_name__ ) " Example data for testing purposes option_data ={ "AAPL" : \{ "last_price": 150.0, "rate": 0.02, "div": 0.01 , "vol": 8.2 , "expiry": 1.e, \} \} eapp -route("/hw5/black-scholes/", nethods=['GET "]) def calculate_option_price(ticker): if ticker not in option_data: return jsonify(\{"error": "Ticker not found" }),484 is_ca11 = request.args.get('isca11', type=boo1) strike = request.args.get('strike', type=float) rate = request.args.get('rate', type=float) div = request.args.get ("div', type=float) vol = request.args.get ('vol', type=float) expiry = request.args.get('expiry', type=float) date = request.args.get('date', type=str) if date: A Use the provided date for pricing if available A In a real-world scenario, you would fetch historical data for the given date A For simplicity, we'11 use the last known closing price for this example 1ast_price = option_data[ticker]["last_price"] else: 1ast_price = option_data[ticker]["last_price"] try: option_price, greeks = calculate_black_scholes( is_ca11=is_ca11, 1ast_price=1ast_price, strike=strike, rate-rate, div=div, vol=vol, expiry=expiry ) return jsonify(\{"option_price": option_price, "greeks": greeks\}), 200 except ValueError as e: return jsonify(\{"error": str(e)}),400 if __nane__== '_main_ ": app.run(debug=True) import math def calculate_black_scholes(is_cal1, 1ast_price, strike, rate, div, vol, expiry): N Calculate d1 and d2 d1 = (math. log (1ast_price / strike) + (rate - div +0.5 vol ** 2) * expiry) / (vol * mati d2=d1vol math.sqrt(expiry) A Calculate Black-Scholes option price if is_cal1: option_price = 1ast_price * math.exp (-div * expiry) * norn_cdf(d1) - strike * nath.expi else: option_price = strike * math.exp (-rate * expiry) * norn_cdf(-2) - 1ast_price * nath.e: H Calculate option Greeks delta = math.exp(-div * expiry) * norm_cdf(d1) if is_call else -nath.exp(-div * expiry) * , ganna = math.exp(-div * explry) * norm_pdf(d1) / (1ast_price * vol * math.sqrt(expiry)) theta =( last_price * vol * math.exp(-div * expiry) * norn_pdf(d1)) / (2 * math.sqrt(expi - rate * strike * nath.exp(-rate * expiry) * norm_cdf(d2) if is_call else \ -(1ast_price * vol * math.exp(-div * expiry) * norm_pdf(d1)) / (2 * math.sqrt(expi + rate * strike * nath.exp(-rate * expiry) * norm_cdf(-d2) -expiry * strike * nath.exp(-rate * expiry) * norm_cdf(-d2) return option_price, \{"delta": delta, "ganna": gamma, "theta": theta, "rho": rho\} def norn_cdf(x): N Cunulative distribution function for the standard normal distribution return 0.5(1+ nath.erf (x/ math.sqrt(2)) def norn_pdf(x): I Probability density function for the standard normal distribution return (1/ math.sqrt (2 math.pi )) math.exp (0.5x2) * Serving Flask app '__main__' * Debug mode: on INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server ins tead. * Running on http://127.0.0.1:5000 INFO:werkzeug:Press CTRL+C to quit INFO:werkzeug: * Restarting with watchdog (windowsapi) An exception has occurred, use \%tb to see the full traceback. SystemExit: 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
