コード例 #1
0
    def testGetCopyAccount(self):
        tc = TradingCenter()
        accountId1 = tc.createAccountWithMetrix(100000, 0)

        account = tc.getCopyAccount(accountId1)
        print(account)
        self.assertEquals(100000, account.cash)
コード例 #2
0
    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()
コード例 #3
0
    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)
コード例 #4
0
    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))
コード例 #5
0
    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
コード例 #6
0
    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)
コード例 #7
0
    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()
コード例 #8
0
    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()
コード例 #9
0
    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))
コード例 #10
0
    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))
コード例 #11
0
    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()
コード例 #12
0
    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()
コード例 #13
0
    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()
コード例 #14
0
    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)
コード例 #15
0
    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)
コード例 #16
0
    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)
コード例 #17
0
 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
コード例 #18
0
    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()
コード例 #19
0
ファイル: backTester.py プロジェクト: zxlstoner/ultrafinance
 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