示例#1
0
def get_delta(symbol: str, percent_move: float, expiry: str):
    symbol = symbol.upper()
    if is_cache_good(f'{symbol}|getdelta|{percent_move:.2f}|{expiry}'):
        return ast.literal_eval(
            r.hget(f'{symbol}|getdelta|{percent_move:.2f}|{expiry}', 'value'))
    s = Stock(symbol)
    up_px = s.price * (1 + percent_move / 100)
    down_px = s.price * (1 - percent_move / 100)
    call = Call(symbol,
                d=int(expiry[0:2]),
                m=int(expiry[3:5]),
                y=int(expiry[6:10]))
    up_delta_dict = get_strike_bracket(call.strikes, up_px)
    call.set_strike(up_delta_dict['lower_strike'])
    delta1 = call.delta() * up_delta_dict['lower_weight']
    call.set_strike(up_delta_dict['higher_strike'])
    delta2 = call.delta() * (1 - up_delta_dict['lower_weight'])
    delta_up_move = delta1 + delta2

    put = Put(symbol,
              d=int(expiry[0:2]),
              m=int(expiry[3:5]),
              y=int(expiry[6:10]))
    down_delta_dict = get_strike_bracket(put.strikes, down_px)
    put.set_strike(down_delta_dict['lower_strike'])
    delta1 = -put.delta() * down_delta_dict['lower_weight']
    put.set_strike(down_delta_dict['higher_strike'])
    delta2 = -put.delta() * (1 - down_delta_dict['lower_weight'])
    delta_down_move = delta1 + delta2
    return_dict = {'delta_up': delta_up_move, 'delta_down': delta_down_move}
    r.hset(f'{symbol}|getdelta|{percent_move:.2f}|{expiry}', 'time',
           datetime.utcnow().strftime('%s'))
    r.hset(f'{symbol}|getdelta|{percent_move:.2f}|{expiry}', 'value',
           str(return_dict))
    return return_dict
示例#2
0
def get_delta(symbol: str, percent_move: float, expiry: str):
    s = Stock(symbol)
    up_px = s.price * (1 + percent_move / 100)
    down_px = s.price * (1 - percent_move / 100)
    call = Call(symbol,
                d=int(expiry[0:2]),
                m=int(expiry[3:5]),
                y=int(expiry[6:10]))
    up_delta_dict = get_strike_bracket(call.strikes, up_px)
    call.set_strike(up_delta_dict['lower_strike'])
    delta1 = call.delta() * up_delta_dict['lower_weight']
    call.set_strike(up_delta_dict['higher_strike'])
    delta2 = call.delta() * (1 - up_delta_dict['lower_weight'])
    delta_up_move = delta1 + delta2

    put = Put(symbol,
              d=int(expiry[0:2]),
              m=int(expiry[3:5]),
              y=int(expiry[6:10]))
    down_delta_dict = get_strike_bracket(put.strikes, down_px)
    put.set_strike(down_delta_dict['lower_strike'])
    delta1 = -put.delta() * down_delta_dict['lower_weight']
    put.set_strike(down_delta_dict['higher_strike'])
    delta2 = -put.delta() * (1 - down_delta_dict['lower_weight'])
    delta_down_move = delta1 + delta2
    return {'delta_up': delta_up_move, 'delta_down': delta_down_move}
示例#3
0
 def opt_values(self, type, symbol, option, underlying, day, month, year, strike):
     if type == "Equity":
         price = Stock(symbol).price
         delta = 1
     elif option == "CALL":
         call = Call(underlying, day, month, year, strike)
         price = call.price
         delta = call.delta()
     elif option == "PUT":
         put = Put(underlying, day, month, year, strike)
         price = put.price
         delta = put.delta()
     else:
         price = 0
         delta = 0
     return price, delta
示例#4
0
    def get_data(self, df):
        tup = df.shape
        x = tup[0]
        y = tup[1]
        prices = []
        deltas = []
        for i in range(x):
            if df.iloc[i, 1] == "Equity":
                price = Stock(df.iloc[i, 0]).price
                delta = 1
                df.iloc[i, 2] = df.iloc[i, 0]

            elif df.iloc[i, 3] == "CALL":
                ticker = df.iloc[i, 2]
                strike = float(df.iloc[i, 5])
                date = datetime.strptime(df.iloc[i, 6], '%m/%d/%Y')
                month = date.month
                day = date.day
                year = date.year
                call = Call(ticker, day, month, year, strike)
                price = call.price
                delta = call.delta()

            elif df.iloc[i, 3] == "PUT":
                ticker = df.iloc[i, 2]
                strike = float(df.iloc[i, 5])
                date = datetime.strptime(df.iloc[i, 6], '%m/%d/%Y')
                month = date.month
                day = date.day
                year = date.year
                put = Put(ticker, day, month, year, strike)
                price = put.price
                delta = put.delta()

            else:
                price = 0
            prices.append(price)
            deltas.append(delta)
            print(prices, deltas)

        df["Prices"] = prices
        df["Deltas"] = deltas
        return df
示例#5
0
def find_score_each_expiration(expiration, udlying):
    """

    :param expiration:
    :param udlying:
    :return:
    """
    #print(f"Processing {udlying}")
    ticker = yf.Ticker(udlying)
    opt = ticker.option_chain(expiration)
    df = opt.puts
    s_array = df[["strike", "inTheMoney"]]
    indexNames = s_array[(s_array['inTheMoney'] == True)].index
    s_array.drop(indexNames, inplace=True)
    df = []
    strikes = s_array[["strike"]].to_numpy()[::-1]
    Best_option_score = 0
    Best_option = []
    if len(strikes) == 0:
        return Best_option_score, Best_option
    for strike in strikes:
        option = Put(udlying,
                     d=int(expiration[8:10]),
                     m=int(expiration[5:7]),
                     y=int(expiration[0:4]),
                     strike=strike)
        premium = (2 * option.price + option.bid + option.ask) / 4
        delta = option.delta()
        gamma = option.gamma()
        theta = option.theta()
        score, K2 = find_score(expiration, premium, delta, gamma, theta,
                               strike)
        #print(expiration, "on", udlying, float(strike), "put has a score", int(score),float(K2))
        if abs(delta) < 0.1 or premium < 0.025 * strike or DTE(
                expiration) > 50:
            return Best_option_score, Best_option
        if int(score) > Best_option_score:
            Best_option_score = score
            Best_option = "{} {} {} put with score: {} price:{:10.3f} tg ratio=:{:10.2f}.".format(
                udlying, expiration, float(strike), int(score), float(premium),
                float(K2))
示例#6
0
def best_put_trades(symbol, num_of_days):
    symbol = symbol.upper()
    if is_cache_good(f'{symbol}|puttrade|{num_of_days}'):
        return ast.literal_eval(
            r.hget(f'{symbol}|puttrade|{num_of_days}', 'value'))
    return_dict = {"error": "no options"}
    try:
        p = Put(symbol)
        range_dict = range_data_from_symbol(symbol, num_of_days)
        curr_date = str(datetime.date(datetime.now()))
        expiries = p.expirations
        expiry_to_use = expiries[0]
        for i in expiries:
            days_to_exp = abs(
                datetime.strptime(i, '%d-%m-%Y') -
                datetime.strptime(curr_date, '%Y-%m-%d')).days
            expiry_to_use = i
            if days_to_exp >= num_of_days:
                break
        p = Put(symbol,
                d=int(expiry_to_use[0:2]),
                m=int(expiry_to_use[3:5]),
                y=int(expiry_to_use[6:10]))
        counter = 0
        spread_list = []
        strikes = p.strikes
        for i in strikes:
            if i <= range_dict["high_range"] and counter < 15:
                counter = counter + 1
                p.set_strike(i)
                spread_list.append({
                    'strike': i,
                    'bid': p.bid,
                    'ask': p.ask,
                    'delta': -p.delta()
                })
        max_amt = 0
        max_put_amt = 0
        best_spread = {}
        best_put_written = {}
        spread_list.reverse()
        for i in spread_list:
            #for call
            prob_winning_put = 1 - i['delta']
            premium_put = i['bid']
            put_win_amt = premium_put * prob_winning_put
            if put_win_amt > max_put_amt:
                max_put_amt = put_win_amt
                best_put_written = i
            for j in spread_list:
                if i['strike'] > j['strike']:
                    #for spread
                    premium_per_dollar = (
                        i['bid'] - j['ask']) / abs(j['strike'] - i['strike'])
                    prob_winning_spread = 1 - j['delta']
                    win_amt = premium_per_dollar * prob_winning_spread
                    if win_amt > max_amt:
                        max_amt = win_amt
                        best_spread = {
                            'strike_long': i['strike'],
                            'strike_short': j['strike'],
                            'premium_received': i['bid'],
                            'premium_paid': j['ask'],
                            'expiry': expiry_to_use
                        }
        best_put_written['expiry'] = expiry_to_use
        return_dict = {
            "symbol": symbol,
            'best_spread': best_spread,
            'best_put': best_put_written
        }
        if best_spread and best_put_written:
            r.hset(f'{symbol}|puttrade|{num_of_days}', 'time',
                   datetime.utcnow().strftime('%s'))
            r.hset(f'{symbol}|puttrade|{num_of_days}', 'value',
                   str(return_dict))
            return return_dict
    except:
        return return_dict
示例#7
0
def get_option_limit_price(symbol: str, pc: str, strike_hint: float,
                           n_days: int):
    symbol = symbol.upper()
    pc = pc.upper()
    print(f'{symbol}|{pc}|{n_days}|{strike_hint}')
    if is_cache_good(f'{symbol}|{pc}|{n_days}|{strike_hint}', CACHE_TIMEOUT):
        return ast.literal_eval(
            r.hget(f'{symbol}|{pc}|{n_days}|{strike_hint}', 'value'))
    try:
        exp_dict = get_expiries_bracket(symbol, n_days)
        expiry_to_use = exp_dict['longer_expiry']
        if exp_dict['shorter_weight'] > 0.5:
            expiry_to_use = exp_dict['shorter_expiry']
        y = yf.Ticker(symbol)
        expiry = f'{expiry_to_use[6:10]}-{expiry_to_use[3:5]}-{expiry_to_use[0:2]}'
        o = y.option_chain(date=expiry)
        p = o.puts
        chart_list = list()
        p_strike = p.strike.tolist()
        p_ivol = p.impliedVolatility.tolist()
        for i in range(0, len(p_strike)):
            chart_list.append({
                "group": "put",
                "ivol": p_ivol[i],
                "strike": p_strike[i]
            })
        c = o.calls
        c_strike = c.strike.tolist()
        c_ivol = c.impliedVolatility.tolist()
        for i in range(0, len(c_strike)):
            chart_list.append({
                "group": "call",
                "ivol": c_ivol[i],
                "strike": c_strike[i]
            })
        strike_dict = get_strike_bracket(o.calls.strike.tolist(), strike_hint)
        if pc == "P":
            strike_dict = get_strike_bracket(o.puts.strike.tolist(),
                                             strike_hint)
        strike_to_use = strike_dict['higher_strike']
        if strike_dict['lower_weight'] > 0.5:
            expiry_to_use = exp_dict['lower_strike']
        my_option = Put(symbol,
                        d=int(expiry_to_use[0:2]),
                        m=int(expiry_to_use[3:5]),
                        y=int(expiry_to_use[6:10]))
        if pc == "C":
            my_option = Call(symbol,
                             d=int(expiry_to_use[0:2]),
                             m=int(expiry_to_use[3:5]),
                             y=int(expiry_to_use[6:10]))
        my_option.set_strike(strike_to_use)
        my_delta = my_option.delta()
        st = y.history(interval='5m', period='1d')
        stock_move = (max(st.High) - min(st.Low)) / 2
        option_move = my_delta * stock_move
        return_dict = {
            'symbol': symbol,
            'pc': pc,
            'strike': strike_to_use,
            'bid': my_option.bid,
            'ask': my_option.ask,
            'last': my_option.price,
            'expiry': expiry,
            'delta': my_delta,
            'option_move': option_move,
            'chart_data': chart_list
        }
        r.hset(f'{symbol}|{pc}|{n_days}|{strike_hint}', 'time',
               datetime.utcnow().strftime('%s'))
        r.hset(f'{symbol}|{pc}|{n_days}|{strike_hint}', 'value',
               str(return_dict))
        return return_dict
    except:
        return {"error": "No options were found"}
示例#8
0
def best_put_protection(symbol, num_of_days):
    symbol = symbol.upper()
    if is_cache_good(f'{symbol}|putprotection|{num_of_days}'):
        return ast.literal_eval(
            r.hget(f'{symbol}|putprotection|{num_of_days}', 'value'))
    try:
        p = Put(symbol)
        s = Stock(symbol)
        curr_date = str(datetime.date(datetime.now()))
        expiries = p.expirations
        expiry_to_use = expiries[0]
        for i in expiries:
            days_to_exp = abs(
                datetime.strptime(i, '%d-%m-%Y') -
                datetime.strptime(curr_date, '%Y-%m-%d')).days
            expiry_to_use = i
            if days_to_exp >= num_of_days:
                break
        p = Put(symbol,
                d=int(expiry_to_use[0:2]),
                m=int(expiry_to_use[3:5]),
                y=int(expiry_to_use[6:10]))
        counter = 0
        spread_list = []
        strikes = p.strikes
        for i in reversed(strikes):
            if i <= s.price and counter < 10:
                counter = counter + 1
                p.set_strike(i)
                spread_list.append({
                    'strike': i,
                    'bid': p.bid,
                    'ask': p.ask,
                    'last': p.price,
                    'using_last': 'false',
                    'delta': -p.delta()
                })
        min_put_strength = 100000
        best_put = {}
        spread_list.reverse()
        for i in spread_list:
            #for put
            prob_in_the_money_put = i['delta']
            i['using_last'] = 'false'
            if i['bid'] == 0 or i['ask'] == 0:
                i['bid'] = i['last']
                i['ask'] = i['last']
                i['using_last'] = 'true'
            premium_put = i['ask']

            put_cost_per_money = premium_put / prob_in_the_money_put

            if put_cost_per_money < min_put_strength:
                min_put_strength = put_cost_per_money
                best_put = i

        best_put['expiry'] = expiry_to_use
        return_dict = {"symbol": symbol, 'best_put': best_put}
        if best_put:
            r.hset(f'{symbol}|putprotection|{num_of_days}', 'time',
                   datetime.utcnow().strftime('%s'))
            r.hset(f'{symbol}|putprotection|{num_of_days}', 'value',
                   str(return_dict))
            return return_dict
    except:
        return {"error": "No options were found"}
示例#9
0
def best_put_trades(symbol, num_of_days):
    symbol = symbol.upper()
    if is_cache_good(f'{symbol}|puttrade|{num_of_days}'):
        return ast.literal_eval(
            r.hget(f'{symbol}|puttrade|{num_of_days}', 'value'))
    try:
        p = Put(symbol)
        range_dict = range_data_from_symbol(symbol, num_of_days)
        curr_date = str(datetime.date(datetime.now()))
        expiries = p.expirations
        expiry_to_use = expiries[0]
        min_day_diff = 1000
        for i in expiries:
            days_to_exp = abs(
                datetime.strptime(i, '%d-%m-%Y') -
                datetime.strptime(curr_date, '%Y-%m-%d')).days
            my_day_diff = abs(days_to_exp - num_of_days)
            if my_day_diff < min_day_diff:
                expiry_to_use = i
                min_day_diff = my_day_diff
        p = Put(symbol,
                d=int(expiry_to_use[0:2]),
                m=int(expiry_to_use[3:5]),
                y=int(expiry_to_use[6:10]))
        counter = 0
        spread_list = []
        strikes = p.strikes
        for i in reversed(strikes):
            if i <= range_dict["low_range"] and counter < 10:
                counter = counter + 1
                p.set_strike(i)
                spread_list.append({
                    'strike': i,
                    'bid': p.bid,
                    'ask': p.ask,
                    'last': p.price,
                    'using_last': 'false',
                    'delta': -p.delta()
                })
        max_amt = 0
        max_put_amt = 0
        best_spread = {}
        best_put_written = {}
        spread_list.reverse()
        for i in spread_list:
            #for put
            prob_winning_put = 1 - i['delta']
            i['using_last'] = 'false'
            if i['bid'] == 0 or i['ask'] == 0:
                i['bid'] = i['last']
                i['ask'] = i['last']
                i['using_last'] = 'true'
            premium_put = i['bid']
            put_win_amt = premium_put * prob_winning_put
            if put_win_amt > max_put_amt:
                max_put_amt = put_win_amt
                best_put_written = i
            for j in spread_list:
                if i['strike'] > j['strike']:
                    #for spread
                    premium_per_dollar = (
                        i['bid'] - j['ask']) / abs(j['strike'] - i['strike'])
                    spread_using_last = 'false'
                    if i['using_last'] == 'true' or j[
                            'using_last'] == 'true':  #If any leg uses last mark spread as last
                        spread_using_last = 'true'
                    prob_winning_spread = 1 - j['delta']
                    win_amt = premium_per_dollar * prob_winning_spread
                    if win_amt > max_amt:
                        max_amt = win_amt
                        if spread_using_last == 'true':
                            best_spread = {
                                'strike_to_sell': i['strike'],
                                'strike_to_buy': j['strike'],
                                'premium_received': i['last'],
                                'premium_paid': j['last'],
                                'expiry': expiry_to_use,
                                'spread_using_last': spread_using_last
                            }
                        else:
                            best_spread = {
                                'strike_to_sell': i['strike'],
                                'strike_to_buy': j['strike'],
                                'premium_received': i['bid'],
                                'premium_paid': j['ask'],
                                'expiry': expiry_to_use,
                                'spread_using_last': spread_using_last
                            }

        best_put_written['expiry'] = expiry_to_use
        return_dict = {
            "symbol": symbol,
            'best_spread': best_spread,
            'best_put': best_put_written
        }
        if best_spread or best_put_written:
            r.hset(f'{symbol}|puttrade|{num_of_days}', 'time',
                   datetime.utcnow().strftime('%s'))
            r.hset(f'{symbol}|puttrade|{num_of_days}', 'value',
                   str(return_dict))
            return return_dict
    except:
        return {"error": "No options were found"}