コード例 #1
0
    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()
コード例 #2
0
    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
コード例 #3
0
ファイル: test_stock.py プロジェクト: yumikohey/fast_arrow
    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))
コード例 #4
0
    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]
コード例 #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
コード例 #6
0
#
# get auth_data (see https://github.com/westonplatter/fast_arrow_auth)
#
with open("fast_arrow_auth.json") as f:
    auth_data = json.loads(f.read())

#
# initialize client with auth_data
#
client = Client(auth_data)

#
# fetch spy options
#
symbol = "SPY"
stock = Stock.fetch(client, symbol)

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

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

#
# programmtically generate legs for IronCondor
#
width = 1
put_inner_lte_delta = -0.2
コード例 #7
0
import configparser
import json
from fast_arrow import (Client, StockMarketdata, Stock)

print("----- running {}".format(__file__))

#
# get auth_data (see https://github.com/westonplatter/fast_arrow_auth)
#
with open("fast_arrow_auth.json") as f:
    auth_data = json.loads(f.read())

#
# initialize client with auth_data
#
client = Client(auth_data)

#
# fetch option_positions
#
symbols = ["AAPL", "SQ", "USO"]

for symbol in symbols:
    md = StockMarketdata.quote_by_symbol(client, symbol)
    stock_id = md["instrument"].split("/")[-2]
    data = Stock.popularity(client, [stock_id])[0]
    num_open_positions = data["num_open_positions"]
    print(f"{symbol} has {num_open_positions} open positions")
コード例 #8
0
 def test_fetch(self):
     client = gen_client()
     symbol = "TLT"
     with self.assertRaises(ApiDoesNotSupportError):
         Stock.fetch(client, symbol)
コード例 #9
0
 def test_all(self):
     client = gen_client()
     symbols = ["TLT", "USO"]
     with self.assertRaises(ApiDoesNotSupportError):
         Stock.all(client, symbols)