Ejemplo n.º 1
0
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("")
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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))
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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))
Ejemplo n.º 9
0
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))
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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))
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
def get_historical_bar_to_db(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)
    """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)

    Instrument.init()
    Feed.init()
    symbol = ticker.upper()
    instrument_list = Instrument.search().filter('term', **{
        'sym.raw': symbol
    }).execute()
    if instrument_list and len(instrument_list) > 0:
        instrument = instrument_list[0]
    else:
        instrument = Instrument()
        instrument.sym = symbol
        instrument.save()

    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))

    for chunk in bulk_chunks(documents(),
                             docs_per_chunk=500,
                             bytes_per_chunk=10000):
        # We specify a default index and doc type here so we don't
        # have to repeat them in every operation:
        es.bulk(chunk, doc_type='feed', index='beginning')