def get_reference_data(): """Markets, SecTypes, Trade Conditions etc""" table_conn = iq.TableConn(name="pyiqfeed-Example-reference-data") table_listener = iq.VerboseIQFeedListener("Reference Data Listener") table_conn.add_listener(table_listener) with iq.ConnConnector([table_conn]) as connector: table_conn.update_tables() print("Markets:") print(table_conn.get_markets()) print("") print("Security Types:") print(table_conn.get_security_types()) print("") print("Trade Conditions:") print(table_conn.get_trade_conditions()) print("") print("SIC Codes:") print(table_conn.get_sic_codes()) print("") print("NAIC Codes:") print(table_conn.get_naic_codes()) print("") table_conn.remove_listener(table_listener)
def getOptionData(ticker: str): toreturn = "" lookup_conn = iq.LookupConn(name="pyiqfeed-Example-Eq-Option-Chain") lookup_listener = iq.VerboseIQFeedListener("EqOptionListener") lookup_conn.add_listener(lookup_listener) with iq.ConnConnector([lookup_conn]) as connector: # noinspection PyArgumentEqualDefault e_opt = lookup_conn.request_equity_option_chain( symbol=ticker, opt_type='pc', month_codes="".join(iq.LookupConn.call_month_letters + iq.LookupConn.put_month_letters), near_months=None, include_binary=True, filt_type=0, filt_val_1=None, filt_val_2=None) #print("Currently trading options for %s" % ticker) #print(e_opt) lookup_conn.remove_listener(lookup_listener) for i in e_opt['c'][:10]: fundamentals, summary = getQuoteData(i) toreturn = toreturn + str(summary) return toreturn
def get_news(): """Exercise NewsConn functionality""" news_conn = iq.NewsConn("pyiqfeed-example-News-Conn") news_listener = iq.VerboseIQFeedListener("NewsListener") news_conn.add_listener(news_listener) with iq.ConnConnector([news_conn]) as connector: cfg = news_conn.request_news_config() print("News Configuration:") print(cfg) print("") print("Latest 10 headlines:") headlines = news_conn.request_news_headlines(sources=[], symbols=[], date=None, limit=10) print(headlines) print("") story_id = headlines[0].story_id story = news_conn.request_news_story(story_id) print("Text of story with story id: %s:" % story_id) print(story.story) print("") today = datetime.date.today() week_ago = today - datetime.timedelta(days=7) counts = news_conn.request_story_counts( symbols=["AAPL", "IBM", "TSLA"], bgn_dt=week_ago, end_dt=today) print("Number of news stories in last week for AAPL, IBM and TSLA:") print(counts) print("")
def test_can_pull_data_from_specific_time_period(): results = pkgutil.get_data("tests.data", "SPY_20190311_to_20190318.txt").decode("utf-8") hist_conn = iq.HistoryConn(name="pyiqfeed-Example-historical-bars") hist_listener = iq.VerboseIQFeedListener("History Bar Listener") hist_conn.add_listener(hist_listener) with iq.ConnConnector([hist_conn]) as connector: data = hist_conn.request_bars_in_period( ticker="SPY", interval_len=14400, interval_type="s", bgn_prd=datetime.datetime(year=2019, month=3, day=11, hour=9, minute=30), end_prd=datetime.datetime(year=2019, month=3, day=18, hour=9, minute=30)) print(data) assert results == str(data)
def getQuoteData(ticker: str): """Get level 1 quotes and trades for ticker for seconds seconds.""" fundamentals = "" quote_conn = iq.QuoteConn(name="pyiqfeed-Example-lvl1") quote_listener = iq.VerboseQuoteListener("Level 1 Listener") quote_conn.add_listener(quote_listener) with iq.ConnConnector([quote_conn]) as connector: all_fields = sorted(list(iq.QuoteConn.quote_msg_map.keys())) quote_conn.select_update_fieldnames( ["Bid", "Bid Time", "Ask", "Ask Time"]) quote_conn.watch(ticker) t_end = time.time() + 5 * 60 while time.time() < t_end: fundamentals = quote_conn.fundamentals summary = quote_conn.summary if (fundamentals != "" and summary != ""): break quote_conn.unwatch(ticker) quote_conn.remove_listener(quote_listener) if (len(summary) > 0): summary = summary[0] if (len(summary) > 0): return fundamentals, summary[0] else: return ""
def get_live_interval_bars(ticker: str, bar_len: int, seconds: int): """Get real-time interval bars""" bar_conn = iq.BarConn(name='pyiqfeed-Example-interval-bars') bar_listener = iq.VerboseBarListener("Bar Listener") bar_conn.add_listener(bar_listener) with iq.ConnConnector([bar_conn]) as connector: bar_conn.watch(symbol=ticker, interval_len=bar_len, interval_type='s', update=1, lookback_bars=10) time.sleep(seconds)
def get_tickdata(ticker: str, max_ticks: int, num_days: int): """Show how to read tick-data""" hist_conn = iq.HistoryConn(name="pyiqfeed-Example-tickdata") hist_listener = iq.VerboseIQFeedListener("History Tick Listener") hist_conn.add_listener(hist_listener) # Look at conn.py for request_ticks, request_ticks_for_days and # request_ticks_in_period to see various ways to specify time periods # etc. with iq.ConnConnector([hist_conn]) as connector: # Get the last 10 trades try: tick_data = hist_conn.request_ticks(ticker=ticker, max_ticks=max_ticks) print(tick_data) # Get the last num_days days trades between 10AM and 12AM # Limit to max_ticks ticks else too much will be printed on screen bgn_flt = datetime.time(hour=10, minute=0, second=0) end_flt = datetime.time(hour=12, minute=0, second=0) tick_data = hist_conn.request_ticks_for_days( ticker=ticker, num_days=num_days, bgn_flt=bgn_flt, end_flt=end_flt, max_ticks=max_ticks, ) print(tick_data) # Get all ticks between 9:30AM 5 days ago and 9:30AM today # Limit to max_ticks since otherwise too much will be printed on # screen today = datetime.date.today() sdt = today - datetime.timedelta(days=5) start_tm = datetime.datetime(year=sdt.year, month=sdt.month, day=sdt.day, hour=9, minute=30) edt = today end_tm = datetime.datetime(year=edt.year, month=edt.month, day=edt.day, hour=9, minute=30) tick_data = hist_conn.request_ticks_in_period(ticker=ticker, bgn_prd=start_tm, end_prd=end_tm, max_ticks=max_ticks) print(tick_data) except (iq.NoDataError, iq.UnauthorizedError) as err: print("No data returned because {0}".format(err))
def get_regional_quotes(ticker: str, seconds: int): """Get level 1 quotes and trades for ticker for seconds seconds.""" quote_conn = iq.QuoteConn(name="pyiqfeed-Example-regional") quote_listener = iq.VerboseQuoteListener("Regional Listener") quote_conn.add_listener(quote_listener) with iq.ConnConnector([quote_conn]) as connector: quote_conn.regional_watch(ticker) time.sleep(seconds) quote_conn.regional_unwatch(ticker)
def get_trades_only(ticker: str, seconds: int): """Get level 1 quotes and trades for ticker for seconds seconds.""" quote_conn = iq.QuoteConn(name="pyiqfeed-Example-trades-only") quote_listener = iq.VerboseQuoteListener("Trades Listener") quote_conn.add_listener(quote_listener) with iq.ConnConnector([quote_conn]) as connector: quote_conn.trades_watch(ticker) time.sleep(seconds) quote_conn.unwatch(ticker)
def _start_listening(self): conn = iq.BarConn(name="{} - live, {} minutes interval".format( self.ticker, self.bars_interval)) conn.add_listener(self) with iq.ConnConnector([conn]) as connector: conn.watch(symbol=self.ticker, interval_len=self.bars_interval, interval_type=Instrument.INTERVAL_TYPE, update=self.bars_interval, lookback_bars=self.bars_to_load) while True: time.sleep(5)
def get_daily_data(ticker: str, num_days: int): """Historical Daily Data""" hist_conn = iq.HistoryConn(name="pyiqfeed-Example-daily-data") hist_listener = iq.VerboseIQFeedListener("History Bar Listener") hist_conn.add_listener(hist_listener) with iq.ConnConnector([hist_conn]) as connector: try: daily_data = hist_conn.request_daily_data(ticker, num_days) print(daily_data) except (iq.NoDataError, iq.UnauthorizedError) as err: print("No data returned because {0}".format(err))
def documents(): """Get level 1 quotes and trades for ticker for seconds seconds.""" quote_conn = iq.QuoteConn(name="pyiqfeed-Example-lvl1") quote_listener = iq.VerboseQuoteListener("Level 1 Listener") quote_conn = iq.QuoteConn(name="pyiqfeed-Example-lvl1") quote_conn.add_listener(quote_listener) with iq.ConnConnector([quote_conn]) as connector: all_fields = sorted(list(iq.QuoteConn.quote_msg_map.keys())) quote_conn.select_update_fieldnames(all_fields) quote_conn.watch(ticker) import time time.sleep(seconds)
def get_level_1_quotes_and_trades(ticker: str, seconds: int): """Get level 1 quotes and trades for ticker for seconds seconds.""" quote_conn = iq.QuoteConn(name="pyiqfeed-Example-lvl1") quote_listener = iq.VerboseQuoteListener("Level 1 Listener") quote_conn.add_listener(quote_listener) with iq.ConnConnector([quote_conn]) as connector: all_fields = sorted(list(iq.QuoteConn.quote_msg_map.keys())) quote_conn.select_update_fieldnames(all_fields) quote_conn.watch(ticker) time.sleep(seconds) quote_conn.unwatch(ticker) quote_conn.remove_listener(quote_listener)
def get_fund_summary(security_type: int, group_id: int, eod_date: datetime): hist_conn = iq.MarketSummaryConn(name="pyiqfeed-Example-mkt-summary-eod") hist_listener = iq.VerboseIQFeedListener("EOD Summary Listener") hist_conn.add_listener(hist_listener) with iq.ConnConnector([hist_conn]) as connector: try: daily_data = hist_conn.request_fundamental_summary( security_type, group_id, eod_date) print(daily_data.dtype.names) print(daily_data) print(f"shape={daily_data.shape}") except (iq.NoDataError, iq.UnauthorizedError) as err: print("No data returned because {0}".format(err))
def get_administrative_messages(seconds: int): """Run and AdminConn and print connection stats to screen.""" admin_conn = iq.AdminConn(name="pyiqfeed-Example-admin-messages") admin_listener = iq.VerboseAdminListener("Admin Listener") admin_conn.add_listener(admin_listener) with iq.ConnConnector([admin_conn]) as connector: admin_conn.set_admin_variables(product=dtn_product_id, login=dtn_login, password=dtn_password, autoconnect=True) admin_conn.client_stats_on() time.sleep(seconds)
def getFuturesData(ticker: str): lookup_conn = iq.LookupConn(name="pyiqfeed-Example-Futures-Chain") lookup_listener = iq.VerboseIQFeedListener("FuturesChainLookupListener") lookup_conn.add_listener(lookup_listener) with iq.ConnConnector([lookup_conn]) as connector: f_syms = lookup_conn.request_futures_chain( symbol=ticker, month_codes="".join(iq.LookupConn.futures_month_letters), years="67", near_months=None, timeout=None) print("Futures symbols with underlying %s" % ticker) print(f_syms) lookup_conn.remove_listener(lookup_listener)
def get_futures_options_chain(ticker: str): """Futures Option Chain""" lookup_conn = iq.LookupConn(name="pyiqfeed-Example-Futures-Options-Chain") lookup_listener = iq.VerboseIQFeedListener("FuturesOptionLookupListener") lookup_conn.add_listener(lookup_listener) with iq.ConnConnector([lookup_conn]) as connector: f_syms = lookup_conn.request_futures_option_chain( symbol=ticker, month_codes="".join(iq.LookupConn.futures_month_letters), years=''.join([str(x) for x in range(0, 10)]), near_months=None, timeout=None) print("Futures Option symbols with underlying %s" % ticker) print(f_syms) lookup_conn.remove_listener(lookup_listener)
def get_5ms_summary(security_type: int, group_id: int): """Historical Daily Data""" hist_conn = iq.MarketSummaryConn(name="pyiqfeed-Example-mkt-summary-5ms") hist_listener = iq.VerboseIQFeedListener("5ms Summary Listener") hist_conn.add_listener(hist_listener) with iq.ConnConnector([hist_conn]) as connector: try: daily_data = hist_conn.request_5min_summary( security_type, group_id) print(daily_data.dtype.names) print(daily_data) print(f"shape={daily_data.shape}") except (iq.NoDataError, iq.UnauthorizedError) as err: print("No data returned because {0}".format(err))
def get_historical_bar_data(ticker: str, bar_len: int, bar_unit: str, num_bars: int): """Shows how to get interval bars.""" hist_conn = iq.HistoryConn(name="pyiqfeed-Example-historical-bars") hist_listener = iq.VerboseIQFeedListener("History Bar Listener") hist_conn.add_listener(hist_listener) with iq.ConnConnector([hist_conn]) as connector: # look at conn.py for request_bars, request_bars_for_days and # request_bars_in_period for other ways to specify time periods etc try: bars = hist_conn.request_bars( ticker=ticker, interval_len=bar_len, interval_type=bar_unit, max_bars=num_bars, ) print(bars) today = datetime.date.today() start_date = today - datetime.timedelta(days=10) start_time = datetime.datetime( year=start_date.year, month=start_date.month, day=start_date.day, hour=0, minute=0, second=0, ) end_time = datetime.datetime( year=today.year, month=today.month, day=today.day, hour=23, minute=59, second=59, ) bars = hist_conn.request_bars_in_period( ticker=ticker, interval_len=bar_len, interval_type=bar_unit, bgn_prd=start_time, end_prd=end_time, ) print(bars) except (iq.NoDataError, iq.UnauthorizedError) as err: print("No data returned because {0}".format(err))
def get_equity_option_chain(ticker: str): """Equity Option Chains""" lookup_conn = iq.LookupConn(name="pyiqfeed-Example-Eq-Option-Chain") lookup_listener = iq.VerboseIQFeedListener("EqOptionListener") lookup_conn.add_listener(lookup_listener) with iq.ConnConnector([lookup_conn]) as connector: # noinspection PyArgumentEqualDefault e_opt = lookup_conn.request_equity_option_chain( symbol=ticker, opt_type='pc', month_codes="".join(iq.LookupConn.call_month_letters + iq.LookupConn.put_month_letters), near_months=None, include_binary=True, filt_type=0, filt_val_1=None, filt_val_2=None) print("Currently trading options for %s" % ticker) print(e_opt) lookup_conn.remove_listener(lookup_listener)
def get_ticker_lookups(ticker: str): """Lookup tickers.""" lookup_conn = iq.LookupConn(name="pyiqfeed-Example-Ticker-Lookups") lookup_listener = iq.VerboseIQFeedListener("TickerLookupListener") lookup_conn.add_listener(lookup_listener) with iq.ConnConnector([lookup_conn]) as connector: syms = lookup_conn.request_symbols_by_filter(search_term=ticker, search_field='s') print("Symbols with %s in them" % ticker) print(syms) print("") sic_symbols = lookup_conn.request_symbols_by_sic(83) print("Symbols in SIC 83:") print(sic_symbols) print("") naic_symbols = lookup_conn.request_symbols_by_naic(10) print("Symbols in NAIC 10:") print(naic_symbols) print("") lookup_conn.remove_listener(lookup_listener)
def run_forever(self, timeframe): """Start listening to IQFeed bars for the specified time frame.""" bar_conn = iq.BarConn(name="IQFeed Bar Conn %s" % bt.TimeFrame.getname(timeframe)) bar_listener = IQFeedBarListener( "IQFeed Bar listener %s" % bt.TimeFrame.getname(timeframe), self.queues[timeframe]) bar_conn.add_listener(bar_listener) watched = [] with iq.ConnConnector([bar_conn]) as connector: while not self.stopped: for data in self.datas: if data.p.timeframe == timeframe and id( data) not in watched: interval_type = 't' if data.p.timeframe == bt.TimeFrame.Ticks else 's' interval_len = { bt.TimeFrame.Ticks: 1, bt.TimeFrame.Seconds: 1, bt.TimeFrame.Minutes: 60 }.get(data.p.timeframe) bar_conn.watch(symbol=data.p.dataname, interval_len=interval_len * data.p.compression, interval_type=interval_type, bgn_bars=None, lookback_days=1, lookback_bars=None, update=0) watched.append(id(data)) time.sleep(0.1) for data in self.datas: if data.p.timeframe == timeframe: quote_conn.unwatch(data.p.dataname) bar_conn.remove_listener(bar_listener)
help="Launch IQFeed in a headless XServer.") parser.add_argument('--control_file', action='store', dest='ctrl_file', default="/tmp/stop_iqfeed.ctrl", help='Stop running if this file exists.') arguments = parser.parse_args() IQ_FEED = CustomFeedService(product=dtn_product_id, version="IQFEED_LAUNCHER", login=dtn_login, password=dtn_password) nohup = arguments.nohup headless = arguments.headless ctrl_file = arguments.ctrl_file IQ_FEED.launch(timeout=60, check_conn=True, headless=headless, nohup=nohup) # Modify code below to connect to the socket etc as described above admin_conn = iq.AdminConn(name="Launcher") admin_listener = CustomVerboseIQFeedListener("Launcher-listen") admin_conn.add_listener(admin_listener) with iq.ConnConnector([admin_conn]) as connected: admin_conn.client_stats_off() while not os.path.isfile(ctrl_file): admin_conn.client_stats_off() time.sleep(30) os.remove(ctrl_file)
def get_history(self, dataname, dtbegin, dtend, timeframe, compression): q = queue.Queue() if timeframe not in (bt.TimeFrame.Ticks, bt.TimeFrame.Seconds, bt.TimeFrame.Minutes, bt.TimeFrame.Days, bt.TimeFrame.Weeks, bt.TimeFrame.Months) or \ (timeframe in (bt.TimeFrame.Days, bt.TimeFrame.Weeks, bt.TimeFrame.Months) and \ compression != 1): err = "Unsupported timeframe/compression: %s/%s" % \ (bt.TimeFrame.getname(timeframe), compression) self.put_notification(err, (), {}) q.put({"error": err}) return q hist_conn = iq.HistoryConn(name="IQFeed Store History") with iq.ConnConnector([hist_conn]) as connector: try: if timeframe in (bt.TimeFrame.Ticks, bt.TimeFrame.Seconds, bt.TimeFrame.Minutes): interval_type = 't' if timeframe == bt.TimeFrame.Ticks else 's' interval_len = compression * 60 if timeframe == bt.TimeFrame.Minutes else compression bars = hist_conn.request_bars_in_period( ticker=dataname, interval_len=interval_len, interval_type=interval_type, bgn_prd=dtbegin, end_prd=dtend, ascend=True) elif timeframe == bt.TimeFrame.Days: bars = hist_conn.request_daily_data_for_dates( ticker=dataname, bgn_dt=dtbegin, end_dt=dtend, ascend=True) elif timeframe == bt.TimeFrame.Weeks: num_weeks = math.ceil((dtend - dtbegin).days / 7.0) bars = hist_conn.request_weekly_data(dataname, num_weeks, ascend=True) elif timeframe == bt.TimeFrame.Months: num_months = 12 * dtend.year + dtend.month - 12 * dtbegin.year - dtbegin.month bars = hist_conn.request_monthly_data(dataname, num_months, ascend=True) except (iq.NoDataError, iq.UnauthorizedError) as err: error = "Unable to get historical data. Error:" % err self.put_notification(error, (), {}) q.put({"error": error}) return q for bar in bars: if len(bar) == 7: dtime, opn, high, low, close, volume, oint = bar else: dt64, tm64, opn, high, low, close, volume, pvolume, oint = bar dtime = dt64 + tm64 q.put({ 'date': dtime.astype(datetime), 'open': opn, 'high': high, 'low': low, 'close': close, 'volume': volume, 'openinterest': oint }) q.put({}) # end of transmission return q
def documents(): with iq.ConnConnector([hist_conn]) as connector: # look at conn.py for request_bars, request_bars_for_days and # request_bars_in_period for other ways to specify time periods etc try: bars = hist_conn.request_bars(ticker=ticker, interval_len=bar_len, interval_type=bar_unit, max_bars=num_bars) ''' today = datetime.now() start_date = today - relativedelta(days=10) start_time = datetime(year=start_date.year, month=start_date.month, day=start_date.day, hour=0, minute=0, second=0) end_time = datetime(year=today.year, month=today.month, day=today.day, hour=23, minute=59, second=59) bars = hist_conn.request_bars_in_period(ticker=ticker, interval_len=bar_len, interval_type=bar_unit, bgn_prd=start_time, end_prd=end_time) print(bars) ''' for bar in bars: date = parse(str(bar[0])) timestamp = int(re.sub('\D', '', str(bar[1]))) sec = timestamp / 1000000 min = int(sec % 3600 / 60) hour = int(sec / 3600) sec = int(sec - hour * 3600 - min * 60) date = datetime(date.year, date.month, date.day, hour, min, sec) #print (ticker, date) frequency = bar_len feed = { 'instrument_id': instrument.id, 'frequency': frequency, 'date': date, 'high': float(bar[2]), 'low': float(bar[3]), 'open': float(bar[4]), 'close': float(bar[5]), 'volume': float(bar[6]) } print(ticker, date, timestamp, feed) bar_list = Feed.search().filter('term', date=date).filter( 'term', instrument_id=instrument.id).filter( 'term', frequency=frequency) if bar_list and bar_list.count() > 0: pass #print 'update', symbol mydoc = bar_list.execute()[0]._id yield es.update_op(doc=feed, id=mydoc, index='beginning', doc_type='feed', doc_as_upsert=True) else: pass #print 'insert', symbol yield es.index_op(feed) #print(bars) print(len(bars)) print("Last Bar Received") except (iq.NoDataError, iq.UnauthorizedError) as err: print("No data returned because {0}".format(err))
def callback(bar_data: np.array): print(bar_data['datetime'][0]) full_name = bar_data['symbol'][0] label_name = full_name.split('.')[0] + '_PRICE' text_field = getattr(ui, label_name) text_field.setText(str(bar_data['close_p'][0])) if __name__ == "__main__": # init listener bar_conn = iq.BarConn(name='test live interval bars') bar_listener = QuoteListener('customized bar listener', callback) bar_conn.add_listener(bar_listener) with iq.ConnConnector([bar_conn]) as conn: for ticker in tickers: print('watching {}'.format(ticker)) bar_conn.watch(symbol=ticker, interval_len=1 * 60, interval_type='s', update=1, lookback_bars=205) bar_conn.watch(symbol=ticker, interval_len=5 * 60, interval_type='s', update=1, lookback_bars=205) bar_conn.watch(symbol=ticker, interval_len=15 * 60, interval_type='s',