osparamatrix / ks-orderapi-python

47 stars 59 forks source link

How to parse responses #79

Open ashwinkp opened 2 years ago

ashwinkp commented 2 years ago

Hi,

I am able to place order what would be the best way to extract the order id from the response? Currently the response has the structure orderId = {'Success': {'NSE': {'message': 'Your AMO has been Placed: 12220207081042.', 'orderId': 12220207081042, 'price': 525, 'quantity': 1, 'tag': 'ALGO1'}}}

I am new to python so I am currently doing order_Id= orderId ['Success']['NSE']['orderId'] which works but I suppose its not the best way to do...

A better way would have been if placer order sent a response of order_status type where one could do if order_status.status == 'Success' print("Order ID: " order_status.status )

Any suggestions/comments?

TIA Ashwin

kalilinux-png commented 2 years ago

`def order_status(order_Id): ''' This Function Is Used To Get Order Status Like Pending Or Traded ['staus'] == TRAD OR OPN OR CAN ['statusMessage']==Open or Completely Traded Order'''

    return client.order_report(order_Id)['success'][-1]['status']`

@ashwinkp let me know if this help's you or not

jayaprakashv2005 commented 2 years ago

even i am doing -> "order_Id= orderId ['Success']['NSE']['orderId']" just add a try catch around it

ashwinkp commented 2 years ago

@jayaprakashv2005

I wrote a class KResponse. This allows me to get the orderID, Success and other info from the response string in structure format. So now I can do.
resp = client.place_order(...) orderResp = KResponse(resp) return str(orderResp.orderId)

class KResponse(object):

def init(self, data): res = data if "Success" in res: self.dict = res['Success']['NSE'] self.Success = True self.exchange = 'NSE' elif 'success' in res: self.dict = res['success']['NSE'] self.Success = True self.exchange = 'NSE'