Esempio n. 1
0
    def testResample(self):
        barDs = bards.BarDataSeries()
        resampledDS = resampled_ds.ResampledDataSeries(barDs.getCloseDataSeries(), bar.Frequency.MINUTE, sum)
        resampledBarDS = resampled_ds.ResampledBarDataSeries(barDs, bar.Frequency.MINUTE)

        barDs.append(bar.BasicBar(datetime.datetime(2011, 1, 1, 1, 1, 1), 2.1, 3, 1, 2, 10, 1, bar.Frequency.SECOND))
        barDs.append(bar.BasicBar(datetime.datetime(2011, 1, 1, 1, 1, 2), 2, 3, 1, 2.3, 10, 2, bar.Frequency.SECOND))
        barDs.append(bar.BasicBar(datetime.datetime(2011, 1, 1, 1, 2, 1), 2, 3, 1, 2, 10, 2, bar.Frequency.SECOND))

        self.assertEqual(len(resampledBarDS), 1)
        self.assertEqual(resampledBarDS[0].getDateTime(), datetime.datetime(2011, 1, 1, 1, 1))
        self.assertEqual(resampledBarDS[0].getOpen(), 2.1)
        self.assertEqual(resampledBarDS[0].getHigh(), 3)
        self.assertEqual(resampledBarDS[0].getLow(), 1)
        self.assertEqual(resampledBarDS[0].getClose(), 2.3)
        self.assertEqual(resampledBarDS[0].getVolume(), 20)
        self.assertEqual(resampledBarDS[0].getAdjClose(), 2)
        self.assertEqual(resampledDS[-1], 2 + 2.3)

        resampledBarDS.pushLast()
        self.assertEqual(len(resampledBarDS), 2)
        self.assertEqual(resampledBarDS[1].getDateTime(), datetime.datetime(2011, 1, 1, 1, 2))
        self.assertEqual(resampledBarDS[1].getOpen(), 2)
        self.assertEqual(resampledBarDS[1].getHigh(), 3)
        self.assertEqual(resampledBarDS[1].getLow(), 1)
        self.assertEqual(resampledBarDS[1].getClose(), 2)
        self.assertEqual(resampledBarDS[1].getVolume(), 10)
        self.assertEqual(resampledBarDS[1].getAdjClose(), 2)

        resampledDS.pushLast()
        self.assertEqual(resampledDS[1], 2)
Esempio n. 2
0
 def testEmpty(self):
     ds = bards.BarDataSeries()
     with self.assertRaises(IndexError):
         ds[-1]
     with self.assertRaises(IndexError):
         ds[0]
     with self.assertRaises(IndexError):
         ds[1000]
Esempio n. 3
0
    def testZeroDivision(self):
        highPrices = [1, 1, 1]
        lowPrices = [1, 1, 1]
        closePrices = [1, 1, 1]

        barDS = bards.BarDataSeries()
        stochFilter = stoch.StochasticOscillator(barDS, 2, 2)
        self.__fillBarDataSeries(barDS, closePrices, highPrices, lowPrices)
        self.assertEqual(stochFilter[-1], 0)
Esempio n. 4
0
    def testNonEmpty(self):
        ds = bards.BarDataSeries()
        for i in xrange(10):
            ds.append(
                bar.BasicBar(
                    datetime.datetime.now() + datetime.timedelta(seconds=i), 0,
                    0, 0, 0, 0, 0, bar.Frequency.SECOND))

        for i in xrange(0, 10):
            self.assertTrue(ds[i].getOpen() == 0)
Esempio n. 5
0
    def testDateTimes(self):
        ds = bards.BarDataSeries()
        firstDt = datetime.datetime.now()
        for i in xrange(10):
            ds.append(
                bar.BasicBar(firstDt + datetime.timedelta(seconds=i), 2, 4, 1,
                             3, 10, 3, bar.Frequency.SECOND))

        for i in xrange(10):
            self.assertEqual(ds[i].getDateTime(), ds.getDateTimes()[i])
            self.assertEqual(ds.getDateTimes()[i],
                             firstDt + datetime.timedelta(seconds=i))
Esempio n. 6
0
    def __loadSarTestBarDs(self):
        seconds = 0

        ret = bards.BarDataSeries()
        for i in xrange(len(SAR_HIGH)):
            dateTime = datetime.datetime.now() + datetime.timedelta(
                seconds=seconds)
            ret.append(
                bar.BasicBar(dateTime, SAR_LOW[i], SAR_HIGH[i], SAR_LOW[i],
                             SAR_HIGH[i], 0, SAR_LOW[i], bar.Frequency.DAY))
            seconds += 1
        return ret
Esempio n. 7
0
    def __loadBarDS(self):
        seconds = 0

        ret = bards.BarDataSeries()
        for i in xrange(len(OPEN_VALUES)):
            dateTime = datetime.datetime.now() + datetime.timedelta(
                seconds=seconds)
            ret.append(
                bar.BasicBar(dateTime, OPEN_VALUES[i], HIGH_VALUES[i],
                             LOW_VALUES[i], CLOSE_VALUES[i], VOLUME_VALUES[i],
                             CLOSE_VALUES[i], bar.Frequency.DAY))
            seconds += 1
        return ret
    def testBlackWhiteReversal(self):
        bds = bards.BarDataSeries()
        lb = linebreak.LineBreak(bds, 2)
        bds.append(bar.BasicBar(datetime.datetime(2008, 3, 5), 10, 12, 8, 9, 1, None, bar.Frequency.DAY))
        self.assertEqual(len(lb), 1)
        bds.append(bar.BasicBar(datetime.datetime(2008, 3, 6), 9, 12, 9, 12, 1, None, bar.Frequency.DAY))
        self.assertEqual(len(lb), 1)
        self.assertEqual(lb[-1].isBlack(), True)
        self.assertEqual(lb[-1].getDateTime(), datetime.datetime(2008, 3, 5))

        bds.append(bar.BasicBar(datetime.datetime(2008, 3, 7), 9, 13, 5, 13, 1, None, bar.Frequency.DAY))
        self.assertEqual(len(lb), 2)
        self.assertEqual(lb[-1].isWhite(), True)
        self.assertEqual(lb[-1].getDateTime(), datetime.datetime(2008, 3, 7))
Esempio n. 9
0
    def testSeqLikeOps(self):
        seq = []
        ds = bards.BarDataSeries()
        for i in xrange(10):
            bar_ = bar.BasicBar(
                datetime.datetime.now() + datetime.timedelta(seconds=i), 2, 4,
                1, 3, 10, 3, bar.Frequency.SECOND)
            ds.append(bar_)
            seq.append(bar_)

        self.assertEqual(ds[-1], seq[-1])
        self.assertEqual(ds[-2], seq[-2])
        self.assertEqual(ds[0], seq[0])
        self.assertEqual(ds[1], seq[1])
        self.assertEqual(ds[-2:][-1], seq[-2:][-1])
Esempio n. 10
0
    def testNestedDataSeries(self):
        ds = bards.BarDataSeries()
        for i in xrange(10):
            ds.append(
                bar.BasicBar(
                    datetime.datetime.now() + datetime.timedelta(seconds=i), 2,
                    4, 1, 3, 10, 3, bar.Frequency.SECOND))

        self.__testGetValue(ds.getOpenDataSeries(), 10, 2)
        self.__testGetValue(ds.getCloseDataSeries(), 10, 3)
        self.__testGetValue(ds.getHighDataSeries(), 10, 4)
        self.__testGetValue(ds.getLowDataSeries(), 10, 1)
        self.__testGetValue(ds.getVolumeDataSeries(), 10, 10)
        self.__testGetValue(ds.getAdjCloseDataSeries(), 10, 3)
        self.__testGetValue(ds.getPriceDataSeries(), 10, 3)
Esempio n. 11
0
 def testAppendInvalidDatetime(self):
     ds = bards.BarDataSeries()
     for i in xrange(10):
         now = datetime.datetime.now() + datetime.timedelta(seconds=i)
         ds.append(bar.BasicBar(now, 0, 0, 0, 0, 0, 0,
                                bar.Frequency.SECOND))
         # Adding the same datetime twice should fail
         with self.assertRaises(Exception):
             ds.append(
                 bar.BasicBar(now, 0, 0, 0, 0, 0, 0, bar.Frequency.SECOND))
         # Adding a previous datetime should fail
         with self.assertRaises(Exception):
             ds.append(
                 bar.BasicBar(now - datetime.timedelta(seconds=i), 0, 0, 0,
                              0, 0, 0, bar.Frequency.SECOND))
Esempio n. 12
0
    def testStockChartsStoch(self):
        # Test data from http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:stochastic_oscillato
        highPrices = [
            127.0090, 127.6159, 126.5911, 127.3472, 128.1730, 128.4317,
            127.3671, 126.4220, 126.8995, 126.8498, 125.6460, 125.7156,
            127.1582, 127.7154, 127.6855, 128.2228, 128.2725, 128.0934,
            128.2725, 127.7353, 128.7700, 129.2873, 130.0633, 129.1182,
            129.2873, 128.4715, 128.0934, 128.6506, 129.1381, 128.6406
        ]
        lowPrices = [
            125.3574, 126.1633, 124.9296, 126.0937, 126.8199, 126.4817,
            126.0340, 124.8301, 126.3921, 125.7156, 124.5615, 124.5715,
            125.0689, 126.8597, 126.6309, 126.8001, 126.7105, 126.8001,
            126.1335, 125.9245, 126.9891, 127.8148, 128.4715, 128.0641,
            127.6059, 127.5960, 126.9990, 126.8995, 127.4865, 127.3970
        ]
        closePrices = lowPrices[:
                                13]  # To keep initial close prince between low/high
        closePrices.extend([
            127.2876, 127.1781, 128.0138, 127.1085, 127.7253, 127.0587,
            127.3273, 128.7103, 127.8745, 128.5809, 128.6008, 127.9342,
            128.1133, 127.5960, 127.5960, 128.6904, 128.2725
        ])
        kValues = [
            None, None, None, None, None, None, None, None, None, None, None,
            None, None, 70.4382, 67.6089, 89.2021, 65.8106, 81.7477, 64.5238,
            74.5298, 98.5814, 70.1045, 73.0561, 73.4178, 61.2313, 60.9563,
            40.3861, 40.3861, 66.8285, 56.7314
        ]
        dValues = [
            None, None, None, None, None, None, None, None, None, None, None,
            None, None, None, None, 75.7497, 74.2072, 78.9201, 70.6940,
            73.6004, 79.2117, 81.0719, 80.5807, 72.1928, 69.2351, 65.2018,
            54.1912, 47.2428, 49.2003, 54.6487
        ]

        barDS = bards.BarDataSeries()
        stochFilter = stoch.StochasticOscillator(barDS, 14)
        self.__fillBarDataSeries(barDS, closePrices, highPrices, lowPrices)

        for i in range(len(kValues)):
            self.assertTrue(values_equal(stochFilter[i], kValues[i]))
            self.assertTrue(values_equal(stochFilter.getD()[i], dValues[i]))

        self.assertEqual(len(stochFilter.getDateTimes()), len(closePrices))
        for i in range(len(stochFilter)):
            self.assertNotEqual(stochFilter.getDateTimes()[i], None)
Esempio n. 13
0
    def testCheckNow(self):
        barDs = bards.BarDataSeries()
        resampledBarDS = resampled_ds.ResampledBarDataSeries(barDs, bar.Frequency.MINUTE)

        barDateTime = datetime.datetime(2014, 7, 7, 22, 46, 28, 10000)
        barDs.append(bar.BasicBar(barDateTime, 2.1, 3, 1, 2, 10, 1, bar.Frequency.MINUTE))
        self.assertEqual(len(resampledBarDS), 0)

        resampledBarDS.checkNow(barDateTime + datetime.timedelta(minutes=1))
        self.assertEqual(len(resampledBarDS), 1)
        self.assertEqual(barDs[0].getOpen(), resampledBarDS[0].getOpen())
        self.assertEqual(barDs[0].getHigh(), resampledBarDS[0].getHigh())
        self.assertEqual(barDs[0].getLow(), resampledBarDS[0].getLow())
        self.assertEqual(barDs[0].getClose(), resampledBarDS[0].getClose())
        self.assertEqual(barDs[0].getVolume(), resampledBarDS[0].getVolume())
        self.assertEqual(barDs[0].getAdjClose(), resampledBarDS[0].getAdjClose())
        self.assertEqual(resampledBarDS[0].getDateTime(), datetime.datetime(2014, 7, 7, 22, 46))
Esempio n. 14
0
    def testShortPeriod(self):
        highPrices = [3, 3, 3]
        lowPrices = [1, 1, 1]
        closePrices = [2, 2, 3]

        barDS = bards.BarDataSeries()
        stochFilter = stoch.StochasticOscillator(barDS, 2, 2)
        self.__fillBarDataSeries(barDS, closePrices, highPrices, lowPrices)

        self.assertTrue(values_equal(stochFilter[0], None))
        self.assertTrue(values_equal(stochFilter[1], 50))
        self.assertTrue(values_equal(stochFilter[2], 100))

        self.assertTrue(values_equal(stochFilter.getD()[0], None))
        self.assertTrue(values_equal(stochFilter.getD()[1], None))
        self.assertTrue(values_equal(stochFilter.getD()[2], 75))

        self.assertEqual(len(stochFilter.getDateTimes()), len(closePrices))
        for i in range(len(stochFilter)):
            self.assertNotEqual(stochFilter.getDateTimes()[i], None)
Esempio n. 15
0
    def testStockChartsATRAdjusted(self):
        # Test data from http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:average_true_range_a
        high = [
            48.70, 48.72, 48.90, 48.87, 48.82, 49.05, 49.20, 49.35, 49.92,
            50.19, 50.12, 49.66, 49.88, 50.19, 50.36, 50.57, 50.65, 50.43,
            49.63, 50.33, 50.29, 50.17, 49.32, 48.50, 48.32, 46.80, 47.80,
            48.39, 48.66, 48.79
        ]
        low = [
            47.790, 48.140, 48.390, 48.370, 48.240, 48.635, 48.940, 48.860,
            49.500, 49.870, 49.200, 48.900, 49.430, 49.725, 49.260, 50.090,
            50.300, 49.210, 48.980, 49.610, 49.200, 49.430, 48.080, 47.640,
            41.550, 44.283, 47.310, 47.200, 47.900, 47.730
        ]
        close = [
            48.160, 48.610, 48.750, 48.630, 48.740, 49.030, 49.070, 49.320,
            49.910, 50.130, 49.530, 49.500, 49.750, 50.030, 50.310, 50.520,
            50.410, 49.340, 49.370, 50.230, 49.238, 49.930, 48.430, 48.180,
            46.570, 45.410, 47.770, 47.720, 48.620, 47.850
        ]
        expected = [
            None, None, None, None, None, None, None, None, None, None, None,
            None, None, 0.555000, 0.593929, 0.585791, 0.568949, 0.615452,
            0.617920, 0.642354, 0.674329, 0.692770, 0.775429, 0.781470,
            1.209229, 1.302620, 1.380290, 1.366698, 1.336219, 1.316482
        ]

        # Build a bar dataseries using the test data.
        barDataSeries = bards.BarDataSeries()
        atrDS = atr.ATR(barDataSeries, 14, True)
        now = datetime.datetime(2000, 1, 1)
        for i in xrange(len(high)):
            b = bar.BasicBar(now + datetime.timedelta(days=i), close[i],
                             high[i], low[i], close[i], 100, close[i] / 2,
                             bar.Frequency.DAY)
            barDataSeries.append(b)
            if expected[i] is None:
                self.assertEqual(atrDS[-1], None)
            else:
                self.assertEqual(common.safe_round(atrDS[-1], 2),
                                 round(expected[i] / 2, 2))
Esempio n. 16
0
    def testStockChartsStoch_Bounded(self):
        # Test data from http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:stochastic_oscillato
        highPrices = [
            127.0090, 127.6159, 126.5911, 127.3472, 128.1730, 128.4317,
            127.3671, 126.4220, 126.8995, 126.8498, 125.6460, 125.7156,
            127.1582, 127.7154, 127.6855, 128.2228, 128.2725, 128.0934,
            128.2725, 127.7353, 128.7700, 129.2873, 130.0633, 129.1182,
            129.2873, 128.4715, 128.0934, 128.6506, 129.1381, 128.6406
        ]
        lowPrices = [
            125.3574, 126.1633, 124.9296, 126.0937, 126.8199, 126.4817,
            126.0340, 124.8301, 126.3921, 125.7156, 124.5615, 124.5715,
            125.0689, 126.8597, 126.6309, 126.8001, 126.7105, 126.8001,
            126.1335, 125.9245, 126.9891, 127.8148, 128.4715, 128.0641,
            127.6059, 127.5960, 126.9990, 126.8995, 127.4865, 127.3970
        ]
        closePrices = lowPrices[:
                                13]  # To keep initial close prince between low/high
        closePrices.extend([
            127.2876, 127.1781, 128.0138, 127.1085, 127.7253, 127.0587,
            127.3273, 128.7103, 127.8745, 128.5809, 128.6008, 127.9342,
            128.1133, 127.5960, 127.5960, 128.6904, 128.2725
        ])
        kValues = [40.3861, 66.8285, 56.7314]
        dValues = [47.2428, 49.2003, 54.6487]

        barDS = bards.BarDataSeries()
        stochFilter = stoch.StochasticOscillator(barDS, 14, maxLen=3)
        self.__fillBarDataSeries(barDS, closePrices, highPrices, lowPrices)

        self.assertEqual(len(stochFilter), 3)
        self.assertEqual(len(stochFilter[:]), 3)
        self.assertEqual(len(stochFilter.getDateTimes()), 3)
        self.assertEqual(len(stochFilter.getD()), 3)
        self.assertEqual(len(stochFilter.getD().getDateTimes()), 3)

        for i in xrange(3):
            self.assertEqual(round(stochFilter[i], 4), kValues[i])
            self.assertEqual(round(stochFilter.getD()[i], 4), dValues[i])