Example #1
0
    def testResampleNinjaTraderDay(self):
        # Resample.
        feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
        feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
        resampledBarDS = resampled.ResampledBarDataSeries(feed["spy"], barfeed.Frequency.DAY)
        resampledFile = os.path.join(common.get_temp_path(), "day-nt-spy-minute-2011.csv")
        resample.resample_to_csv(feed, barfeed.Frequency.DAY, resampledFile)
        resampledBarDS.pushLast()  # Need to manually push the last stot since time didn't change.

        # Load the resampled file.
        feed = csvfeed.GenericBarFeed(barfeed.Frequency.DAY)
        feed.addBarsFromCSV("spy", resampledFile, marketsession.USEquities.getTimezone())
        feed.loadAll()

        self.assertEqual(len(feed["spy"]), 25)
        self.assertEqual(
            feed["spy"][0].getDateTime(),
            dt.localize(datetime.datetime(2011, 1, 3), marketsession.USEquities.getTimezone()),
        )
        self.assertEqual(
            feed["spy"][-1].getDateTime(),
            dt.localize(datetime.datetime(2011, 2, 1), marketsession.USEquities.getTimezone()),
        )

        self.assertEqual(len(resampledBarDS), len(feed["spy"]))
        self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3)))
        self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1)))
Example #2
0
    def testResampleMtGoxMinute(self):
        # Resample.
        feed = mtgoxfeed.CSVTradeFeed()
        feed.addBarsFromCSV(
            common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
        resampledBarDS = resampled.ResampledBarDataSeries(
            feed["BTC"], barfeed.Frequency.MINUTE)
        resample.resample_to_csv(feed, barfeed.Frequency.MINUTE,
                                 "minute-mgtox-usd-2013-01-01.csv")
        resampledBarDS.pushLast(
        )  # Need to manually push the last stot since time didn't change.

        # Load the resampled file.
        feed = csvfeed.GenericBarFeed(barfeed.Frequency.MINUTE)
        feed.addBarsFromCSV("BTC", "minute-mgtox-usd-2013-01-01.csv")
        feed.loadAll()

        self.assertEqual(len(feed["BTC"]), 537)
        self.assertEqual(feed["BTC"][0].getDateTime(),
                         datetime.datetime(2013, 01, 01, 00, 04, 59))
        self.assertEqual(feed["BTC"][-1].getDateTime(),
                         datetime.datetime(2013, 01, 01, 23, 58, 59))

        self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
        self.assertEqual(resampledBarDS[0].getDateTime(),
                         dt.as_utc(feed["BTC"][0].getDateTime()))
        self.assertEqual(resampledBarDS[-1].getDateTime(),
                         dt.as_utc(feed["BTC"][-1].getDateTime()))
    def testMarketTimes(self):
        # 9:30 in GMT-5
        dateTime = dt.as_utc(datetime.datetime(2013, 1, 1, 9 + 5, 30))
        self.assertEqual(
            dt.unlocalize(api.to_market_datetime(dateTime, "XNYS")),
            datetime.datetime(2013, 1, 1, 9, 30))
        self.assertEqual(
            dt.unlocalize(api.to_market_datetime(dateTime, "XASE")),
            datetime.datetime(2013, 1, 1, 9, 30))
        self.assertEqual(
            dt.unlocalize(api.to_market_datetime(dateTime, "XNAS")),
            datetime.datetime(2013, 1, 1, 9, 30))
        self.assertEqual(
            dt.unlocalize(api.to_market_datetime(dateTime, "XNYS")),
            datetime.datetime(2013, 1, 1, 9, 30))

        # 8:00 in GMT
        dateTime = dt.as_utc(datetime.datetime(2013, 1, 1, 8))
        self.assertEqual(
            dt.unlocalize(api.to_market_datetime(dateTime, "CHIX")),
            datetime.datetime(2013, 1, 1, 8))
        # From Apr~Oct CHIX is GMT+1
        dateTime = dt.as_utc(datetime.datetime(2013, 4, 1, 8))
        self.assertEqual(
            dt.unlocalize(api.to_market_datetime(dateTime, "CHIX")),
            datetime.datetime(2013, 4, 1, 9))
Example #4
0
    def testResampleNinjaTraderHour(self):
        with common.TmpDir() as tmp_path:
            # Resample.
            feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
            feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
            resampledBarDS = resampled_ds.ResampledBarDataSeries(feed["spy"], bar.Frequency.HOUR)
            resampledFile = os.path.join(tmp_path, "hour-nt-spy-minute-2011.csv")
            resample.resample_to_csv(feed, bar.Frequency.HOUR, resampledFile)
            resampledBarDS.pushLast()  # Need to manually push the last stot since time didn't change.

            # Load the resampled file.
            feed = csvfeed.GenericBarFeed(bar.Frequency.HOUR, marketsession.USEquities.getTimezone())
            feed.addBarsFromCSV("spy", resampledFile)
            feed.loadAll()

        self.assertEqual(len(feed["spy"]), 340)
        self.assertEqual(feed["spy"][0].getDateTime(), dt.localize(datetime.datetime(2011, 1, 3, 9), marketsession.USEquities.getTimezone()))
        self.assertEqual(feed["spy"][-1].getDateTime(), dt.localize(datetime.datetime(2011, 2, 1, 1), marketsession.USEquities.getTimezone()))
        self.assertEqual(feed["spy"][0].getOpen(), 126.35)
        self.assertEqual(feed["spy"][0].getHigh(), 126.45)
        self.assertEqual(feed["spy"][0].getLow(), 126.3)
        self.assertEqual(feed["spy"][0].getClose(), 126.4)
        self.assertEqual(feed["spy"][0].getVolume(), 3397.0)
        self.assertEqual(feed["spy"][0].getAdjClose(), None)

        self.assertEqual(len(resampledBarDS), len(feed["spy"]))
        self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3, 9)))
        self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1, 1)))
    def testResampleMtGoxDay(self):
        # Resample.
        feed = mtgoxfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
        resampledBarDS = resampled.ResampledBarDataSeries(feed["BTC"], barfeed.Frequency.DAY)
        resampledFile = os.path.join(common.get_temp_path(), "day-mgtox-usd-2013-01-01.csv")
        resample.resample_to_csv(feed, barfeed.Frequency.DAY, resampledFile)
        resampledBarDS.pushLast()  # Need to manually push the last stot since time didn't change.

        # Load the resampled file.
        feed = csvfeed.GenericBarFeed(barfeed.Frequency.DAY)
        feed.addBarsFromCSV("BTC", resampledFile)
        feed.loadAll()

        self.assertEqual(len(feed["BTC"]), 1)
        self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 1, 1))
        self.assertEqual(feed["BTC"][0].getOpen(), 13.51001)
        self.assertEqual(feed["BTC"][0].getHigh(), 13.56)
        self.assertEqual(feed["BTC"][0].getLow(), 13.16123)
        self.assertEqual(feed["BTC"][0].getClose(), 13.30413)
        self.assertEqual(feed["BTC"][0].getVolume(), 28168.9114596)
        self.assertEqual(feed["BTC"][0].getAdjClose(), 13.30413)

        self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
        self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime()))
        self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime()))
        self.assertEqual(resampledBarDS[0].getOpen(), feed["BTC"][0].getOpen())
        self.assertEqual(resampledBarDS[0].getHigh(), feed["BTC"][0].getHigh())
        self.assertEqual(resampledBarDS[0].getLow(), feed["BTC"][0].getLow())
        self.assertEqual(resampledBarDS[0].getClose(), feed["BTC"][0].getClose())
        self.assertEqual(round(resampledBarDS[0].getVolume(), 5), round(feed["BTC"][0].getVolume(), 5))
        self.assertEqual(resampledBarDS[0].getAdjClose(), feed["BTC"][0].getAdjClose())
def load_intraday_bars():
	global feed

	print "Loading bars from file"
	feed = ninjatraderfeed.Feed(barfeed.Frequency.MINUTE)
	feed.setBarFilter(csvfeed.DateRangeFilter(dt.as_utc(datetime.datetime(2008, 1, 1)), dt.as_utc(datetime.datetime(2008, 12, 31))))
	feed.addBarsFromCSV(instrument, "/Users/gabo/Downloads/etf-quotes/SPY.Last.txt")
Example #7
0
    def testLoadFilterFrom(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"),
                            INSTRUMENT,
                            fromDateTime=dt.as_utc(
                                datetime.datetime(2012, 5, 29)))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEqual(len(loaded), 646)

        self.assertEqual(loaded[0][0],
                         dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        b = loaded[0][1].getBar(INSTRUMENT)
        self.assertEqual(b.getDateTime(),
                         dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEqual(b.getClose(), 5.07)
        self.assertEqual(b.getPrice(), 5.07)
        self.assertEqual(b.getVolume(), 1.39081288)

        self.assertEqual(
            loaded[-1][0],
            dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        b = loaded[-1][1].getBar(INSTRUMENT)
        self.assertEqual(
            b.getDateTime(),
            dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEqual(b.getClose(), 5.1)
        self.assertEqual(b.getPrice(), 5.1)
        self.assertEqual(b.getVolume(), 0.39215686)
Example #8
0
    def testLoadFilterFromAndTo(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"),
                            instrument=INSTRUMENT,
                            fromDateTime=dt.as_utc(
                                datetime.datetime(2012, 5, 29)),
                            toDateTime=datetime.datetime(2012, 5, 31))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEqual(len(loaded), 579)

        self.assertEqual(loaded[0][0],
                         dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        b = loaded[0][1].getBar(INSTRUMENT)
        self.assertEqual(b.getDateTime(),
                         dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEqual(b.getClose(), 5.07)
        self.assertEqual(b.getVolume(), 1.39081288)

        self.assertEqual(loaded[-1][0],
                         dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21)))
        b = loaded[-1][1].getBar(INSTRUMENT)
        self.assertEqual(b.getDateTime(),
                         dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21)))
        self.assertEqual(b.getClose(), 5.14)
        self.assertEqual(b.getVolume(), 20)
Example #9
0
    def testResampleMtGoxHour(self):
        # Resample.
        feed = mtgoxfeed.CSVTradeFeed()
        feed.addBarsFromCSV(
            common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
        resampledBarDS = resampled.ResampledBarDataSeries(
            feed["BTC"], barfeed.Frequency.HOUR)
        resampledFile = os.path.join(common.get_temp_path(),
                                     "hour-mgtox-usd-2013-01-01.csv")
        resample.resample_to_csv(feed, barfeed.Frequency.HOUR, resampledFile)
        resampledBarDS.pushLast(
        )  # Need to manually push the last stot since time didn't change.

        # Load the resampled file.
        feed = csvfeed.GenericBarFeed(barfeed.Frequency.HOUR)
        feed.addBarsFromCSV("BTC", resampledFile)
        feed.loadAll()

        self.assertEqual(len(feed["BTC"]), 24)
        self.assertEqual(feed["BTC"][0].getDateTime(),
                         datetime.datetime(2013, 01, 01, 00, 59, 59))
        self.assertEqual(feed["BTC"][-1].getDateTime(),
                         datetime.datetime(2013, 01, 01, 23, 59, 59))

        self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
        self.assertEqual(resampledBarDS[0].getDateTime(),
                         dt.as_utc(feed["BTC"][0].getDateTime()))
        self.assertEqual(resampledBarDS[-1].getDateTime(),
                         dt.as_utc(feed["BTC"][-1].getDateTime()))
    def testResampleNinjaTraderHour(self):
        # Resample.
        feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
        feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
        resampledBarDS = resampled.ResampledBarDataSeries(feed["spy"], barfeed.Frequency.HOUR)
        resampledFile = os.path.join(common.get_temp_path(), "hour-nt-spy-minute-2011.csv")
        resample.resample_to_csv(feed, barfeed.Frequency.HOUR, resampledFile)
        resampledBarDS.pushLast()  # Need to manually push the last stot since time didn't change.

        # Load the resampled file.
        feed = csvfeed.GenericBarFeed(barfeed.Frequency.HOUR, marketsession.USEquities.getTimezone())
        feed.addBarsFromCSV("spy", resampledFile)
        feed.loadAll()

        self.assertEqual(len(feed["spy"]), 340)
        self.assertEqual(feed["spy"][0].getDateTime(), dt.localize(datetime.datetime(2011, 1, 3, 9), marketsession.USEquities.getTimezone()))
        self.assertEqual(feed["spy"][-1].getDateTime(), dt.localize(datetime.datetime(2011, 2, 1, 1), marketsession.USEquities.getTimezone()))
        self.assertEqual(feed["spy"][0].getOpen(), 126.35)
        self.assertEqual(feed["spy"][0].getHigh(), 126.45)
        self.assertEqual(feed["spy"][0].getLow(), 126.3)
        self.assertEqual(feed["spy"][0].getClose(), 126.4)
        self.assertEqual(feed["spy"][0].getVolume(), 3397.0)
        self.assertEqual(feed["spy"][0].getAdjClose(), None)

        self.assertEqual(len(resampledBarDS), len(feed["spy"]))
        self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3, 9)))
        self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1, 1)))
Example #11
0
    def testResampleNinjaTraderDay(self):
        with common.TmpDir() as tmp_path:
            # Resample.
            feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE)
            feed.addBarsFromCSV(
                "spy", common.get_data_file_path("nt-spy-minute-2011.csv"))
            resampledBarDS = resampled_ds.ResampledBarDataSeries(
                feed["spy"], bar.Frequency.DAY)
            resampledFile = os.path.join(tmp_path,
                                         "day-nt-spy-minute-2011.csv")
            resample.resample_to_csv(feed, bar.Frequency.DAY, resampledFile)
            resampledBarDS.pushLast(
            )  # Need to manually push the last stot since time didn't change.

            # Load the resampled file.
            feed = csvfeed.GenericBarFeed(bar.Frequency.DAY)
            feed.addBarsFromCSV("spy", resampledFile,
                                marketsession.USEquities.getTimezone())
            feed.loadAll()

        self.assertEqual(len(feed["spy"]), 25)
        self.assertEqual(
            feed["spy"][0].getDateTime(),
            dt.localize(datetime.datetime(2011, 1, 3),
                        marketsession.USEquities.getTimezone()))
        self.assertEqual(
            feed["spy"][-1].getDateTime(),
            dt.localize(datetime.datetime(2011, 2, 1),
                        marketsession.USEquities.getTimezone()))

        self.assertEqual(len(resampledBarDS), len(feed["spy"]))
        self.assertEqual(resampledBarDS[0].getDateTime(),
                         dt.as_utc(datetime.datetime(2011, 1, 3)))
        self.assertEqual(resampledBarDS[-1].getDateTime(),
                         dt.as_utc(datetime.datetime(2011, 2, 1)))
Example #12
0
    def testLoadNoFilter(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEqual(len(loaded), 9999)

        self.assertEqual(loaded[0][0],
                         dt.as_utc(datetime.datetime(2011, 9, 13, 13, 53, 36)))
        b = loaded[0][1].getBar(INSTRUMENT)
        self.assertEqual(b.getDateTime(),
                         dt.as_utc(datetime.datetime(2011, 9, 13, 13, 53, 36)))
        self.assertEqual(b.getClose(), 5.8)
        self.assertEqual(b.getPrice(), 5.8)
        self.assertEqual(b.getVolume(), 1.0)

        self.assertEqual(
            loaded[-1][0],
            dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        b = loaded[-1][1].getBar(INSTRUMENT)
        self.assertEqual(
            b.getDateTime(),
            dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEqual(b.getClose(), 5.1)
        self.assertEqual(b.getPrice(), 5.1)
        self.assertEqual(b.getVolume(), 0.39215686)
Example #13
0
def load_intraday_bars():
    global feed

    print "Loading bars from file"
    feed = ninjatraderfeed.Feed(barfeed.Frequency.MINUTE)
    # feed.setBarFilter(csvfeed.DateRangeFilter(dt.as_utc(datetime.datetime(2008, 1, 1)), dt.as_utc(datetime.datetime(2008, 12, 31))))
    feed.setBarFilter(
        csvfeed.DateRangeFilter(dt.as_utc(datetime.datetime(2008, 1, 1)),
                                dt.as_utc(datetime.datetime(2008, 3, 31))))
    feed.addBarsFromCSV(instrument,
                        "/Users/gabo/Downloads/etf-quotes/SPY.Last.txt")
Example #14
0
    def testMarketTimes(self):
        # 9:30 in GMT-5
        dateTime = dt.as_utc(datetime.datetime(2013, 1, 1, 9+5, 30))
        self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "XNYS")), datetime.datetime(2013, 1, 1, 9, 30))
        self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "XASE")), datetime.datetime(2013, 1, 1, 9, 30))
        self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "XNAS")), datetime.datetime(2013, 1, 1, 9, 30))
        self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "XNYS")), datetime.datetime(2013, 1, 1, 9, 30))

        # 8:00 in GMT
        dateTime = dt.as_utc(datetime.datetime(2013, 1, 1, 8))
        self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "CHIX")), datetime.datetime(2013, 1, 1, 8))
        # From Apr~Oct CHIX is GMT+1
        dateTime = dt.as_utc(datetime.datetime(2013, 4, 1, 8))
        self.assertEqual(dt.unlocalize(api.to_market_datetime(dateTime, "CHIX")), datetime.datetime(2013, 4, 1, 9))
Example #15
0
    def testLoadNoFilter(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEquals(len(loaded), 9999)

        self.assertEquals(loaded[0][0], dt.as_utc(datetime.datetime(2011, 9, 13, 13, 53, 36)))
        self.assertEquals(loaded[0][1]["BTC"].getDateTime(), dt.as_utc(datetime.datetime(2011, 9, 13, 13, 53, 36)))
        self.assertEquals(loaded[0][1]["BTC"].getClose(), 5.8)
        self.assertEquals(loaded[0][1]["BTC"].getVolume(), 1.0)

        self.assertEquals(loaded[-1][0], dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEquals(loaded[-1][1]["BTC"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEquals(loaded[-1][1]["BTC"].getClose(), 5.1)
        self.assertEquals(loaded[-1][1]["BTC"].getVolume(), 0.39215686)
Example #16
0
    def testLoadFilterFromAndTo(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"), "bitstampUSD", fromDateTime=dt.as_utc(datetime.datetime(2012, 5, 29)), toDateTime=datetime.datetime(2012, 5, 31))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEquals(len(loaded), 579)

        self.assertEquals(loaded[0][0], dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEquals(loaded[0][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEquals(loaded[0][1]["bitstampUSD"].getClose(), 5.07)
        self.assertEquals(loaded[0][1]["bitstampUSD"].getVolume(), 1.39081288)

        self.assertEquals(loaded[-1][0], dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21)))
        self.assertEquals(loaded[-1][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21)))
        self.assertEquals(loaded[-1][1]["bitstampUSD"].getClose(), 5.14)
        self.assertEquals(loaded[-1][1]["bitstampUSD"].getVolume(), 20)
Example #17
0
    def testLoadFilterFrom(self):
        feed = barfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"), "bitstampUSD", fromDateTime=dt.as_utc(datetime.datetime(2012, 5, 29)))
        loaded = [(dateTime, bars) for dateTime, bars in feed]

        self.assertEquals(len(loaded), 646)

        self.assertEquals(loaded[0][0], dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEquals(loaded[0][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52)))
        self.assertEquals(loaded[0][1]["bitstampUSD"].getClose(), 5.07)
        self.assertEquals(loaded[0][1]["bitstampUSD"].getVolume(), 1.39081288)

        self.assertEquals(loaded[-1][0], dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEquals(loaded[-1][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 31, 8, 41, 18, 5)))
        self.assertEquals(loaded[-1][1]["bitstampUSD"].getClose(), 5.1)
        self.assertEquals(loaded[-1][1]["bitstampUSD"].getVolume(), 0.39215686)
Example #18
0
    def testTimeStampConversions(self):
        dateTime = datetime.datetime(2000, 1, 1)
        self.assertEqual(
            dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime),
                                     False), dateTime)

        dateTime = dt.as_utc(datetime.datetime(2000, 1, 1, 1, 1))
        self.assertEqual(
            dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), True),
            dateTime)
Example #19
0
    def testResampleMtGoxDay(self):
        # Resample.
        feed = mtgoxfeed.CSVTradeFeed()
        feed.addBarsFromCSV(
            common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
        resampledBarDS = resampled.ResampledBarDataSeries(
            feed["BTC"], barfeed.Frequency.DAY)
        resampledFile = os.path.join(common.get_temp_path(),
                                     "day-mgtox-usd-2013-01-01.csv")
        resample.resample_to_csv(feed, barfeed.Frequency.DAY, resampledFile)
        resampledBarDS.pushLast(
        )  # Need to manually push the last stot since time didn't change.

        # Load the resampled file.
        feed = csvfeed.GenericBarFeed(barfeed.Frequency.DAY)
        feed.addBarsFromCSV("BTC", resampledFile)
        feed.loadAll()

        self.assertEqual(len(feed["BTC"]), 1)
        self.assertEqual(feed["BTC"][0].getDateTime(),
                         datetime.datetime(2013, 01, 01, 23, 59, 59))
        self.assertEqual(feed["BTC"][0].getOpen(), 13.51001)
        self.assertEqual(feed["BTC"][0].getHigh(), 13.56)
        self.assertEqual(feed["BTC"][0].getLow(), 13.16123)
        self.assertEqual(feed["BTC"][0].getClose(), 13.30413)
        self.assertEqual(feed["BTC"][0].getVolume(), 28168.9114596)
        self.assertEqual(feed["BTC"][0].getAdjClose(), 13.30413)

        self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
        self.assertEqual(resampledBarDS[0].getDateTime(),
                         dt.as_utc(feed["BTC"][0].getDateTime()))
        self.assertEqual(resampledBarDS[-1].getDateTime(),
                         dt.as_utc(feed["BTC"][-1].getDateTime()))
        self.assertEqual(resampledBarDS[0].getOpen(), feed["BTC"][0].getOpen())
        self.assertEqual(resampledBarDS[0].getHigh(), feed["BTC"][0].getHigh())
        self.assertEqual(resampledBarDS[0].getLow(), feed["BTC"][0].getLow())
        self.assertEqual(resampledBarDS[0].getClose(),
                         feed["BTC"][0].getClose())
        self.assertEqual(round(resampledBarDS[0].getVolume(), 5),
                         round(feed["BTC"][0].getVolume(), 5))
        self.assertEqual(resampledBarDS[0].getAdjClose(),
                         feed["BTC"][0].getAdjClose())
Example #20
0
	def testResampleMtGoxHour(self):
		# Resample.
		feed = mtgoxfeed.CSVTradeFeed()
		feed.addBarsFromCSV(common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
		resampledBarDS = resampled.ResampledBarDataSeries(feed["BTC"], barfeed.Frequency.HOUR)
		resample.resample_to_csv(feed, barfeed.Frequency.HOUR, "hour-mgtox-usd-2013-01-01.csv")
		resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change.

		# Load the resampled file.
		feed = csvfeed.GenericBarFeed(barfeed.Frequency.HOUR)
		feed.addBarsFromCSV("BTC", "hour-mgtox-usd-2013-01-01.csv")
		feed.loadAll()
	
		self.assertEqual(len(feed["BTC"]), 24)
		self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 01, 01, 00, 59, 59))
		self.assertEqual(feed["BTC"][-1].getDateTime(), datetime.datetime(2013, 01, 01, 23, 59, 59))

		self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
		self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime()))
		self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime()))
Example #21
0
    def testTimeStampConversionsWithMicroseconds(self):
        dateTime = datetime.datetime(2000, 1, 1, 1, 1, 1, microsecond=10)
        self.assertEqual(
            dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime),
                                     False), dateTime)

        dateTime = dt.as_utc(
            datetime.datetime(2000, 1, 1, 1, 1, 1, microsecond=10))
        self.assertEqual(
            dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), True),
            dateTime)
    def testResampleMtGoxMinute(self):
        # Resample.
        feed = mtgoxfeed.CSVTradeFeed()
        feed.addBarsFromCSV(common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv"))
        resampledBarDS = resampled.ResampledBarDataSeries(feed["BTC"], barfeed.Frequency.MINUTE)
        resampledFile = os.path.join(common.get_temp_path(), "minute-mgtox-usd-2013-01-01.csv")
        resample.resample_to_csv(feed, barfeed.Frequency.MINUTE, resampledFile)
        resampledBarDS.pushLast()  # Need to manually push the last stot since time didn't change.

        # Load the resampled file.
        feed = csvfeed.GenericBarFeed(barfeed.Frequency.MINUTE)
        feed.addBarsFromCSV("BTC", resampledFile)
        feed.loadAll()

        self.assertEqual(len(feed["BTC"]), 537)
        self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 1, 1, 0, 4))
        self.assertEqual(feed["BTC"][-1].getDateTime(), datetime.datetime(2013, 1, 1, 23, 58))

        self.assertEqual(len(resampledBarDS), len(feed["BTC"]))
        self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime()))
        self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime()))
Example #23
0
    def initializeFeed(self):
        assert self.__dbFilePath is not None
        assert self.__currentDate is not None
        assert self.__maxLen is not None

        fromDate = self.__currentDate - timedelta(days=self.__maxLen)
        toDate = self.__currentDate + timedelta(days=5)
        self.__feed = DynamicFeed(self.__dbFilePath,
                                  self.__codes,
                                  fromDateTime=fromDate,
                                  toDateTime=toDate,
                                  maxLen=self.__maxLen)
        self.__feed.positionFeed(dt.as_utc(self.__currentDate))
Example #24
0
def build_order_from_open_order(openOrder, instrumentTraits):
    #order_id = openOrder.order.m_permId   #we use the TWS id for the order rather than our id - not sure this is a good idea but its apparently consistent across sessions - https://www.interactivebrokers.com/en/software/api/apiguide/java/order.htm
    order_id = openOrder.order.m_orderId
    #doesn't seem to be a useable date/time for orders so going to use current time
    order_time = dt.as_utc(datetime.datetime.now())

    order_type = openOrder.order.m_orderType  #stop, limit, stoplimit, market

    order_action = openOrder.order.m_action

    order_amount = openOrder.order.m_totalQuantity
    order_limprice = openOrder.order.m_lmtPrice
    order_auxprice = openOrder.order.m_auxPrice
    contract_symbol = openOrder.contract.m_symbol

    if order_action == 'BUY':
        action = broker.Order.Action.BUY
    elif order_action == 'SELL':
        action = broker.Order.Action.SELL
    elif order_action == 'SSHORT':
        action = broker.Order.Action.SELL_SHORT
    else:
        raise Exception("Invalid order action")

    if order_type == 'LMT':  #Limit
        ret = broker.LimitOrder(action, contract_symbol, order_limprice,
                                order_amount, instrumentTraits)
    elif order_type == 'MKT':  #Market
        ret = broker.MarketOrder(action, contract_symbol, order_amount, False,
                                 instrumentTraits)
    elif order_type == 'MOC':  #Market On Close
        ret = broker.MarketOrder(action, contract_symbol, order_amount, True,
                                 instrumentTraits)
    elif order_type == 'STP':  #Stop order
        ret = broker.StopOrder(action, contract_symbol, order_auxprice,
                               order_amount, instrumentTraits)
    elif order_type == 'STP LMT':
        ret = broker.StopLimitOrder(action, contract_symbol, order_auxprice,
                                    order_limprice, order_amount,
                                    instrumentTraits)
    else:
        #Totally possible if you use pyalgotrade and TWS to manage the same account which is not really a good idea
        raise Exception("Unsupported order type - %s" % order_type)

    ret.setSubmitted(order_id, order_time)
    ret.setState(broker.Order.State.ACCEPTED)
    return ret
    def testBuildBar(self):
        # This is the response to http://globalrealtime.xignite.com/v3/xGlobalRealTime.json/GetBar?Identifier=RIOl.CHIX&IdentifierType=Symbol&EndTime=3/19/2014%2010:00:00&Precision=Minutes&Period=5
        response = """{
            "Outcome": "Success",
            "Message": null,
            "Identity": "Request",
            "Delay": 0.0330687,
            "Bar": {
                "StartDate": "3/19/2014",
                "StartTime": "9:55:00 AM",
                "EndDate": "3/19/2014",
                "EndTime": "10:00:00 AM",
                "UTCOffset": 0,
                "Open": 31.71,
                "High": 31.71,
                "Low": 31.68,
                "Close": 31.69,
                "Volume": 2966,
                "Trades": 19,
                "TWAP": 31.6929,
                "VWAP": 31.693
            },
            "Security": {
                "CIK": "0000863064",
                "CUSIP": null,
                "Symbol": "RIOl.CHIX",
                "ISIN": null,
                "Valoren": "402589",
                "Name": "Rio Tinto PLC",
                "Market": "CHI-X EUROPE LIMITED.",
                "MarketIdentificationCode": "CHIX",
                "MostLiquidExchange": false,
                "CategoryOrIndustry": "IndustrialMetalsAndMinerals"
            }
        }"""

        responseDict = json.loads(response)
        bar = barfeed.build_bar(responseDict["Bar"], "RIOl.CHIX", 60 * 5)

        self.assertEqual(bar.getOpen(), 31.71)
        self.assertEqual(bar.getHigh(), 31.71)
        self.assertEqual(bar.getLow(), 31.68)
        self.assertEqual(bar.getClose(), 31.69)
        self.assertEqual(bar.getVolume(), 2966)
        self.assertEqual(bar.getDateTime(),
                         dt.as_utc(datetime.datetime(2014, 3, 19, 9, 55)))
Example #26
0
    def testBuildBar(self):
        # This is the response to http://globalrealtime.xignite.com/v3/xGlobalRealTime.json/GetBar?Identifier=RIOl.CHIX&IdentifierType=Symbol&EndTime=3/19/2014%2010:00:00&Precision=Minutes&Period=5
        response = """{
            "Outcome": "Success",
            "Message": null,
            "Identity": "Request",
            "Delay": 0.0330687,
            "Bar": {
                "StartDate": "3/19/2014",
                "StartTime": "9:55:00 AM",
                "EndDate": "3/19/2014",
                "EndTime": "10:00:00 AM",
                "UTCOffset": 0,
                "Open": 31.71,
                "High": 31.71,
                "Low": 31.68,
                "Close": 31.69,
                "Volume": 2966,
                "Trades": 19,
                "TWAP": 31.6929,
                "VWAP": 31.693
            },
            "Security": {
                "CIK": "0000863064",
                "CUSIP": null,
                "Symbol": "RIOl.CHIX",
                "ISIN": null,
                "Valoren": "402589",
                "Name": "Rio Tinto PLC",
                "Market": "CHI-X EUROPE LIMITED.",
                "MarketIdentificationCode": "CHIX",
                "MostLiquidExchange": false,
                "CategoryOrIndustry": "IndustrialMetalsAndMinerals"
            }
        }"""

        responseDict = json.loads(response)
        bar = barfeed.build_bar(responseDict["Bar"], "RIOl.CHIX", 60*5)

        self.assertEqual(bar.getOpen(), 31.71)
        self.assertEqual(bar.getHigh(), 31.71)
        self.assertEqual(bar.getLow(), 31.68)
        self.assertEqual(bar.getClose(), 31.69)
        self.assertEqual(bar.getVolume(), 2966)
        self.assertEqual(bar.getDateTime(), dt.as_utc(datetime.datetime(2014, 3, 19, 9, 55)))
Example #27
0
def build_order_from_open_order(openOrder, instrumentTraits):
    #order_id = openOrder.order.m_permId   #we use the TWS id for the order rather than our id - not sure this is a good idea but its apparently consistent across sessions - https://www.interactivebrokers.com/en/software/api/apiguide/java/order.htm
    order_id = openOrder.order.m_orderId
    #doesn't seem to be a useable date/time for orders so going to use current time
    order_time = dt.as_utc(datetime.datetime.now())

    order_type = openOrder.order.m_orderType     #stop, limit, stoplimit, market
    

    order_action = openOrder.order.m_action

    order_amount = openOrder.order.m_totalQuantity
    order_limprice = openOrder.order.m_lmtPrice
    order_auxprice = openOrder.order.m_auxPrice
    contract_symbol = openOrder.contract.m_symbol


    if order_action == 'BUY':
        action = broker.Order.Action.BUY
    elif order_action == 'SELL':
        action = broker.Order.Action.SELL
    elif order_action == 'SSHORT':
        action = broker.Order.Action.SELL_SHORT
    else:
        raise Exception("Invalid order action")

    if order_type == 'LMT':     #Limit
        ret = broker.LimitOrder(action, contract_symbol, order_limprice, order_amount, instrumentTraits)
    elif order_type == 'MKT':   #Market
        ret = broker.MarketOrder(action, contract_symbol, order_amount, False, instrumentTraits)
    elif order_type == 'MOC':   #Market On Close
        ret = broker.MarketOrder(action, contract_symbol, order_amount, True, instrumentTraits)
    elif order_type == 'STP':   #Stop order
        ret = broker.StopOrder(action, contract_symbol, order_auxprice, order_amount, instrumentTraits)
    elif order_type == 'STP LMT':
        ret = broker.StopLimitOrder(action, contract_symbol, order_auxprice, order_limprice, order_amount, instrumentTraits)
    else:
        #Totally possible if you use pyalgotrade and TWS to manage the same account which is not really a good idea
        raise Exception("Unsupported order type - %s" % order_type)
    

    ret.setSubmitted(order_id, order_time)
    ret.setState(broker.Order.State.ACCEPTED)
    return ret
Example #28
0
    def __init__(self,
                 dbfilepah="/var/pytrade/sqlitedb",
                 googleFinanceDir="/var/pytrade/googlefinance",
                 username=None,
                 tradingAlgorithmGenerator=None,
                 codes=None,
                 date=dt.as_utc(datetime.now()),
                 maxlen=90,
                 debugmode=False):
        if codes is None:
            self.__codes = [
                "ABEV3", "BBAS3", "BBDC3", "BBDC4", "BBSE3", "BRAP4", "BRFS3",
                "BRKM5", "BRML3", "BVMF3", "CCRO3", "CIEL3", "CMIG4", "CPFE3",
                "CPLE6", "CSAN3", "CSNA3", "CTIP3", "CYRE3", "ECOR3", "EGIE3",
                "EMBR3", "ENBR3", "EQTL3", "ESTC3", "FIBR3", "GGBR4", "GOAU4",
                "HYPE3", "ITSA4", "ITUB4", "JBSS3", "KLBN11", "KROT3", "LAME4",
                "LREN3", "MRFG3", "MRVE3", "MULT3", "NATU3", "PCAR4", "PETR3",
                "PETR4", "QUAL3", "RADL3", "RENT3", "RUMO3", "SANB11", "SBSP3",
                "SMLE3", "SUZB5", "TIMP3", "UGPA3", "USIM5", "VALE3", "VALE5",
                "VIVT4", "WEGE3"
            ]
        else:
            self.__codes = codes

        self.__logger = logger.getLogger(PytradeApi.LOGGER_NAME)
        self.__dbFilePath = dbfilepah
        self.__googleFinanceDir = googleFinanceDir
        self.__debugMode = debugmode
        self.__tradingAlgorithmGenerator = tradingAlgorithmGenerator
        self.__currentDate = date
        self.__maxLen = maxlen

        self.initializeDataProvider()
        self.initializeFeed()

        self.__username = username
        if username is not None:
            self.initializeBroker(username)
            self.initializeStrategy(
            )  # tradingAlgorithmGenerator must not be None
 def __init__(self):
     self.__datetime = dt.as_utc(datetime.datetime.utcnow())
Example #30
0
def utcnow():  # 世界时间
    return dt.as_utc(datetime.utcnow())
Example #31
0
def to_utc_if_naive(dateTime):
    if dateTime is not None and dt.datetime_is_naive(dateTime):
        dateTime = dt.as_utc(dateTime)
    return dateTime
Example #32
0
def utcnow():
    return dt.as_utc(datetime.datetime.utcnow())
Example #33
0
def parse_datetime(dateTime):
    try:
        ret = datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S")
    except ValueError:
        ret = datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S.%f")
    return dt.as_utc(ret)
Example #34
0
 def onBars(self, bars):
     if self.pos is None:
         self.pos = self.enterLongLimit(INSTRUMENT, 100, 1, True)
     elif bars.getDateTime() == dt.as_utc(
             datetime.datetime(2000, 1, 3)):
         self.pos.exitLimit(101)
 def getDateTime(self):
     return dt.as_utc(datetime.datetime.utcnow())
Example #36
0
def parse_datetime(dateTime):
    try:
        ret = datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S")
    except ValueError:
        ret = datetime.datetime.strptime(dateTime, "%Y-%m-%d %H:%M:%S.%f")
    return dt.as_utc(ret)
Example #37
0
def to_utc_if_naive(dateTime):
    if dateTime is not None and dt.datetime_is_naive(dateTime):
        dateTime = dt.as_utc(dateTime)
    return dateTime
Example #38
0
def utcnow():
    return dt.as_utc(datetime.datetime.utcnow())
Example #39
0
def datetime_to_tid(dateTime):
    unixTime = dt.datetime_to_timestamp(dt.as_utc(dateTime))
    return timestamp_to_tid(unixTime)
Example #40
0
def localnow():
    return dt.as_utc(datetime.datetime.now())
Example #41
0
def datetime_to_tid(dateTime):
    unixTime = dt.datetime_to_timestamp(dt.as_utc(dateTime))
    return timestamp_to_tid(unixTime)
Example #42
0
    def testTimeStampConversionsWithMicroseconds(self):
        dateTime = datetime.datetime(2000, 1, 1, 1, 1, 1, microsecond=10)
        self.assertEqual(dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), False), dateTime)

        dateTime = dt.as_utc(datetime.datetime(2000, 1, 1, 1, 1, 1, microsecond=10))
        self.assertEqual(dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), True), dateTime)
Example #43
0
    def testTimeStampConversions(self):
        dateTime = datetime.datetime(2000, 1, 1)
        self.assertEqual(dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), False), dateTime)

        dateTime = dt.as_utc(datetime.datetime(2000, 1, 1, 1, 1))
        self.assertEqual(dt.timestamp_to_datetime(dt.datetime_to_timestamp(dateTime), True), dateTime)