Esempio n. 1
0
    def estimate_last(self, args):
        # initialize
        N = len(self.z)  # use close market's value
        t_init = 0
        y_init = self.z[t_init]

        # BSM model
        model = bsm.BSM(N, args.mu, args.sigma, t_init, y_init, self.seed)
        y = model.predict()
        #print(float(y[-1]),float(self.z[-1]),(float(y[-1])-float(self.z[-1])))
        error = (float(y[-1]) - float(self.z[-1]))**2
        return error
Esempio n. 2
0
    def estimate(self):
        # initialize
        N = len(self.z)  # use close market's value
        t_init = 0
        y_init = self.z[t_init]

        # BSM model
        model = bsm.BSM(N, self.params["mu"], self.params["sgm"], t_init,
                        y_init, self.seed)
        y = model.predict()
        loss = model.calc_loss(np.array(self.z, dtype=float))
        return loss, y
Esempio n. 3
0
def import_iv():
    # set option param
    spot = 2.483
    today = dt.date(2015, 1, 27)
    risk_free = 0.05
    qList = []

    # 读取行情

    with open('option_20150127.csv') as f:
        f_csv = csv.reader(f)
        headers = next(f_csv)
        for row in f_csv:
            price = float(row[5])
            if (row[10] == "PO"):
                mytype = -1
            elif (row[10] == "CO"):
                mytype = 1
            else:
                print "error"
            strike = float(row[11])
            T = (dt.datetime.strptime(row[9], "%Y/%m/%d").date() -
                 today).days / 365.0

            qList.append(Quote(price, strike, T, mytype))

    ivList = []
    for i in qList:
        # print (i.optionType, i.strike, riskFree, i.T, spot, i.optionPrice)
        bsm = BSM.BSM(i.optionType, i.strike, risk_free, i.T, spot)
        ivList.append(bsm.get_iv(i.optionType, i.optionPrice))

    svi_sample_list = []
    for i in range(0, len(ivList)):
        svi_sample_list.append(
            Svi_sample(ivList[i], (math.log(qList[i].strike / spot))))
    #svi_sample_list_sub = svi_sample_list[1:6]+svi_sample_list[28:34]
    svi_sample_list_sub = svi_sample_list[0:7] + svi_sample_list[28:35]
    for i6 in svi_sample_list_sub:
        print i6.iv, ",", i6.k
    return svi_sample_list_sub
Esempio n. 4
0
# read csv file
with open(args.csvfile, "r") as file:
    reader = csv.reader(file)
    header = next(reader)
    for row in reader:
        z.append(float(row[4]))

Nt = len(z)
init = z[0]

# calc BSM model
fig = plt.figure(figsize=(16, 10))
t = range(Nt)
for i in range(args.Nsample):
    model = bsm.BSM(Nt, args.mu, args.sigma, t_init=0.0, y_init=init)
    y = model.predict()
    rows = args.Nsample / 2
    cols = args.Nsample / rows
    plt.subplot(cols, rows, i + 1)
    # plot BTC data
    plt.plot(t, z, "bs:", markeredgecolor="black")
    # plot BSM model
    plt.plot(t, y, "ro-", markeredgecolor="black")
    plt.xlabel("time")
    plt.ylabel("BTC")
    plt.yscale("log")
    del model
fig.suptitle("(mu,sigma)=(" + str(args.mu) + "," + str(args.sigma) + ")",
             fontsize=30)
plt.savefig("BTC_BSM_mu" + str(args.mu) + "_s" + str(args.sigma) + ".png")
Esempio n. 5
0
import BSM

T=34/365
r=None
spot=2.3440
option_dict={}
bsm_list=[]

#read data
file_name="D://option.csv"

with open(file_name) as f:
    f_csv = csv.reader(f)
    headers = next(f_csv)
    for row in f_csv:
        if(row[0]=="C"):
            temp=1
        elif row[0]=="P":
            temp=-1
        else:
            print "error"
            exit(0)

        option_dict[(temp,float(row[1]))]=tuple([float(x) for x in row[5:11]])

for i in option_dict:
    bsm=BSM.BSM(i[0],i[1],r,T,spot)
    bsm.update
    bsm_list.append()

Esempio n. 6
0
    def get_iv(self):

        bsm = BSM.BSM(self.option_type, self.strike, self.interest,
                      self.param_t, self.future_price)
        return bsm.get_iv(self.option_type, self.quote.last_price)
Esempio n. 7
0
parser.add_argument("-se", "--seed", type=int, default=-1)
args = parser.parse_args()

z = list()

# read csv file
with open(args.csvfile, "r") as file:
    reader = csv.reader(file)
    header = next(reader)
    for row in reader:
        z.append(float(row[4]))

Nt = len(z)
init = z[0]

# calc BSM model
model = bsm.BSM(Nt, args.mu, args.sigma, y_init=init, seed=args.seed)
y = model.predict()

# plot
t = range(Nt)
# plot BSM model
plt.plot(t, y, "ro-", label="BSM model", markeredgecolor="black")
# plot BTC data
plt.plot(t, z, "bs:", label="market price", markeredgecolor="black")
plt.yscale("log")
plt.xlabel("time")
plt.ylabel("BTC")
plt.legend()
plt.show()