Note able to execute kite.holdings()

ansumanm
ansumanm edited November 2017 in Python client
Hi,

I wrote a very simple app where I am trying to get the holdings in my a/c.
75         kite = KiteConnect(api_key, api_secret)                                  
76
77 # Fetch all orders
78 print("All kite holdings:\n")
79 print(kite.holdings())
80
I am getting the error:-
Invalid session

I am getting if I execute kite.orders()

Invalid `api_key` or `access_token`.

BTW, I am able to run a websocket where I am receiving streams from few of the instruments that I have subscribed to.
I tried logging in again, but that does not work.
Earlier , I had fetched instruments using kite.instruments() and that had worked.

Am I missing something? I am running a websocket process where I am receiving ticks. This is a new process that I executing parallelly. Do I need to pickle the kite object and use the same object in all the processes?

Thanks,
Ansuman
Tagged:
  • ansumanm
    Surprisingly, kite.instruments() works fine.

    Thanks,
    Ansuman
  • ansumanm
    I regenerated the api secret and tried. Still facing the same issue.

    Thanks,
    Ansuman
  • sujith
    Hi,
    The request token is valid for only once and validity is only for a couple of seconds.
    So once you get access token store it and re-use it in subsequent runs. Don't request for access token every time you run the script.
    The access token is valid for one whole day.
    It will be invalidated only if you log into other platforms like Pi or Nest. It is recommended to use Kite for monitoring purposes. If you log out of Kite then also you are logged out everywhere.
  • ansumanm
    ansumanm edited November 2017
    Hi Sujith,

    Thanks for your response.
    To create a kite context, I am not using request token or access token, I am using api key and secret.
    131   try:                                                                         
    132 print(" Creating kite context...")
    133 kite = KiteConnect(api_key, api_secret)
    134 except Exception as e:
    135 print(e)
    136 sys.exit(1)
    137
    138 print("Done.")
    139
    Creating kite context...
    Done.

    Looks like I am able to create the kite context. But I am not able to place an order,
    140     odr_id = place_an_order(ktc=kite, tsb='CRUDEOILM17DECFUT', exc='MCX',        
    141 txn='BUY', qty=1, odr="MARKET", pdt="NRML")

    29 def place_an_order(ktc, tsb, exc, txn, qty, odr, pdt):
    30 """
    31 Place an order
    32 ktc: Kite Context
    33 tsb: tradingsymbol. Ex:- Infy
    34 exc: exchange Ex:- NSE
    35 txn: transaction_type Ex:- BUY
    36 qty: quantity Ex:- 1
    37 odr: order_type Ex:- "MARKET"
    38 pdt: product Ex:- "NRML"
    39 """
    40 try:
    41 order_id = ktc.order_place(tradingsymbol=tsb,
    42 exchange=exc,
    43 transaction_type=txn,
    44 quantity=qty,
    45 order_type=odr,
    46 product=pdt)
    47 print("Order placed. ID is", order_id)
    48 except Exception as e:
    49 print("Order placement failed: {}".format(e))
    50 return None
    51
    52 return order_id
    Order placement failed: Invalid session

    Can you please point me to an working example?

    Thanks,
    Ansuman
  • ansumanm
    ansumanm edited November 2017
    The following code works:-
    15     def place_an_order(apk, xst, tsb, xch, txn, odt, qty, pdt, vld):                 
    16 """
    17 apk = api_key
    18 xst = access_token
    19 tsb = tradingsymbol
    20 xch = exchange
    21 txn = transaction
    22 odt = order_type
    23 qty = quantity
    24 pdt = product
    25 vld = validity
    26 """
    27
    28 payload = {
    29 "api_key": apk,
    30 "access_token": xst,
    31 "tradingsymbol": tsb,
    32 "exchange": xch,
    33 "transaction_type": txn,
    34 "order_type": odt,
    35 "quantity": qty,
    36 "product": pdt,
    37 "validity": vld
    38 }
    39
    40 response = requests.post("https://api.kite.trade/orders/regular",
    41 data=payload)
    42 print(response)
    43 print(type(response))
    44 return None
    But I am not getting the order ID:-
    The response that I am getting is



    Will figure that out. But please correct the documentation. The payload is written wrong in documentation.
    https://kite.trade/docs/connect/v1/?python#placing-orders
  • ansumanm
    Response:-

  • ansumanm
    Looks like there is some bug in the form. Data in between the greater than and lesser than symbols not coming..
    Response [200]
    class 'requests.models.Response'
  • ansumanm
    ansumanm edited November 2017
    Found the correct way to parse the response.
    This code works:-
    113 def get_orders(apk, xst):                                                        
    114 """
    115 Retrieve orders.
    116 apk = api_key
    117 xst = access_token
    118 """
    119 url = "https://api.kite.trade/orders?api_key=%s&access_token=%s" % (apk,
    120 xst)
    121 response = requests.get(url)
    122 resp_content = json.loads(response.content)
    123 status = resp_content['status']
    124
    125 if status == 'success':
    126 data = resp_content['data']
    127 return data
    128 else:
    129 return None
  • sujith
    Thank you. We will update documentation.
This discussion has been closed.