Spaces:
Build error
Build error
File size: 5,787 Bytes
12f4bef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# Standart python imports
from enum import Enum
from datetime import datetime, timedelta
# Third party imports
import streamlit as st
# Local package imports
from option_Pricing import BlackScholesModel, MonteCarloPricing, BinomialTreeModel, Ticker
class OPTION_PRICING_MODEL(Enum):
BLACK_SCHOLES = 'Black Scholes Model'
MONTE_CARLO = 'Monte Carlo Simulation'
BINOMIAL = 'Binomial Model'
@st.cache_data
def get_historical_data(ticker):
"""Getting historical data for speified ticker and caching it with streamlit app."""
return Ticker.get_historical_data(ticker)
# Ignore the Streamlit warning for using st.pyplot()
st.set_option('deprecation.showPyplotGlobalUse', False)
# Main title
st.title('Option pricing')
# User selected model from sidebar
pricing_method = st.sidebar.radio('Please select option pricing method', options = [model.value for model in OPTION_PRICING_MODEL])
# Displaying specified model
st.subheader(f'Pricing method: {pricing_method}')
if pricing_method == OPTION_PRICING_MODEL.BLACK_SCHOLES.value:
# Parameters for Black-Scholes model
ticker = st.text_input('Ticker symbol', 'AAPL')
strike_price = st.number_input('Strike price', 0)
risk_free_rate = st.slider('Risk-free rate (%)', 0, 100, 10)
sigma = st.slider('Sigma (%)', 0, 100, 20)
exercise_date = st.date_input('Exercise date', min_value = datetime.today() + timedelta(days = 1), value = datetime.today() + timedelta(days = 365))
if st.button(f'Calculate option price for {ticker}'):
# Getting data for selected ticker
data = get_historical_data(ticker)
st.write(data.tail())
Ticker.plot_data(data, ticker, 'Adj Close')
st.pyplot()
# Formating selected model parameters
spot_price = Ticker.get_last_price(data, 'Adj Close')
risk_free_rate = risk_free_rate / 100
sigma = sigma / 100
days_to_maturity = (exercise_date - datetime.now().date()).days
# Calculating option price
BSM = BlackScholesModel(spot_price, strike_price, days_to_maturity, risk_free_rate, sigma)
call_option_price = BSM.calculate_option_price('Call Option')
put_option_price = BSM.calculate_option_price('Put Option')
# Displaying call/put option price
st.subheader(f'Call option price: {call_option_price}')
st.subheader(f'Put option price: {put_option_price}')
elif pricing_method == OPTION_PRICING_MODEL.MONTE_CARLO.value:
# Parameters for Monte Carlo simulation
ticker = st.text_input('Ticker symbol', 'AAPL')
strike_price = st.number_input('Strike price', 0)
risk_free_rate = st.slider('Risk-free rate (%)', 0, 100, 10)
sigma = st.slider('Sigma (%)', 0, 100, 20)
exercise_date = st.date_input('Exercise date', min_value = datetime.today() + timedelta(days = 1), value = datetime.today() + timedelta(days = 365))
number_of_simulations = st.slider('Number of simulations', 100, 100000, 10000)
num_of_movements = st.slider('Number of price movement simulations to be visualized ', 0, int(number_of_simulations / 10), 100)
if st.button(f'Calculate option price for {ticker}'):
# Getting data for selected ticker
data = get_historical_data(ticker)
st.write(data.tail())
Ticker.plot_data(data, ticker, 'Adj Close')
st.pyplot()
# Formating simulation parameters
spot_price = Ticker.get_last_price(data, 'Adj Close')
risk_free_rate = risk_free_rate / 100
sigma = sigma / 100
days_to_maturity = (exercise_date - datetime.now().date()).days
# ESimulating stock movements
MC = MonteCarloPricing(spot_price, strike_price, days_to_maturity, risk_free_rate, sigma, number_of_simulations)
MC.simulate_prices()
# Visualizing Monte Carlo Simulation
MC.plot_simulation_results(num_of_movements)
st.pyplot()
# Calculating call/put option price
call_option_price = MC.calculate_option_price('Call Option')
put_option_price = MC.calculate_option_price('Put Option')
# Displaying call/put option price
st.subheader(f'Call option price: {call_option_price}')
st.subheader(f'Put option price: {put_option_price}')
elif pricing_method == OPTION_PRICING_MODEL.BINOMIAL.value:
# Parameters for Binomial-Tree model
ticker = st.text_input('Ticker symbol', 'AAPL')
strike_price = st.number_input('Strike price', 0)
risk_free_rate = st.slider('Risk-free rate (%)', 0, 100, 10)
sigma = st.slider('Sigma (%)', 0, 100, 20)
exercise_date = st.date_input('Exercise date', min_value = datetime.today() + timedelta(days = 1), value = datetime.today() + timedelta(days = 365))
number_of_time_steps = st.slider('Number of time steps', 5000, 100000, 15000)
if st.button(f'Calculate option price for {ticker}'):
# Getting data for selected ticker
data = get_historical_data(ticker)
st.write(data.tail())
Ticker.plot_data(data, ticker, 'Adj Close')
st.pyplot()
# Formating simulation parameters
spot_price = Ticker.get_last_price(data, 'Adj Close')
risk_free_rate = risk_free_rate / 100
sigma = sigma / 100
days_to_maturity = (exercise_date - datetime.now().date()).days
# Calculating option price
BOPM = BinomialTreeModel(spot_price, strike_price, days_to_maturity, risk_free_rate, sigma, number_of_time_steps)
call_option_price = BOPM.calculate_option_price('Call Option')
put_option_price = BOPM.calculate_option_price('Put Option')
# Displaying call/put option price
st.subheader(f'Call option price: {call_option_price}')
st.subheader(f'Put option price: {put_option_price}') |