is there a way to close all positions at once from the openapi?

Created at 12 Dec 2024, 05:26
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!
WW

wwzz

Joined 12.12.2024

is there a way to close all positions at once from the openapi?
12 Dec 2024, 05:26


Hi, I am currently using ProtoOAReconcileReq() to build a position list and ProtoOAClosePositionReq() to close positions in a loop. This process takes about 10 seconds to close approximately 50 orders. I noticed that the "close" button in the desktop app only takes about 1-2 seconds for the same number of orders. I wonder if I am doing something wrong or if there is a better way to do this. Thanks!


@wwzz
Replies

swapd0
15 Dec 2024, 18:42

Why you don't already have the position list? Try to create the list when your application start.

Also, for each ProtoOAClosePositionReq() message, are you waiting for the response or just “send and forget” style?

 


@swapd0

PanagiotisCharalampous
16 Dec 2024, 08:27

Hi there,

Please share your source code so that we can understand what you are doing.

Best regards,

Panagiotis


@PanagiotisCharalampous

wwzz
16 Dec 2024, 18:20 ( Updated at: 17 Dec 2024, 07:44 )

RE: is there a way to close all positions at once from the openapi?

PanagiotisCharalampous said: 

Hi there,

Please share your source code so that we can understand what you are doing.

Best regards,

Panagiotis

Hi, Thanks for your reply. Here are my code - python + jupyter notebook + crochet + win11 (not the best setup I know…)

########message call back to extract position list

    elif message.payloadType == ProtoOAReconcileRes().payloadType:
       global position_list;
       position_list = Protobuf.extract(message)

########create a dictionary contains volumn and position id #

request = ProtoOAReconcileReq()
request.ctidTraderAccountId = ctrader_log["AccountId"]
client.send(request, responseTimeoutInSeconds = 1)
close_list = {};
for p in position_list.position:
   close_list[p.positionId] = [p.tradeData.volume];

########loop the close list to close all positions #
for c in close_list:
   request = ProtoOAClosePositionReq()
   request.ctidTraderAccountId = ctrader_log["AccountId"]
   request.positionId = c
   request.volume = close_list[c][0]
   client.send(request, responseTimeoutInSeconds = 10)

   

this process takes about 10 seconds to close 50 positions (5 per second), way below 50/second limit. 

maybe the open api doesn't like python loop? I also tried joblib / ThreadPoolExecutor/ multiprocess to do this in parallel, but no luck. Thanks!


@wwzz

wwzz
16 Dec 2024, 18:27 ( Updated at: 17 Dec 2024, 07:44 )

RE: is there a way to close all positions at once from the openapi?

swapd0 said: 

Why you don't already have the position list? Try to create the list when your application start.

Also, for each ProtoOAClosePositionReq() message, are you waiting for the response or just “send and forget” style?

 

Hi, Thanks for reply - I am using python + crochet. I am creating the position list as part of code. and for every request I have to wait for response (My bot trade 5 mins bars, need to open/close on everything 4:59)


@wwzz

PanagiotisCharalampous
17 Dec 2024, 08:14

RE: RE: is there a way to close all positions at once from the openapi?

wwzz said: 

swapd0 said: 

Why you don't already have the position list? Try to create the list when your application start.

Also, for each ProtoOAClosePositionReq() message, are you waiting for the response or just “send and forget” style?

 

Hi, Thanks for reply - I am using python + crochet. I am creating the position list as part of code. and for every request I have to wait for response (My bot trade 5 mins bars, need to open/close on everything 4:59)

Hi there,

I am not familiar with python but it seems that your send() method is synchronous i.e. waits for a response before it proceeds to the next step. You need to use an asynchronous sending method so that all messages are dispatched immediately without waiting for a response.

Best regards,

Panagiotis


@PanagiotisCharalampous

wwzz
21 Dec 2024, 02:17 ( Updated at: 23 Dec 2024, 00:18 )

RE: RE: RE: is there a way to close all positions at once from the openapi?

PanagiotisCharalampous said: 

wwzz said: 

swapd0 said: 

Why you don't already have the position list? Try to create the list when your application start.

Also, for each ProtoOAClosePositionReq() message, are you waiting for the response or just “send and forget” style?

 

Hi, Thanks for reply - I am using python + crochet. I am creating the position list as part of code. and for every request I have to wait for response (My bot trade 5 mins bars, need to open/close on everything 4:59)

Hi there,

I am not familiar with python but it seems that your send() method is synchronous i.e. waits for a response before it proceeds to the next step. You need to use an asynchronous sending method so that all messages are dispatched immediately without waiting for a response.

Best regards,

Panagiotis

Thanks Panagiotis, I am switching to ctrader's fix API - it seems be able to avoid the sync/async issue completely . 


@wwzz