示例#1
0
 def opt_vol(self, type, symbol, option, underlying, day, month, year, strike):
     if type == "Equity":
         vol = 1
     elif option == "CALL":
         call = Call(underlying, day, month, year, strike)
         vol = float(call.implied_volatility())
     elif option == "PUT":
         put = Put(underlying, day, month, year, strike)
         vol = float(put.implied_volatility())
     else:
         vol = 0
     return vol
def get_quotes_from_yahoo(reload_weekly=False):
    if reload_weekly:
        tickers = save_weekly_earnings()
    else:
        with open("saveweekly.pickle", "rb") as f:
            tickers = pickle.load(f)

    for ticker in tickers:
        print(ticker)
        g = Stock(ticker, source='yahoo')
        price = g.price

        PutATM = Put(ticker, strike=price, source='yahoo')
        b = PutATM.price

        CallATM = Call(ticker, strike=price, source='yahoo')
        a = CallATM.price

        print("Put ATM:", a, "Call ATM:", b)

        iv = (a + b)
        poms = PutATM.strike
        pom = poms - iv
        coms = CallATM.strike
        com = coms + iv

        PutIV = Put(ticker, strike=pom, source='yahoo')
        PutIV2 = Put(ticker, strike=pom - 3, source='yahoo')
        pl = PutIV.price
        pl2 = PutIV2.price
        pis = PutIV.strike
        pis2 = PutIV2.strike

        CallIV = Call(ticker, strike=com, source='yahoo')
        CallIV2 = Call(ticker, strike=com + 3, source='yahoo')
        cl = CallIV.price
        cl2 = CallIV2.price
        cis = CallIV.strike
        cis2 = CallIV2.strike

        MaxLossP = pis - pis2
        MaxLossC = cis - cis2
        ProfitP = 100 * (pl - pl2) / (MaxLossP + 0.00001)
        ProfitC = 100 * (cl2 - cl) / (MaxLossC + 0.00001)

        print("Implied Volatility:", iv, "Puts Outside of IV:", pl,
              "Calls Outside of IV:", cl)
        print("Profit from selling puts at", pis, "and buying at", pis2, ":",
              ProfitP, "%")
        print("Profit from selling calls at", cis, "and buying at", cis2, ":",
              ProfitC, "%")
示例#3
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
示例#4
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}
示例#5
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
示例#6
0
def expirationsOfInterest(tick, minDays, maxDays):

    putOption = Put(tick)

    allExpirations = putOption.expirations
    print('List of all expirations\n')
    print(allExpirations)
    print('\n')
    validExpirations = []
    minDaysTillExpiration = 15
    maxDaysTillExpiration = 45

    for expDate in allExpirations:

        expir = expDate.split('-')
        day = int(expir[0])
        month = int(expir[1])
        year = int(expir[2])

        daysOut = datetime.date(year, month, day) - datetime.date.today()
        daysOut = daysOut.days

        if daysOut > minDaysTillExpiration and daysOut < maxDaysTillExpiration:
            validExpirations.append(expDate)

    return validExpirations
def yFinanceToWallStreet(optionChain, strikePrice):
    # Gets the relevant data to calculate greeks
    data = op.findGreekData(optionChain)
    # Gets the specific row with strike price wanted
    optionContract = data.loc[data['strike'] == strikePrice]

    # Checks if the option contract is a call
    if optionContract.iloc[0]['typeOfOption'] == 'Call':
        # Gets the date and converts it to work with WallStreet
        date = dateConverter(optionContract.iloc[0]['expirationDate'])
        # Returns the Call object that will be used to find greeks
        return Call(op.findTickerName(
            optionContract.iloc[0]['contractSymbol']),
                    d=date[2],
                    m=date[1],
                    y=date[0],
                    strike=strikePrice,
                    source='yahoo')
    # If not a call option, assumes it is a Put option
    else:
        # Gets the date and converts it to work with WallStreet
        date = dateConverter(optionContract.iloc[0]['expirationDate'])
        # Returns the Put object that will be used to find greeks
        return Put(op.findTickerName(optionContract.iloc[0]['contractSymbol']),
                   d=date[2],
                   m=date[1],
                   y=date[0],
                   strike=strikePrice,
                   source='yahoo')
示例#8
0
def updatePortfolio(currentPortfolio):
    value = currentPortfolio["Value"]
    for i in reversed(range(len(currentPortfolio["Current Options"]))):
        optionInfo = currentPortfolio["Current Options"][i]
        date = convertDate(optionInfo["Expiry"])
        dateTup = dateToTuple(date)
        #update the Price, Underlying Price, and add it to value
        name = optionInfo["Stock Name"]
        strike = optionInfo["Strike"]
        isCall = optionInfo["Call"]
        option = Call(name,
                      d=dateTup[0],
                      m=dateTup[1],
                      y=dateTup[2],
                      strike=strike,
                      source="yahoo") if isCall else Put(name,
                                                         d=dateTup[0],
                                                         m=dateTup[1],
                                                         y=dateTup[2],
                                                         strike=strike,
                                                         source="yahoo")
        #lets update
        oldValue = value
        value += (option.price - optionInfo["Price"]) * 100
        refreshLog(name, date, strike, optionInfo["Price"], option.price,
                   oldValue, value, currentPortfolio["Spent"])
        optionInfo["Price"] = option.price
        optionInfo["Underlying Price"] = option.underlying.price
        #if option is expired, lets move it
        if (date <= dt.datetime.now()):
            currentPortfolio["Past Options"].append(optionInfo)
            del currentPortfolio["Current Options"][i]
    #reset the new value for the portfolio
    currentPortfolio["Value"] = value
    return currentPortfolio
示例#9
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
示例#10
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))
示例#11
0
 def opt_rate(self, type, symbol, option, underlying, day, month, year, strike):
     if type == "Equity":
         rate = 1
     elif option == "CALL":
         call = Call(underlying, day, month, year, strike)
         rate = float(call.BandS.r)
     elif option == "PUT":
         put = Put(underlying, day, month, year, strike)
         rate = float(put.BandS.r)
     else:
         rate = 0
     return rate
示例#12
0
def getOption(tickerSymbol, isCall):
    price = getStockPrice(tickerSymbol)
    date = getFutureDate()
    opt = None
    try:
        opt = Call(
            tickerSymbol, d=date[0], m=date[1], y=date[2],
            source="yahoo") if isCall else Put(
                tickerSymbol, d=date[0], m=date[1], y=date[2], source="yahoo")
    except:
        print("No options avail")
    return opt
示例#13
0
 def opt_time(self, type, symbol, option, underlying, day, month, year, strike):
     if type == "Equity":
         time = 1
     elif option == "CALL":
         call = Call(underlying, day, month, year, strike)
         time = call.BandS.T
     elif option == "PUT":
         put = Put(underlying, day, month, year, strike)
         time = put.BandS.T
     else:
         time = 0
     return time
示例#14
0
    def opt_vol_r_T(self, type, symbol, option, underlying, day, month, year, strike):
        if type == "Equity":
            time = 1
            rate = 1
            vol = 1
        elif option == "CALL":
            call = Call(underlying, day, month, year, strike)
            time = call.BandS.T
            rate = float(call.BandS.r)
            vol = float(call.implied_volatility())
        elif option == "PUT":
            put = Put(underlying, day, month, year, strike)
            time = put.BandS.T
            rate = float(put.BandS.r)
            vol = float(put.implied_volatility())
        else:
            time = 0;
            rate = 0;
            vol = 0

        return time, rate, vol
示例#15
0
def get_options(sym, strike_date):
    put = Put(sym,
              d=strike_date.day,
              m=strike_date.month,
              y=strike_date.year,
              strict=True,
              source='yahoo')
    call = Call(sym,
                d=strike_date.day,
                m=strike_date.month,
                y=strike_date.year,
                strict=True,
                source='yahoo')
    stock = Stock(sym)
    price = get_closest(stock.price, put.strikes)
    put.set_strike(price)
    call.set_strike(price)

    return [
        stock.price, price, put.price, put.volume, call.price, call.volume,
        format_date(put.expiration),
        format_date(call.expiration)
    ]
示例#16
0
def implied_forward(symbol, n_days):
    s = Stock(symbol)
    c = Call(symbol)
    curr_date = str(datetime.date(datetime.now()))
    expiries = c.expirations
    expiry_to_use = expiries[0]
    my_n_days = 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
        my_n_days = days_to_exp
        if days_to_exp >= n_days:
            break
    call = Call(symbol,
                d=int(expiry_to_use[0:2]),
                m=int(expiry_to_use[3:5]),
                y=int(expiry_to_use[6:10]))
    put = Put(symbol,
              d=int(expiry_to_use[0:2]),
              m=int(expiry_to_use[3:5]),
              y=int(expiry_to_use[6:10]))
    bracket_dict = get_strike_bracket(call.strikes, s.price)
    forward = s.price
    if bracket_dict['lower_weight'] > 0.5:
        call.set_strike(bracket_dict['lower_strike'])
        put.set_strike(bracket_dict['lower_strike'])
        forward = bracket_dict['lower_strike'] - put.price + call.price
    else:
        call.set_strike(bracket_dict['higher_strike'])
        put.set_strike(bracket_dict['higher_strike'])
        forward = bracket_dict['higher_strike'] - put.price + call.price
    return {
        "symbol": symbol,
        "forward_price": forward,
        "current_price": s.price,
        "expiry": expiry_to_use
    }
示例#17
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
示例#18
0
文件: Options.py 项目: minxhe/stonks
method = args[1]
symbol = args[2]
day = int(args[3])
month = int(args[4])
year = int(args[5])
min_strike = int(args[6])
max_strike = int(args[7])
step = int(args[8])

options = []
price = min_strike

while price <= max_strike:
    option = Call(symbol, d=day, m=month, y=year,
                  strike=price) if method == 'call' else Put(
                      symbol, d=day, m=month, y=year, strike=price)
    options.append([
        price, option.price,
        option.implied_volatility(), option.volume, option.underlying.price,
        option.delta(),
        option.gamma(),
        option.theta(), option.expiration
    ])

    price += step

df = pd.DataFrame(options,
                  columns=[
                      'Strike', 'Price', 'Implied Vol', 'Volume', 'Underlying',
                      'Delta', 'Gamma', 'Theta', 'Expiration'
                  ])
示例#19
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"}
示例#20
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"}
示例#21
0
def putCreditSpread(tick, minDays, maxDays, strikeSpread, minPoP):

    s = Stock(tick)
    stockPrice = s.price
    validExpirations = expirationsOfInterest(tick, minDays, maxDays)

    print('List of valid expirations:\n')
    print(validExpirations)
    print('\n')
    my_columns = ['Expiry', 'PoP', 'Premium', 'Long Strike', 'Short Strike']
    #spread_dataframe = pd.DataFrame(columns = my_columns)

    frames = []
    for expDate in validExpirations[:1]:

        print('Current expiration being analyzed\n')
        print(expDate)
        expir = expDate.split('-')
        day = int(expir[0])
        month = int(expir[1])
        year = int(expir[2])

        if day == 3:
            blah = 5

        day = 5
        month = 3
        year = 2021
        temp = Put(tick, d=day, m=month, y=year)

        spread_dataframe = pd.DataFrame(columns=my_columns)
        strikes = []
        strike1 = []
        strike2 = []
        longStrike = []
        shortStrike = []
        longPremium = []
        shortPremium = []

        strikes = (temp.strikes)

        strikes = [
            strike for strike in strikes
            if (strike / stockPrice > 1 - strikeSpread and strike /
                stockPrice < 1 + strikeSpread)
        ]

        strike1 = [a for (a, b) in itertools.product(strikes, strikes)]
        strike2 = [b for (a, b) in itertools.product(strikes, strikes)]

        for i in range(0, len(strike1)):
            if strike1[i] < strike2[i]:
                longStrike.append(strike1[i])
                shortStrike.append(strike2[i])

                temp.set_strike(strike1[i])

                price = (temp.bid + temp.ask) / 2
                longPremium.append(price)

                temp.set_strike(strike2[i])
                price = (temp.bid + temp.ask) / 2
                shortPremium.append(price)

            elif strike1[i] > strike2[i]:
                longStrike.append(strike2[i])
                shortStrike.append(strike1[i])

                temp.set_strike(strike1[i])

                price = (temp.bid + temp.ask) / 2
                shortPremium.append(price)

                temp.set_strike(strike2[i])
                price = (temp.bid + temp.ask) / 2
                longPremium.append(price)
            else:
                continue

        for i in range(0, len(shortStrike)):
            credit = shortPremium[i] - longPremium[i]
            strikeWidth = shortStrike[i] - longStrike[i]
            spread_dataframe = spread_dataframe.append(pd.Series(
                [
                    expir, 100 - credit / strikeWidth * 100, credit,
                    longStrike[i], shortStrike[i]
                ],
                index=my_columns),
                                                       ignore_index=True)

        frames.append(spread_dataframe)

    result = pd.concat(frames)

    spread_dataframe.sort_values('PoP', ascending=False, inplace=True)
    spread_dataframe.reset_index(drop=True, inplace=True)
    spread_dataframe = spread_dataframe[spread_dataframe['PoP'] >= minPoP]

    result.sort_values('PoP', ascending=False, inplace=True)
    result.reset_index(drop=True, inplace=True)
    result = result[spread_dataframe['PoP'] >= minPoP]

    return result
示例#22
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"}
示例#23
0
def index(request):
    trending = trendingtickers.getTrendingTickers()
    gainers = trendingtickers.getBiggestGainers()
    losers = trendingtickers.getBiggestLosers()
    account = Account.objects.get(user_id=request.user.get_username())
    balance = account.balance
    contracts = {
        t.contract_symbol: t.contract_symbol
        for t in account.transaction.all()
    }.values()
    holdings = dict()
    total = 0
    numCalls = 0
    numPuts = 0
    for contract in contracts:
        transactions = account.transaction.filter(contract_symbol=contract)
        quantity = sum([t.quantity for t in transactions])
        if quantity == 0:
            continue
        option = options_info.getOptionInfoFromContract(contract)
        ticker = option['ticker']
        date = option['expirationDate']
        optionType = option['type']
        strike = option['strike']
        index_number = re.search(r"\d", contract)
        ticker = contract[0:index_number.start()]
        stock = stock_info.StockInfo(ticker)
        name = stock.name
        price = 0
        if optionType == "Call":
            numCalls += 1
            dates = greek_options.dateConverter(date)
            call = Call(ticker,
                        d=dates[2],
                        m=dates[1],
                        y=dates[0],
                        strike=strike,
                        source='yahoo')
            price = call.price
        else:
            numPuts += 1
            dates = greek_options.dateConverter(date)
            put = Put(ticker,
                      d=dates[2],
                      m=dates[1],
                      y=dates[0],
                      strike=strike,
                      source='yahoo')
            price = put.price
        current_price = price

        profit = round(
            sum([
                t.quantity * (current_price - t.purchase_price)
                for t in transactions
            ]), 2)
        owned = quantity * current_price
        total += owned
        balance += total
        holdings[contract] = {
            'current_price': current_price,
            'quantity': quantity,
            'portfolio_share': owned,
            'profit': profit
        }
    watchlist = account.watchlist.split(',')
    total_contracts = 0
    for holding in holdings:
        holdings[holding]['portfolio_share'] = round(
            holdings[holding]['portfolio_share'] / total * 100, 2)
        total_contracts += holdings[holding]['quantity']
    inform = watchlistDisplay.getWatchListInfo(watchlist)
    balance = round(balance, 2)
    return render(
        request, "index.html", {
            'trending': trending,
            'gainers': gainers,
            'losers': losers,
            'watchlist': watchlist,
            'info': inform,
            'holdings': holdings,
            'balance': balance,
            'contractTotal': total_contracts,
            'numCalls': numCalls,
            'numPuts': numPuts
        })
示例#24
0
         window['__price'].update(s.price)
     except requests.exceptions.ConnectionError as ConnectionError:
         window['__price'].update("ERROR")
         errors = True
 # Get strikes and populate.
 # TODO: Wait for the price to update
 if expdate is not None and not errors:
     o = None  # This is our option object.
     if is_call:
         o = Call(symbol,
                  d=expdate.day,
                  m=expdate.month,
                  y=expdate.year)
         print("CALLS")
     else:
         o = Put(symbol, d=expdate.day, m=expdate.month, y=expdate.year)
         print("PUTS")
     if len(o.strikes) > 0:
         print("Strikes", o.strikes)
         strikes = get_strikes(price, o.strikes, is_call, num_options)
         for i, strik in enumerate(strikes):
             strike = Decimal(strik)
             o.set_strike(strike)
             row = f'r{i+1}'
             price = Decimal(o.price)
             window[row + 'c1'].update(o.strike)
             window[row + 'c2'].update(o.price)
             if target1 > 0:
                 gain = strike - target1 - price
                 if is_call:
                     gain = target1 - strike - price
示例#25
0
options_delta = pd.DataFrame()
options_delta['Strike'] = options_chain['Strike']
options_delta['Delta'] = np.NaN
if (optionType == 'calls'):
    for strike in range(0, len(options_delta)):
        d1 = Call(benchmark,
                  d=int(hedgeDay),
                  m=int(hedgeMonth),
                  y=int(hedgeYear),
                  strike=options_delta['Strike'][strike])
        options_delta.iloc[strike, 1] = d1.delta()
else:
    for strike in range(0, len(options_delta)):
        d1 = Put(benchmark,
                 d=int(hedgeDay),
                 m=int(hedgeMonth),
                 y=int(hedgeYear),
                 strike=options_delta['Strike'][strike])
        options_delta.iloc[strike, 1] = d1.delta()
st.write(options_delta)

# 6: strike prices for delta range
strike_range = 0
if reqDelta > 0:
    strike_range = options_delta[(0.35 <= options_delta['Delta'])
                                 & (options_delta['Delta'] <= 0.65)]
else:
    strike_range = options_delta[(-0.65 <= options_delta['Delta'])
                                 & (options_delta['Delta'] <= -0.35)]

# 7: calls/puts details
示例#26
0
def contract(request, contract):
    typ = ""

    try:
        option = options_info.getOptionInfoFromContract(contract)
        ticker = option['ticker']
        date = option['expirationDate']
        optionType = option['type']
        strike = option['strike']

        index_number = re.search(r"\d", contract)
        ticker = contract[0:index_number.start()]
        stock = stock_info.StockInfo(ticker)
        name = stock.name
        amount = request.GET.get("quantity", "")

        if optionType == "Call":
            delta, gamma, rho, vega, theta = greek_options.getGreeks(
                greek_options.yFinanceToWallStreet(
                    yfinance.Ticker(ticker).option_chain(date).calls, strike))
            dates = greek_options.dateConverter(date)
            call = Call(ticker,
                        d=dates[2],
                        m=dates[1],
                        y=dates[0],
                        strike=strike,
                        source='yahoo')
            price = call.price
            typ = "Call"
        else:
            delta, gamma, rho, vega, theta = greek_options.getGreeks(
                greek_options.yFinanceToWallStreet(
                    yfinance.Ticker(ticker).option_chain(date).puts, strike))
            dates = greek_options.dateConverter(date)
            put = Put(ticker,
                      d=dates[2],
                      m=dates[1],
                      y=dates[0],
                      strike=strike,
                      source='yahoo')
            price = put.price
            typ = "Put"
    except:
        context = {}
        html_template = loader.get_template('error-404.html')
        return HttpResponse(html_template.render(context, request))

    account = Account.objects.get(user_id=request.user.get_username())
    if request.GET.get('type', "") == "Buy":
        # TODO type of option, strike
        transaction = Transaction(expiration_date=date,
                                  contract_symbol=contract,
                                  purchase_price=price,
                                  quantity=amount,
                                  typ=typ,
                                  strike=strike)
        transaction.save(force_insert=True)
        transaction.full_clean()
        account.balance -= int(price * float(int(amount)) * float(100))
        account.save()
        account.transaction.add(transaction)
        transactions = account.transaction.filter(contract_symbol=contract)
        try:
            quantity = sum([t.quantity for t in transactions])

        except:
            # TODO: Change this to "you cannot sell because you do not own the stock"
            context = {}
            html_template = loader.get_template('error-404.html')
            return HttpResponse(html_template.render(context, request))

    elif request.GET.get('type', "") == "Sell":
        transactions = account.transaction.filter(contract_symbol=contract)
        try:
            transaction = transactions[0]
            transaction.pk = None

        except:
            # TODO: Change this to "you cannot sell because you do not own the stock"
            context = {}
            html_template = loader.get_template('error-404.html')
            return HttpResponse(html_template.render(context, request))
        transaction.save(force_insert=True)
        transaction.full_clean()
        transaction.quantity = int(amount) * -1
        transaction.save()
        transaction.full_clean()
        account.balance += int(price * float(int(amount)) * float(100))
        account.save()
        account.transaction.add(transaction)
        quantity = sum([t.quantity for t in transactions])
    else:
        pass
    account.save()
    account.full_clean()

    valuation = volatility_analysis.overorunder(ticker, date,
                                                optionType)[contract]
    quantity = sum([
        t.quantity
        for t in account.transaction.filter(contract_symbol=contract)
    ])
    return render(
        request, "contract.html", {
            'contract': contract,
            'delta': round(delta, 5),
            'gamma': round(gamma, 5),
            'rho': round(rho, 5),
            'vega': round(vega, 5),
            'theta': round(theta, 5),
            'IV': ticker,
            'name': name,
            'option': option,
            'price': price,
            'quantity': quantity,
            'valuation': valuation
        })
示例#27
0
def main():
    parser = argparse.ArgumentParser(
        description="Find best trading strategies")
    group = parser.add_mutually_exclusive_group()
    parser.add_argument("ticker", type=str, help="stock ticker")
    # group.add_argument("-t", "--ticker", action="store_true")
    group.add_argument("-d", "--date", action="store_true")
    parser.add_argument("-s", "--strategy", help="the strategy")
    # parser.add_argument("y", type=int, help="the exponent")
    args = parser.parse_args()

    ticker = yf.Ticker(args.ticker)

    # get stock info
    print('****** Info **********')
    print(ticker.info)
    # input("Press Enter to continue...")

    # get historical market data
    print('****** History **********')
    hist = ticker.history(period="max")
    print(hist)
    # input("Press Enter to continue...")

    print('****** Actions **********')
    # show actions (dividends, splits)
    print(ticker.actions)
    # input("Press Enter to continue...")

    print('****** dividends **********')
    # show dividends
    print(ticker.dividends)
    # input("Press Enter to continue...")

    # show splits
    ticker.splits

    # show financials
    print('****** financials **********')
    print(ticker.financials)
    print(ticker.quarterly_financials)
    # input("Press Enter to continue...")

    # show major holders
    print(ticker.major_holders)

    # show institutional holders
    print(ticker.institutional_holders)
    # input("Press Enter to continue...")
    # show balance heet
    print(ticker.balance_sheet)
    print(ticker.quarterly_balance_sheet)

    # show cashflow
    ticker.cashflow
    ticker.quarterly_cashflow

    # show earnings
    print(ticker.earnings)
    print(ticker.quarterly_earnings)

    # show sustainability
    print(ticker.sustainability)

    # show analysts recommendations
    print(ticker.recommendations)

    # show next event (earnings, etc)
    print('****** calendars **********')
    print(ticker.calendar)
    # input("Press Enter to continue...")

    # show ISIN code - *experimental*
    # ISIN = International Securities Identification Number
    print(ticker.isin)

    # show options expirations
    print('****** calendars **********')
    print(ticker.options)
    # input("Press Enter to continue...")
    # get option chain for specific expiration
    opt = ticker.option_chain(ticker.options[0])
    # data available via: opt.calls, opt.puts
    print(opt.calls)
    # input("Press Enter to continue...")
    print(opt.puts)
    # input("Press Enter to continue...")
    history = ticker.history()
    last_quote = (history.tail(1)['Close'].iloc[0])
    last_price = int(last_quote)
    # calls
    for opt in ticker.options:
        try:
            data = []
            dt = datetime.fromisoformat(opt)
            option = Call(args.ticker
                          )  # d=dt.day, m=dt.month, y=dt.year, strike=ticker.)
            for strike in option.strikes:
                if strike < last_price - 40 or strike > last_price + 25:
                    continue
                print(f'strike = {strike}')
                option.set_strike(strike)
                item = {}
                item['strike'] = strike
                item['price'] = option.price
                item['iv'] = option.implied_volatility()
                item['delta'] = option.delta()
                item['vega'] = option.vega()
                item['gamma'] = option.gamma()
                # print(f'theta = {option.theta()}')
                # print(f'vega = {option.vega()}')
                data.append(item)
            dataframe = pd.DataFrame(data)
            dataframe.to_csv(f'call-{args.ticker}-{dt}')
            print(dataframe.to_csv())
        except Exception as e:
            print(f'{type(e)}: {str(e)} ')
            continue

        # put
        for opt in ticker.options:
            try:
                data = []
                dt = datetime.fromisoformat(opt)
                option = Put(
                    args.ticker
                )  # d=dt.day, m=dt.month, y=dt.year, strike=ticker.)
                for strike in option.strikes:
                    if strike < last_price - 40 or strike > last_price + 25:
                        continue
                    print(f'strike = {strike}')
                    option.set_strike(strike)
                    item = {}
                    item['strike'] = strike
                    item['price'] = option.price
                    item['iv'] = option.implied_volatility()
                    item['delta'] = option.delta()
                    item['vega'] = option.vega()
                    item['gamma'] = option.gamma()
                    # print(f'Rho = {option.rho()}')
                    # print(f'type = {option.Option_type}')
                    # print(f'theta = {option.theta()}')
                    # print(f'vega = {option.vega()}')
                    data.append(item)
                dataframe = pd.DataFrame(data)
                dataframe.to_csv(f'put-{args.ticker}-{dt}')
                print(dataframe.to_csv())
            except Exception as e:
                print(f'{type(e)}: {str(e)} ')
                continue