def testFields(self): datafile = '%s/data/AC___CCB.csv' % os.path.dirname(__file__) i = Instrument('AC',datafile,pointValue=500,currency='USD', exchange='NYMEX', initialMargin=1480, maintMargin=1170, sector='Ag Commodities', description='test description') i.load_data() self.assertEqual( i.point_value(), 500 ) self.assertEqual( i.currency(), 'USD' ) self.assertEqual( i.exchange(), 'NYMEX' ) self.assertEqual( i.initial_margin(), 1480 ) self.assertEqual( i.maint_margin(), 1170 ) self.assertEqual( i.sector(), 'Ag Commodities' ) self.assertEqual( i.description(), 'test description' )
def testBasic(self): datafile = '%s/data/AC___CCB.csv' % os.path.dirname(__file__) i = Instrument('AC',datafile) i.load_data() self.assertEqual( i.symbol(), 'AC' ) self.assertEqual( len(i.bars()), 252 ) self.assertAlmostEqual( i.bars()[251].open(), 2.35 ) self.assertAlmostEqual( i.bars()[0].high(), 2.227 ) self.assertAlmostEqual( i.bars()[100].low(), 2.031 ) self.assertAlmostEqual( i.bars()[200].close(), 2.337 )
def testFutures(self): datafile = '%s/data/AC___CCB.csv' % os.path.dirname(__file__) i = Instrument('AC', datafile, pointValue=50) pos = PositionTracker(i) pos.buy(datetime.datetime(2012, 12, 31), 100, 25.0, 7.95) self.assertEqual(pos.getBasis(), 125000.0) self.assertEqual(pos.getNetProfit(26.0, includeCommissions=True), 4992.05) self.assertEqual(pos.getEntryDate(), datetime.datetime(2012, 12, 31)) pos.sell(datetime.datetime(2013, 1, 1), 100, 26.0, 7.95) self.assertEqual(pos.getBasis(), 125000.0) self.assertEqual(pos.getNetProfit(0.0, includeCommissions=True), 4984.1) self.assertEqual(pos.getNetProfit(0.0, includeCommissions=False), 5000.0) self.assertEqual(pos.getCommissions(), 15.90) self.assertEqual(pos.getExitDate(), datetime.datetime(2013, 1, 1)) pos.reset() pos.buy(datetime.datetime(2013, 1, 6), 100, 27.0, 7.95) self.assertEqual(pos.getBasis(), 135000.0) self.assertEqual(pos.getNetProfit(26.0, includeCommissions=True), -5007.95) pos.sell(datetime.datetime(2013, 1, 7), 100, 26.0, 7.95) self.assertEqual(pos.getBasis(), 135000.0) self.assertEqual(pos.getNetProfit(0.0, includeCommissions=True), -5015.90)
def setUp(self): datafile = '%s/data/AC___CCB.csv' % os.path.dirname(__file__) self._inst1 = Instrument('AC', datafile) datafile = '%s/data/ACM__CCB.csv' % os.path.dirname(__file__) self._inst2 = Instrument('ACM', datafile) # these are dates where only one of the two feeds above traded and has data. # we use this in the on_bars_basic as part of the start/emit() test self._special_dates = sets.Set() self._special_dates.add(datetime.datetime(2012, 2, 20, 0, 0)) self._special_dates.add(datetime.datetime(2012, 4, 9, 0, 0)) self._special_dates.add(datetime.datetime(2012, 5, 28, 0, 0)) self._special_dates.add(datetime.datetime(2012, 7, 4, 0, 0)) self._special_dates.add(datetime.datetime(2012, 9, 3, 0, 0)) self._special_dates.add(datetime.datetime(2012, 11, 22, 0, 0)) self._special_dates.add(datetime.datetime(2012, 12, 26, 0, 0))
def testStock(self): datafile = '%s/data/AC___CCB.csv' % os.path.dirname(__file__) i = Instrument('AC', datafile) pos = PositionTracker(i) pos.buy(datetime.datetime(2012, 12, 31), 100, 25.0, 7.95) self.assertEqual(pos.getBasis(), 2500.0) self.assertEqual(pos.getNetProfit(26.0, includeCommissions=True), 92.05) self.assertEqual(pos.getEntryDate(), datetime.datetime(2012, 12, 31)) pos.sell(datetime.datetime(2013, 1, 1), 100, 26.0, 7.95) self.assertEqual(pos.getBasis(), 2500.0) self.assertEqual(pos.getNetProfit(0.0, includeCommissions=True), 84.1) self.assertEqual(pos.getNetProfit(0.0, includeCommissions=False), 100.0) self.assertEqual(pos.getCommissions(), 15.90) self.assertEqual(pos.getExitDate(), datetime.datetime(2013, 1, 1)) self.assertAlmostEqual(pos.getReturn(0.0, includeCommissions=True), 0.0336, places=4) self.assertEqual(pos.getTradeSize(), 100) self.assertEqual(pos.getEntryPrice(), 25.0) self.assertEqual(pos.getExitPrice(), 26.0) pos.reset() pos.buy(datetime.datetime(2013, 1, 6), 100, 27.0, 7.95) self.assertEqual(pos.getBasis(), 2700.0) self.assertEqual(pos.getNetProfit(26.0, includeCommissions=True), -107.95) pos.sell(datetime.datetime(2013, 1, 7), 100, 26.0, 7.95) self.assertEqual(pos.getBasis(), 2700.0) self.assertEqual(pos.getNetProfit(0.0, includeCommissions=True), -115.90) self.assertAlmostEqual(pos.getReturn(0.0, includeCommissions=True), -0.0429, places=4) pos.reset() pos.sell(datetime.datetime(2013, 1, 10), 100, 28.0, 7.95) pos.buy(datetime.datetime(2013, 1, 11), 100, 26.0, 7.95) self.assertEqual(pos.getTradeSize(), -100) self.assertEqual(pos.getEntryPrice(), 28.0) self.assertEqual(pos.getExitPrice(), 26.0)
def setUp(self): datafile = '%s/data/AC___CCB-10d.csv' % os.path.dirname(__file__) self._feed = Feed(Instrument('AC',datafile))
def testAltDate(self): datafile = '%s/data/S2___CCB.csv' % os.path.dirname(__file__) i = Instrument('S2',datafile) i.load_data() self.assertAlmostEqual( i.bars()[0].close(), 1001.75 )
def setUp(self): datafile = '%s/data/AC___CCB.csv' % os.path.dirname(__file__) self._inst = Instrument('AC', datafile)