zerodha / pykiteconnect

The official Python client library for the Kite Connect trading APIs
MIT License
981 stars 469 forks source link

How get tick data again #87

Closed karthickbala123 closed 4 years ago

karthickbala123 commented 4 years ago

In my code first i get current ltp of subscribed stock then i place buy order when my strategy applied, then i need tick data again for check current ltp for placing sell order. but when i call on_ticks(ws, ticks) function am getting below error. so kindly help me how get current ltp from outside of on_tick function.

on_ticks(ws, ticks) builtins.NameError: name 'ws' is not defined

vijaykz commented 4 years ago

on_ticks is a callback: you don't call it, it gets called by the web socket (ws) library.

Please check some examples on how to setup callbacks - https://github.com/zerodhatech/pykiteconnect/tree/master/examples. These should help you to setup live streaming of market ticks.

karthickbala123 commented 4 years ago

Sorry sir i am new for python so i cant understand callbacks if any example code please show me. i want tickdata after execution of buy order how i get. thanks in advance

karthickbala123 commented 4 years ago

tar_order_detail = kite.order_history(order_id=tar_order_id)[-1] if tar_order_detail['status'] == "OPEN": tsht.range((2, 7)).value = 'Target order still Pending' print(curr_ltp) # Here i want current LTP if curr_ltp <= sl_price: tar_order_detail = kite.order_history(order_id=tar_order_id)[-1] cancel_order_id = kite.cancel_order(variety=tar_order_detail['variety'], order_id=tar_order_detail['order_id'], parent_order_id=tar_order_detail['parent_order_id']) print('Target order cancelled Successfully: ', cancel_order_id) tsht.range((2, 7)).value = 'Target order cancelled Successfully: ' + str(cancel_order_id) sl_order_id = kite.place_order(variety=kite.VARIETY_REGULAR, price=sl_price, exchange=kite.EXCHANGE_NSE, tradingsymbol=stock, transaction_type=kite.TRANSACTION_TYPE_SELL, quantity=qty, product=kite.PRODUCT_MIS, order_type=kite.ORDER_TYPE_LIMIT) print('Stoploss order placed Successfully: ', sl_order_id) image

vijaykz commented 4 years ago

threaded_ticker.py is simple example on setting up tickers. on_ticks will continuously provide ticks for the subscribed tokens.

Once you've initialized the web socket connection in the threaded mode, you can continuously receive market ticks. This is where you would implement your strategy.

karthickbala123 commented 4 years ago

Thank you for your timely help sir. now i its working.