Python API - Place and close order sample code



  • A very basic question. I am not a expert programmer. Lets say I place a cover order using
    u.place_order(TransactionType.Buy, u.get_instrument_by_symbol('NSE_FO', 'BANKNIFTY17JUN15FUT'), 40, OrderType.Limit, ProductType.CoverOrder, 23001.0, 22995.00, None, DurationType.DAY)

    Now, how do I square off the order? What method is used and where do I get the orderid for the last placed order? I dont want to use listeners. Is it possible?


  • Global Moderator

    Hey @ganesh ,
    you will find an open order of CO. For that order send a cancel order command and position will get squared off. Same logic applies to OCO.
    Open order will be available in order book



  • But to cancel order from api I will need order id. How do I get that programmatically?
    Does the placeorder method returns order id ?


  • Global Moderator

    Hi @ganesh ,
    When you place order you get a response in form of order update.

    0_1499316270704_db958a79-d362-41d9-9bed-20635909e963-image.png

    This response contains a field called "order_id" , this field would be used to square off; wherein you call order history and check order with "parent_order_id" value same as "order_id" value and then call cancel for that order providing its "order_id"

    e.g

    • place order response gave "oder_id" as : 170328000000030
    • Now you call order history, one of orders in order history has values -
      "parent_order_id": "170328000000030",
      "order_id": "170328000000032",
    • Now call cancel order request by giving 170328000000032


  • Thanks. Will try that.



  • Hi Ganesh, are you able to cancel an open order using python..if yes please reply me how did you do it.
    I tried

    u.cancel_order('order_id') #order_id of the open order



  • I didn't try yet. So cannot comment. I believe they said that get parent id and then get the order id.


  • administrators

    @Venkat what error do you get when you try to cancel the order that way? That should work...



  • @shrini-viswanath I am getting this error,

    this was the open order_id 170711000072877 i wanted to cancel, I sent the order as shown below,
    u.cancel_order('170711000072877')

    This is the Error:
    raise TypeError("Required parameter order_id not of type int")
    TypeError: Required parameter order_id not of type int


  • administrators

    @Venkat please don't wrap the order ID in quotes. use

    u.cancel_order(170711000072877)
    

Log in to reply
 

Looks like your connection to Upstox Forum was lost, please wait while we try to reconnect.