A question about a code

Hello there:





 I am traying to write a function in python , so that it sends a limit order and it also cancels the order, if it doesn't get filled after a specific ammount of time from the time the order is send.



The code I wrote is the following:





from datetime import datetime 



def limit_order_with_time (context.security, shares, entry, time_in_seconds)  

    bool_var1= False

    init = datetime.now()

    orderId = order(context.security, shares , LimitOrder(limit_price=entry))

    while (bool_var1==False and position.amount < shares):

        final = datetime.now() 

        diff = final - init

        if (diff.total_seconds() >= time_in_seconds)

            cancel_order(get_order(orderId))

        bool_var1= True 

    break 



I expect to save it into a file and then import it within another code, so that I can use it to trade…



Is my function good for What I expect it to do?? 

what do you think??





Thanks



 

Hi Ghery,



Based on the code you've provided, I think it should work just fine. Before using this code in the live market, you should backtest it and ensure that it's working as intended.



​​​​​​​Thanks 

OK, thanks a lot