Example #1
0
 def setUp(self):
     self.entryDate = datetime(2015,11,15)
     self.exitDate = datetime(2015,11,21)
     self.trade = ScaleTrade("AAPL", self.entryDate, 100.0, 0.0, quantity=100)
     tradeEntry = TradeEntry(self.exitDate, -100, 110.0)
     self.trade.addEntry(tradeEntry)
Example #2
0
class ScaleTradeTest(unittest.TestCase):
    def setUp(self):
        self.entryDate = datetime(2015,11,15)
        self.exitDate = datetime(2015,11,21)
        self.trade = ScaleTrade("AAPL", self.entryDate, 100.0, 0.0, quantity=100)
        tradeEntry = TradeEntry(self.exitDate, -100, 110.0)
        self.trade.addEntry(tradeEntry)

    def test_get_entry_date(self):
        self.assertTrue(self.trade.getEntryDate() == self.entryDate)

    def test_get_exit_date(self):
        self.assertTrue(self.trade.getExitDate() == self.exitDate)

    def test_is_closed(self):
        self.assertTrue(self.trade.isClosed())

    def test_is_open(self):
        self.assertFalse(self.trade.is_open())

    def test_is_long(self):
        self.assertTrue(self.trade.is_long())

    def test_is_short(self):
        self.assertFalse(self.trade.is_short())

    def test_pandl(self):
        self.assertTrue(self.trade.pandl() == 1000.0)

    def test_pandl_percent(self):
        self.assertTrue(self.trade.pandlPercent() == 0.1)

    def test_adjust_account(self):
        settings = ConfigParser.ConfigParser()

        account = Account(settings)
        datastore = MockDataStore()
        positionSizer = MockPositionSizer()

        # run date before trade, should be no effect
        fromdt = datetime(2015,11,1)
        todt = datetime(2015,11,2)
        self.trade.adjust_account(datastore, fromdt, todt, account, positionSizer)
        self.assertTrue(account.cash==100000)

        # run date of entry, should be down 10k in cash
        fromdt = datetime(2015,11,15)
        todt = datetime(2015,11,16)
        self.trade.adjust_account(datastore, fromdt, todt, account, positionSizer)
        self.assertTrue(account.cash == 90000)

        # run during trade, should still be 90k
        fromdt = datetime(2015,11,18)
        todt = datetime(2015,11,19)
        self.trade.adjust_account(datastore, fromdt, todt, account, positionSizer)
        self.assertTrue(account.cash == 90000)

        # run exit, should be 110k
        fromdt = datetime(2015,11,21)
        todt = datetime(2015,11,22)
        self.trade.adjust_account(datastore, fromdt, todt, account, positionSizer)
        self.assertTrue(account.cash == 101000)

        # run post-exit, should still be 110k
        fromdt = datetime(2015,11,25)
        todt = datetime(2015,11,26)
        self.trade.adjust_account(datastore, fromdt, todt, account, positionSizer)
        self.assertTrue(account.cash == 101000)