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 save_tick_data(): maturity = '' if len(product) < 6: pass # # Future # if product == 'HSIX3': # # this is the future at Nov # maturity = 'X' # elif product == 'HSIZ3': # # this is the future at Dec # maturity = 'Z' # else: # return maturity # # hsi_dict = db.find_future(maturity) # last_trade_time = get_future_last_trade_time(hsi_dict, tick, accumulated_num) # hsi = HSIFuture(tick, date, maturity, last_trade_price, last_trade_time, accumulated_num, ask_price, # bid_price) # db.update_future(maturity, hsi.to_sql()) elif len(product) > 5: # Option maturity = product[8] # product type and maturity day strike_price = float(product[3:8]) # product strike price option_type = '' if maturity == 'K': # call option at Nov option_type = 'call' elif maturity == 'L': # call option at Dec option_type = 'call' elif maturity == 'W': # put option at Nov option_type = 'put' elif maturity == 'X': # put option at Dec option_type = 'put' else: return maturity last_trade_time = get_option_last_trade_time( db.find_option(maturity, strike_price, option_type), tick, accumulated_num) option = HSIOption(strike_price, maturity, option_type, date, tick, last_trade_price, last_trade_time, accumulated_num, ask_price, bid_price) db.update_option(maturity, strike_price, option_type, option.to_sql()) return maturity
def get_current_price(self): option_dict = db.find_option(self.__maturity, self.__strike_price, self.__option_type) return option_dict.get_option_price()
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