예제 #1
0
 def test_analytical_gamma(self):
     while self.tdi.has_next():
         row = self.tdi.next_row()
         S, K, t, r, sigma = row['S'], row['K'], row['t'], row['R'], row[
             'v']
         self.assertAlmostEqual(analytical.gamma('c', S, K, t, r, sigma),
                                row['CG'],
                                delta=0.000001)
         self.assertAlmostEqual(analytical.gamma('p', S, K, t, r, sigma),
                                row['PG'],
                                delta=0.000001)
예제 #2
0
    def test_analytical_gamma(self):
        S = 49
        K = 50
        sigma = .2
        r = .05
        t = 0.3846

        flag = 'p'
        c_put = c_analytical.gamma(flag, S, K, t, r, sigma)
        py_put = py_analytical.gamma(flag, S, K, t, r, sigma)
        self.assertTrue(almost_equal(c_put, py_put))

        flag = 'c'
        c_call = c_analytical.gamma(flag, S, K, t, r, sigma)
        py_call = py_analytical.gamma(flag, S, K, t, r, sigma)
        self.assertTrue(almost_equal(c_call, py_call))
예제 #3
0
 def test_gamma(self):
     gammas = [
         gamma(flag, S, K, t, r, sigma)
         for flag, S, K, t, r, sigma in self.arg_combinations
     ]
     ngammas = [
         ngamma(flag, S, K, t, r, sigma)
         for flag, S, K, t, r, sigma in self.arg_combinations
     ]
     self.assertTrue(self.diff_mean(gammas, ngammas) < self.epsilon)
예제 #4
0
def Calculate_gamma(each_row):
    if each_row['Implied_Volatility'] == 0:
        measure= 0
    else:
        try:
            measure = bs_a.gamma(flag=each_row['CallOrPut'], S=each_row['Spot'], K=each_row['Strike_Price'],
                                 t=each_row['Days_to_Expiry']/365, r=.1,sigma=each_row['Implied_Volatility'])
        except:
            measure = 0

    print("Gamma", end=" ")
    return measure
예제 #5
0
 def calc_all(self, b):
     h = self.all_set(b)
     if not h: return False
     if not h.get("sigma"): self.civ(h)
     if not h.get("sigma"): return False
     a = (h["flag"], h["F"], h["K"], h["t"], h["r"], h["sigma"])
     r = self.rounding
     h["price"] = round(oprice(*a), r)
     h["delta"] = round(delta(*a), r)
     h["gamma"] = round(gamma(*a) * 100, r)
     h["vega"] = round(vega(*a), r)
     return h
예제 #6
0
    def gamma(self, option_obj, asset_price, timestamp, expiration,
              call_put_flag):
        flag = call_put_flag
        S = asset_price
        K = option_obj.strike
        t = self.time_2_expiration(timestamp, expiration)
        r = self.risk_free_rate
        sigma = option_obj.sigma

        gamma = vollib_greeks.gamma(flag, S, K, t, r, sigma)

        return gamma
예제 #7
0
def calculateAllGreeks(ticker, date, type, strike):
        S = si.get_live_price(ticker)
        K = strike
        sigma = calculateAnnualizedVolatility(ticker)
        t = optionExpiration(date)
        r = calculateTreasuryYield(date)
        if type == 'Call' or type == "call" or type == "C" or type == 'c':
            flag = 'c'
        else:
            flag = 'p'
        dlta = delta(flag, S, K, t, r, sigma)
        gam = gamma(flag, S, K, t, r, sigma)
        thta = theta(flag, S, K, t, r, sigma)
        vga = vega(flag, S, K, t, r, sigma)
        rh = rho(flag, S, K, t, r, sigma)
        return ({"delta": dlta, "gamma": gam, "theta": thta, "vega": vga, "rho": rh})
예제 #8
0
def calculateGreek(ticker, date, type, strike, greek):
        grk = 0
        S = si.get_live_price(ticker)
        K = strike
        sigma = calculateAnnualizedVolatility(ticker)
        t = optionExpiration(date)
        r = calculateTreasuryYield(date)
        flag = ''
        if type == 'Call' or type == "call" or type == "C" or type == 'c':
            flag = 'c'
        flag = 'p'
        if greek == "Delta" or greek == "delta":
            grk = delta(flag, S, K, t, r, sigma)
        elif greek == "Gamma" or greek == "gamma":
            grk = gamma(flag, S, K, t, r, sigma)
        elif greek == "Theta" or greek == "theta":
            grk = theta(flag, S, K, t, r, sigma)
        elif greek == "Vega" or greek == "vega":
            grk = vega(flag, S, K, t, r, sigma)
        else:
            grk = rho(flag, S, K, t, r, sigma)
        return grk
예제 #9
0
def on_update(msg, order):
    now = time.time()
    global tick
    ms = msg['market_state']
    tck = ms['ticker']
    last_price = msg['market_state']['last_price']
    tickers[tck]['price'].append(last_price)

    tick += 1

    if tck == fut:
        return

    S = tickers[fut]['price'][-1] if len(tickers[fut]['price']) > 0 else None
    K = int(tck[1:4]) if len(tck) == 5 else int(tck[1:3])
    t = (450 - tick) / 450 * 1 / 12
    flag = 'c' if tck[-1] == 'C' else 'p'

    if S is not None:
        try:
            iv = implied_volatility.implied_volatility(last_price, S, K, t, r,
                                                       flag)
            theo_p = black_scholes.black_scholes(flag, S, K, t, r, iv)
            delta = analytical.delta(flag, S, K, t, r, iv)
            gamma = analytical.gamma(flag, S, K, t, r, iv)
            # vega = analytical.vega(flag,S,K,t,r,iv)
            tickers[tck]['vol'].append(vol)
            tickers[tck]['delta'].append(delta)
            tickers[tck]['gamma'].append(gamma)
            # tickers[tck]['vega'].append(vega)

            if len(tickers[tck]['vol']) > 5:
                vol_sma = np.mean(np.array(ticker[tck]['vol'][-6:-1]))
                if abs(iv - vol_sma) / vol_sma > .015:
                    if iv > vol_sma: order.addSell(tck, 10, last_price + dx)
                    else: order.addBuy(tck, 10, last_price - dx)

        except:
            print(S, K, last_price, flag)