Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Monte Carlo simulation in option pricing models #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion option_pricing/MonteCarloSimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,28 @@ def plot_simulation_results(self, num_of_movements):
plt.xlabel('Days in future')
plt.title(f'First {num_of_movements}/{self.N} Random Price Movements')
plt.legend(loc='best')
plt.show()
plt.show()

def get_calculation_steps(self):
"""
Returns a dictionary containing input parameters, intermediate calculations, and final calculations.
"""
steps = {
"Input Parameters": {
"Spot Price": self.S_0,
"Strike Price": self.K,
"Time to Maturity (days)": self.T * 365,
"Risk-free Rate": self.r,
"Volatility": self.sigma,
"Number of Simulations": self.N
},
"Intermediate Calculations": {
"Time Step (dt)": self.dt,
"Simulated Prices": self.simulation_results_S
},
"Final Calculations": {
"Call Option Price": self._calculate_call_option_price(),
"Put Option Price": self._calculate_put_option_price()
}
}
return steps
19 changes: 19 additions & 0 deletions option_pricing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,23 @@
print(MC.calculate_option_price('Put Option'))
MC.plot_simulation_results(20)

# Test cases for get_calculation_steps method in MonteCarloPricing class
calculation_steps = MC.get_calculation_steps()
print("Calculation Steps:")
print(calculation_steps)

# Verify input parameters
assert calculation_steps["Input Parameters"]["Spot Price"] == 100
assert calculation_steps["Input Parameters"]["Strike Price"] == 100
assert calculation_steps["Input Parameters"]["Time to Maturity (days)"] == 365
assert calculation_steps["Input Parameters"]["Risk-free Rate"] == 0.1
assert calculation_steps["Input Parameters"]["Volatility"] == 0.2
assert calculation_steps["Input Parameters"]["Number of Simulations"] == 10000

# Verify intermediate calculations
assert calculation_steps["Intermediate Calculations"]["Time Step (dt)"] == MC.dt
assert calculation_steps["Intermediate Calculations"]["Simulated Prices"] is not None

# Verify final calculations
assert calculation_steps["Final Calculations"]["Call Option Price"] == MC.calculate_option_price('Call Option')
assert calculation_steps["Final Calculations"]["Put Option Price"] == MC.calculate_option_price('Put Option')
8 changes: 7 additions & 1 deletion streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ def get_current_price(ticker):

st.subheader(f'Call option price: {call_option_price:.2f}')
st.subheader(f'Put option price: {put_option_price:.2f}')

# Display calculation steps
calculation_steps = MC.get_calculation_steps()
st.subheader("Calculation Steps")
st.json(calculation_steps)

else:
st.error("Unable to proceed with calculations due to data fetching error.")
except Exception as e:
Expand Down Expand Up @@ -247,4 +253,4 @@ def get_current_price(ticker):
except Exception as e:
st.error(f"Error during calculation: {str(e)}")
else:
st.info("Click 'Calculate option price' to see results.")
st.info("Click 'Calculate option price' to see results.")