Exemplo n.º 1
0
    def getNextBars(self):
        ret = None

        if len(self.__barDicts):
            ret = bar.Bars(self.__barDicts.pop(0))

        return ret
Exemplo n.º 2
0
    def doGetHistory(self):
        while not self.stopped():
            endTimestamp = dt.datetime_to_timestamp(self.__nextBarClose)
            self.__updateNextBarClose()
            dicts = {}

            try:
                for indentifier in self.__identifiers:
                    response = api.getKLineBar(
                        indentifier, endTimestamp - self.__frequency * 2,
                        self.__period, 100)
                    if response is None:
                        raise Exception("getKLineBar return None!")
                    dicts[indentifier] = response
                break
            except BaseException:
                time.sleep(1)
                continue

        while not self.stopped():
            barDict = {}
            for indentifier in self.__identifiers:
                response = dicts[indentifier]
                if len(response) == 0:
                    break
                barDict[indentifier] = liveBar(response.pop(-1),
                                               self.__frequency)

            if len(barDict) == 0:
                break
            bars = bar.Bars(barDict)
            self.__queue.put((GetBarThread.ON_HISTORY_BARS, bars))
Exemplo n.º 3
0
    def getGrouped(self):
        bar_dict = {}

        for instrument, grouper in list(self.__barGroupers.items()):
            bar_dict[instrument] = grouper.getGrouped()

        return bar.Bars(bar_dict)
Exemplo n.º 4
0
    def testBaseBarFeedNoAdjClose(self):
        bars = [
            bar.Bars({
                "orcl":
                bar.BasicBar(datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1,
                             None, bar.Frequency.DAY)
            }),
            bar.Bars({
                "orcl":
                bar.BasicBar(datetime.datetime(2001, 1, 2), 1, 1, 1, 1, 1,
                             None, bar.Frequency.DAY)
            }),
        ]

        barFeed = barfeed.OptimizerBarFeed(bar.Frequency.DAY, ["orcl"], bars)
        check_base_barfeed(self, barFeed, False)
Exemplo n.º 5
0
    def testInvalidDateTimes(self):
        b1 = bar.BasicBar(datetime.datetime.now(), 2, 3, 1, 2.1, 10, 5,
                          bar.Frequency.DAY)
        b2 = bar.BasicBar(datetime.datetime.now() + datetime.timedelta(days=1),
                          2, 3, 1, 2.1, 10, 5, bar.Frequency.DAY)

        with self.assertRaises(Exception):
            bar.Bars({"a": b1, "b": b2})
Exemplo n.º 6
0
    def __fill_bars(self, bars_dict):
        for index, value in enumerate(bars_dict[self.__identifiers[0]]):
            bar_dict = dict()
            for identifier in self.__identifiers:
                if bars_dict[identifier][index] is not None:
                    bar_dict[identifier] = bars_dict[identifier][index]

            if len(bar_dict):
                bars = bar.Bars(bar_dict)
                self.__queue.put((LiveFeedThread.ON_BARS, bars))
Exemplo n.º 7
0
    def testDateTimesNotInOrder(self):
        bars = [
            bar.Bars({
                "orcl":
                bar.BasicBar(datetime.datetime(2001, 1, 2), 1, 1, 1, 1, 1, 1,
                             bar.Frequency.DAY)
            }),
            bar.Bars({
                "orcl":
                bar.BasicBar(datetime.datetime(2001, 1, 1), 1, 1, 1, 1, 1, 1,
                             bar.Frequency.DAY)
            }),
        ]

        f = barfeed.OptimizerBarFeed(bar.Frequency.DAY, ["orcl"], bars)

        with self.assertRaisesRegex(Exception,
                                    "Bar date times are not in order.*"):
            for dt, b in f:
                pass
Exemplo n.º 8
0
    def testBasic(self):
        dt = datetime.datetime.now()
        b1 = bar.BasicBar(dt, 1, 1, 1, 1, 10, 1, bar.Frequency.DAY)
        b2 = bar.BasicBar(dt, 2, 2, 2, 2, 10, 2, bar.Frequency.DAY)

        bars = bar.Bars({"a": b1, "b": b2})

        self.assertEqual(bars["a"].getClose(), 1)
        self.assertEqual(bars["b"].getClose(), 2)
        self.assertTrue("a" in bars)

        self.assertEqual(list(bars.items()), [("a", b1), ("b", b2)])
        self.assertEqual(list(bars.keys()), ["a", "b"])
        self.assertEqual(bars.getInstruments(), ["a", "b"])
        self.assertEqual(bars.getDateTime(), dt)
        self.assertEqual(bars.getBar("a").getClose(), 1)
Exemplo n.º 9
0
    def doCall(self):
        endDateTime = self.__nextBarClose
        self.__updateNextBarClose()
        bar_dict = {}

        for identifier in self._identifiers:
            try:
                if not self._tickDSDict[identifier].empty():
                    bar_dict[identifier] = build_bar(
                        to_market_datetime(endDateTime),
                        self._tickDSDict[identifier])
            except Exception as e:
                logger.error(e)

        if len(bar_dict):
            bars = bar.Bars(bar_dict)
            self.__queue.put((LiveFeedThread.ON_BARS, bars))
Exemplo n.º 10
0
    def doCall(self):
        endTimestamp = dt.datetime_to_timestamp(self.__nextBarClose)
        barDict = {}

        for indentifier in self.__identifiers:
            try:
                response = api.getKLineBar(indentifier,
                                           endTimestamp - self.__frequency,
                                           self.__period)
                if response is None:
                    raise Exception("getKLineBar return None!")
                # logger.debug(response)
                barDict[indentifier] = liveBar(response[-1], self.__frequency)
            except BaseException:
                time.sleep(1)
                return False

        if len(barDict):
            bars = bar.Bars(barDict)
            self.__queue.put((GetBarThread.ON_BARS, bars))
            self.__updateNextBarClose()
            return True
        return False
Exemplo n.º 11
0
    def doCall(self):
        endDateTime = self.__nextBarClose
        barDict = {}

        self.__updateNextBarClose()

        for indentifier in self.__identifiers:
            try:
                logger.debug(
                    "Requesting bars with precision {} and period {} for {}".
                    format(self.__precision, self.__period, indentifier))
                response = api.XigniteGlobalRealTime_GetBar(
                    self.__apiToken, indentifier, "Symbol", endDateTime,
                    self.__precision, self.__period)
                # logger.debug(response)
                barDict[indentifier] = build_bar(response["Bar"], indentifier,
                                                 self.__frequency)
            except api.XigniteError as e:
                logger.error(e)

        if len(barDict):
            bars = bar.Bars(barDict)
            self.__queue.put((GetBarThread.ON_BARS, bars))
Exemplo n.º 12
0
    def getNextBars(self):
        # All bars must have the same datetime. We will return all the ones with the smallest datetime.
        smallestDateTime = self.peekDateTime()

        if smallestDateTime is None:
            return None

        # Make a second pass to get all the bars that had the smallest datetime.
        ret = {}

        for instrument, bars in self.__bars.items():
            nextPos = self.__nextPos[instrument]

            if nextPos < len(bars) and bars[nextPos].getDateTime(
            ) == smallestDateTime:
                ret[instrument] = bars[nextPos]
                self.__nextPos[instrument] += 1

        if self.__currDateTime == smallestDateTime:
            raise Exception("Duplicate bars found for %s on %s" %
                            (ret.keys(), smallestDateTime))

        self.__currDateTime = smallestDateTime
        return bar.Bars(ret)
Exemplo n.º 13
0
 def testEmptyDict(self):
     with self.assertRaises(Exception):
         bar.Bars({})
Exemplo n.º 14
0
    def getNextBars(self):
        if len(self.__bars):
            return bar.Bars(self.__bars.pop(0))

        return None