Exemplo n.º 1
0
def LookbackFixOption(spot,
                      strike,
                      maturity,
                      risk_free,
                      volatility,
                      dividend=0,
                      call=True,
                      s_minmax=None,
                      method='BS',
                      early_exercise=False,
                      method_coef=None,
                      Greeks=False):

    input_kwarg = {
        'spot': spot,
        'strike': strike,
        'maturity': maturity,
        'risk_free': risk_free,
        'volatility': volatility,
        'dividend': dividend,
        'call': call,
        's_minmax': s_minmax
    }
    if method_coef is not None:
        input_kwarg.update(method_coef)
    if method == 'BS':
        anw = BS.LookbackFixOption(
            **input_kwarg).cal_price() if not Greeks else BS.LookbackFixOption(
                **input_kwarg).Greeks()

    elif method == 'MC':
        anw = MC.LookbackFixOption(
            **input_kwarg).cal_price() if not Greeks else MC.LookbackFixOption(
                **input_kwarg).Greeks()
    return anw
Exemplo n.º 2
0
 def SampleStatistics(self, meanlist=[0.,], stdevlist=[0.1,], size=10000, plotvol=False):
     assert size>0, "number of sample should be positive"
     resultDict = {}
     lencol     = len(stdevlist)
     lenrow     = len(meanlist)
     plotcount  = 1
     for mean in meanlist:
         for stdev in stdevlist:
             slist  = numpy.array(self.MapGaussianSample(mean, stdev, size))
             smean  = numpy.mean(slist)
             sstdev = numpy.std(slist)
             klist  = numpy.arange(-8,8)*sstdev/4.+smean
             klist  = self.rf+(klist-self.rf).clip(0.)
             klist  = self.nc-(self.nc-klist).clip(0.)
             plist  = [numpy.mean((slist-k).clip(0.)) for k in klist]
             vlist  = []
             try:
                 vlist  = [brentq(BlackScholes.getNormalVolSolvFuncCall(smean,k,1.0,p), BlackScholes.MINFLOAT, 100.*sstdev) for k,p in zip(klist,plist)]
             except:
                 print "Failed for mean=%8.3e stdev=%8.3e, vol larger than %8.3e" % (mean, stdev, 100.*sstdev)
                 continue
             if plotvol:
                 pyplot.subplot(lenrow, lencol, plotcount)
                 pyplot.plot(klist, vlist, 'ko-')
                 pyplot.title('xm=%8.3e, xs=%8.3e, sm=%8.3e, ss=%8.3e' % (mean, stdev, smean, sstdev))
                 pyplot.plot(numpy.array([smean]*11), numpy.arange(11)/10.*(max(vlist)-min(vlist))+min(vlist), 'r--')
                 plotcount+=1
             resultDict[(mean, stdev)] = (smean, sstdev, klist, plist, vlist)
     if plotvol:
         pyplot.show()
     return resultDict
Exemplo n.º 3
0
 def _op_tree_(self):
     for i in range(0, self.N):
         self.op_tree[:, i] = BS.EuropeanOption(self.sp_tree[:, i],
                                                self.sp_tree[:, i],
                                                (self.t - i * self.dt),
                                                self.r, self.v, self.q,
                                                self.is_call).cal_price()
     self.op_tree[:,-1] = np.maximum(self.sp_tree[:,-1] - self.k,0) if self.is_call \
                         else np.maximum(self.k - self.sp_tree[:,-1],0)
Exemplo n.º 4
0
def bisection(S0, K, T, r, p, a, b, imax):
    for i in range(0, imax):
        mid = (a + b) / 2
        BS = bs.EuroCall(S0, K, T, r, mid)
        if (abs(BS - p) < 0.02):
            return mid
        elif (BS < p):
            a = mid
        elif (BS >= p):
            b = mid
    print("Nonetype error")
Exemplo n.º 5
0
def BarrierOption(spot,
                  strike,
                  maturity,
                  risk_free,
                  volatility,
                  barrier,
                  dividend=0,
                  call=True,
                  knock_out=True,
                  method='BS',
                  early_exercise=False,
                  method_coef=None,
                  Greeks=False):
    input_kwarg = {
        'spot': spot,
        'strike': strike,
        'maturity': maturity,
        'risk_free': risk_free,
        'volatility': volatility,
        'dividend': dividend,
        'call': call,
        'barrier': barrier,
        'knock_out': knock_out
    }
    if method_coef is not None:
        input_kwarg.update(method_coef)

    if method == 'BS':
        anw = BS.BarrierOption(
            **input_kwarg).cal_price() if not Greeks else BS.BarrierOption(
                **input_kwarg).Greeks()

    elif method == 'MC':
        anw = MC.BarrierOption(
            **input_kwarg).cal_price() if not Greeks else MC.BarrierOption(
                **input_kwarg).Greeks()
    return anw
Exemplo n.º 6
0
def ImpliedVolatility(spot,
                      strike,
                      maturity,
                      risk_free,
                      option_price,
                      dividend=0,
                      call=True,
                      est_v=0.5,
                      it=100):
    s, k, t, r, q, P = spot, strike, maturity, risk_free, dividend, option_price
    vega = lambda v: BS.EuropeanOption(s, k, t, r, v, q, call).vega()
    for i in range(it):
        est_v1 = est_v - (VanillaOption(s, k, t, r, est_v, q, call) -
                          P) / vega(est_v)
        print(est_v)
    return est_v
Exemplo n.º 7
0
def VanillaOption(spot,
                  strike,
                  maturity,
                  risk_free,
                  volatility,
                  dividend=0,
                  call=True,
                  method='BS',
                  early_exercise=False,
                  Greeks=False,
                  method_coef=None):

    input_kwarg = {
        'spot': spot,
        'strike': strike,
        'maturity': maturity,
        'risk_free': risk_free,
        'volatility': volatility,
        'dividend': dividend,
        'call': call,
        'early_exercise': early_exercise
    }
    if method_coef is not None:
        input_kwarg.update(method_coef)

    if method == 'BS':
        obj = BS.EuropeanOption(**input_kwarg)

    elif method == 'BT':
        obj = BT.VanillaOption(**input_kwarg)

    elif method == 'FD':
        obj = FD.CrankNicolson(**input_kwarg)

    elif method == 'MC':
        obj = MC.VanillaOption(**input_kwarg)

    return obj.cal_price() if not Greeks else obj.Greeks()
Exemplo n.º 8
0
def valueBlackScholes(S_init, Strike, Exp_Time, Int_Rate, Vol, PTYPE):

    return BlackScholes.valueEuropeanOption(S_init, Strike, Exp_Time, Int_Rate,
                                            Vol, PTYPE)
Exemplo n.º 9
0
def valueBlackScholes(S_init,Strike,Exp_Time,Int_Rate,Vol,PTYPE):
    
    return BlackScholes.valueEuropeanOption(S_init,Strike,Exp_Time,Int_Rate,Vol,PTYPE)