Esempio n. 1
0
def fn(date, c_strike_price, p_strike_price):
    if date != None:
        return float(
            ts.find_volatility_by_date(date, c_strike_price).get(
                "volatility", init_vol)) + float(
                    ts.find_volatility_by_date(date, p_strike_price).get(
                        "volatility", init_vol))
    else:
        return init_vol * 2
    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
Esempio n. 3
0
 def update_tickdate(self, row):
     self.tick, self.product, self.last_trade_price, self.accumulated_num, self.bid_price, self.ask_price = util.format_row(
         row)
     if self.sche == 0:
         self.sche = float(util.tick_convert_to_seconds(self.tick))
     self.maturity = ts.save_tick_data(self.date, self.tick, self.product,
                                       self.last_trade_price,
                                       self.accumulated_num, self.bid_price,
                                       self.ask_price, call_month,
                                       put_month)
    def get_adjustment_option(self, current_delta, option_type):
        # if current_delta >= 0:
        #     trade_type = -1
        # else:
        #     trade_type = 1
        trade_type = 1
        option = ts.get_atm_option(self.tick, self.hsi_price, self.maturity, option_type, trade_type)
        if option.get_price() == 999999.0:
            return 0, 0

        delta = option.get_delta(self.hsi_price, R)
        # print "current_delta:", current_delta
        # print "delta:", delta
        count = abs(current_delta / float(delta))
        # print "count", count
        if count < 1:
            return 0, 0
        else:
            return int(count), option
Esempio n. 5
0
    def get_adjustment_option(self, current_delta, option_type):
        # if current_delta >= 0:
        #     trade_type = -1
        # else:
        #     trade_type = 1
        trade_type = 1
        option = ts.get_atm_option(self.tick, self.hsi_price, self.maturity,
                                   option_type, trade_type)
        if option.get_price() == 999999.0:
            return 0, 0

        delta = option.get_delta(self.hsi_price, R)
        # print "current_delta:", current_delta
        # print "delta:", delta
        count = abs(current_delta / float(delta))
        # print "count", count
        if count < 1:
            return 0, 0
        else:
            return int(count), option
Esempio n. 6
0
 def save_volatility(self):
     ts.save_today_volatility(self.date, self.today_volatility)
     ts.save_normal_volatility()
Esempio n. 7
0
 def update_hsi_price(self):
     self.hsi_price = ts.find_hsi_price(self.date, self.tick)
Esempio n. 8
0
 def __init__(self, date):
     self.sche = 0
     ts.clearTempDB()
     self.today_volatility = {}
     self.hsi_interval = 1.0
     self.date = date
Esempio n. 9
0
    def save_volatility(self):
        ts.save_today_volatility(self.date, self.today_volatility)
        ts.save_normal_volatility()

    def save_init_vol(self):
        c, p = ts.get_init_vol(call_month, put_month)
        import numpy
        return numpy.mean(c), numpy.mean(p)


if __name__ == '__main__':
    all_files = util.read_raw_filename(local_path)
    start = time.time()
    print "start algo", start
    yesterday = None
    ts.clearALLDB()
    trade_number = 0
    win = 0
    loss = 0

    winPL = 0
    lossPL = 0

    for day in all_files:  # one day
        # s = time.time()
        all_day_tick = util.read_csvfile(file(day, 'rb'))
        date = str(day).split('/')[9].split('.')[0]
        call_month, put_month, fut_month = util.get_month(date)
        no_tran_date = util.get_no_tran_date(date)
        LS = LongStaddle(date)
 def save_init_vol(self):
     c, p = ts.get_init_vol(call_month,put_month)
     import numpy
     return numpy.mean(c), numpy.mean(p)
 def save_volatility(self):
     ts.save_today_volatility(self.date, self.today_volatility)
     ts.save_normal_volatility()
 def update_hsi_price(self):
     self.hsi_price = ts.find_hsi_price(self.date, self.tick)
 def update_tickdate(self, row):
     self.tick, self.product, self.last_trade_price, self.accumulated_num, self.bid_price, self.ask_price = util.format_row(row)
     if self.sche == 0:
         self.sche = float(util.tick_convert_to_seconds(self.tick))
     self.maturity = ts.save_tick_data(self.date, self.tick, self.product, self.last_trade_price, self.accumulated_num, self.bid_price, self.ask_price,call_month, put_month)
 def __init__(self, date):
     self.sche = 0
     ts.clearTempDB()
     self.today_volatility = {}
     self.hsi_interval = 1.0
     self.date = date
    def save_volatility(self):
        ts.save_today_volatility(self.date, self.today_volatility)
        ts.save_normal_volatility()

    def save_init_vol(self):
        c, p = ts.get_init_vol(call_month,put_month)
        import numpy
        return numpy.mean(c), numpy.mean(p)

if __name__ == '__main__':
    all_files = util.read_raw_filename(local_path)
    start = time.time()
    print "start algo", start
    yesterday = None
    ts.clearALLDB()
    trade_number = 0
    win = 0
    loss = 0

    winPL = 0
    lossPL = 0

    for day in all_files: # one day
        # s = time.time()
        all_day_tick = util.read_csvfile(file(day, 'rb'))
        date = str(day).split('/')[9].split('.')[0]
        call_month, put_month, fut_month = util.get_month(date)
        no_tran_date = util.get_no_tran_date(date)
        LS = LongStaddle(date)
Esempio n. 16
0
 def save_init_vol(self):
     c, p = ts.get_init_vol(call_month, put_month)
     import numpy
     return numpy.mean(c), numpy.mean(p)
Esempio n. 17
0
    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
Esempio n. 18
0
                # print "tick:", tick
                print "pl is ", pl
                count += 1
                # print "hsi_price",hsi_price
                # print "tran.get_payoff(hsi_price):", tran.get_payoff(hsi_price)
                PL += pl
    return PL, count


if __name__ == '__main__':
    all_files = util.read_raw_filename(local_path)

    start = time.time()
    print "start algo", start
    yesterday = None
    ts.clearALLDB()
    trade_number = 0

    for f in all_files:  # one day
        csvfile = file(f, 'rb')
        reader = util.read_csvfile(csvfile)
        filename = str(f)
        date = filename.split('/')[9].split('.')[0]
        sche = 0
        ts.clearTempDB()
        today_volatility = {}
        s = time.time()
        hsi_interval = 1.0
        end_hsi_price = 0.0
        for row in reader:  # one tick
            tick, product, last_trade_price, accumulated_num, bid_price, ask_price = format_raw(