def test_request_working_orders(fake_request_sender): # test including count and since order = fake_request_sender.request_completed_orders( account_info=AccountInfo(account_id=110), count=2, since=1536267034.) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 110 assert order.count == 2 assert order.since == pytest.approx(1536267034., rel=1e-2) # test including count, not since order = fake_request_sender.request_completed_orders( account_info=AccountInfo(account_id=110), count=2) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 110 assert order.count == 2 # test including since, not count order = fake_request_sender.request_completed_orders( account_info=AccountInfo(account_id=110), since=1536267034.) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 110 assert order.since == pytest.approx(1536267034., rel=1e-2) # test excluding both count and since order = fake_request_sender.request_completed_orders( account_info=AccountInfo(account_id=110)) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 110
def test_place_contingent_oco_order(fake_request_sender): order0 = Order(account_info=AccountInfo(account_id=100), client_order_id=str(8675309), client_order_link_id='a123', symbol='BTC/USD', side=Side.buy.name, order_type=OrderType.limit.name, quantity=1.1, price=6000.01, stop_price=0.0, time_in_force=TimeInForce.gtc.name, expire_at=0.0, leverage_type=LeverageType.none.name) order1 = Order(account_info=AccountInfo(account_id=100), client_order_id=str(8675310), client_order_link_id='a124', symbol='BTC/USD', side=Side.sell.name, order_type=OrderType.limit.name, quantity=1.1, price=6200.11, stop_price=0.0, time_in_force=TimeInForce.gtc.name, expire_at=0.0, leverage_type=LeverageType.none.name) contingent_order = OCO(orders=[order0, order1]) contingent_order_capnp = fake_request_sender.place_contingent_order( request_header=__FAKE_REQUEST_HEADER, contingent_order=contingent_order) assert type( contingent_order_capnp) == capnp.lib.capnp._DynamicStructBuilder oco_order0 = contingent_order_capnp.type.oco.orders[0] assert oco_order0.accountInfo.accountID == 100 assert oco_order0.symbol == 'BTC/USD' assert oco_order0.side == 'buy' assert oco_order0.orderType == 'limit' assert oco_order0.quantity == 1.1 assert oco_order0.price == 6000.01 assert oco_order0.stopPrice == 0.0 assert oco_order0.timeInForce == 'gtc' assert oco_order0.expireAt == 0.0 oco_order1 = contingent_order_capnp.type.oco.orders[1] assert oco_order1.accountInfo.accountID == 100 assert oco_order1.symbol == 'BTC/USD' assert oco_order1.side == 'sell' assert oco_order1.orderType == 'limit' assert oco_order1.quantity == 1.1 assert oco_order1.price == 6200.11 assert oco_order1.stopPrice == 0.0 assert oco_order1.timeInForce == 'gtc' assert oco_order1.expireAt == 0.0
def test_place_order(fake_request_sender): order = Order(account_info=AccountInfo(account_id=100), client_order_id=str(8675309), client_order_link_id='a123', symbol='BTC/USD', side=Side.buy.name, order_type=OrderType.limit.name, quantity=1.1, price=6000.01, stop_price=0.0, time_in_force=TimeInForce.gtc.name, expire_at=0.0, leverage_type=LeverageType.none.name) order = fake_request_sender.place_order( request_header=__FAKE_REQUEST_HEADER, order=order) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 100 assert order.symbol == 'BTC/USD' assert order.side == 'buy' assert order.orderType == 'limit' assert order.quantity == 1.1 assert order.price == 6000.01 assert order.stopPrice == 0.0 assert order.timeInForce == 'gtc' assert order.expireAt == 0.0
def test_place_order_margin_custom(fake_request_sender): custom_margin_order = Order(account_info=AccountInfo(account_id=100), client_order_id=str(9876), client_order_link_id='a123', symbol='BTC/USD', side=Side.buy.name, order_type=OrderType.market.name, quantity=1.1, price=0.0, time_in_force=TimeInForce.gtc.name, leverage_type=LeverageType.custom.name, leverage=2.0) # custom margin order = fake_request_sender.place_order( request_header=__FAKE_REQUEST_HEADER, order=custom_margin_order) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 100 assert order.symbol == 'BTC/USD' assert order.side == 'buy' assert order.orderType == 'market' assert order.quantity == 1.1 assert order.price == 0.0 assert order.timeInForce == 'gtc' assert order.leverageType == msgs_capnp.LeverageType.custom assert order.leverage == 2.0
def test_place_order_margin_default(fake_request_sender): default_margin_order = Order( account_info=AccountInfo(account_id=100), client_order_id=str(9876), client_order_link_id='a123', symbol='BTC/USD', side=Side.buy.name, order_type=OrderType.market.name, quantity=1.1, price=0.0, stop_price=0.0, time_in_force=TimeInForce.gtc.name, expire_at=0.0, leverage_type=LeverageType.exchangeDefault.name) # exchange default margin order = fake_request_sender.place_order(order=default_margin_order) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 100 assert order.symbol == 'BTC/USD' assert order.side == 'buy' assert order.orderType == 'market' assert order.quantity == 1.1 assert order.price == 0.0 assert order.timeInForce == 'gtc' assert order.leverageType == msgs_capnp.LeverageType.exchangeDefault assert order.leverage == 0.0 assert order.stopPrice == 0.0 assert order.expireAt == 0.0
def test_request_order_status(fake_request_sender): order = fake_request_sender.request_order_status( request_header=__FAKE_REQUEST_HEADER, account_info=AccountInfo(account_id=110), order_id='poiuytrewq123') assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 110 assert order.orderID == 'poiuytrewq123'
def test_cancel_all_orders_symbol(fake_request_sender): orders = fake_request_sender.cancel_all_orders( request_header=__FAKE_REQUEST_HEADER, account_info=AccountInfo(account_id=100), side=str(Side.buy.name)) assert type(orders) == capnp.lib.capnp._DynamicStructBuilder assert orders.accountInfo.accountID == 100 assert orders.side == str(Side.buy.name)
def test_cancel_order(fake_request_sender): order = fake_request_sender.cancel_order( request_header=__FAKE_REQUEST_HEADER, account_info=AccountInfo(account_id=100), order_id='c137') assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 100 assert order.orderID == 'c137'
def account_info_py(account_info): """ Converts a capnp AccountInfo to Python object. :param account_info: (capnp._DynamicStructBuilder) AccountInfo object. :return: (AccountInfo) Populated Python object. """ return AccountInfo(account_id=account_info.accountID, exchange_account_id=account_info.exchangeAccountID, account_type=str(account_info.accountType), exchange_client_id=account_info.exchangeClientID)
def main(): client_id = 1 sender_comp_id = str(uuid.uuid4()) omega_connection = configure_single_client_omega_connection( OMEGA_ENDPOINT, OMEGA_SERVER_KEY, client_id, sender_comp_id, PrintingResponseHandler()) omega_connection.start() omega_connection.wait_until_running() account_id = 2 api_key = "api_key" secret_key = "secret_key" passphrase = "passphrase" credentials = AccountCredentials(AccountInfo(account_id), api_key, secret_key, passphrase) omega_connection.logon([credentials]) omega_connection.send_heartbeat() order = Order( account_info=AccountInfo(account_id=account_id), # ID generated by client to keep track of the order client_order_id=str(123), client_order_link_id='test', # A str to identify and group orders symbol='ETH/USD', side=Side.sell.name, order_type=OrderType.market.name, # Optional param quantity=1.1, price=0.0, time_in_force=TimeInForce.gtc.name, leverage_type=LeverageType.none.name ) omega_connection.place_order(order) time.sleep(2) omega_connection.logoff() time.sleep(2) omega_connection.cleanup()
def test_replace_order(fake_request_sender): order = fake_request_sender.replace_order( account_info=AccountInfo(account_id=100), order_id='c137', quantity=1.1, order_type=OrderType.limit.name, price=6000.01, stop_price=0.0, time_in_force=TimeInForce.gtc.name, expire_at=0.0) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 100 assert order.orderID == 'c137' assert order.orderType == 'limit' assert order.quantity == 1.1 assert order.price == 6000.01 assert order.stopPrice == 0.0 assert order.timeInForce == 'gtc' assert order.expireAt == 0.0
def main(): client_id = 1 # sender_comp_id is a unique identifier. Omega supports the # use case of multiple omega_clients sending messages with the same # client_id, hence a sender_comp_id is needed to distinguish the machine # and client in the middle of a request and response communication. # Clients would have to manage their own client_id and sender_comp_id. client_id_machine_dict = dict() # The simplest approach is to generate a uuid per machine per clientId as # the senderCompId and store them in a dictionary. # Feel free to generate uuids with an approach that fits your use case, # but this is the recommended way by Python: if DISTRIBUTED_CLIENTS: # Unique uuid across different machines, taking into account the MAC # address sender_comp_id = str(uuid.uuid1()) else: # Random uuid sender_comp_id = str(uuid.uuid4()) client_id_machine_dict[client_id] = sender_comp_id omega_connection = configure_single_client_omega_connection( OMEGA_ENDPOINT, OMEGA_SERVER_KEY, client_id, sender_comp_id, PrintingResponseHandler()) omega_connection.start() omega_connection.wait_until_running() account_id = 2 api_key = "api_key" secret_key = "secret_key" passphrase = "passphrase" # Optional, only for certain exchanges credentials = AccountCredentials(AccountInfo(account_id), api_key, secret_key, passphrase) omega_connection.logon([credentials]) omega_connection.send_heartbeat() omega_connection.logoff() time.sleep(2) omega_connection.cleanup()
def test_place_contingent_opo_batch_order(fake_request_sender): order0 = Order(account_info=AccountInfo(account_id=100), client_order_id=str(8675309), client_order_link_id='a123', symbol='BTC/USD', side=Side.buy.name, order_type=OrderType.limit.name, quantity=1.1, price=6000.01, stop_price=0.0, time_in_force=TimeInForce.gtc.name, expire_at=0.0, leverage_type=LeverageType.none.name) order10 = Order(account_info=AccountInfo(account_id=100), client_order_id=str(8675310), client_order_link_id='a124', symbol='BTC/USD', side=Side.buy.name, order_type=OrderType.limit.name, quantity=1.1, price=5910.11, stop_price=0.0, time_in_force=TimeInForce.gtc.name, expire_at=0.0, leverage_type=LeverageType.none.name) order11 = Order(account_info=AccountInfo(account_id=100), client_order_id=str(8675310), client_order_link_id='a125', symbol='BTC/USD', side=Side.buy.name, order_type=OrderType.limit.name, quantity=1.1, price=5900.11, stop_price=0.0, time_in_force=TimeInForce.gtc.name, expire_at=0.0, leverage_type=LeverageType.none.name) contingent_order = OPO(primary=order0, secondary=Batch(orders=[order10, order11])) contingent_order_capnp = fake_request_sender.place_contingent_order( request_header=__FAKE_REQUEST_HEADER, contingent_order=contingent_order) assert type( contingent_order_capnp) == capnp.lib.capnp._DynamicStructBuilder primary_opo = contingent_order_capnp.type.opo.primary assert primary_opo.accountInfo.accountID == 100 assert primary_opo.symbol == 'BTC/USD' assert primary_opo.side == 'buy' assert primary_opo.orderType == 'limit' assert primary_opo.quantity == 1.1 assert primary_opo.price == 6000.01 assert primary_opo.stopPrice == 0.0 assert primary_opo.timeInForce == 'gtc' assert primary_opo.expireAt == 0.0 batch_order0 = contingent_order_capnp.type.opo.secondary.batch[0] assert batch_order0.accountInfo.accountID == 100 assert batch_order0.symbol == 'BTC/USD' assert batch_order0.side == 'buy' assert batch_order0.orderType == 'limit' assert batch_order0.quantity == 1.1 assert batch_order0.price == 5910.11 assert batch_order0.stopPrice == 0.0 assert batch_order0.timeInForce == 'gtc' assert batch_order0.expireAt == 0.0 batch_order1 = contingent_order_capnp.type.opo.secondary.batch[1] assert batch_order1.accountInfo.accountID == 100 assert batch_order1.symbol == 'BTC/USD' assert batch_order1.side == 'buy' assert batch_order1.orderType == 'limit' assert batch_order1.quantity == 1.1 assert batch_order1.price == 5900.11 assert batch_order1.stopPrice == 0.0 assert batch_order1.timeInForce == 'gtc' assert batch_order1.expireAt == 0.0
def main(): # The client_id is the id assigned by Fund3. It is unique # per client and one client can have multiple accounts (denoted by # account_id). client_id = 1 # Sender comp id is a uuid unique to each machine a client uses to identify # the machine and to route the appropriate responses back to the machine # that sent the request. sender_comp_id = str(uuid.uuid4()) # configure_single_client_omega_connection sets up a default TesConnection # with one default client_id # The ResponseHandler is a command dispatch callback class. Basically, # when the response is received from Omega, TesConnection will route it to # ResponseReceiver, and the type of the response will be determined. # Each type of response will trigger a specific function that can be # overridden in a child class of ResponseHandler for client designated # action upon receiving a certain type of response. E.g. updating # internal order status when ExecutionReport is received, updating # balance when balance is received etc. # See omega_client.messaging.response_handler and # omega_client.messaging.printing_response_handler (example child class # that just prints everything). omega_connection = configure_single_client_omega_connection( omega_endpoint=OMEGA_ENDPOINT, omega_server_key=OMEGA_SERVER_KEY, client_id=client_id, sender_comp_id=sender_comp_id, response_handler=PrintingResponseHandler()) # Starting the TesConnection thread. omega_connection.start() # Waiting for the TesConnection to be set up. omega_connection.wait_until_running() # Account id is assigned by Fund3 and is unique per exchange account. account_id = 2 # exchange API credentials api_key = "api_key" secret_key = "secret_key" passphrase = "passphrase" # Optional, only for certain exchanges. # Set up AccountCredentials object. credentials = AccountCredentials(account_info=AccountInfo(account_id), api_key=api_key, secret_key=secret_key, passphrase=passphrase) # Send logon message, which when received will start and update token for # session_refresher. session_refresher will run until stopped omega_connection.logon([credentials]) time.sleep(2) # send a heartbeat every minute for 2 hours (during which the session # should refresh at least once) minutes_left = 120 while minutes_left > 0: omega_connection.send_heartbeat() time.sleep(60) # stop and cleanup omega_connection.logoff() time.sleep(2) omega_connection.cleanup()
def test_request_open_positions(fake_request_sender): open_pos = fake_request_sender.request_open_positions( request_header=__FAKE_REQUEST_HEADER, account_info=AccountInfo(account_id=110)) assert type(open_pos) == capnp.lib.capnp._DynamicStructBuilder assert open_pos.accountInfo.accountID == 110
API_KEY = creds['FPG_API_KEY'] API_SECRET = creds['FPG_API_SECRET'] auth = FPGAuth(API_KEY, API_SECRET) # Hit an error trying to fetch a nonexistent order api_url = 'https://fund3-staging.floating.group/v1/orders/foo' r = requests.get(api_url, auth=auth) print(r.status_code) print(r.json()) # successful usage of create_SOR_order function orders, status_code, error_message = create_SOR_order( order=Order( account_info=AccountInfo(account_id=100), # ignored, junk client_order_id='vnuiebwe', # ignored, junk symbol='BTC/USD', side=Side.buy.name, order_type=OrderType.limit.name, quantity=6., price=5000.), accounts={Exchange.gemini.name: AccountInfo(account_id=200), Exchange.kraken.name: AccountInfo(account_id=201)}, auth=auth ) for order in orders: print('order:', order) print('status_code', status_code) print('error_message', error_message)
def test_omega_logon(fake_request_sender): creds = [ AccountCredentials(account_info=AccountInfo(account_id=100), api_key='fakeApiKey', secret_key='fakeSecret', passphrase='fakePassphrase'), AccountCredentials(account_info=AccountInfo(account_id=110), api_key='fakeApiKey', secret_key='fakeSecret', passphrase='fakePassphrase'), AccountCredentials(account_info=AccountInfo(account_id=200), api_key='fakeApiKey1', secret_key='fakeSecret1', passphrase='fakePassphrase1'), AccountCredentials(account_info=AccountInfo(account_id=210), api_key='fakeApiKey1', secret_key='fakeSecret1', passphrase='fakePassphrase1') ] fake_request_sender._omega_credentials = creds logon = fake_request_sender.logon(request_header=__FAKE_REQUEST_HEADER, client_secret=__FAKE_CLIENT_SECRET, credentials=creds) assert type(logon) == capnp.lib.capnp._DynamicStructBuilder assert logon.clientSecret == __FAKE_CLIENT_SECRET assert logon.credentials[0].accountInfo.accountID == 100 assert logon.credentials[0].apiKey == 'fakeApiKey' assert logon.credentials[0].secretKey == 'fakeSecret' assert logon.credentials[0].passphrase == 'fakePassphrase' assert logon.credentials[1].accountInfo.accountID == 110 assert logon.credentials[1].apiKey == 'fakeApiKey' assert logon.credentials[1].secretKey == 'fakeSecret' assert logon.credentials[1].passphrase == 'fakePassphrase' assert logon.credentials[2].accountInfo.accountID == 200 assert logon.credentials[2].apiKey == 'fakeApiKey1' assert logon.credentials[2].secretKey == 'fakeSecret1' assert logon.credentials[2].passphrase == 'fakePassphrase1' assert logon.credentials[3].accountInfo.accountID == 210 assert logon.credentials[3].apiKey == 'fakeApiKey1' assert logon.credentials[3].secretKey == 'fakeSecret1' assert logon.credentials[3].passphrase == 'fakePassphrase1' # logon missing passphrase - check for capnp default None creds1 = [ AccountCredentials(account_info=AccountInfo(account_id=100), api_key='fakeApiKey', secret_key='fakeSecret') ] fake_request_sender._omega_credentials = creds1 logon1 = fake_request_sender.logon(request_header=__FAKE_REQUEST_HEADER, client_secret=__FAKE_CLIENT_SECRET, credentials=creds1) assert type(logon) == capnp.lib.capnp._DynamicStructBuilder assert logon.clientSecret == __FAKE_CLIENT_SECRET assert logon1.credentials[0].accountInfo.accountID == 100 assert logon1.credentials[0].apiKey == 'fakeApiKey' assert logon1.credentials[0].secretKey == 'fakeSecret' # capnp default assert logon1.credentials[0].passphrase == '<NONE>' or \ logon1.credentials[0].passphrase == '' # logon missing apiKey - Attribute Error with pytest.raises(Exception or AttributeError): creds2 = [ AccountCredentials(account_info=AccountInfo(account_id=100), secret_key='fakeSecret') ] fake_request_sender._omega_credentials = creds2 logon2 = fake_request_sender.logon( request_header=__FAKE_REQUEST_HEADER, client_secret=__FAKE_CLIENT_SECRET, credentials=creds2) # logon missing apiSecret - Attribute Error with pytest.raises(Exception or AttributeError): creds3 = [ AccountCredentials(account_info=AccountInfo(account_id=100), api_key='fakeApiKey') ] fake_request_sender._omega_credentials = creds3 logon3 = fake_request_sender.logon( request_header=__FAKE_REQUEST_HEADER, client_secret=__FAKE_CLIENT_SECRET, credentials=creds3) fake_request_sender._omega_credentials = TEST_ACCOUNT_CREDS_1
import capnp import pytest import zmq import omega_protocol.Exchanges_capnp as exch_capnp import omega_protocol.TradeMessage_capnp as msgs_capnp from omega_client.communication.request_sender import RequestSender from omega_client.messaging.common_types import AccountBalancesReport, \ AccountCredentials, AccountDataReport, AccountInfo, AuthorizationRefresh, \ CompletedOrdersReport, Exchange, ExchangePropertiesReport, LeverageType, \ ExecutionReport, OpenPosition, OpenPositionsReport, Order, OrderInfo, \ OrderStatus, OrderType, RequestHeader, Side, SymbolProperties, \ TimeInForce, WorkingOrdersReport, Batch, OPO, OCO from omega_client.messaging.message_factory import heartbeat_capnp TEST_ACCOUNT_CREDS_1 = AccountCredentials(AccountInfo(0), api_key='api_key', secret_key='secret_key', passphrase='passphrase') TEST_OMEGA_CONFIG = { 'OMEGA_CONNECTION_STR': 'tcp://127.0.0.1:5555', 'CREDENTIALS': [TEST_ACCOUNT_CREDS_1] } TEST_ZMQ_ENCRYPTION_KEY = b'encryptionkeyencryptionkeyencryptionkeye' __FAKE_ROUTER_SOCKET_CONNECTION_STR = 'inproc://FAKE_ROUTER_SOCKET' __FAKE_DEALER_SOCKET_CONNECTION_STR = 'inproc://FAKE_DEALER_SOCKET' __FAKE_REQUEST_SENDER_CONNECTION_STR = 'inproc://FAKE_REQUEST_SENDER' __FAKE_CLIENT_SECRET = ('2B24_ih9IFVdWgxR2sEA3rj0fKlY212Ec_TwTNVCD663ktYb1' + 'ABPz4qJy0Ouze6O9vgdueei0XmZ6uGGFM34nw')
def test_request_working_orders(fake_request_sender): order = fake_request_sender.request_working_orders( request_header=__FAKE_REQUEST_HEADER, account_info=AccountInfo(account_id=110)) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 110
def test_request_account_balances(fake_request_sender): order = fake_request_sender.request_account_balances( account_info=AccountInfo(account_id=110)) assert type(order) == capnp.lib.capnp._DynamicStructBuilder assert order.accountInfo.accountID == 110