def get_options_robinhood(self, symbol, **exp):
        """Get Robinhood Options Chains for Puts and Calls.
        The code returns three objects. This code returns two pandas dataframes
        The first is Calls, the Second is Puts. The final output is the spot price.


        """

        try:
            stock = Stock.fetch(self.client, symbol)
            stock_id = stock["id"]
            option_chain = OptionChain.fetch(self.client,
                                             stock_id,
                                             symbol=symbol)
            eds = option_chain['expiration_dates']

            oc_id = option_chain["id"]

            spot = self.get_spot_price(symbol)

            spot_price = self._process_spot_price(spot)
            if exp:
                if exp['exp'] not in eds:
                    print(
                        'Expiry not a Valid Expiration,Here are the valid Expirations \n'
                    )
                    print(eds)
                    return np.nan, np.nan, np.nan

                expiry = exp['exp']
                eds = [expiry]
            else:
                print(
                    'Expiry not a Valid Expiration,Here are the valid Expirations \n'
                )
                print(eds)
                return np.nan, np.nan, np.nan

            ops = Option.in_chain(self.client, oc_id, expiration_dates=eds)
            ops = Option.mergein_marketdata_list(self.client, ops)
            df = pd.DataFrame(ops)
            df.index = np.arange(0, len(df))

            #calls = df.loc[df.type=='call']
            #calls = calls.sort_index()
            #puts = df.loc[df.type=='put']
            #puts = puts.sort_index()
            df['spot_price'] = spot_price
            #puts['spot_price'] = spot_price
            df = df.applymap(self.__convert_to_float)
            df['expiration_date'] = pd.to_datetime(
                df['expiration_date'].values)
            #puts  = puts.applymap(self.__convert_to_float)

            return df.fillna(0)

        except:
            return pd.DataFrame()
    def get_option_chain(self, symbol):
        """
        This returns the option chain id and an array of the expiration dates
        """

        md = StockMarketdata.quote_by_symbol(self.client, symbol)
        stock_id = md['instrument'].split('/')[-2]
        option_chain = OptionChain.fetch(self.client, stock_id, symbol)
        option_chain_id = option_chain["id"]
        expiration_dates = option_chain['expiration_dates']

        return option_chain_id, np.array(
            expiration_dates)  #.reshape(len(expiration_dates),-1)
    def get_all_options_robinhood(self, symbol):
        """Here, we use a library called 'DASK' to parallelize our code to fetch the data. We can be clever
        and fetch it all at once, to HOPEFULLY, speed up data retrieval for our larger datasets,
        like SPY and AMZN, to name a couple.
        from dask import delayed"""

        df_list = []
        stock = Stock.fetch(self.client, symbol)
        stock_id = stock["id"]
        expiries = OptionChain.fetch(self.client, stock_id,
                                     symbol=symbol)['expiration_dates']

        for expiration_date in expiries:
            #print(expiration_date)
            y = delayed(self.get_options_robinhood)(symbol,
                                                    exp=expiration_date)
            df_list.append(y)

        ans = delayed(pd.concat)(df_list)
        df = ans.compute()
        return df.loc[df.type == 'call'], df.loc[df.type ==
                                                 'put'], df.spot_price.iloc[0]
Пример #4
0
#
client = Client(auth_data)


#
# fetch the stock info for TLT
#
symbol = "TLT"
md = StockMarketdata.quote_by_symbol(client, symbol)


#
# get the TLT option chain info
#
stock_id = md["instrument"].split("/")[-2]
option_chain = OptionChain.fetch(client, stock_id, symbol)
option_chain_id = option_chain["id"]
expiration_dates = option_chain['expiration_dates']


#
# reduce the number of expiration dates we're interested in
#
next_3_expiration_dates = expiration_dates[0:3]


#
# get all options on the TLT option chain
#
ops = Option.in_chain(client, option_chain_id, expiration_dates=next_3_expiration_dates)
Пример #5
0
password = config['account']['password']

#
# initialize and authenticate Client
#
client = Client(username=username, password=password)
client.authenticate()

#
# fetch spy options
#
symbol = "SPY"
stock = Stock.fetch(client, symbol)
stock = Stock.mergein_marketdata_list(client, [stock])[0]

oc = OptionChain.fetch(client, stock["id"], symbol)
ed = oc['expiration_dates'][0]
ops = Option.in_chain(client, oc["id"], expiration_dates=[ed])

#
# enrich options with market data
#
ops = Option.mergein_marketdata_list(client, ops)

#
# genrate vertical spread table
#
width = 1
spread_type = "put"
spread_kind = "sell"
df = Vertical.gen_df(ops, width, spread_type, spread_kind)
Пример #6
0
 def get_option_chain(self, symbol, stock):
     stock_id = stock["instrument"].split("/")[-2]
     return OptionChain.fetch(self.client, stock_id, symbol)