aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/analytics/black.py22
-rw-r--r--python/analytics/option.py3
-rw-r--r--python/collateral_calc.py8
3 files changed, 23 insertions, 10 deletions
diff --git a/python/analytics/black.py b/python/analytics/black.py
index ba9d8e96..94f91efb 100644
--- a/python/analytics/black.py
+++ b/python/analytics/black.py
@@ -3,13 +3,16 @@ from numba import jit, float64, boolean
from scipy.stats import norm
import math
+
def d1(F, K, sigma, T):
return (log(F / K) + sigma**2 * T / 2) / (sigma * math.sqrt(T))
+
def d2(F, K, sigma, T):
return d1(F, K, sigma, T) - sigma * math.sqrt(T)
-@jit(cache=True,nopython=True)
+
+@jit(cache=True, nopython=True)
def d12(F, K, sigma, T):
sigmaT = sigma * sqrt(T)
d1 = log(F / K) / sigmaT
@@ -18,27 +21,32 @@ def d12(F, K, sigma, T):
d2 -= 0.5 * sigmaT
return d1, d2
-@jit(float64(float64),cache=True,nopython=True)
+
+@jit(float64(float64), cache=True, nopython=True)
def cnd_erf(d):
+ """ 2 * Phi where Phi is the cdf of a Normal """
RSQRT2 = 0.7071067811865475
return 1 + erf(RSQRT2 * d)
-@jit(float64(float64,float64,float64,float64,boolean),cache=True,nopython=True)
+
+@jit(float64(float64, float64, float64, float64, boolean), cache=True, nopython=True)
def black(F, K, T, sigma, payer=True):
d1, d2 = d12(F, K, sigma, T)
if payer:
return 0.5 * (F * cnd_erf(d1) - K * cnd_erf(d2))
else:
- return 0.5 * (K * cnd_erf(-d2) - F * cnd_erf(-d1))
+ return 0.5 * (K * cnd_erf(-d2) - F * cnd_erf(-d1))
+
-@jit(float64(float64,float64,float64,float64),cache=True,nopython=True)
+@jit(float64(float64, float64, float64, float64), cache=True, nopython=True)
def Nx(F, K, sigma, T):
- return cnd_erf((log(F/K) - sigma**2 * T /2) / (sigma * sqrt(T))) /2
+ return cnd_erf((log(F/K) - sigma**2 * T / 2) / (sigma * sqrt(T))) / 2
+
def bachelier(F, K, T, sigma):
""" Bachelier formula for normal dynamics
need to multiply by discount factor
"""
- d1 = (F - K) / ( sigma * sqrt(T))
+ d1 = (F - K) / (sigma * sqrt(T))
return (0.5 * (F - K) * cnd_erf(d1) + sigma * sqrt(T) * norm.pdf(d1))
diff --git a/python/analytics/option.py b/python/analytics/option.py
index fbb84caf..5924fc56 100644
--- a/python/analytics/option.py
+++ b/python/analytics/option.py
@@ -86,7 +86,8 @@ class BlackSwaption(ForwardIndex):
if rec is None:
return ValueError("trade_id doesn't exist")
if index is None:
- index = CreditIndex(redcode=rec.security_id, maturity=rec.maturity, value_date=rec.trade_date)
+ index = CreditIndex(redcode=rec.security_id, maturity=rec.maturity,
+ value_date=rec.trade_date)
index.ref = rec.index_ref
instance = cls(index, rec.expiration_date, rec.strike, rec.option_type.lower(),
direction="Long" if rec.buysell else "Short")
diff --git a/python/collateral_calc.py b/python/collateral_calc.py
index af2e49c3..9bed262d 100644
--- a/python/collateral_calc.py
+++ b/python/collateral_calc.py
@@ -96,6 +96,7 @@ def download_ms_emails(count=20):
with open(DATA_DIR / fname, "wb") as fh:
fh.write(attach.content)
+
def download_gs_emails(count=20):
emails = get_msgs(path=["NYops", "Margin calls"],
subject_filter="Regulatory VM Margin",
@@ -180,15 +181,17 @@ def ms_collateral(d):
r.append(["M_CSH_CASH", -collat - acc, "USD"])
return pd.DataFrame.from_records(r, columns=['Strategy', 'Amount', 'Currency'])
+
def load_gs_file(d, pattern):
try:
- fname = next( (DAILY_DIR / "GS_reports").
- glob(f"{pattern}*{d.strftime('%d_%b_%Y')}*"))
+ fname = next((DAILY_DIR / "GS_reports").
+ glob(f"{pattern}*{d.strftime('%d_%b_%Y')}*"))
except StopIteration:
raise FileNotFoundError(f"GS {pattern} file not found for date {d}")
df = pd.read_excel(fname, skiprows=9, skipfooter=77)
return df
+
def gs_collateral(d):
df = load_gs_file(d, "Collateral_Detail")
collateral = float(df.Quantity)
@@ -207,6 +210,7 @@ def gs_collateral(d):
'Currency': "USD"}, ignore_index=True)
return df
+
def send_email(account, df_ms, df_sg, df_gs):
pd.set_option('display.float_format', '{:.2f}'.format)
content = HTMLBody('<html><body>'