Skip to content

Commit

Permalink
FinOptionTypes to OptionTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Jul 27, 2021
1 parent ba965d8 commit 287ce1e
Show file tree
Hide file tree
Showing 113 changed files with 2,509 additions and 2,209 deletions.
306 changes: 306 additions & 0 deletions docs/FinancePyManual.fls

Large diffs are not rendered by default.

Binary file modified docs/FinancePyManual.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/head.tex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
commentstyle=\color{blue},
frame=single,
otherkeywords = {Date, CalendarTypes, BusDayAdjustTypes, DateGenRuleTypes, DayCountTypes, FrequencyTypes,
FinLiborSwaptionTypes, FinOptionExerciseTypes, FinOptionTypes, EquityBarrierTypes, EquityRainbowOptionTypes, List}
FinLiborSwaptionTypes, FinOptionExerciseTypes, OptionTypes, EquityBarrierTypes, EquityRainbowOptionTypes, List}
}

%\makeindex
Expand Down
20 changes: 10 additions & 10 deletions docs/makeUserGuide.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def buildHead():
lines = f.readlines()
f.close()

f = open(userGuideFileName, 'w')
f = open(userGuideFileName, 'w', encoding="utf-8")
f.writelines(lines)
f.close()

Expand All @@ -177,7 +177,7 @@ def buildTail():
lines = f.readlines()
f.close()

f = open(userGuideFileName, 'a')
f = open(userGuideFileName, 'a', encoding="utf-8")
f.writelines(lines)
f.close()

Expand All @@ -194,7 +194,7 @@ def buildIntro(introfile):

parsedLines = parseMarkdown(lines)

f = open(userGuideFileName, 'a')
f = open(userGuideFileName, 'a', encoding="utf-8")

f.write("\\chapter{Introduction to FinancePy}")
f.writelines(parsedLines)
Expand All @@ -210,7 +210,7 @@ def buildChapter(folderName):
print("Building chapter in folder:", folderName)

readMeFile = folderName + "//" + "README.md"
f = open(readMeFile, 'r', encoding="utf8")
f = open(readMeFile, 'r', encoding="utf-8")
readMeLines = f.readlines()
f.close()

Expand All @@ -224,7 +224,7 @@ def buildChapter(folderName):
# newLines.append("\\section{Introduction}")
# newLines.append("\n")

f = open(userGuideFileName, 'a')
f = open(userGuideFileName, 'a', encoding="utf-8")
f.writelines(newLines)
f.close()

Expand Down Expand Up @@ -254,7 +254,7 @@ def buildChapter(folderName):

readMeLines = parseMarkdown(readMeLines)

f = open(userGuideFileName, 'a')
f = open(userGuideFileName, 'a', encoding="utf-8")
f.writelines(readMeLines)
f.close()

Expand All @@ -263,7 +263,7 @@ def buildChapter(folderName):
for module in modules:
moduleName = module.split("\\")[-1]
escapedModuleName = sub("_", "\\_", moduleName[0:-3])
f = open(userGuideFileName, 'a')
f = open(userGuideFileName, 'a', encoding="utf-8")
f.write("\\newpage\n")
f.write("\\section{" + escapedModuleName + "}\n")
f.write("\n")
Expand All @@ -275,7 +275,7 @@ def buildChapter(folderName):
for module in modules:
moduleName = module.split("\\")[-1]
escapedModuleName = sub("_", "\\_", moduleName[0:-3])
f = open(userGuideFileName, 'a')
f = open(userGuideFileName, 'a', encoding="utf-8")
f.write("\n")
f.write("\\newpage\n")
f.write("\\section{" + escapedModuleName + "}\n")
Expand All @@ -290,7 +290,7 @@ def parseModule(moduleName):
""" Parse a module looking for classes, functions and classes for
enumerated types. Functions inside classes are parsed inside the class. """
print(moduleName)
f = open(moduleName, 'r', encoding="utf8")
f = open(moduleName, 'r', encoding="utf-8")
lines = f.readlines()
f.close()

Expand Down Expand Up @@ -330,7 +330,7 @@ def parseModule(moduleName):

# print("startClassLines", startClassLines)

f = open(userGuideFileName, 'a')
f = open(userGuideFileName, 'a', encoding="utf-8")

for c in range(0, numEnums):
newLines = parseEnum(lines, startEnumLines[c], startEnumLines[c + 1])
Expand Down
2 changes: 1 addition & 1 deletion financepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cr = "\n"

s = "####################################################################" + cr
s += "# FINANCEPY BETA Version " + str('0.202') + " - This build: 17 Jul 2021 at 13:32 #" + cr
s += "# FINANCEPY BETA Version " + str('0.202') + " - This build: 27 Jul 2021 at 00:28 #" + cr
s += "# **** NEW PEP8 COMPLIANT VERSION -- PLEASE UPDATE YOUR CODE **** #" + cr
s += "# This software is distributed FREE & WITHOUT ANY WARRANTY #" + cr
s += "# Report bugs as issues at https://github.com/domokane/FinancePy #" + cr
Expand Down
19 changes: 8 additions & 11 deletions financepy/market/curves/README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
# Discount
These modules create a family of discount curve types related to the term structures of interest rates. Discount curves that can be used to present value a future cash flow. These differ from best fits curves in that they exactly refit the prices of bonds or CDS. The different discount curves are created by calibrating to different instruments. They also differ in terms of the term structure shapes they can have. Different shapes have different impacts in terms of locality on risk management performed using these different curves. There is often a trade-off between smoothness and locality.
# Discount Curves
These modules create a family of discount curve types related to the term structures of interest rates. Discount curves that can be used to present value a future cash flow. These differ from best fits curves in that they exactly refit the prices of bonds or CDS. The different discount curves are created by calibrating to different instruments. They also differ in terms of the term structure shapes they can have. Different shapes have different impacts in terms of locality on risk management performed using these different curves. There is often a trade-off between smoothness and locality. These are curves which supply a discount factor that can be used to present-value future payments.

## Discount Curves
These are curves which supply a discount factor that can be used to present-value future payments.

### FinDiscountCurve
### DiscountCurve
This is a curve made from a Numpy array of times and discount factor values that represents a discount curve. It also requires a specific interpolation scheme. A function is also provided to return a survival probability so that this class can also be used to handle term structures of survival probabilities. Other curves inherit from this in order to share common functionality.

### FinDiscountCurveFlat
### DiscountCurveFlat
This is a class that takes in a single flat rate.

### FinDiscountCurveNS
### DiscountCurveNS
Implementation of the Nelson-Siegel curve parametrisation.

### FinDiscountCurveNSS
### DiscountCurveNSS
Implementation of the Nelson-Siegel-Svensson curve parametrisation.

### FinDiscountCurveZeros
### DiscountCurveZeros
This is a discount curve that is made from a vector of times and zero rates.


### FinInterpolate
### Interpolate
This module contains the interpolation function used throughout the discount curves when a discount factor needs to be interpolated. There are three interpolation methods:

1. PIECEWISE LINEAR - This assumes that a discount factor at a time between two other known discount factors is obtained by linear interpolation. This approach does not guarantee any smoothness but is local. It does not guarantee positive forwards (assuming positive zero rates).
Expand Down
4 changes: 2 additions & 2 deletions financepy/market/curves/discount_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np

from .interpolator import FinInterpolator, InterpTypes, interpolate
from .interpolator import Interpolator, InterpTypes, interpolate

from ...utils.date import Date
from ...utils.error import FinError
Expand Down Expand Up @@ -78,7 +78,7 @@ def __init__(self,
self._interp_type = interp_type
self._freq_type = FrequencyTypes.CONTINUOUS
self._day_count_type = None # Not needed for this curve
self._interpolator = FinInterpolator(self._interp_type)
self._interpolator = Interpolator(self._interp_type)
self._interpolator.fit(self._times, self._dfs)

###############################################################################
Expand Down
4 changes: 2 additions & 2 deletions financepy/market/curves/discount_curve_zeros.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ...utils.helpers import times_from_dates
from ...market.curves.discount_curve import DiscountCurve
from ...utils.helpers import check_argument_types
from .interpolator import InterpTypes, FinInterpolator
from .interpolator import InterpTypes, Interpolator


###############################################################################
Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self,
self._day_count_type)

self._dfs = np.array(dfs)
self._interpolator = FinInterpolator(self._interp_type)
self._interpolator = Interpolator(self._interp_type)
self._interpolator.fit(self._times, self._dfs)

# ###############################################################################
Expand Down
2 changes: 1 addition & 1 deletion financepy/market/curves/interpolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _vinterpolate(xValues,
###############################################################################


class FinInterpolator():
class Interpolator():

def __init__(self,
interpolatorType: InterpTypes):
Expand Down
10 changes: 5 additions & 5 deletions financepy/market/volatility/equity_vol_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ...utils.error import FinError
from ...utils.date import Date
from ...utils.global_vars import gDaysInYear
from ...utils.global_types import FinOptionTypes
from ...utils.global_types import OptionTypes
from ...models.option_implied_dbn import option_implied_dbn
from ...utils.helpers import check_argument_types, label_to_string
from ...market.curves.discount_curve import DiscountCurve
Expand Down Expand Up @@ -413,7 +413,7 @@ def volatility_from_strike_date(self, K, expiry_date):
# initialGuess = self._K_ATM[index0]

# K0 = _solver_for_smile_strike(s, texp, self._rd[index0], self._rf[index0],
# FinOptionTypes.EUROPEAN_CALL.value,
# OptionTypes.EUROPEAN_CALL.value,
# vol_type_value, callDelta,
# delta_method_value,
# initialGuess,
Expand All @@ -426,7 +426,7 @@ def volatility_from_strike_date(self, K, expiry_date):
# K1 = _solver_for_smile_strike(s, texp,
# self._rd[index1],
# self._rf[index1],
# FinOptionTypes.EUROPEAN_CALL.value,
# OptionTypes.EUROPEAN_CALL.value,
# vol_type_value, callDelta,
# delta_method_value,
# initialGuess,
Expand Down Expand Up @@ -514,7 +514,7 @@ def volatility_from_delta_date(self, callDelta, expiry_date,
texp,
self._r[index0],
self._q[index0],
FinOptionTypes.EUROPEAN_CALL.value,
OptionTypes.EUROPEAN_CALL.value,
vol_type_value, callDelta,
initialGuess,
self._parameters[index0])
Expand All @@ -527,7 +527,7 @@ def volatility_from_delta_date(self, callDelta, expiry_date,
K1 = _solver_for_smile_strike(s, texp,
self._r[index1],
self._q[index1],
FinOptionTypes.EUROPEAN_CALL.value,
OptionTypes.EUROPEAN_CALL.value,
vol_type_value, callDelta,
initialGuess,
self._parameters[index1])
Expand Down
30 changes: 15 additions & 15 deletions financepy/market/volatility/fx_vol_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ...utils.error import FinError
from ...utils.date import Date
from ...utils.global_vars import gDaysInYear
from ...utils.global_types import FinOptionTypes
from ...utils.global_types import OptionTypes
from ...products.fx.fx_vanilla_option import FXVanillaOption
from ...models.option_implied_dbn import option_implied_dbn
from ...products.fx.fx_mkt_conventions import FinFXATMMethod
Expand Down Expand Up @@ -99,12 +99,12 @@ def obj_fast(params, *args):
sigma_K_25D_C_MS = vol_function(vol_type_value, params, f, K_25D_C_MS, t)

V_25D_C_MS = bs_value(s, t, K_25D_C_MS, rd, rf, sigma_K_25D_C_MS,
FinOptionTypes.EUROPEAN_CALL.value)
OptionTypes.EUROPEAN_CALL.value)

sigma_K_25D_P_MS = vol_function(vol_type_value, params, f, K_25D_P_MS, t)

V_25D_P_MS = bs_value(s, t, K_25D_P_MS, rd, rf, sigma_K_25D_P_MS,
FinOptionTypes.EUROPEAN_PUT.value)
OptionTypes.EUROPEAN_PUT.value)

V_25D_MS = V_25D_C_MS + V_25D_P_MS
term2 = (V_25D_MS - V_25D_MS_target)**2
Expand All @@ -114,15 +114,15 @@ def obj_fast(params, *args):
###########################################################################

K_25D_C = solver_for_smile_strike_fast(s, t, rd, rf,
FinOptionTypes.EUROPEAN_CALL.value,
OptionTypes.EUROPEAN_CALL.value,
vol_type_value, +0.2500,
delta_method_value, K_25D_C_MS,
params)

sigma_K_25D_C = vol_function(vol_type_value, params, f, K_25D_C, t)

K_25D_P = solver_for_smile_strike_fast(s, t, rd, rf,
FinOptionTypes.EUROPEAN_PUT.value,
OptionTypes.EUROPEAN_PUT.value,
vol_type_value, -0.2500,
delta_method_value, K_25D_P_MS,
params)
Expand Down Expand Up @@ -156,23 +156,23 @@ def solve_to_horizon_fast(s, t,
vol_25D_MS = atm_vol + ms25DVol

K_25D_C_MS = solve_for_strike(s, t, rd, rf,
FinOptionTypes.EUROPEAN_CALL.value,
OptionTypes.EUROPEAN_CALL.value,
+0.2500,
delta_method_value,
vol_25D_MS)

K_25D_P_MS = solve_for_strike(s, t, rd, rf,
FinOptionTypes.EUROPEAN_PUT.value,
OptionTypes.EUROPEAN_PUT.value,
-0.2500,
delta_method_value,
vol_25D_MS)

# USE MARKET STRANGLE VOL TO DETERMINE PRICE OF A MARKET STRANGLE
V_25D_C_MS = bs_value(s, t, K_25D_C_MS, rd, rf, vol_25D_MS,
FinOptionTypes.EUROPEAN_CALL.value)
OptionTypes.EUROPEAN_CALL.value)

V_25D_P_MS = bs_value(s, t, K_25D_P_MS, rd, rf, vol_25D_MS,
FinOptionTypes.EUROPEAN_PUT.value)
OptionTypes.EUROPEAN_PUT.value)

# Market price of strangle in the domestic currency
V_25D_MS = V_25D_C_MS + V_25D_P_MS
Expand All @@ -192,13 +192,13 @@ def solve_to_horizon_fast(s, t,
params = np.array(xopt)

K_25D_C = solver_for_smile_strike_fast(s, t, rd, rf,
FinOptionTypes.EUROPEAN_CALL.value,
OptionTypes.EUROPEAN_CALL.value,
vol_type_value, +0.2500,
delta_method_value, K_25D_C_MS,
params)

K_25D_P = solver_for_smile_strike_fast(s, t, rd, rf,
FinOptionTypes.EUROPEAN_PUT.value,
OptionTypes.EUROPEAN_PUT.value,
vol_type_value, -0.2500,
delta_method_value, K_25D_P_MS,
params)
Expand Down Expand Up @@ -318,7 +318,7 @@ def solve_for_strike(spot_fx_rate,
domDF = np.exp(-rd*tdel)
forDF = np.exp(-rf*tdel)

if option_type_value == FinOptionTypes.EUROPEAN_CALL.value:
if option_type_value == OptionTypes.EUROPEAN_CALL.value:
phi = +1.0
else:
phi = -1.0
Expand All @@ -335,7 +335,7 @@ def solve_for_strike(spot_fx_rate,
domDF = np.exp(-rd*tdel)
forDF = np.exp(-rf*tdel)

if option_type_value == FinOptionTypes.EUROPEAN_CALL.value:
if option_type_value == OptionTypes.EUROPEAN_CALL.value:
phi = +1.0
else:
phi = -1.0
Expand Down Expand Up @@ -750,14 +750,14 @@ def check_calibration(self, verbose: bool, tol: float = 1e-6):
call = FXVanillaOption(expiry_date,
K_dummy,
self._currency_pair,
FinOptionTypes.EUROPEAN_CALL,
OptionTypes.EUROPEAN_CALL,
1.0,
self._notional_currency, )

put = FXVanillaOption(expiry_date,
K_dummy,
self._currency_pair,
FinOptionTypes.EUROPEAN_PUT,
OptionTypes.EUROPEAN_PUT,
1.0,
self._notional_currency)

Expand Down
Loading

0 comments on commit 287ce1e

Please sign in to comment.