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_spot_price(self, symbol):
        """Returns a dictionary of data about the spot price,
        which includes bid, ask, bid size, ask size, as well as some api identification"""

        stock1 = Stock.fetch(self.client, symbol)
        stock = StockMarketdata.quote_by_instrument(self.client,
                                                    _id=stock1['id'])

        return stock
Exemplo n.º 3
0
    def test_fetch_fields(self):
        client = gen_client()
        symbol = "TLT"
        with gen_vcr().use_cassette('stock_fetch.yaml'):
            stock = Stock.fetch(client, symbol)

            expected_fields = [
                'margin_initial_ratio', 'rhs_tradability', 'id', 'market',
                'simple_name', 'min_tick_size', 'maintenance_ratio',
                'tradability', 'state', 'type', 'tradeable', 'fundamentals',
                'quote', 'symbol', 'day_trade_ratio', 'name',
                'tradable_chain_id', 'splits', 'url', 'country',
                'bloomberg_unique', 'list_date'
            ]

            actual_fields = list(stock.keys())

            assert (set(expected_fields) == set(actual_fields))
    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]
Exemplo n.º 5
0
config = configparser.ConfigParser()
config.read(config_file)
username = config['account']['username']
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
Exemplo n.º 6
0
 def test_fetch(self):
     client = gen_client()
     symbol = "TLT"
     with self.assertRaises(ApiDoesNotSupportError):
         Stock.fetch(client, symbol)