def testFirstLastBoundary(self): f1 = Feed(self._inst1) f2 = Feed(self._inst2) mf = MultiFeed() mf.register_feed(f1) mf.register_feed(f2) mf.subscribe(self.on_bars_basic) self._count = 0 mf.start(first=datetime.datetime(2012, 7, 2), last=datetime.datetime(2012, 7, 2)) self.assertEqual(self._count, 1) self.assertEqual(mf.get_next_bars_date(), datetime.datetime(2012, 7, 3)) self._count = 0 mf.start(first=datetime.datetime(2012, 7, 1), last=datetime.datetime(2012, 7, 2)) self.assertEqual(self._count, 1) self.assertEqual(mf.get_next_bars_date(), datetime.datetime(2012, 7, 3)) # 7/1 is not a trading day so first bar is actually 7/2 which is after last self._count = 0 mf.start(first=datetime.datetime(2012, 7, 1), last=datetime.datetime(2012, 7, 1)) self.assertEqual(self._count, 0) self.assertEqual(mf.get_next_bars_date(), datetime.datetime(2012, 7, 2))
def testNeg(self): f1 = Feed(self._inst1) f2 = Feed(self._inst1) mf = MultiFeed() mf.register_feed(f1) # this should fail because both feeds are for the same symbol with self.assertRaises(Exception): mf.register_feed(f2)
def setUp(self): manifest = '%s/data/manifest1.csv' % os.path.dirname(__file__) self._db = InstrumentDb.Instance() self._db.load(manifest) self._feed = Feed(self._db.get('AC')) self._feed.insert(talibfunc.SMA('SMA50', self._feed, 50)) self._feed.insert(talibfunc.SMA('SMA100', self._feed, 100)) self._feed.insert(talibfunc.MAX('MAX50', self._feed, 50)) self._feed.insert(talibfunc.MIN('MIN50', self._feed, 50)) self._feed.insert(talibfunc.ATR('ATR10', self._feed, 10))
def testFirstOption(self): f1 = Feed(self._inst1) f2 = Feed(self._inst2) mf = MultiFeed() mf.register_feed(f1) mf.register_feed(f2) self._count = 0 mf.subscribe(self.on_bars_basic) mf.start(first=datetime.datetime(2012, 7, 1)) self.assertEqual(self._count, 143)
def testBasic2(self): w = Feed(self._inst) w.insert(talibfunc.MAX('MAX50', w, 50)) w.insert(talibfunc.MAX('MAX25', w, 25)) w.insert(talibfunc.MIN('MIN50', w, 50)) w.insert(talibfunc.MIN('MIN25', w, 25)) self.assertEqual(len(w.values()), 10) self.assertEqual(len(w.values()[0]), 252) self.assertAlmostEqual(w.get_series('MAX50')[232], 2.459) self.assertAlmostEqual(w.get_series('MAX25')[231], 2.448) self.assertAlmostEqual(w.get_series('MIN50')[230], 2.267) self.assertAlmostEqual(w.get_series('MIN25')[229], 2.289)
def testFirstLast(self): f1 = Feed(self._inst1) f2 = Feed(self._inst2) mf = MultiFeed() mf.register_feed(f1) mf.register_feed(f2) self._count = 0 mf.subscribe(self.on_bars_basic) mf.start(first=datetime.datetime(2012, 7, 1), last=datetime.datetime(2012, 7, 31)) self.assertEqual(self._count, 22) self.assertEqual(mf.get_last_close('AC'), 2.565)
def testBasic(self): f1 = Feed(self._inst1) f2 = Feed(self._inst2) mf = MultiFeed() mf.register_feed(f1) mf.register_feed(f2) self.assertEqual(f1, mf.get_feed('AC')) self._count = 0 mf.subscribe(self.on_bars_basic) mf.start() self.assertEqual(self._count, 257)
def testCursor(self): w = Feed(self._inst) w.insert( talibfunc.ATR('ATR10',w,10) ) w.set_cursor(None) self.assertEqual(w.get_last_close(), 0.0 ) b = w.get_current_bar() self.assertEqual(b.datetime(), datetime.datetime(2012,1,23,0,0)) self.assertTrue(math.isnan(b.ATR10())) self.assertEqual(w.get_next_bar_date(), datetime.datetime(2012,1,24,0,0)) count = 1 while w.get_next_bar_date()!= None: count += 1 lastbar = w.get_current_bar() self.assertEqual(count, 252) self.assertEqual(lastbar.datetime(),datetime.datetime(2013,1,18,0,0))
def testPriceVelocity(self): w = Feed(self._inst) w.insert( talibfunc.SMA('SMA50',w,50)) w.insert( PriceVelocity('PVEL',period=10,baseIndicator='SMA50') ) self.assertEqual( len(w.values()), 8 ) self.assertEqual( len(w.values()[0]), 252 ) self.assertAlmostEqual( w.get_series('PVEL')[251], -0.001576, places=4 ) feedout = '%s/writefeed.csv' % os.path.dirname(__file__) wf = open(feedout,'w') w.write_csv(wf) wf.close() self.assertTrue(test_util.file_compare('%s/writefeed.refcsv' % os.path.dirname(__file__), feedout)) os.remove(feedout)
def testBasic2(self): w = Feed(self._inst) w.insert( talibfunc.MAX('MAX50',w,50) ) w.insert( talibfunc.MAX('MAX25',w,25) ) w.insert( talibfunc.MIN('MIN50',w,50) ) w.insert( talibfunc.MIN('MIN25',w,25) ) self.assertEqual( len(w.values()), 10 ) self.assertEqual( len(w.values()[0]), 252 ) self.assertAlmostEqual( w.get_series('MAX50')[232], 2.459 ) self.assertAlmostEqual( w.get_series('MAX25')[231], 2.448 ) self.assertAlmostEqual( w.get_series('MIN50')[230], 2.267 ) self.assertAlmostEqual( w.get_series('MIN25')[229], 2.289 )
def testBasic3(self): w = Feed(self._inst) w.insert(talibfunc.ATR('ATR10', w, 10)) self.assertEqual(len(w.values()), 7) self.assertEqual(len(w.values()[0]), 252) self.assertAlmostEqual(w.get_series('ATR10')[251], 0.031196, places=6)
def __init__(self, sectorMap, modelType=None, cash=1000000, tradeStart=None, compounding=True, positionsFile=None, equityFile=None, returnsFile=None, summaryFile=None, parms=None): self._runGroups = {} self._startingCash = cash self._db = InstrumentDb.Instance() self._feed = MultiFeed() self._broker = BacktestingFuturesBroker( cash, self._feed, commission=FuturesCommission(2.50)) show = True for sec in sectorMap: for sym in sectorMap[sec]: self._feed.register_feed(Feed(self._db.get(sym))) # if desired can instantiate a strategy per symbol - may actually # want to think about this as a default behavior. The only thing # impacted is reporting of sector results # strategySymbols = { sym } # rgkey = '%s-%s' % (sec, sym) strategySymbols = sectorMap[sec] rgkey = sec if not modelType: modelType = 'breakout' strategy = StrategyFactory.create(modelType, self._feed, symbols=strategySymbols, broker=self._broker, cash=cash, compounding=compounding, parms=parms) self._runGroups[rgkey] = InstrumentedStrategy(strategy) if show: show = False strategy.showParms() self._trading = False self._posfile = self.__openFileorNull(positionsFile) self._equityfile = self.__openFileorNull(equityFile) self._returnsfile = self.__openFileorNull(returnsFile) self._summaryfile = self.__openFileorNull(summaryFile) self._dd = DrawDown() self._dd.attached(self) self._positionAlerts = []
def testCum(self): w = Feed(self._inst) w.insert(talibfunc.SMA('SMA50', w, 50)) w.insert(CUM(name='cum2(SMA50)', period=2, baseIndicator='SMA50')) # just going to spot check a few values randomly (but hard-coded) # we know that the base indicator is NAN up through at least 50 cum = w.get_series('cum2(SMA50)') base = w.get_series('SMA50') self.assertAlmostEqual(cum[79], base[79] + base[78], places=4) self.assertAlmostEqual(cum[112], base[112] + base[111], places=4) self.assertAlmostEqual(cum[176], base[176] + base[175], places=4)
def setUp(self): manifest = '%s/data/manifest1.csv' % os.path.dirname(__file__) self._db = InstrumentDb.Instance() self._db.load(manifest) self._feed = Feed(self._db.get('AC')) self._feed.insert( talibfunc.SMA('SMA50',self._feed,50) ) self._feed.insert( talibfunc.SMA('SMA100',self._feed,100) ) self._feed.insert( talibfunc.MAX('MAX50',self._feed,50) ) self._feed.insert( talibfunc.MIN('MIN50',self._feed,50) ) self._feed.insert( talibfunc.ATR('ATR10',self._feed,10) )
def setUp(self): manifest = "%s/data/manifest1.csv" % os.path.dirname(__file__) self._db = InstrumentDb.Instance() self._db.load(manifest) self._feed = Feed(self._db.get("RR")) self._feed.insert(talibfunc.SMA("SMA50", self._feed, 50)) self._feed.insert(talibfunc.SMA("SMA100", self._feed, 100)) self._feed.insert(talibfunc.MAX("MAX50", self._feed, 50)) self._feed.insert(talibfunc.MIN("MIN50", self._feed, 50)) self._feed.insert(talibfunc.ATR("ATR10", self._feed, 10))
def testMarketOrder(self): self._placed_markorder = False mf = MultiFeed() mf.register_feed(Feed(InstrumentDb.Instance().get('AC'))) self._broker = BacktestingFuturesBroker(1000000, mf) mf.subscribe(self.on_bars_1) self._broker.getOrderUpdatedEvent().subscribe(self.on_order_update_1) mf.start() self.assertEqual(self._broker.getCash(), 1000015.0) self.assertEqual(self._broker.calc_margin(), 160000.0)
def testShortEntry(self): self._placed_markorder = False mf = MultiFeed() mf.register_feed(Feed(InstrumentDb.Instance().get('CT'))) self._broker = BacktestingFuturesBroker(1000000, mf) mf.subscribe(self.on_bars_3) self._broker.getOrderUpdatedEvent().subscribe(self.on_order_update_3) mf.start() self.assertAlmostEqual(self._broker.getCash(), 1119750.0, places=2) self.assertEqual(self._broker.calc_margin(), 800000.0)
def testMarketOrderMarginCall(self): self._placed_markorder = False mf = MultiFeed() mf.register_feed(Feed(InstrumentDb.Instance().get('CT'))) self._broker = BacktestingFuturesBroker(1000000, mf) mf.subscribe(self.on_bars_2) self._broker.getOrderUpdatedEvent().subscribe(self.on_order_update_2) with self.assertRaisesRegexp(Exception, "Margin Call"): mf.start() self.assertAlmostEqual(self._broker.getCash(), 214000.0, places=2) self.assertEqual(self._broker.calc_margin(), 800000.0)
def testBasic3(self): w = Feed(self._inst) w.insert( talibfunc.ATR('ATR10',w,10) ) self.assertEqual( len(w.values()), 7 ) self.assertEqual( len(w.values()[0]), 252 ) self.assertAlmostEqual( w.get_series('ATR10')[251], 0.031196, places=6 )
def testDup(self): w = Feed(self._inst) w.insert(ATR()) with self.assertRaisesRegexp(Exception, "Workspace already has an indicator.*"): w.insert(ATR())
def testCum(self): w = Feed(self._inst) w.insert( talibfunc.SMA('SMA50',w,50)) w.insert( CUM( name='cum2(SMA50)', period=2, baseIndicator='SMA50') ) # just going to spot check a few values randomly (but hard-coded) # we know that the base indicator is NAN up through at least 50 cum = w.get_series('cum2(SMA50)') base = w.get_series('SMA50') self.assertAlmostEqual( cum[79], base[79] + base[78], places=4 ) self.assertAlmostEqual( cum[112], base[112] + base[111], places=4 ) self.assertAlmostEqual( cum[176], base[176] + base[175], places=4 )
class Test(unittest.TestCase): def setUp(self): manifest = '%s/data/manifest1.csv' % os.path.dirname(__file__) self._db = InstrumentDb.Instance() self._db.load(manifest) self._feed = Feed(self._db.get('RR')) self._feed.insert(talibfunc.SMA('SMA50', self._feed, 50)) self._feed.insert(talibfunc.SMA('SMA100', self._feed, 100)) self._feed.insert(talibfunc.MAX('MAX50', self._feed, 50)) self._feed.insert(talibfunc.MIN('MIN50', self._feed, 50)) self._feed.insert(talibfunc.ATR('ATR10', self._feed, 10)) def tearDown(self): pass def testBasic(self): mf = MultiFeed() mf.register_feed(self._feed) strat = MyStrategy(mf) # Attach a few analyzers to the strategy before executing it. retAnalyzer = returns.Returns() strat.attachAnalyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) drawDownAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() strat.attachAnalyzer(tradesAnalyzer) mf.start() strat.exitPositions() self.assertAlmostEqual(strat.getResult(), 996193.19, places=2) self.assertEqual(tradesAnalyzer.getCount(), 2) self.assertEqual(tradesAnalyzer.getProfitableCount(), 0) self.assertEqual(tradesAnalyzer.getUnprofitableCount(), 2) tlog = '%s/trade.log' % os.path.dirname(__file__) tradesAnalyzer.writeTradeLog(tlog) self.assertTrue( test_util.file_compare( '%s/trade1.reflog' % os.path.dirname(__file__), tlog)) os.remove(tlog)
class Test(unittest.TestCase): def setUp(self): manifest = "%s/data/manifest1.csv" % os.path.dirname(__file__) self._db = InstrumentDb.Instance() self._db.load(manifest) self._feed = Feed(self._db.get("RR")) self._feed.insert(talibfunc.SMA("SMA50", self._feed, 50)) self._feed.insert(talibfunc.SMA("SMA100", self._feed, 100)) self._feed.insert(talibfunc.MAX("MAX50", self._feed, 50)) self._feed.insert(talibfunc.MIN("MIN50", self._feed, 50)) self._feed.insert(talibfunc.ATR("ATR10", self._feed, 10)) def tearDown(self): pass def testBasic(self): mf = MultiFeed() mf.register_feed(self._feed) strat = MyStrategy(mf) # Attach a few analyzers to the strategy before executing it. retAnalyzer = returns.Returns() strat.attachAnalyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) drawDownAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() strat.attachAnalyzer(tradesAnalyzer) mf.start() strat.exitPositions() self.assertAlmostEqual(strat.getResult(), 996193.19, places=2) self.assertEqual(tradesAnalyzer.getCount(), 2) self.assertEqual(tradesAnalyzer.getProfitableCount(), 0) self.assertEqual(tradesAnalyzer.getUnprofitableCount(), 2) tlog = "%s/trade.log" % os.path.dirname(__file__) tradesAnalyzer.writeTradeLog(tlog) self.assertTrue(test_util.file_compare("%s/trade1.reflog" % os.path.dirname(__file__), tlog)) os.remove(tlog)
class Test(unittest.TestCase): def setUp(self): manifest = '%s/data/manifest1.csv' % os.path.dirname(__file__) self._db = InstrumentDb.Instance() self._db.load(manifest) self._feed = Feed(self._db.get('AC')) self._feed.insert( talibfunc.SMA('SMA50',self._feed,50) ) self._feed.insert( talibfunc.SMA('SMA100',self._feed,100) ) self._feed.insert( talibfunc.MAX('MAX50',self._feed,50) ) self._feed.insert( talibfunc.MIN('MIN50',self._feed,50) ) self._feed.insert( talibfunc.ATR('ATR10',self._feed,10) ) def tearDown(self): pass def testBasic(self): mf = MultiFeed() mf.register_feed(self._feed) strat = MyStrategy(mf) # Attach a few analyzers to the strategy before executing it. retAnalyzer = returns.Returns() strat.attachAnalyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) drawDownAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() strat.attachAnalyzer(tradesAnalyzer) mf.start() self.assertAlmostEqual(strat.getResult(),1007071.67,places=2) self.assertEqual(tradesAnalyzer.getCount(),1) self.assertEqual(tradesAnalyzer.getProfitableCount(),1) self.assertEqual(tradesAnalyzer.getUnprofitableCount(),0)
def testPriceVelocity(self): w = Feed(self._inst) w.insert(talibfunc.SMA('SMA50', w, 50)) w.insert(PriceVelocity('PVEL', period=10, baseIndicator='SMA50')) self.assertEqual(len(w.values()), 8) self.assertEqual(len(w.values()[0]), 252) self.assertAlmostEqual(w.get_series('PVEL')[251], -0.001576, places=4) feedout = '%s/writefeed.csv' % os.path.dirname(__file__) wf = open(feedout, 'w') w.write_csv(wf) wf.close() self.assertTrue( test_util.file_compare( '%s/writefeed.refcsv' % os.path.dirname(__file__), feedout)) os.remove(feedout)
def testBasic(self): w = Feed(self._inst) w.insert( ATR() ) w.insert( talibfunc.SMA('SMA10',w,10)) self.assertEqual( len(w.values()), 8 ) self.assertEqual( len(w.values()[0]), 252 ) self.assertEqual( w.values()[0][251], datetime.datetime(2013,1,18,0,0) ) self.assertTrue( math.isnan(w.get_series('ATR')[5])) self.assertTrue( math.isnan(w.get_series('ATR')[98])) self.assertAlmostEqual( w.get_series('ATR')[99], 0.03493, places=5 ) self.assertAlmostEqual( w.get_series('ATR')[251], 0.03307, places=5 ) self.assertAlmostEqual( w.get_series('SMA10')[251], 2.2921 ) self.assertAlmostEqual( w.get_series('SMA10')[188], 2.3989 )
def testBadNewSeries(self): w = Feed(self._inst) # this add_series should fail because the length doesn't match with self.assertRaisesRegexp(Exception,"Error adding new series.*"): w._add_series('foobar', [1,2,3,4,5])
def testBasic(self): w = Feed(self._inst) w.insert(ATR()) w.insert(talibfunc.SMA('SMA10', w, 10)) self.assertEqual(len(w.values()), 8) self.assertEqual(len(w.values()[0]), 252) self.assertEqual(w.values()[0][251], datetime.datetime(2013, 1, 18, 0, 0)) self.assertTrue(math.isnan(w.get_series('ATR')[5])) self.assertTrue(math.isnan(w.get_series('ATR')[98])) self.assertAlmostEqual(w.get_series('ATR')[99], 0.03493, places=5) self.assertAlmostEqual(w.get_series('ATR')[251], 0.03307, places=5) self.assertAlmostEqual(w.get_series('SMA10')[251], 2.2921) self.assertAlmostEqual(w.get_series('SMA10')[188], 2.3989)
def testNeg(self): w = Feed(self._inst) with self.assertRaisesRegexp(Exception, "Workspace does not have a.*series in"): w.get_series('notexist')
def setUp(self): datafile = '%s/data/AC___CCB-10d.csv' % os.path.dirname(__file__) self._feed = Feed(Instrument('AC',datafile))
def testCursor2(self): w = Feed(self._inst) w.set_cursor(datetime.datetime(2012,6,29,0,0)) self.assertEqual(w.get_next_bar_date(), datetime.datetime(2012,6,29,0,0)) self.assertEqual(w.get_last_close(), 2.182 )
def setUp(self): manifest = '%s/data/manifest.csv' % os.path.dirname(__file__) self._db = InstrumentDb.Instance() self._db.load(manifest) self._feed = Feed(self._db.get('AC'))
def testNeg(self): w = Feed(self._inst) with self.assertRaisesRegexp(Exception,"Workspace does not have a.*series in"): w.get_series('notexist')
def testBadNewSeries(self): w = Feed(self._inst) # this add_series should fail because the length doesn't match with self.assertRaisesRegexp(Exception, "Error adding new series.*"): w._add_series('foobar', [1, 2, 3, 4, 5])
def setupFeed(self, barFeed): barFeed.register_feed(Feed(self._db.get('RR')))
def testCursor2(self): w = Feed(self._inst) w.set_cursor(datetime.datetime(2012, 6, 29, 0, 0)) self.assertEqual(w.get_next_bar_date(), datetime.datetime(2012, 6, 29, 0, 0)) self.assertEqual(w.get_last_close(), 2.182)
def testCursor(self): w = Feed(self._inst) w.insert(talibfunc.ATR('ATR10', w, 10)) w.set_cursor(None) self.assertEqual(w.get_last_close(), 0.0) b = w.get_current_bar() self.assertEqual(b.datetime(), datetime.datetime(2012, 1, 23, 0, 0)) self.assertTrue(math.isnan(b.ATR10())) self.assertEqual(w.get_next_bar_date(), datetime.datetime(2012, 1, 24, 0, 0)) count = 1 while w.get_next_bar_date() != None: count += 1 lastbar = w.get_current_bar() self.assertEqual(count, 252) self.assertEqual(lastbar.datetime(), datetime.datetime(2013, 1, 18, 0, 0))
def testDup(self): w = Feed(self._inst) w.insert( ATR() ) with self.assertRaisesRegexp(Exception,"Workspace already has an indicator.*"): w.insert( ATR() )