Dealing with a problem concerning time and orders

Hello there:



As you all know, there are times when a specified order is not always filled, for those cases… Is it possible to write a method in python so that it sends an order and cancel the order if it does get filled ( it can get partially filled or not filled at all) after a specified ammount of time (let's say one minute)… ??



Can you help me with that??

Hi Ghery, you can try this approach:



Create a function that does the following:

    - Access your order book 

    - Filter the orders with status as 'pending' or 'open' (depending on the broker API you are using)

    - For the open orders, calculate the difference between the time of order placed and the current time 

    - Make a list of orderIDs with a time difference  greater than 1 min 

    - Using the cancel_orders function (a standard function provided by all trading APIs), cancel the orders using the list of orderIDs with a time difference greater than 1 min 



The above function should run repeatedly in regular intervals (1 min in your case).

You can use time.sleep() function to list of commands repeatedly in regular intervals. You can also refer to this link to know more ways to do this.



Hope this helps!



Thank you