Пример #1
0
    def testShort3(self):
        strat = self.__createStrategy()
        stratAnalyzer = trades.Trades()
        strat.attach_analyzer(stratAnalyzer)

        # Enter short
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 0),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.SELL_SHORT,
                       TradesAnalyzerTestCase.TestInstrument, 2)  # 127.14
        # Decrease short position
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 16),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY_TO_COVER,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.16
        # Exit short
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 30),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY_TO_COVER,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.2
        strat.run()

        self.assertTrue(
            round(strat.get_broker().get_cash(), 2) == round(
                1000 + (127.14 - 127.16) + (127.14 - 127.2), 2))

        self.assertTrue(stratAnalyzer.get_count() == 1)
        self.assertTrue(stratAnalyzer.get_even_count() == 0)

        self.assertTrue(round(stratAnalyzer.get_all().mean(), 2) == -0.08)

        self.assertTrue(stratAnalyzer.get_unprofitable_count() == 1)
        self.assertTrue(round(stratAnalyzer.get_losses().mean(), 2) == -0.08)

        self.assertTrue(stratAnalyzer.get_profitable_count() == 0)
Пример #2
0
    def testLong2(self):
        strat = self.__createStrategy()
        stratAnalyzer = trades.Trades()
        strat.attach_analyzer(stratAnalyzer)

        # Enter long
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 0),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.14
        # Extend long position
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 16),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.16
        # Exit long
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 30),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.SELL,
                       TradesAnalyzerTestCase.TestInstrument, 2)  # 127.2
        strat.run()

        self.assertTrue(
            round(strat.get_broker().get_cash(), 2) == round(
                1000 + (127.2 - 127.14) + (127.2 - 127.16), 2))

        self.assertTrue(stratAnalyzer.get_count() == 1)
        self.assertTrue(stratAnalyzer.get_even_count() == 0)

        self.assertTrue(round(stratAnalyzer.get_all().mean(), 2) == 0.1)

        self.assertTrue(stratAnalyzer.get_profitable_count() == 1)
        self.assertTrue(round(stratAnalyzer.get_profits().mean(), 2) == 0.1)

        self.assertTrue(stratAnalyzer.get_unprofitable_count() == 0)
Пример #3
0
    def testNoTrades(self):
        strat = self.__createStrategy()
        stratAnalyzer = trades.Trades()
        strat.attach_analyzer(stratAnalyzer)

        strat.run()

        self.assertTrue(strat.get_broker().get_cash() == 1000)

        self.assertTrue(stratAnalyzer.get_count() == 0)
        self.assertTrue(stratAnalyzer.get_even_count() == 0)
        self.assertTrue(stratAnalyzer.get_profitable_count() == 0)
        self.assertTrue(stratAnalyzer.get_unprofitable_count() == 0)
Пример #4
0
    def testSomeTradesWithCommissions(self):
        strat = self.__createStrategy()
        strat.get_broker().set_commission(backtesting.FixedCommission(0.01))
        stratAnalyzer = trades.Trades()
        strat.attach_analyzer(stratAnalyzer)

        # Losing trade
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 30),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.2
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 31),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.SELL,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.16
        # Winning trade
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 38),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.16
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 42),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.SELL,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.26
        # Open trade.
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 47),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.34
        strat.run()

        self.assertTrue(
            round(strat.get_broker().get_cash(), 2) == round(
                1000 + (127.16 - 127.2) + (127.26 - 127.16) - 127.34 -
                0.01 * 5, 2))
        self.assertTrue(
            numpy.array_equal(stratAnalyzer.get_commissions_for_all_trades(),
                              numpy.array([0.02, 0.02])))
        self.assertTrue(
            numpy.array_equal(
                stratAnalyzer.get_commissions_for_profitable_trades(),
                numpy.array([0.02])))
        self.assertTrue(
            numpy.array_equal(
                stratAnalyzer.get_commissions_for_unprofitable_trades(),
                numpy.array([0.02])))
        self.assertTrue(
            numpy.array_equal(stratAnalyzer.get_commissions_for_even_trades(),
                              numpy.array([])))
Пример #5
0
    def testLongShort(self):
        strat = self.__createStrategy()
        stratAnalyzer = trades.Trades()
        strat.attach_analyzer(stratAnalyzer)

        # Enter long
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 0),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.14
        # Exit long and enter short
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 16),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.SELL,
                       TradesAnalyzerTestCase.TestInstrument, 2)  # 127.16
        # Exit short
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 30),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY_TO_COVER,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.2
        strat.run()

        self.assertTrue(
            round(strat.get_broker().get_cash(), 2) == round(
                1000 + (127.16 - 127.14) + (127.16 - 127.2), 2))

        self.assertTrue(stratAnalyzer.get_count() == 2)
        self.assertTrue(stratAnalyzer.get_even_count() == 0)

        self.assertTrue(round(stratAnalyzer.get_all().mean(), 2) == -0.01)
        self.assertTrue(
            round(stratAnalyzer.get_all().std(ddof=1), 4) == 0.0424)

        self.assertTrue(stratAnalyzer.get_profitable_count() == 1)
        self.assertTrue(round(stratAnalyzer.get_profits().mean(), 2) == 0.02)

        self.assertTrue(stratAnalyzer.get_unprofitable_count() == 1)
        self.assertTrue(round(stratAnalyzer.get_losses().mean(), 2) == -0.04)
Пример #6
0
    def testSomeTrades_Position(self):
        strat = self.__createStrategy()
        stratAnalyzer = trades.Trades()
        strat.attach_analyzer(stratAnalyzer)

        # Winning trade
        strat.addPosEntry(buildUTCDateTime(2011, 1, 3, 15,
                                           0), strat.enter_long,
                          TradesAnalyzerTestCase.TestInstrument, 1)  # 127.14
        strat.addPosExit(buildUTCDateTime(2011, 1, 3, 15, 16),
                         strat.exit_position)  # 127.16
        # Losing trade
        strat.addPosEntry(buildUTCDateTime(2011, 1, 3, 15,
                                           30), strat.enter_long,
                          TradesAnalyzerTestCase.TestInstrument, 1)  # 127.2
        strat.addPosExit(buildUTCDateTime(2011, 1, 3, 15, 31),
                         strat.exit_position)  # 127.16
        # Winning trade
        strat.addPosEntry(buildUTCDateTime(2011, 1, 3, 15,
                                           38), strat.enter_long,
                          TradesAnalyzerTestCase.TestInstrument, 1)  # 127.16
        strat.addPosExit(buildUTCDateTime(2011, 1, 3, 15, 42),
                         strat.exit_position)  # 127.26
        # Unfinished trade not closed
        strat.addPosEntry(buildUTCDateTime(2011, 1, 3, 15,
                                           47), strat.enter_long,
                          TradesAnalyzerTestCase.TestInstrument, 1)  # 127.34
        strat.run()

        self.assertTrue(
            round(strat.get_broker().get_cash(), 2) == round(
                1000 + (127.16 - 127.14) + (127.16 - 127.2) +
                (127.26 - 127.16) - 127.34, 2))

        self.assertTrue(stratAnalyzer.get_count() == 3)
        self.assertTrue(stratAnalyzer.get_even_count() == 0)
        self.assertTrue(round(stratAnalyzer.get_all().mean(), 2) == 0.03)
        self.assertTrue(round(stratAnalyzer.get_all().std(ddof=1), 2) == 0.07)
        self.assertTrue(round(stratAnalyzer.get_all().std(ddof=0), 2) == 0.06)

        self.assertTrue(stratAnalyzer.get_profitable_count() == 2)
        self.assertTrue(round(stratAnalyzer.get_profits().mean(), 2) == 0.06)
        self.assertTrue(
            round(stratAnalyzer.get_profits().std(ddof=1), 2) == 0.06)
        self.assertTrue(
            round(stratAnalyzer.get_profits().std(ddof=0), 2) == 0.04)
        self.assertEqual(stratAnalyzer.get_positive_returns()[0],
                         (127.16 - 127.14) / 127.14)
        self.assertEqual(stratAnalyzer.get_positive_returns()[1],
                         (127.26 - 127.16) / 127.16)

        self.assertTrue(stratAnalyzer.get_unprofitable_count() == 1)
        self.assertTrue(round(stratAnalyzer.get_losses().mean(), 2) == -0.04)
        if version.LooseVersion(
                numpy.__version__) >= version.LooseVersion("1.6.2"):
            self.assertTrue(math.isnan(stratAnalyzer.get_losses().std(ddof=1)))
        else:
            self.assertTrue(stratAnalyzer.get_losses().std(ddof=1) == 0)
        self.assertTrue(stratAnalyzer.get_losses().std(ddof=0) == 0)
        self.assertEqual(stratAnalyzer.get_negative_returns()[0],
                         (127.16 - 127.2) / 127.2)
Пример #7
0
    def testSomeTrades(self):
        strat = self.__createStrategy()
        stratAnalyzer = trades.Trades()
        strat.attach_analyzer(stratAnalyzer)

        # Winning trade
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 0),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.14
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 16),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.SELL,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.16
        # Losing trade
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 30),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.2
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 31),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.SELL,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.16
        # Winning trade
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 38),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.16
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 42),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.SELL,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.26
        # Open trade.
        strat.addOrder(buildUTCDateTime(2011, 1, 3, 15, 47),
                       strat.get_broker().create_market_order,
                       broker.Order.Action.BUY,
                       TradesAnalyzerTestCase.TestInstrument, 1)  # 127.34
        strat.run()

        self.assertTrue(
            round(strat.get_broker().get_cash(), 2) == round(
                1000 + (127.16 - 127.14) + (127.16 - 127.2) +
                (127.26 - 127.16) - 127.34, 2))

        self.assertTrue(stratAnalyzer.get_count() == 3)
        self.assertTrue(stratAnalyzer.get_even_count() == 0)
        self.assertTrue(round(stratAnalyzer.get_all().mean(), 2) == 0.03)
        self.assertTrue(round(stratAnalyzer.get_all().std(ddof=1), 2) == 0.07)
        self.assertTrue(round(stratAnalyzer.get_all().std(ddof=0), 2) == 0.06)

        self.assertTrue(stratAnalyzer.get_profitable_count() == 2)
        self.assertTrue(round(stratAnalyzer.get_profits().mean(), 2) == 0.06)
        self.assertTrue(
            round(stratAnalyzer.get_profits().std(ddof=1), 2) == 0.06)
        self.assertTrue(
            round(stratAnalyzer.get_profits().std(ddof=0), 2) == 0.04)

        self.assertTrue(stratAnalyzer.get_unprofitable_count() == 1)
        self.assertTrue(round(stratAnalyzer.get_losses().mean(), 2) == -0.04)
        if version.LooseVersion(
                numpy.__version__) >= version.LooseVersion("1.6.2"):
            self.assertTrue(math.isnan(stratAnalyzer.get_losses().std(ddof=1)))
        else:
            self.assertTrue(stratAnalyzer.get_losses().std(ddof=1) == 0)
        self.assertTrue(stratAnalyzer.get_losses().std(ddof=0) == 0)
# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.add_bars_from_csv("orcl", "orcl-2000.csv")

# Evaluate the strategy with the feed's bars.
myStrategy = smacross_strategy.Strategy(feed, 20)

# Attach different analyzers to a strategy before executing it.
retAnalyzer = returns.Returns()
myStrategy.attach_analyzer(retAnalyzer)
sharpeRatioAnalyzer = sharpe.SharpeRatio()
myStrategy.attach_analyzer(sharpeRatioAnalyzer)
drawDownAnalyzer = drawdown.DrawDown()
myStrategy.attach_analyzer(drawDownAnalyzer)
tradesAnalyzer = trades.Trades()
myStrategy.attach_analyzer(tradesAnalyzer)

# Run the strategy.
myStrategy.run()

print "Final portfolio value: $%.2f" % myStrategy.get_result()
print "Cumulative returns: %.2f %%" % (
    retAnalyzer.get_cumulative_returns()[-1] * 100)
print "Sharpe ratio: %.2f" % (sharpeRatioAnalyzer.get_sharpe_ratio(0.05, 252))
print "Max. drawdown: %.2f %%" % (drawDownAnalyzer.get_max_draw_down() * 100)
print "Longest drawdown duration: %d days" % (
    drawDownAnalyzer.get_longest_draw_down_duration())

print
print "Total trades: %d" % (tradesAnalyzer.get_count())