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
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)
# with open("fast_arrow_auth.json") as f: auth_data = json.loads(f.read()) # # initialize client with auth_data # 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]
# with open("fast_arrow_auth.json") as f: auth_data = json.loads(f.read()) # # initialize client with auth_data # client = Client(auth_data) # # fetch stock positions # symbols = ["AAPL", "MU"] span = "week" bounds = "regular" data = StockMarketdata.historical_quote_by_symbols(client, symbols, span, bounds) if len(data) > 0: filename = "examples/data_{}.csv".format("_".join(symbols)) csv_columns = ['open_price', 'high_price', 'low_price', 'close_price'] num_rows = len(data[0]['historicals']) with open(filename, 'w') as csv_file: writer = csv.writer(csv_file) headers = ['begins_at'] for sym in symbols: for col in csv_columns: headers.append('{}_{}'.format(sym, col)) writer.writerow(headers)
# initialize and authenticate Client # client = Client(username=username, password=password) client.authenticate() def print_symbol_price(marketdata): msg = "Symbol = {}. Ask price = {}. Bid price = {}".format( marketdata['symbol'], marketdata['ask_price'], marketdata['bid_price']) print(msg) # # fetch by single stock symbol # symbol = "AAPL" md = StockMarketdata.quote_by_symbol(client, symbol) print_symbol_price(md) # # fetch by multiple stock symbols # symbols = ['AAPL', 'MU', 'FB'] mds = StockMarketdata.quote_by_symbols(client, symbols) for md in mds: print_symbol_price(md) # # fetch by single 'instrument_id' # symbol = 'AAPL' md = StockMarketdata.quote_by_symbol(client, symbol)
def get_quote(self, symbol): return StockMarketdata.quote_by_symbol(self.client, symbol)
# # get the authentication configs # config_file = "config.debug.ini" 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 stock positions # symbol = "AAPL" data = StockMarketdata.historical(client, symbol) if len(data["historicals"]) > 0: csv_columns = data["historicals"][0].keys() filename = "examples/data_{}.csv".format(symbol) with open(filename, 'w') as csv_file: writer = csv.DictWriter(csv_file, fieldnames=csv_columns) writer.writeheader() for rec in data["historicals"]: writer.writerow(rec)
def retrieve_current_price(client, ticker): from fast_arrow import StockMarketdata stock = StockMarketdata.quote_by_symbol(client, ticker) stock_price = round( (float(stock['ask_price']) + float(stock['bid_price'])) / 2, 2) return stock_price