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)
Esempio n. 2
0
    def __enter__(self):
        launch_service()

        self.conn = [iq.HistoryConn() for _ in range(self.num_connections)]
        for c in self.conn:
            c.connect()

        return self
Esempio n. 3
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))
Esempio n. 4
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))
Esempio n. 5
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))
Esempio n. 6
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')
Esempio n. 7
0
    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