OpenApi Python SDK message queue

Created at 14 Sep 2023, 18:14
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
SJ

Sjored

Joined 03.02.2023

OpenApi Python SDK message queue
14 Sep 2023, 18:14


Hello dear fellow software developers and traders!

 

Looking at the Best Practices section of “Establishing a Connection” of the API documentation, I'm reading this i.a.:



Use a message queue for sending/receiving data to avoid concurrent send/receive events.

 

I'm using the python SDK in my python application (not surprisingly :-)) with a TCP client constructed like this:

client = Client([cTraderEndpointHost], EndPoints.PROTOBUF_PORT, TcpProtocol)

What I want to do is call the client to create multiple orders in one invocation, for instance in loop, see the code snipped below. 

Does anyone know if calling the client.send in a loop is a good practice? 

Is it, under the hood, translated into adding a message to a queue when I do it like this using the python SDK? 

Calling the client in a loop, will mean many call in one instance, possibly causing issues if no message queueing is used I suppose.

 

Please let me know if you have some knowledge about this, it would really help me! 

(or maybe the clever people at https://ctrader.com/u/Spotware can give me some advice? :))

Thanks guys :-)

Code snippet:

    for orderAsString in ordersList:    	request = ProtoOANewOrderReq()    	request.ctidTraderAccountId = int(accountId)    	    	# ...  set other request paramerters extracted from the orderAsString field    	    	deferred = client.send(request, clientMsgId = clientMsgId)    	    	deferred.addErrback(onError)    	return deferred  							

@Sjored
Replies

the.innovative214
25 Sep 2023, 16:32

Not really am expert. I'm new to this myself but I learned due to the asynchronous nature of the connection, it doesn't seem to like typical loops… I have used recursion instead such as if I had a list of forex pairs for example I would pop off the first one in the list, do whatever work I wanted on it and then call the original definition again until I had popped off all the forex_pairs and then once the list is empty I stop the reactor. Example…

def define forex_pairs

    forex_pairs = [some list here]

     process_forex_pairs()

def Process_forex_pairs

     forex_pair = next popped off from the list of forex_pairs

     if forex_pairs not empty

            execute_trade(forex_pair)

      else

             reactor.stop

def execute_trade(forex_pair)

    do stufff here

     when finished..

    process_forex_pairs()

 

This is a pseudo idea of the  recursion I am using. Anyone else feel free to chime in and correct.

    

 


@the.innovative214

Sjored
10 Oct 2023, 21:09 ( Updated at: 11 Oct 2023, 06:33 )

RE: OpenApi Python SDK message queue

Thanks a lot the.innovative214 !

I really appreciate that you shared your knowledge and experience. And indeed, it doesn't seem to go well when I use a loop. So I'll try the recursive thing :-).

Kind regards,

Sjored.

 

 

 

the.innovative214 said: 

Not really am expert. I'm new to this myself but I learned due to the asynchronous nature of the connection, it doesn't seem to like typical loops… I have used recursion instead such as if I had a list of forex pairs for example I would pop off the first one in the list, do whatever work I wanted on it and then call the original definition again until I had popped off all the forex_pairs and then once the list is empty I stop the reactor. Example…

def define forex_pairs

    forex_pairs = [some list here]

     process_forex_pairs()

def Process_forex_pairs

     forex_pair = next popped off from the list of forex_pairs

     if forex_pairs not empty

            execute_trade(forex_pair)

      else

             reactor.stop

def execute_trade(forex_pair)

    do stufff here

     when finished..

    process_forex_pairs()

 

This is a pseudo idea of the  recursion I am using. Anyone else feel free to chime in and correct.

    

 

 


@Sjored