def testSetSharesWithNoStartingCash(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( AnalyzerTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = test_strategy.BacktestingStrategy(barFeed, 0) initialShares = 2 initialPrice = 16.25 strat.getBroker().setShares(AnalyzerTestCase.TestInstrument, initialShares, initialPrice) # Close initial position closingPrice = 32.50 strat.scheduleCall( datetime.datetime(2001, 1, 4), lambda: strat.marketOrder( AnalyzerTestCase.TestInstrument, -initialShares)) stratAnalyzer = returns.Returns() strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertEqual( strat.getBroker().getShares(AnalyzerTestCase.TestInstrument), 0) self.assertEqual(strat.getBroker().getCash(), closingPrice * initialShares) # Check period returns. expectedPeriodReturns = [ (26.37 - initialPrice) / initialPrice, (32.00 - 26.37) / 26.37, (32.56 - 32.00) / 32.00, (32.50 - 32.56) / 32.56, 0, # Nothing should change moving forward 0, 0, ] for i, expectedReturn in enumerate(expectedPeriodReturns): self.assertEqual(stratAnalyzer.getReturns()[i], expectedReturn) self.assertEqual(stratAnalyzer.getReturns()[-1], expectedPeriodReturns[-1]) # Check cumulative returns. expectedCumulativeReturns = [ (26.37 - initialPrice) / initialPrice, (32.00 - initialPrice) / initialPrice, (32.56 - initialPrice) / initialPrice, (32.50 - initialPrice) / initialPrice, # Nothing should change moving forward (32.50 - initialPrice) / initialPrice, (32.50 - initialPrice) / initialPrice, ] for i, expectedReturn in enumerate(expectedCumulativeReturns): self.assertEqual(round(stratAnalyzer.getCumulativeReturns()[i], 4), round(expectedReturn, 4)) self.assertEqual(round(stratAnalyzer.getCumulativeReturns()[-1], 4), round(expectedCumulativeReturns[-1], 4))
def testSetSharesForUnusedInstrument(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( INSTRUMENT, common.get_data_file_path("orcl-2001-yahoofinance.csv") ) initialCash = 1000 strat = test_strategy.BacktestingStrategy(barFeed, balances={PRICE_CURRENCY: initialCash}) unkSharesSet = 10 unkSharePrice = 10.4 strat.getBroker().setShares("UNK/USD", unkSharesSet, unkSharePrice) # 33.06 strat.scheduleCall( datetime.datetime(2001, 1, 12), lambda: strat.marketOrder(INSTRUMENT, 1) ) # 14.32 strat.scheduleCall( datetime.datetime(2001, 11, 27), lambda: strat.marketOrder(INSTRUMENT, -1) ) stratAnalyzer = returns.Returns(PRICE_CURRENCY) strat.attachAnalyzer(stratAnalyzer) initialEquity = strat.getBroker().getEquity(PRICE_CURRENCY) strat.run() finalEquity = strat.getBroker().getEquity(PRICE_CURRENCY) # The unknown instrument should have no impact cash or returns. finalCash = strat.getBroker().getBalance(PRICE_CURRENCY) self.assertEqual( round(finalCash, 2), round(initialCash + (14.32 - 33.06), 2) ) self.assertEqual( round(stratAnalyzer.getCumulativeReturns()[-1], 4), round((finalEquity - initialEquity) / initialEquity, 4) ) self.assertEqual(stratAnalyzer.getReturns()[-1], 0) self.assertEqual(strat.getBroker().getBalance(SYMBOL), 0) self.assertEqual(strat.getBroker().getBalance("UNK"), unkSharesSet) self.assertEqual(strat.getBroker().getEquity(PRICE_CURRENCY), finalCash + unkSharesSet * unkSharePrice)