def testGetHoldingCost(self): share = 10 price = 9.1 account = Account(1000, 1) account._Account__holdings = {'stock1': (share, price)} holdingCost = account.getHoldingCost() self.assertAlmostEquals(share * price, holdingCost)
def testTotalValue(self): share = 10 price = 9.1 curPrice = 10.1 account = Account(1000, 1) account._Account__holdings = {'stock1': (share, price)} account.setLastTickDict({'stock1': Quote(0, 0, 0, 0, curPrice, 0, 0)}) totalValue = account.getTotalValue() self.assertAlmostEquals(1000 + share * curPrice, totalValue)
def testValidate(self): share = 10 price = 9.1 symbol = 'stock1' account = Account(1000, 1) account._Account__holdings = {symbol: (share, price)} # can't buy because price too high order1 = Order(accountId = 'id1', side = Side.BUY, symbol = symbol, price = 10000, share = 100000) self.assertEquals(False, account.validate(order1) ) # can't buy because of commission fee order1 = Order(accountId = 'id1', side = Side.BUY, symbol = symbol, price = 100, share = 10) self.assertEquals(False, account.validate(order1) ) # buy it order1 = Order(accountId = 'id1', side = Side.BUY, symbol = symbol, price = 100, share = 9) self.assertEquals(True, account.validate(order1) ) # can't sell because don't have the stock order1 = Order(accountId = 'id1', side = Side.SELL, symbol = 'fas89ph2', price = 100, share = 9) self.assertEquals(False, account.validate(order1) ) # can't sell because don't have the enough share order1 = Order(accountId = 'id1', side = Side.SELL, symbol = symbol, price = 100, share = 9000) self.assertEquals(False, account.validate(order1) ) # sell it order1 = Order(accountId = 'id1', side = Side.SELL, symbol = symbol, price = 100, share = 9) self.assertEquals(True, account.validate(order1) )
def testPlaceOrder_failed(self): tc = TradingCenter() account = Account(1000, 0) self.mock.StubOutWithMock(account, "validate") accountId = tc.accountManager.addAccount(account) order1 = Order(accountId = accountId, side = Side.BUY, symbol = 'symbol', price = 13.25, share = 10) account.validate(order1).AndReturn(False) self.mock.ReplayAll() self.assertEquals(None, tc.placeOrder(order1) ) # True self.mock.VerifyAll()
def createAccount(self, cash, commission=0): ''' create account ''' account = Account(cash, commission) self.__accounts[account.accountId] = account self.__accountPositions[account.accountId] = [ ] # list contains tuple (time, position) return account.accountId
def testValidOrder(self): tc = TradingCenter() account = Account(1000, 10) accountId = tc.accountManager.addAccount(account) order1 = Order(accountId = accountId, side = Side.BUY, symbol = 'symbol', price = 13.25, share = 10) order2 = Order(accountId = 'unknowAccount', side = Side.BUY, symbol = 'symbol', price = 13.25, share = 10) self.assertEquals(False, tc.validateOrder(order2) ) # invalid account id self.assertEquals(True, tc.validateOrder(order1) ) # True
def _setupStrategy(self): ''' setup tradingEngine''' strategy = StrategyFactory.createStrategy( self.__config.getOption(CONF_STRATEGY, CONF_STRATEGY_NAME), self.__config.getSection(CONF_STRATEGY)) strategy.setSymbols([self.__symbol]) strategy.indexHelper = self.__indexHelper strategy.history = self.__history #associate account accountId = self.__accountManager.addAccount( Account(BackTester.CASH, 0)) strategy.accountId = accountId #register on trading engine strategy.tradingEngine = self.__tradingEngine self.__tradingEngine.register(strategy)
def testGetCash(self): account = Account(1000, 1) self.assertEquals(1000, account.cash)