def get_current_volatility(self, date, hsi_price, R): vol = 0.0 for co in self.__call_option: time_to_maturity = util.time_to_maturity(co.get_maturity(), date) temp_vol = bs.get_volatility_quick(hsi_price, co.get_strike_price(), R, time_to_maturity, co.get_current_price(), co.get_option_type()) # k = co.get_trade() * co.get_option_type() # vol = vol + k * temp_vol vol = vol + temp_vol for po in self.__put_option: time_to_maturity = util.time_to_maturity(po.get_maturity(), date) temp_vol = bs.get_volatility_quick(hsi_price, po.get_strike_price(), R, time_to_maturity, po.get_current_price(), po.get_option_type()) # k = po.get_trade() * po.get_option_type() # vol = vol + k * temp_vol vol = vol + temp_vol return vol
def take_position(self): option_gamma = {} left = self.hsi_price * (1 - bandwidth) right = self.hsi_price * (1 + bandwidth) if self.maturity in [call_month, put_month]: for call_option_dict in db.find_all_option(call_month, 'call'): # call = util.to_HSIOption(call_option_dict) call = call_option_dict strike_price = call.get_strike_price() put_option_dict = db.find_option(call_month, strike_price, 'put') if put_option_dict is not None: # put = util.to_HSIOption(db.find_option('W', strike_price, 'put')) put = put_option_dict if None not in [call, put] and 999999.0 not in [call.get_option_price(),put.get_option_price()] \ and util.range_in_defined(left, strike_price, right): time_to_maturity = util.time_to_maturity(self.maturity, self.date) c_volatility = bs.get_volatility_quick(self.hsi_price, strike_price, R, time_to_maturity, call.get_option_price(), 'call') call_delta = bs.get_delta(self.hsi_price, strike_price, R, time_to_maturity, c_volatility, 'call') p_volatility = bs.get_volatility_quick(self.hsi_price, strike_price, R, time_to_maturity, put.get_option_price(), 'put') put_delta = bs.get_delta(self.hsi_price, strike_price, R, time_to_maturity, p_volatility, 'put') if (strike_price, call_month, 'call') not in self.today_volatility: self.today_volatility[(strike_price, call_month, 'call')] = [c_volatility] else: self.today_volatility[(strike_price, call_month, 'call')].append(c_volatility) if (strike_price, put_month, 'put') not in self.today_volatility: self.today_volatility[(strike_price, put_month, 'put')] = [p_volatility] else: self.today_volatility[(strike_price, put_month, 'put')].append(p_volatility) if True and (c_volatility > mean_vol and p_volatility > mean_vol): if abs(call_delta + put_delta) < 0.1: if ts.compare_volatility(ts.fn(yesterday, (strike_price, call_month, 'call'), (strike_price, put_month, 'put'),[init_vol_k, init_vol_w]), c_volatility + p_volatility): option_gamma[(call, put)] = bs.get_gamma(self.hsi_price, strike_price, R, time_to_maturity, c_volatility) + bs.get_gamma(self.hsi_price, strike_price,R, time_to_maturity,p_volatility) if option_gamma == {}: return None call, put = ts.get_max_gamma(option_gamma) calls = map(lambda x:call.generate_option(1),range(0,2)) puts = map(lambda x:put.generate_option(1),range(0,2)) tran = Transaction([],calls,puts,3,self.tick,self.date) db.insert_position(tran.to_dict()) return tran
def get_volatility(self, s, r): return bs.get_volatility_quick(s, self.__strike_price, r, util.time_to_maturity(self.__maturity, self.__date), self.get_price(), self.__option_type)
def get_delta(self, s, r): return self.__trade * bs.get_delta(s, self.__strike_price, r, util.time_to_maturity(self.__maturity, self.__date), self.get_volatility(s, r), self.__option_type)
def get_volatility(self, s, r): bs.get_volatility(s, self.__strike_price, r, util.time_to_maturity(self.__maturity, self.__date), self.get_option_price(), 0.1, self.__option_type)
def take_position(self): option_gamma = {} left = self.hsi_price * (1 - bandwidth) right = self.hsi_price * (1 + bandwidth) if self.maturity in [call_month, put_month]: for call_option_dict in db.find_all_option(call_month, 'call'): # call = util.to_HSIOption(call_option_dict) call = call_option_dict strike_price = call.get_strike_price() put_option_dict = db.find_option(call_month, strike_price, 'put') if put_option_dict is not None: # put = util.to_HSIOption(db.find_option('W', strike_price, 'put')) put = put_option_dict if None not in [call, put] and 999999.0 not in [call.get_option_price(),put.get_option_price()] \ and util.range_in_defined(left, strike_price, right): time_to_maturity = util.time_to_maturity( self.maturity, self.date) c_volatility = bs.get_volatility_quick( self.hsi_price, strike_price, R, time_to_maturity, call.get_option_price(), 'call') call_delta = bs.get_delta(self.hsi_price, strike_price, R, time_to_maturity, c_volatility, 'call') p_volatility = bs.get_volatility_quick( self.hsi_price, strike_price, R, time_to_maturity, put.get_option_price(), 'put') put_delta = bs.get_delta(self.hsi_price, strike_price, R, time_to_maturity, p_volatility, 'put') if (strike_price, call_month, 'call') not in self.today_volatility: self.today_volatility[(strike_price, call_month, 'call')] = [c_volatility] else: self.today_volatility[( strike_price, call_month, 'call')].append(c_volatility) if (strike_price, put_month, 'put') not in self.today_volatility: self.today_volatility[(strike_price, put_month, 'put')] = [p_volatility] else: self.today_volatility[(strike_price, put_month, 'put')].append(p_volatility) if True and (c_volatility > mean_vol and p_volatility > mean_vol): if abs(call_delta + put_delta) < 0.1: if ts.compare_volatility( ts.fn( yesterday, (strike_price, call_month, 'call'), (strike_price, put_month, 'put'), [init_vol_k, init_vol_w]), c_volatility + p_volatility): option_gamma[(call, put)] = bs.get_gamma( self.hsi_price, strike_price, R, time_to_maturity, c_volatility) + bs.get_gamma( self.hsi_price, strike_price, R, time_to_maturity, p_volatility) if option_gamma == {}: return None call, put = ts.get_max_gamma(option_gamma) calls = map(lambda x: call.generate_option(1), range(0, 2)) puts = map(lambda x: put.generate_option(1), range(0, 2)) tran = Transaction([], calls, puts, 3, self.tick, self.date) db.insert_position(tran.to_dict()) return tran
def take_position(tick, date, maturity, hsi_price): option_gamma = {} left = hsi_price * (1 - bandwidth) right = hsi_price * (1 + bandwidth) if maturity in ['K', 'W']: for call_option_dict in db.find_all_option('K', 'call'): # call = util.to_HSIOption(call_option_dict) call = call_option_dict strike_price = call.get_strike_price() put_option_dict = db.find_option('W', strike_price, 'put') if put_option_dict is not None: # put = util.to_HSIOption(db.find_option('W', strike_price, 'put')) put = put_option_dict if None not in [call, put] and 999999.0 not in [ call.get_option_price(), put.get_option_price() ] and util.range_in_defined(left, strike_price, right): time_to_maturity = util.time_to_maturity(maturity, date) # c_volatility = bs.get_ATF_volatility(call.get_option_price(), hsi_price, time_to_maturity) c_volatility = bs.get_volatility_quick( hsi_price, strike_price, R, time_to_maturity, call.get_option_price(), 'call') call_delta = bs.get_delta(hsi_price, strike_price, R, time_to_maturity, c_volatility, 'call') # p_volatility = bs.get_ATF_volatility(put.get_option_price(), hsi_price, time_to_maturity) p_volatility = bs.get_volatility_quick( hsi_price, strike_price, R, time_to_maturity, put.get_option_price(), 'put') put_delta = bs.get_delta(hsi_price, strike_price, R, time_to_maturity, p_volatility, 'put') if (strike_price, 'K', 'call') not in today_volatility: today_volatility[(strike_price, 'K', 'call')] = [c_volatility] else: today_volatility[(strike_price, 'K', 'call')].append(c_volatility) if (strike_price, 'W', 'put') not in today_volatility: today_volatility[(strike_price, 'W', 'put')] = [p_volatility] else: today_volatility[(strike_price, 'W', 'put')].append(p_volatility) if abs(call_delta + put_delta) < 0.1: if compare_volatility( fn(yesterday, (strike_price, 'K', 'call'), (strike_price, 'W', 'put')), c_volatility + p_volatility): option_gamma[(call, put)] = bs.get_gamma( hsi_price, strike_price, R, time_to_maturity, c_volatility) + bs.get_gamma( hsi_price, strike_price, R, time_to_maturity, p_volatility) elif maturity in ['L', 'X']: pass if option_gamma == {}: return None call, put = get_max_gamma(option_gamma) calls = map(lambda x: call.generate_option(1), range(0, 2)) puts = map(lambda x: put.generate_option(1), range(0, 2)) tran = Transaction([], calls, puts, 3, tick, date) # tran = Transaction([], [call.generate_option(1)], [put.generate_option(1)], 3, tick, date) db.insert_position(tran.to_dict()) return tran