class SellTest(unittest.TestCase): def setUp(self): self.xapi = XApi() def _confirm(self, result): print(result) @unittest.skip("Enable this when Sell breaks") def testSellBinance(self): self.xapi.sell(Exchange.BNC, BaseQuotePair('WAVES', 'BTC'), 1, 1, True) @unittest.skip("Enable this when Sell breaks") def testSellBittrex(self): self.xapi.sell(Exchange.BTX, BaseQuotePair('XMR', 'BTC'), 0.05, 0.03, True).addCallbacks(self._confirm, self._confirm)
class BuyTest(unittest.TestCase): def setUp(self): self.xapi = XApi() def _confirm(self, result): print(result) @unittest.skip("Enable this when Buy breaks") def testBuyBittrex(self): self.xapi.buy(Exchange.BTX, BaseQuotePair('XMR', 'BTC'), 0.05, 0.0247, False).addCallbacks(self._confirm, self._confirm) @unittest.skip("Enable this when Buy breaks") def testBuyBinance(self): self.xapi.buy(Exchange.BNC, BaseQuotePair('ETH', 'BTC'), 0.001, 0.00047, False).addErrback(lambda error: self.fail(error))
def setUp(self): self.callbacks = [LtsTradeCallbacks(), FileLogger(b"C:/tmp/log")] self.lts = XApi("QuantBox_LTS_Trade.dll") self.lts.debug = True if not self.lts.init(): print self.lts.get_last_error() else: server = ServerInfoField() server.BrokerID = b"2011" server.Address = b"tcp://211.144.195.163:44505" server.UserProductInfo = b"" server.AuthCode = b"" user = UserInfoField() user.UserID = b"020000000352" user.Password = b"123321" self.lts.connect(b"c:\\tmp\\lts", server, user) self.lts.set_callbacks(self.callbacks)
def config_md(): """ 行情信息 :return: """ print('config_md') # 行情连接 api = XApi(r'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\XAPI_CPP_x64.dll', 'md') api.ServerInfo.Address = br'tcp://180.168.146.187:13040' api.ServerInfo.BrokerID = b'9999' api.UserInfo.UserID = b'140786' api.UserInfo.Password = b'123abc' return api
def config_td(): """ 交易信息 :return: """ print('config_td') # 行情连接 api = XApi(r'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\XAPI_CPP_x64.dll', 'td') api.ServerInfo.Address = br'tcp://218.202.237.33:10102' api.ServerInfo.BrokerID = b'9999' api.UserInfo.UserID = b'037505' api.UserInfo.UserID = b'654321' api.UserInfo.Password = b'123456' return api
def config_td(): """ 交易信息 :return: """ print('config_td') # 行情连接 api = XApi(r'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\XAPI_CPP_x64.dll', 'td') api.ServerInfo.Address = br'tcp://180.168.146.187:13030' api.ServerInfo.BrokerID = b'9999' api.ServerInfo.AppID = b'8500342533' api.ServerInfo.AuthCode = b'0000000000000000' api.UserInfo.UserID = b'140786' api.UserInfo.Password = b'123456' return api
class TransferTest(unittest.TestCase): def setUp(self): self.xapi = XApi(debug=True) def tearDown(self): pass def test_address_retrieval(self): BNC_BTC_BASE_ADDRESS = '1J42EbVnN3UsKjL5EX2GR3HKF2fQxKn5CP' BTX_BTC_BASE_ADDRESS = '1LRKuCUzQ2mN2BiHxYzxKQhjiLx4o5pszY' BNC_XLM_BASE_ADDRESS = 'GAHK7EEG2WWHVKDNT4CEQFZGKF2LGDSW2IVM4S5DP42RBW3K6BTODB4A' BNC_XLM_ALT_ADDRESS = '1048079343' BTX_XLM_BASE_ADDRESS = 'GB6YPGW5JFMMP2QB2USQ33EUWTXVL4ZT5ITUNCY3YKVWOJPP57CANOF3' BTX_XLM_ALT_ADDRESS = 'f10286d56c3a4bd8843' bnc_btc_result = self.xapi.get_deposit_address(Exchange.BNC, 'BTC') btx_btc_result = self.xapi.get_deposit_address(Exchange.BTX, 'BTC') bnc_xlm_result = self.xapi.get_deposit_address(Exchange.BNC, 'XLM') btx_xlm_result = self.xapi.get_deposit_address(Exchange.BTX, 'XLM') # Correct currency self.assertTrue(bnc_btc_result['currency'] == 'BTC') self.assertTrue(btx_btc_result['currency'] == 'BTC') self.assertTrue(bnc_xlm_result['currency'] == 'XLM') self.assertTrue(btx_xlm_result['currency'] == 'XLM') # Base address for currencies self.assertTrue(bnc_btc_result['base_address'] == BNC_BTC_BASE_ADDRESS) self.assertTrue(btx_btc_result['base_address'] == BTX_BTC_BASE_ADDRESS) self.assertTrue(bnc_xlm_result['base_address'] == BNC_XLM_BASE_ADDRESS) self.assertTrue(btx_xlm_result['base_address'] == BTX_XLM_BASE_ADDRESS) # Alt address for curries that require it self.assertTrue(bnc_xlm_result['alt_address'] == BNC_XLM_ALT_ADDRESS) self.assertTrue(btx_xlm_result['alt_address'] == BTX_XLM_ALT_ADDRESS) @unittest.skip("Enable this when transfer breaks") def test_transfer(self): # Eventually integrate with get_holding to confirm withdrawal print('Before: ') print(self.xapi.get_holdings(Exchange.BTX)) print(self.xapi.get_holdings(Exchange.BNC)) self.assertTrue( self.xapi.transfer(Exchange.BTX, Exchange.BNC, 'XLM', 21)) self.assertTrue( self.xapi.transfer(Exchange.BNC, Exchange.BTX, 'XLM', 21)) print('AFTER: ') print(self.xapi.get_holdings(Exchange.BTX)) print(self.xapi.get_holdings(Exchange.BNC))
def config_md(): """ 行情信息 :return: """ print('config_md') # 行情连接 api = XApi(r'C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\x64\XAPI_CPP_x64.dll', 'md') api.ServerInfo.Address = br'tcp://218.202.237.33:10012' api.ServerInfo.BrokerID = b'9999' api.UserInfo.UserID = b'654321' api.UserInfo.Password = b'123456' api.ServerInfo.Address = br'tcp://140.207.238.1:41213' api.ServerInfo.BrokerID = b'8000' api.UserInfo.UserID = b'654321' api.UserInfo.Password = b'123456' return api
class LtsTradeTests(unittest.TestCase): lts = None instrument_ids = [b"600623", b'600601', b'600268', b'600818', b'603997', b'600234'] exchange_id = b"SSE" def setUp(self): self.callbacks = [LtsTradeCallbacks(), FileLogger(b"C:/tmp/log")] self.lts = XApi("QuantBox_LTS_Trade.dll") self.lts.debug = True if not self.lts.init(): print self.lts.get_last_error() else: server = ServerInfoField() server.BrokerID = b"2011" server.Address = b"tcp://211.144.195.163:44505" server.UserProductInfo = b"" server.AuthCode = b"" user = UserInfoField() user.UserID = b"020000000352" user.Password = b"123321" self.lts.connect(b"c:\\tmp\\lts", server, user) self.lts.set_callbacks(self.callbacks) def tearDown(self): self.lts.disconnect() self.callbacks[1].flush() # 处理消息队列 def process(self, max_wait=60, stop_condition=lambda: False): itr = 0 while itr < max_wait and not stop_condition(): itr += 1 time.sleep(1) def test_get_api_name(self): self.assertEqual(self.lts.get_api_name(), 'LTS') def test_connect(self): self.process(5, lambda: self.lts.is_connected) self.assertTrue(self.lts.is_connected, msg='Login Failed!') def test_get_trading_account(self): self.test_connect() self.lts.req_qry_trading_account() self.process(10, stop_condition=lambda: self.callbacks[0].trading_account is not None) self.assertNotEqual(self.callbacks[0].trading_account, None, msg='Get Trading Account Failed!') def test_get_future(self): self.test_connect() self.lts.req_qry_instrument(instrument_id=self.instrument_ids[0], exchange_id=self.exchange_id) self.process(stop_condition=lambda: self.callbacks[0].future_detail is not None) self.assertNotEqual(self.callbacks[0].future_detail, None, msg='Get Future Failed!') def test_get_investor_position(self): """ Works only afer querying trading account or send orders. """ self.test_connect() self.lts.req_qry_trading_account() self.lts.req_qry_investor_position(instrument_id=self.instrument_ids[0], exchange_id=self.exchange_id) self.process(stop_condition=lambda: self.callbacks[0].investor_position is not None) self.assertNotEqual(self.callbacks[0].investor_position, None, msg='Get Investor Position info Failed!') def test_order(self): self.test_connect() order = OrderField() order.InstrumentID = self.instrument_ids[0] order.ExchangeID = self.exchange_id order.Type = Limit order.Side = Buy order.OpenClose = Open order.HedgeFlag = Hedge order.Qty = 1000 order.Price = 30 order.StopPx = 31 order.TimeInForce = Day order_id = create_string_buffer(64) self.lts.send_order(order, order_id, 1) print "Create Order: %s" % order_id.value self.process(stop_condition=lambda: self.callbacks[0].order is not None and self.callbacks[0].order.ID == order_id) self.assertNotEqual(self.callbacks[0].order, None, msg='Send Order Failed!') cancel_order_id = create_string_buffer(64) self.lts.cancel_order(order_id, cancel_order_id, 1) print "Cancel Order: %s" % cancel_order_id.value self.process(stop_condition=lambda: self.callbacks[0].cancel_order) self.assertTrue(self.callbacks[0].cancel_order, msg="Cancel Order Failed")
def setUp(self): self.xapi = XApi()
def setUp(self): self.xapi = XApi(debug=True)
class LtsTradeTests(unittest.TestCase): lts = None instrument_ids = [ b"600623", b'600601', b'600268', b'600818', b'603997', b'600234' ] exchange_id = b"SSE" def setUp(self): self.callbacks = [LtsTradeCallbacks(), FileLogger(b"C:/tmp/log")] self.lts = XApi("QuantBox_LTS_Trade.dll") self.lts.debug = True if not self.lts.init(): print self.lts.get_last_error() else: server = ServerInfoField() server.BrokerID = b"2011" server.Address = b"tcp://211.144.195.163:44505" server.UserProductInfo = b"" server.AuthCode = b"" user = UserInfoField() user.UserID = b"020000000352" user.Password = b"123321" self.lts.connect(b"c:\\tmp\\lts", server, user) self.lts.set_callbacks(self.callbacks) def tearDown(self): self.lts.disconnect() self.callbacks[1].flush() # 处理消息队列 def process(self, max_wait=60, stop_condition=lambda: False): itr = 0 while itr < max_wait and not stop_condition(): itr += 1 time.sleep(1) def test_get_api_name(self): self.assertEqual(self.lts.get_api_name(), 'LTS') def test_connect(self): self.process(5, lambda: self.lts.is_connected) self.assertTrue(self.lts.is_connected, msg='Login Failed!') def test_get_trading_account(self): self.test_connect() self.lts.req_qry_trading_account() self.process(10, stop_condition=lambda: self.callbacks[0].trading_account is not None) self.assertNotEqual(self.callbacks[0].trading_account, None, msg='Get Trading Account Failed!') def test_get_future(self): self.test_connect() self.lts.req_qry_instrument(instrument_id=self.instrument_ids[0], exchange_id=self.exchange_id) self.process( stop_condition=lambda: self.callbacks[0].future_detail is not None) self.assertNotEqual(self.callbacks[0].future_detail, None, msg='Get Future Failed!') def test_get_investor_position(self): """ Works only afer querying trading account or send orders. """ self.test_connect() self.lts.req_qry_trading_account() self.lts.req_qry_investor_position( instrument_id=self.instrument_ids[0], exchange_id=self.exchange_id) self.process(stop_condition=lambda: self.callbacks[0].investor_position is not None) self.assertNotEqual(self.callbacks[0].investor_position, None, msg='Get Investor Position info Failed!') def test_order(self): self.test_connect() order = OrderField() order.InstrumentID = self.instrument_ids[0] order.ExchangeID = self.exchange_id order.Type = Limit order.Side = Buy order.OpenClose = Open order.HedgeFlag = Hedge order.Qty = 1000 order.Price = 30 order.StopPx = 31 order.TimeInForce = Day order_id = create_string_buffer(64) self.lts.send_order(order, order_id, 1) print "Create Order: %s" % order_id.value self.process(stop_condition=lambda: self.callbacks[0].order is not None and self.callbacks[0].order.ID == order_id) self.assertNotEqual(self.callbacks[0].order, None, msg='Send Order Failed!') cancel_order_id = create_string_buffer(64) self.lts.cancel_order(order_id, cancel_order_id, 1) print "Cancel Order: %s" % cancel_order_id.value self.process(stop_condition=lambda: self.callbacks[0].cancel_order) self.assertTrue(self.callbacks[0].cancel_order, msg="Cancel Order Failed")
def test_holding(self): # Consider changing tests to something more general self.assertTrue(len(XApi().get_holdings(Exchange.BTX)) > 0) self.assertTrue(len(XApi().get_holdings(Exchange.BNC)) > 0)
class LtsMarketTests(unittest.TestCase): lts = None instrument_ids = [b"600109", b'600601', b'600268', b'600818', b'603997', b'600234'] exchange_id = b"SSE" def setUp(self): self.callbacks = [LtsMarketCallbacks(), FileLogger(b"C:/tmp/log")] self.lts = XApi("QuantBox_LTS_Quote.dll") self.lts.debug = True if not self.lts.init(): print self.lts.get_last_error() else: server = ServerInfoField() server.BrokerID = b"2011" server.Address = b"tcp://211.144.195.163:44513" server.UserProductInfo = b"" server.AuthCode = b"" user = UserInfoField() user.UserID = b"020000000352" user.Password = b"123" self.lts.connect(b"c:\\tmp\\lts", server, user) self.lts.set_callbacks(self.callbacks) def tearDown(self): self.lts.disconnect() self.callbacks[1].flush() # 处理消息队列 def process(self, max_wait=60, stop_condition=lambda: False): itr = 0 while itr < max_wait and not stop_condition(): itr += 1 time.sleep(1) def test_get_api_name(self): self.assertEqual(self.lts.get_api_name(), 'LTS') def test_connect(self): self.process(5, lambda: self.lts.is_connected) self.assertTrue(self.lts.is_connected, msg='Login Failed!') def test_depth_market_data_single(self): self.test_connect() self.lts.subscribe(instrument_ids=self.instrument_ids[0], exchange_id=self.exchange_id) self.process(10, stop_condition=lambda: len(self.callbacks[0].market_data.get(self.instrument_ids[0], [])) > 10) self.lts.unsubscribe(self.instrument_ids[0]) self.assertNotEqual(len(self.callbacks[0].market_data.get(self.instrument_ids[0], [])), 0, msg='Get Depth Market Data Failed!') def test_depth_market_data_multi(self): self.test_connect() self.lts.subscribe(instrument_ids=self.instrument_ids[:5], exchange_id=self.exchange_id) self.process(stop_condition=lambda: len(self.callbacks[0].market_data) > 10) self.lts.unsubscribe(self.instrument_ids[:5]) for ins in self.instrument_ids[:5]: self.assertNotEqual(len(self.callbacks[0].market_data.get(ins, [])), 0, msg='Get Depth Market Data for %s Failed!' % ins)