Пример #1
0
    def testTwoBarReturns_CloseClose(self):
        barFeed = yahoofeed.Feed()
        barFeed.setBarFilter(
            csvfeed.DateRangeFilter(datetime.datetime(2001, 12, 06),
                                    datetime.datetime(2001, 12, 07)))
        barFeed.addBarsFromCSV(
            ReturnsTestCase.TestInstrument,
            common.get_data_file_path("orcl-2001-yahoofinance.csv"))
        strat = strategy_test.TestStrategy(barFeed, 1000)

        # 2001-12-06,15.61,16.03,15.50,15.90,66944900,15.55
        # 2001-12-07,15.74,15.95,15.55,15.91,42463200,15.56
        # Manually place the entry order, to get it filled on the first bar.
        order = strat.getBroker().createMarketOrder(
            broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1,
            True)  # Close: 15.90
        strat.getBroker().placeOrder(order)
        strat.addOrder(datetime.datetime(2001, 12, 06),
                       strat.getBroker().createMarketOrder,
                       broker.Order.Action.SELL,
                       ReturnsTestCase.TestInstrument, 1, True)  # Close: 15.91

        returnsDS = returns.ReturnsDataSeries(strat)
        strat.run()
        self.assertTrue(strat.getBroker().getCash() == 1000 + (15.91 - 15.90))
        # First day returns: 0
        self.assertTrue(returnsDS.getValueAbsolute(0) == 0)
        # Second day returns: Open vs Prev. day's close
        self.assertTrue(
            returnsDS.getValueAbsolute(1) == (15.91 - 15.90) / 15.90)
Пример #2
0
from pyalgotrade import plotter
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.stratanalyzer import returns
import smacross_strategy

# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl", "orcl-2000.csv")

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

# Attach the plotter to the strategy.
plt = plotter.StrategyPlotter(myStrategy)
# Include the SMA in the instrument's subplot to get it displayed along with the closing prices.
plt.getInstrumentSubplot("orcl").addDataSeries("SMA", myStrategy.getSMA())
# Plot the strategy returns at each bar.
plt.getOrCreateSubplot("returns").addDataSeries(
    "Net return", returns.ReturnsDataSeries(myStrategy))
plt.getOrCreateSubplot("returns").addDataSeries(
    "Cum. return", returns.CumulativeReturnsDataSeries(myStrategy))

# Run the strategy.
myStrategy.run()
print "Final portfolio value: $%.2f" % myStrategy.getResult()

# Plot the strategy.
plt.plot()