예제 #1
0
    def testControllerPosAlerts(self):
        plog = '%s/positions.csv' % os.path.dirname(__file__)
        elog = '%s/equity.csv' % os.path.dirname(__file__)
        rlog = '%s/returns.csv' % os.path.dirname(__file__)
        slog = '%s/summary.csv' % os.path.dirname(__file__)

        ctrl = ClenowController({ 'Ag-1' : ['RR'] }, plog, elog, rlog, summaryFile=slog)
        ctrl.run(datetime.datetime(2011,12,31),datetime.datetime(2012,8,1),datetime.datetime(2013,12,31))

        tlog = '%s/posalert.log' % os.path.dirname(__file__)
        ctrl.writePositionAlerts(tlog)

        self.assertTrue(test_util.file_compare('%s/posalert.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)

        tlog = '%s/trade7.log' % os.path.dirname(__file__)
        ctrl.writeAllTrades(tlog)
        
        self.assertTrue(test_util.file_compare('%s/trade7.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)
        
        tlog = '%s/tssb' % os.path.dirname(__file__)
        ctrl.writeTSSBTrades(tlog)

        self.assertTrue(test_util.file_compare('%s_long.reflog' % tlog, '%s_long.csv' % tlog))
        self.assertTrue(test_util.file_compare('%s_short.reflog' % tlog, '%s_short.csv' % tlog))
        os.remove('%s_long.csv' % tlog)
        os.remove('%s_short.csv' % tlog)
        os.remove(plog)
        os.remove(elog)
        os.remove(rlog)
        os.remove(slog)
예제 #2
0
    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)
예제 #3
0
    def testMacross(self):
        mf = MultiFeed()
        self.setupFeed(mf)
        strat = MACrossStrategy(mf,shortPeriod=10,longPeriod=100)
        
        # 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()

        tlog = '%s/trade5.log' % os.path.dirname(__file__)
        tradesAnalyzer.writeTradeLog(tlog)
        
        self.assertAlmostEqual(strat.getResult(),992595.00,places=2)
        self.assertEqual(tradesAnalyzer.getCount(),10)
        self.assertEqual(tradesAnalyzer.getProfitableCount(),4)
        self.assertEqual(tradesAnalyzer.getUnprofitableCount(),6)
        
        self.assertTrue(test_util.file_compare('%s/trade5.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)
예제 #4
0
    def testClenowTradeStart(self):
        mf = MultiFeed()
        self.setupFeed(mf)
        strat = ClenowBreakoutStrategy(mf,
                                       tradeStart=datetime.datetime(
                                           2012, 8, 11))

        # 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()
        strat.getBroker().executeSessionClose()

        tlog = '%s/trade3.log' % os.path.dirname(__file__)
        tradesAnalyzer.writeTradeLog(tlog)

        self.assertAlmostEqual(strat.getResult(), 986794.67, places=2)
        self.assertEqual(tradesAnalyzer.getCount(), 5)
        self.assertEqual(tradesAnalyzer.getProfitableCount(), 1)
        self.assertEqual(tradesAnalyzer.getUnprofitableCount(), 4)

        self.assertTrue(
            test_util.file_compare(
                '%s/trade3.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)
예제 #5
0
    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)
예제 #6
0
    def testClenowTradeStart(self):
        mf = MultiFeed()
        self.setupFeed(mf)
        strat = ClenowBreakoutStrategy(mf,tradeStart=datetime.datetime(2012,8,11))
        
        # 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()
        strat.getBroker().executeSessionClose()

        tlog = '%s/trade3.log' % os.path.dirname(__file__)
        tradesAnalyzer.writeTradeLog(tlog)
        
        self.assertAlmostEqual(strat.getResult(),986794.67,places=2)
        self.assertEqual(tradesAnalyzer.getCount(),5)
        self.assertEqual(tradesAnalyzer.getProfitableCount(),1)
        self.assertEqual(tradesAnalyzer.getUnprofitableCount(),4)
        
        self.assertTrue(test_util.file_compare('%s/trade3.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)
예제 #7
0
    def testControllerPosAlerts(self):
        plog = '%s/positions.csv' % os.path.dirname(__file__)
        elog = '%s/equity.csv' % os.path.dirname(__file__)
        rlog = '%s/returns.csv' % os.path.dirname(__file__)
        slog = '%s/summary.csv' % os.path.dirname(__file__)

        ctrl = ClenowController({'Ag-1': ['RR']},
                                plog,
                                elog,
                                rlog,
                                summaryFile=slog)
        ctrl.run(datetime.datetime(2011, 12,
                                   31), datetime.datetime(2012, 8, 1),
                 datetime.datetime(2013, 12, 31))

        tlog = '%s/posalert.log' % os.path.dirname(__file__)
        ctrl.writePositionAlerts(tlog)

        self.assertTrue(
            test_util.file_compare(
                '%s/posalert.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)

        tlog = '%s/trade7.log' % os.path.dirname(__file__)
        ctrl.writeAllTrades(tlog)

        self.assertTrue(
            test_util.file_compare(
                '%s/trade7.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)

        tlog = '%s/tssb' % os.path.dirname(__file__)
        ctrl.writeTSSBTrades(tlog)

        self.assertTrue(
            test_util.file_compare('%s_long.reflog' % tlog,
                                   '%s_long.csv' % tlog))
        self.assertTrue(
            test_util.file_compare('%s_short.reflog' % tlog,
                                   '%s_short.csv' % tlog))
        os.remove('%s_long.csv' % tlog)
        os.remove('%s_short.csv' % tlog)
        os.remove(plog)
        os.remove(elog)
        os.remove(rlog)
        os.remove(slog)
예제 #8
0
    def testClenowRunGroup(self):
        #crg = ClenowRunGroup(['RR','LH','O'])
        
        #crg.feed().start()
        #crg.strategy().exitPositions()
        plog = '%s/positions.csv' % os.path.dirname(__file__)
        elog = '%s/equity.csv' % os.path.dirname(__file__)
        rlog = '%s/returns.csv' % os.path.dirname(__file__)
        slog = '%s/summary.csv' % os.path.dirname(__file__)

        ctrl = ClenowController({ 'Ag-1' : ['RR','LH','O'], 'Ag-2' : ['LB','LC']}, plog, elog, rlog, summaryFile=slog)
        ctrl.run(datetime.datetime(2011,12,31),datetime.datetime(2012,8,1),datetime.datetime(2013,12,31))

        tlog = '%s/trade4.log' % os.path.dirname(__file__)
        ctrl.writeAllTrades(tlog)

        self.assertAlmostEqual(ctrl.get_net_profit(),ctrl.get_trade_profit(),places=2)
        
        crg = ctrl._runGroups['Ag-1']
        trades = crg.trades_analyzer()
        
        self.assertEqual(trades.getCount(),6)
        self.assertEqual(trades.getProfitableCount(),1)
        self.assertEqual(trades.getUnprofitableCount(),5)
        
        self.assertTrue(test_util.file_compare('%s/trade4.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)
        self.assertTrue(test_util.file_compare('%s/positions.refcsv' % os.path.dirname(__file__), plog))
        os.remove(plog)
        self.assertTrue(test_util.file_compare('%s/equity.refcsv' % os.path.dirname(__file__), elog))
        os.remove(elog)
        self.assertTrue(test_util.file_compare('%s/returns.refcsv' % os.path.dirname(__file__), rlog))
        os.remove(rlog)
        self.assertTrue(test_util.file_compare('%s/summary.refcsv' % os.path.dirname(__file__), slog))
        os.remove(slog)

        # now check the last open trades 
        pos_alerts = ctrl.get_position_alerts()
        self.assertEqual(len(pos_alerts),1)
        self.assertEqual(pos_alerts[0].stop,1476.35)
예제 #9
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)
예제 #10
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)
예제 #11
0
    def testClenowRunGroup(self):
        #crg = ClenowRunGroup(['RR','LH','O'])

        #crg.feed().start()
        #crg.strategy().exitPositions()
        plog = '%s/positions.csv' % os.path.dirname(__file__)
        elog = '%s/equity.csv' % os.path.dirname(__file__)
        rlog = '%s/returns.csv' % os.path.dirname(__file__)
        slog = '%s/summary.csv' % os.path.dirname(__file__)

        ctrl = ClenowController(
            {
                'Ag-1': ['RR', 'LH', 'O'],
                'Ag-2': ['LB', 'LC']
            },
            plog,
            elog,
            rlog,
            summaryFile=slog)
        ctrl.run(datetime.datetime(2011, 12,
                                   31), datetime.datetime(2012, 8, 1),
                 datetime.datetime(2013, 12, 31))

        tlog = '%s/trade4.log' % os.path.dirname(__file__)
        ctrl.writeAllTrades(tlog)

        self.assertAlmostEqual(ctrl.get_net_profit(),
                               ctrl.get_trade_profit(),
                               places=2)

        crg = ctrl._runGroups['Ag-1']
        trades = crg.trades_analyzer()

        self.assertEqual(trades.getCount(), 6)
        self.assertEqual(trades.getProfitableCount(), 1)
        self.assertEqual(trades.getUnprofitableCount(), 5)

        self.assertTrue(
            test_util.file_compare(
                '%s/trade4.reflog' % os.path.dirname(__file__), tlog))
        os.remove(tlog)
        self.assertTrue(
            test_util.file_compare(
                '%s/positions.refcsv' % os.path.dirname(__file__), plog))
        os.remove(plog)
        self.assertTrue(
            test_util.file_compare(
                '%s/equity.refcsv' % os.path.dirname(__file__), elog))
        os.remove(elog)
        self.assertTrue(
            test_util.file_compare(
                '%s/returns.refcsv' % os.path.dirname(__file__), rlog))
        os.remove(rlog)
        self.assertTrue(
            test_util.file_compare(
                '%s/summary.refcsv' % os.path.dirname(__file__), slog))
        os.remove(slog)

        # now check the last open trades
        pos_alerts = ctrl.get_position_alerts()
        self.assertEqual(len(pos_alerts), 1)
        self.assertEqual(pos_alerts[0].stop, 1476.35)