def testGetCopyAccount(self): tc = TradingCenter() accountId1 = tc.createAccountWithMetrix(100000, 0) account = tc.getCopyAccount(accountId1) print(account) self.assertEquals(100000, account.cash)
def testPlaceOrder_invalidAccountId(self): tc = TradingCenter() order2 = Order(accountId = 'unknowAccount', side = Side.BUY, symbol = 'symbol', price = 13.25, share = 10) self.mock.ReplayAll() self.assertEquals(None, tc.placeOrder(order2) ) # invalid account id self.mock.VerifyAll()
def testGetOpenOrdersBySymbol(self): order1 = Order(accountId = 'accountId', side = Side.BUY, symbol = 'symbol1', price = 13.2, share = 10, orderId = 'id1') order2 = Order(accountId = 'accountId', side = Side.BUY, symbol = 'symbol1', price = 13.25, share = 10, orderId = 'id2') tc = TradingCenter() tc._TradingCenter__openOrders = {'symbol1': [order1, order2]} orders = tc.getOpenOrdersBySymbol('symbol1') self.assertEquals([order1, order2], orders)
def testGetCopyAccounts(self): tc = TradingCenter() tc.createAccountWithMetrix(100000, 0) tc.createAccountWithMetrix(200000, 0) accounts = tc.getCopyAccounts('.*') print([str(account) for account in accounts]) self.assertEquals(2, len(accounts))
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 testCancelAllOpenOrders(self): order1 = Order(accountId = 'accountId', side = Side.BUY, symbol = 'symbol1', price = 13.2, share = 10, orderId = 'id1') order2 = Order(accountId = 'accountId', side = Side.BUY, symbol = 'symbol1', price = 13.25, share = 10, orderId = 'id2') tc = TradingCenter() tc._TradingCenter__openOrders = {'symbol1': [order1, order2]} tc.cancelAllOpenOrders() self.assertEquals({}, tc._TradingCenter__openOrders) self.assertEquals({'id1': order1, 'id2': order2}, tc._TradingCenter__closedOrders)
def testPlaceOrder_invalidAccountId(self): tc = TradingCenter() order2 = Order(accountId='unknowAccount', side=Side.BUY, symbol='symbol', price=13.25, share=10) self.mock.ReplayAll() self.assertEquals(None, tc.placeOrder(order2)) # invalid account id self.mock.VerifyAll()
def testPlaceOrder_failed(self): tc = TradingCenter() accountId = 'accountId' order1 = Order(accountId = accountId, side = Side.BUY, symbol = 'symbol', price = 13.25, share = 10) account = self.mock.CreateMock(Account) account.validate(order1).AndReturn(False) tc._TradingCenter__accounts = {accountId: account} self.mock.ReplayAll() self.assertEquals(None, tc.placeOrder(order1) ) # True self.mock.VerifyAll()
def testIsOrderMet(self): tc = TradingCenter() tick1 = Tick('time', 'open', 'high', 'low', 13.20, 'volume') order1 = Order(accountId = None, side = Side.BUY, symbol = 'symbol', price = 13.25, share = 10) order2 = Order(accountId = None, side = Side.BUY, symbol = 'symbol', price = 13.15, share = 10) order3 = Order(accountId = None, side = Side.SELL, symbol = 'symbol', price = 13.25, share = 10) order4 = Order(accountId = None, side = Side.SELL, symbol = 'symbol', price = 13.15, share = 10) self.assertEquals(True, tc.isOrderMet(tick1, order1)) self.assertEquals(False, tc.isOrderMet(tick1, order2)) self.assertEquals(False, tc.isOrderMet(tick1, order3)) self.assertEquals(True, tc.isOrderMet(tick1, order4))
def testIsOrderMet(self): tc = TradingCenter() tick1 = Tick(datetime.now(), 30.0, 35.0, 13.20, 13.20, 100000) order1 = Order(accountId = None, side = Side.BUY, symbol = 'symbol', price = 13.25, share = 10) order2 = Order(accountId = None, side = Side.BUY, symbol = 'symbol', price = 13.15, share = 10) order3 = Order(accountId = None, side = Side.SELL, symbol = 'symbol', price = 13.25, share = 10) order4 = Order(accountId = None, side = Side.SELL, symbol = 'symbol', price = 13.15, share = 10) self.assertEquals(True, tc.isOrderMet(tick1, order1)) self.assertEquals(False, tc.isOrderMet(tick1, order2)) self.assertEquals(False, tc.isOrderMet(tick1, order3)) self.assertEquals(True, tc.isOrderMet(tick1, order4))
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 testPlaceOrder_failed(self): tc = TradingCenter() accountId = 'accountId' order1 = Order(accountId=accountId, side=Side.BUY, symbol='symbol', price=13.25, share=10) account = self.mock.CreateMock(Account) account.validate(order1).AndReturn(False) tc._TradingCenter__accounts = {accountId: account} self.mock.ReplayAll() self.assertEquals(None, tc.placeOrder(order1)) # True self.mock.VerifyAll()
def testValidOrder(self): tc = TradingCenter() accountId = 'accountId' 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) account = self.mock.CreateMock(Account) account.validate(order1).AndReturn(True) account.validate(order1).AndReturn(False) tc._TradingCenter__accounts = {accountId: account} self.mock.ReplayAll() self.assertEquals(False, tc.validateOrder(order2) ) # invalid account id self.assertEquals(True, tc.validateOrder(order1) ) # True self.assertEquals(False, tc.validateOrder(order1) ) # False self.mock.VerifyAll()
def testCancelOrder(self): order1 = Order(accountId='accountId', side=Side.BUY, symbol='symbol1', price=13.2, share=10, orderId='id1') order2 = Order(accountId='accountId', side=Side.BUY, symbol='symbol1', price=13.25, share=10, orderId='id2') tc = TradingCenter() tc._TradingCenter__openOrders = {'symbol1': [order1, order2]} tc.cancelOrder('id1') print(tc._TradingCenter__openOrders) print(tc._TradingCenter__closedOrders) self.assertEquals({'symbol1': [order2]}, tc._TradingCenter__openOrders) self.assertEquals({'id1': order1}, tc._TradingCenter__closedOrders) self.assertEquals(Order.CANCELED, order1.status) tc.cancelOrder('id2') print(tc._TradingCenter__openOrders) print(tc._TradingCenter__closedOrders) self.assertEquals({}, tc._TradingCenter__openOrders) self.assertEquals({ 'id1': order1, 'id2': order2 }, tc._TradingCenter__closedOrders)
def testGetOpenOrdersBySymbol(self): order1 = Order(accountId='accountId', side=Side.BUY, symbol='symbol1', price=13.2, share=10, orderId='id1') order2 = Order(accountId='accountId', side=Side.BUY, symbol='symbol1', price=13.25, share=10, orderId='id2') tc = TradingCenter() tc._TradingCenter__openOrders = {'symbol1': [order1, order2]} orders = tc.getOpenOrdersBySymbol('symbol1') self.assertEquals([order1, order2], orders)
def testPlaceOrder_existedOrder(self): tc = TradingCenter() accountId = 'accountId' order1 = Order(accountId=accountId, side=Side.BUY, symbol='symbol', price=13.25, share=10, orderId='orderId1') self.assertRaises(UfException, tc.placeOrder, order1)
def __init__(self, config, mCalculator, symbol): self.__accountManager = AccountManager() self.__tickFeeder = TickFeeder() self.__tradingCenter = TradingCenter() self.__tradingEngine = TradingEngine() self.__indexHelper = IndexHelper() self.__history = History() self.__saver = None self.__symbol = symbol self.__config = config self.__mCalculator = mCalculator
def testValidOrder(self): tc = TradingCenter() accountId = 'accountId' 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) account = self.mock.CreateMock(Account) account.validate(order1).AndReturn(True) account.validate(order1).AndReturn(False) tc._TradingCenter__accounts = {accountId: account} self.mock.ReplayAll() self.assertEquals(False, tc.validateOrder(order2)) # invalid account id self.assertEquals(True, tc.validateOrder(order1)) # True self.assertEquals(False, tc.validateOrder(order1)) # False self.mock.VerifyAll()
def __init__(self, config, metricManager, accounts, symbols, startTickDate, endTradeDate, cash): self.__accountManager = AccountManager() self.__accountId = None self.__startTickDate = startTickDate self.__endTradeDate = endTradeDate self.__tickFeeder = TickFeeder(start=startTickDate, end=endTradeDate) self.__tradingCenter = TradingCenter() self.__tradingEngine = TradingEngine() self.__indexHelper = IndexHelper() self.__accounts = accounts self.__history = History() self.__saver = None self.__symbols = symbols self.__config = config self.__metricManager = metricManager self.__cash = cash