Exemple #1
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)
Exemple #2
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})
Exemple #3
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.assertRaisesRegexp(Exception,
                                  "Bar date times are not in order.*"):
         for dt, b in f:
             pass
Exemple #4
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.assertEquals(bars["a"].getClose(), 1)
     self.assertEquals(bars["b"].getClose(), 2)
     self.assertTrue("a" in bars)
     self.assertEquals(bars.items(), [("a", b1), ("b", b2)])
     self.assertEquals(bars.keys(), ["a", "b"])
     self.assertEquals(bars.getInstruments(), ["a", "b"])
     self.assertEquals(bars.getDateTime(), dt)
     self.assertEquals(bars.getBar("a").getClose(), 1)
Exemple #5
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.iteritems():
            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)
Exemple #6
0
 def getGrouped(self):
     bar_dict = {}
     for instrument, grouper in self.__barGroupers.items():
         bar_dict[instrument] = grouper.getGrouped()
     return bar.Bars(bar_dict)
Exemple #7
0
 def testEmptyDict(self):
     with self.assertRaises(Exception):
         bar.Bars({})
Exemple #8
0
 def getNextBars(self):
     ret = None
     if len(self.__barDicts):
         ret = bar.Bars(self.__barDicts.pop(0))
     return ret
Exemple #9
0
class GetBarThread(PollingThread):

    # Events
    ON_BARS = 1

    def __init__(self, queue, apiToken, identifiers, frequency, apiCallDelay):
        PollingThread.__init__(self)

        # Map frequency to precision and period.
        if frequency < bar.Frequency.MINUTE:
            raise Exception(
                "Frequency must be greater than or equal to bar.Frequency.MINUTE"
            )
        elif frequency < bar.Frequency.HOUR:
            self.__precision = "Minutes"
            self.__period = frequency / bar.Frequency.MINUTE
        elif frequency < bar.Frequency.DAY:
            self.__precision = "Hours"
            self.__period = frequency / bar.Frequency.HOUR
        else:
            raise Exception("Frequency must be less than bar.Frequency.DAY")

        self.__queue = queue
        self.__apiToken = apiToken
        self.__identifiers = identifiers
        self.__frequency = frequency
        self.__nextBarClose = None
        # The delay between the bar's close and the API call.
        self.__apiCallDelay = apiCallDelay

        self.__updateNextBarClose()

    def __updateNextBarClose(self):
        self.__nextBarClose = resamplebase.build_range(
            utcnow(), self.__frequency).getEnding()

    def getNextCallDateTime(self):
        return self.__nextBarClose + self.__apiCallDelay

    def doCall(self):
        endDateTime = self.__nextBarClose
        self.__updateNextBarClose()
        barDict = {}

        for indentifier in self.__identifiers:
            try:
                logger.debug(
                    "Requesting bars with precision %s and period %s for %s" %
                    (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, e:
                logger.error(e)

        if len(barDict):
            bars = bar.Bars(barDict)
            self.__queue.put((GetBarThread.ON_BARS, bars))